Tuesday, March 20, 2012

Compound Primary Key - How to ?

I need to know how to create a compound primary key in Transact-SQL
It's really simple, it's a table with 3 column, two of those make the primary key Right now the CREATE TABLE look like this:
/* SectionsContent Table */
CREATE TABLE SectionsContent (
SectionID Int NOT NULL
CONSTRAINT FK_SectionsContent_SectionID FOREIGN KEY
REFERENCES PsychoCMS.dbo.Sections(ID),
DocumentID Int NOT NULL
CONSTRAINT FK_SectionsContent_DocumentID FOREIGN KEY
REFERENCES PsychoCMS.dbo.Documents(ID),
Position int NOT NULL
)

Can anyone help me ?One of my tricks to figure out how to do stuff like this is use Enterprise Manager to build it and then have it generate a script. You get something like this for the primary key part of the table:

ALTER TABLE [dbo].[titleauthor] ADD
CONSTRAINT [UPKCL_taind] PRIMARY KEY CLUSTERED
(
[au_id],
[title_id]
) ON [PRIMARY]
GOsqlsql

No comments:

Post a Comment