Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Changing Foreign key names to standard naming convention across the DB Expand / Collapse
Author
Message
Posted Saturday, February 27, 2010 11:37 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Sunday, May 11, 2014 8:07 PM
Points: 891, Visits: 235
Hi,
I was trying to reply the Posts by David McKinney and chrs-513176 in the same thread.

Where he is talking about rare scenario of having mutiple foreign keys between Table1 and Table2.

For Example-
Table1
Column1 int not null Primary key
Column2 int not null Unique Key

Table2
Column1 int references table1.column1
Column2 int references table1.column1


Here we can not go with the naming the foreign key like
FK_Table2_Table1, becauase there are two keys. Name duplication will become the issue

Here we can follow names like
FK1_Table2_Table1
FK2_Table2_Table1

As a second option we can add column name(s) along with FK_Table2_Table1, but key name may become lengthy if the keys are composite.








Post #874028
Posted Monday, March 12, 2012 10:53 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 2, 2014 9:15 AM
Points: 10, Visits: 32
This is an interesting topic, which needs further discussion. As an example I have several tables that have duplicated foreign keys to the same relationship. When resolving system generated foreign keys into something more developer/support friendly, there are two options.

1. FK_ForeignTableName_ForeignColumnName(s)_PrimaryTableName_PrimaryColumnName(s) (SQL-92, so some degree)
Or in the parent child order of:
2. FK_PrimaryTableName_PrimaryColumnName(s) _ForeignTableName_ForeignColumnName(s)

The standard doesn't seem to be well established for SQL Server if you want to name your own foreign keys, so from my perspective, this must be decided within the organization or enterprise. I have never liked the system generated method and the number one method above is more to SQL-92 standards. I personally like the second option, for it has a natural flow.

The problem I have had is the renaming of the system established foreign keys is best resolved using 4-points names Option 1 or 2 above. Failure to use something that has just three qualifiers will result in duplicate names for foreign keys and thus be un-usable.

I welcome any return comments.



Hank Freeman
CCFGC – Senior SQL Server DBA/Architect
hafreeman@coca-cola.com
678-414-0090 (Personal Cell)
Post #1265380
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse