Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Sep 20, 2011 06:29 PM by TabAlleman
Sep 20, 2011 06:07 PM|LINK
I want to create a foriegn key that references the primary key in anothertable. I am doing this using an alter statement. Strangely the name of theforeign key created is FK_BBB_Aid_1975C517 ?I am working on a team and they are not strong on Sql. However they don'twant the key to look like that, with the mysterious appended numbers atthe end. Why is this happening? I need the foriegn key to have a namesuch as FK_Aid instead of the one mentioned above. Can someone help me to fix this? below is my Sql where I use the Alter statement to create the foreignkey.-----Also...This is how the tables are defined in Sql Server---AAA Aid (PK, smallint, not null)Name (nvarchar(50), not null)BBBBId (smallint, not null)AId (smallint, not null)----------------------------------------- Here is my sql which alters the tableAlter Table [dbo].BBB ADD FOREIGN KEY [Aid]References AAA(Aid)
Sep 20, 2011 06:11 PM|LINK
Try the following statement and replace the constraintname with the name you want.
Alter Table [dbo].BBB ADD CONSTRAINT constraintname FOREIGN KEY (Aid)
Sep 20, 2011 06:29 PM|LINK
SQL creates Key names like this when you don't specify a name.
To specify a name at the time you create the key, change your ALTER statement to something like this:
-- Here is my sql which alters the table
Alter Table [dbo].BBB ADD
FOREIGN KEY [Aid]