How to add composite primary keys in SQL Server 2008?
I have a table as follows.
testRequest (wardNo nchar(5) , BHTNo nchar(5) , testID nchar(5) , reqDateTime datetime);
I need wardNo, BHTNo
and testID
to be a composite primary key.
How can I do this in SQL Server Management Studio?
It may be a candidate key or primary key. Columns that make up the composite key can be of different data types. SQL Syntax to specify composite key: CREATE TABLE TABLE_NAME.
When two or more Columns are together Identify the unique row in a table Known as Composite Primary Key. A composite key is a key that is the combination of more than one attribute or column of a given table. It may be a candidate key or a primary key.
Composite primary key in SQL Server. Primary Key: A column which is used to identify the records uniquely is referred to as Primary Key. 1. Primary key won't allow Null values.
If you use management studio, simply select the wardNo, BHTNo, testID columns and click on the key mark in the toolbar.
Command for this is,
ALTER TABLE dbo.testRequest ADD CONSTRAINT PK_TestRequest PRIMARY KEY (wardNo, BHTNo, TestID)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With