I have a SQL Server table that has primary keys
CompanyID
ClientID
ReportName
I need to add a column that is an additional key and an incremented ID.
alter table Exports
add id int identity(1,1)
How do I write the statement to make this a key as well?
You can have only 1 primary key or identity column in each table in SQL Server.
If you want to have one more column as primary key, then you may use the combination of the columns as the primary key.
Or you can set the constraints UNIQUE and NOT NULL to the columns so that they will act same as a primary key (but these columns can't be referred as foreign keys)
In case of auto incremental columns (identity), you can only have 1 identity column per table. It can also be set as a primary key. Otherwise, if you need multiple columns as auto-increment, then probably you can use a calculated column, a trigger or a sequence object from which you may fetch the values for each record
Related
Creating table without using SQL table feature:
I'm trying to create table going to "Tables","Create Table" feature. How do I add primary and unique keys using this feature.
Right click on the selected column and set the option primary key from the context menu.
It has been shown below.
The documentation covers this nicely: From Create Primary Keys
Using SQL Server Management Studio
To create a primary key
In Object Explorer, right-click the table to which you want to add a unique constraint, and click Design.
In Table Designer, click the row selector for the database column you want to define as the primary key. If you want to select multiple
columns, hold down the CTRL key while you click the row selectors for
the other columns.
Right-click the row selector for the column and select Set Primary Key.
Or, if you want to use Transact-sQL
Using Transact-SQL
To create a primary key in an existing table
The following example creates a primary key on the column
TransactionID in the AdventureWorks database.
ALTER TABLE Production.TransactionHistoryArchive
ADD CONSTRAINT PK_TransactionHistoryArchive_TransactionID PRIMARY KEY CLUSTERED (TransactionID);
It also goes on to explain how you can use T-SQL to create a NONCLUSTERED Primary key, and then add a further CLUSTERED INDEX on the table:
To create a primary key with clustered index in a new table
The following example creates a table and defines a primary key on the
column CustomerID and a clustered index on TransactionID in the
AdventureWorks database.
-- Create table to add the clustered index
CREATE TABLE Production.TransactionHistoryArchive1
(
CustomerID uniqueidentifier DEFAULT NEWSEQUENTIALID()
, TransactionID int IDENTITY (1,1) NOT NULL
, CONSTRAINT PK_TransactionHistoryArchive1_CustomerID PRIMARY KEY NONCLUSTERED (CustomerID)
)
;
-- Now add the clustered index
CREATE CLUSTERED INDEX CIX_TransactionID ON Production.TransactionHistoryArchive1 (TransactionID);
I am trying to create a composite primary key in Sql server.
The syntax I have tried is:
create table Installment_details
(
constraint P_key primary key(Account_No,month),
Account_No int not null,
foreign key (Account_No) references Account_details(Account_no) on delete cascade,
Month char(15) not null,
D#te date,
Receipt_no varchar(15),
Amount_received int,
Amount_left int,
Amount_receiver char(50),
)
As far as I know it should create column with column name P_key for primary key but whenever I make a entry in table it doesn't show this column.
You are confused about the terms you're using. It's not the same a Primary Key and a Column. For example, you're creating a Primary Key based on two existing columns, and the name P_Key it's the name of the Primary Key, which is the way SQL SERVER (in this case) can identify a row in the Table (it cannot be two rows with the same values on those two columns).
I hope this clarifies a little bit the issue.
I think you are getting it wrong P_key in your code is constraint's name not a column name.
Also composite key is not a column, it is used when you don't have a column with unique values. So you take combination of two or more column as primary key so that we can uniquely identify a row.
I have a table with 100 rows, but my primary key column is not with identity property.(but values of this column seems like identity column and sequential: 1-100)
Now i tried to add new identity column with below code
alter table tableA add new_Iden_col bigint identity(1,1)
but i find that the new column identity values are not in sorted order as per my primary key column.
my output of old Primary key col and new inserted identity column
So can someone explain me why the newly added column is not same as my primary key column even though my old column values are populated naturally in same way and not dynamically sorted for output.
Thanks in advance. :)
The IDENTITY function can be used to create a new IDENTITY column and assign incremental values in the sequence specified by the ORDER BY clause of SELECT...INTO. Below is an example creates a new IDENTITY column for use as a new primary key from the old primary key column order.
SELECT
IDENTITY(bigint, 1,1) AS new_Ident_col
, OtherData
INTO FROM dbo.TableA_New
FROM dbo.TableA
ORDER BY old_PK_col;
DROP TABLE dbo.TableA;
EXEC sp_rename 'dbo.TableA_New', 'TableA';
ALTER TABLE dbo.TableA ADD CONSTRAINT PK_TableA PRIMARY KEY(new_Ident_col);
I need to create table with two columns. These columns have to be created with primary key. For example Column 1 name ID and column 2 name Name. ID is auto incremented. When data is inserted into the table, all names has to be different. How can I do that? Please help me.
To create the primary key:
ALTER TABLE dbo.YourTableNameHere
ADD CONSTRAINT PK_YourTable
PRIMARY KEY(ID, Name)
and to make sure the Name is unique:
ALTER TABLE dbo.YourTableNameHere
ADD CONSTRAINT UNIQUE_Name UNIQUE(Name)
I having trouble creating a foreign key in sql 2005.
my primary key table has a primary key that spans 2 columns.
I want to create my foreign key so it references a column in my primary table but I want to specify a static value for the second column - is this possible?
No, you cannot do this - if you reference a parent table from a child table, you have to reference all columns of the primary key on the parent table:
ALTER TABLE dbo.ChildTable
ADD CONSTRAINT FK_ChildTable_ParentTable
FOREIGN KEY (col1, col2) REFERENCES dbo.ParentTable(pkCol1, pkCol2)
You cannot suddenly introduce for "static" value into your reference.
What you could do on the child table would be to put a CHECK CONSTRAINT on that second column to make it a "static" value - but then you can never have any other value in that column:
ALTER TABLE dbo.ChildTable
ADD CONSTRAINT chk_col2_Static CHECK (col2 = 4)
Is that what you are looking for??