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:
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]
精彩评论