Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating composite primary key in SQL Server

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?

like image 487
Dinithi De Silva Avatar asked Sep 26 '12 04:09

Dinithi De Silva


People also ask

How do I create a composite primary key in SQL Server?

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.

How do you create a composite primary key?

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.

What is composite primary key in SQL Server?

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.


1 Answers

If you use management studio, simply select the wardNo, BHTNo, testID columns and click on the key mark in the toolbar.

enter image description here

Command for this is,

ALTER TABLE dbo.testRequest ADD CONSTRAINT PK_TestRequest  PRIMARY KEY (wardNo, BHTNo, TestID) 
like image 66
Madusanka Avatar answered Oct 14 '22 04:10

Madusanka