SQL Server Performance

how to make composite primary key in SQL Server 2005?

Discussion in 'Getting Started' started by technoboyz, Mar 25, 2009.

  1. technoboyz New Member

    hey geeks,
    m in gr8 trouble, i need to know how to make composite primary key in SQL Server 2005 4 my application?
    help me guys.
  2. melvinlusk Member

    A composite key contains multiple columns. This is pretty easy to do in SQL Server Management Studio. Just go into Design Mode on the existing table (or create a new table), and select the columns you want to be a part of the key (hold shift while clicking on the columns). After selecting the columns, right click on one of the columns and click "Set Primary Key". You're done.
    To do this through T-SQL, the script would look something like this:
    ALTER TABLE dbo.tblTable ADD CONSTRAINT
    PK_tblTable PRIMARY KEY CLUSTERED
    (
    fstrField1,
    fstrField2
    ) ON [PRIMARY]
  3. FrankKalis Moderator

    To expand on Melvin...
    Carefully evaluate if you really need a composite PRIMARY KEY, especially if this is also going to be the clustered index for that table and there are alos nonclustered indices in place. The clustered index keys will be stored in the nonclustered indices and thus essentially widen the indices.

Share This Page