Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 create foreign key with restraint

Author  Topic 

bbxrider
Starting Member

37 Posts

Posted - 2011-08-08 : 15:11:12
I'm making changes to an existing table, so far I used sql to generate a script to create a new table based on the existing table, but that did not include scripting to create an existing foreign key with restraint.
I generated a script for the foreign key but it includes a hex looking id from the existing table, what do you put in the script in place of the that hex code? in the sample below the '__6FCE6818' id from the existing table foreign key needs to be replaced with something?
it looks like a code generated by sql-server?

USE [myDB]
GO
ALTER TABLE [dbo].[myTable] WITH NOCHECK ADD CONSTRAINT [FK__myTable__6FCE6818] FOREIGN KEY([field01], [field02])
REFERENCES [dbo].[myOtherTable] ([field01], [field02])
GO
ALTER TABLE [dbo].[myTable] CHECK CONSTRAINT [FK__myTable__6FCE6818]

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-08 : 15:19:32
It's because you used the GUI to create the original constraint. If you want a duplicate setup, then leave that yucky name in place. Otherwise, change it to whatever your naming convention is. If it were me, I'd change it. You've likely got these yucky names all over the place.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

bbxrider
Starting Member

37 Posts

Posted - 2011-08-08 : 17:30:44
thanks for the reply, I guess the original table provisioning was done with something other than the 2005 management express I'm using. when you say GUI, I'm guessing you meant the 'new foreign key' option when right clik the keys folder. When I invoked that the name of the FK that it generated was the table name and '1' rather than hex looking yucky style. It seemed odd that you wouldn't be able to pick a name of your choosing but now I see that with the GUI you don't have a choice, to pick a name use the script.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-08 : 18:09:27
You're right, it wasn't through the GUI. It occurs when you don't specify a name when creating the foreign key. I have these "hex" default constraints in production due to this, and every chance I get, I correct it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -