Tutorial :sql server 2008 composite key


How to set a field as a composite key in sql server 2008 and how to create a composite key in gui sql server 2008?


You can't set "one field" as a composite key - by definition, "composite" means more than one.

In SQL Server Management Studio, you can highlight more than one column in the table designer, and choose "Set Primary Key" from the context menu:

alt text http://i46.tinypic.com/29w24j4.png

That makes those selected columns a composite primary key.


Here is an example in T-SQL. The first two columns comprise the composite key. In SSMS, just highlight the first columns you want to comprise the key and select the Primary Key button on the toolbar.

CREATE TABLE [Security].[MemberRole](          [MemberID] [int] NOT NULL,          [RoleID] [int] NOT NULL,          [VersionNumber] [timestamp] NOT NULL,  CONSTRAINT [PK_MemberRole] PRIMARY KEY CLUSTERED   (          [MemberID] ASC,          [RoleID] ASC  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]  ) ON [PRIMARY]  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »