Dinesh Kumar
Dinesh Kumar

Reputation: 1499

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?

Upvotes: 1

Views: 4075

Answers (2)

marc_s
marc_s

Reputation: 754488

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

That makes those selected columns a composite primary key.

Upvotes: 2

Randy Minder
Randy Minder

Reputation: 48402

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]

Upvotes: 2

Related Questions