Create Multiple column Primary key on a table that already exists?

  • Hello all.

    So I can go into the design mode of a table and ctrl + select columns to add them to a primary key however, on extremely large tables this times out. After searching online for over an hour, I can only find code to apply this on a create table statement.

    So, other than recreating the table and inserting the data into it, is there a way with T-SQL to apply a multiple column Primary key on a table?

    Link to my blog http://notyelf.com/

  • You can run the following:

    alter table test add constraint PK_test primary key (fld1, fld2)

    The fields you use cannot be nullable, so you may have to use:

    alter table test alter column fld1 int not null

    Randy

  • Randy has the commands right, but you'll have to drop the existing primary key first.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Yeah, thanks G2. Also, love the quote!

    Randy

  • Yeah. Don't remember where I got it from, but thought it was worth hanging on to. (The quote)

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply