April 15, 2010 at 9:17 am
ALTER TABLE dbo.a
ADD FOREIGN KEY (id) REFERENCES dbo.b(id);
How can change the name of this key....after running this command SQL creates a foreign key constraint called FK_4A03EDD9...Can I change this name to something that makes since?
April 15, 2010 at 9:20 am
it's the full syntax for a constraint instead of the shortcut for a foreign key:
ALTER TABLE dbo.a
ADD CONSTRAINT FK_MyForeignKey FOREIGN KEY (id) REFERENCES dbo.b(id);
to rename a constraint that has a name you don't like,you use the proc sp_rename; it's like this:
sp_rename 'FK__GMACT__AACCOMPLT__3C5683AE','FK_MY_ACCOMPLISH'
Lowell
April 15, 2010 at 9:28 am
You can also right-click the constraint (in SSMS), and select rename and do it there.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 22, 2016 at 6:44 am
Here's a script that I've used in the past to rename all constraints - it can handle Primary Keys, Foreign Keys, Defaults, Unique Constraints and Check Constraints.
http://paulfentonsql.co.uk/2016/01/22/rename-all-constraints/%5B/url%5D
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy