alter script naming foreign keys

  • I'm trying to run a script that will put two keys onto a db. So far no problem. But when I go to look at the keys, the name of the fk is ALL Weird. Numbers, Letters, Underscores....

    Is there a way to name the key so when I look at it, it makes sense?

    ALTER TABLE table1

    ADD FOREIGN KEY (UserID) REFERENCES User(userID);

    the name of they key should be something like FKTable1_User, but it's messed up. any way to add in a param to name the key in the alter stmt?

  • You have to manually supply the name, instead of letting SQL Server pick one.

    You can look up the format of the alter table statement in SQL Server Books Online.

  • Matthew Cushing (7/7/2008)


    I'm trying to run a script that will put two keys onto a db. So far no problem. But when I go to look at the keys, the name of the fk is ALL Weird. Numbers, Letters, Underscores....

    Is there a way to name the key so when I look at it, it makes sense?

    ALTER TABLE table1

    ADD FOREIGN KEY (UserID) REFERENCES User(userID);

    the name of they key should be something like FKTable1_User, but it's messed up. any way to add in a param to name the key in the alter stmt?

    ALTER TABLE table1

    ADD CONSTRAINT FKTable1_User FOREIGN KEY (UserID) REFERENCES User(userID);


    * Noel

  • awesome, thanks.

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

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