Wednesday, March 21, 2012

Primary Key on Two Columns

How can I create a primary key on two columns? I could not find any sensible code sample or description how it should be done. It is clear from MSDN that it is possible but there are no examples.

There is a textbox "Included Columns" in one of the tools for setting primary keys for one column but it does not allow me to enter anything.

Thanks.

Drop table T1

CREATE TABLE [dbo].[T1](

[c1] char(10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

Angel [int] NOT NULL,

Beer [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

CONSTRAINT [PK_T1] PRIMARY KEY CLUSTERED

(

Angel ASC,

Beer ASC

)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

one way is as above mentioned , here your pk will be created on column a and b

from Management studio , open the table highlight the multiple colum and press right button. you can select set primary key option

Madhu

|||This is cute, esp the management studio approach. Thank you.

No comments:

Post a Comment