Question Primary Key Question?

raysefo

Well-known member
Joined
Jul 21, 2010
Messages
207
Programming Experience
Beginner
Hi,


We are using MS SQL 2008 R2. I have a table and I would like to put a primary key or composite key for 3 fields. I have VendorID,ConsumeDate and ConsumedItemId fields that I restrict to enter the same data. Lets say, If I have a record like VendorID = 1, ConsumedDate = 2011-02-24 and ConsumedItemID = 1, I dont want the same data is entered to the table. How can I do it?


Thanks in Advance.


Best Regards.
 
In SSMS design-open the table, select the three fields (ctl-click), then right-click for the context menu - Primary Key.
...or maybe your question has some hidden complication that I didn't understand.

Spend some time arguing with yourself whether your composite key makes a good cluster key (and if not, can you live with a heap (and if not, what else can you use as a PK/cluster key together with a unique index on the three fields to enforce your rule))

Chris
 
Last edited:
As suggested, you select all three column and then make them a key. You can either make that combination of columns the PK or you can add an identity column as PK and make the other three columns a Unique Key.
 
Back
Top