Click here to monitor SSC
SQLServerCentral is supported by Redgate
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
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, July 31, 2016 3:25 AM
Points: 891, Visits: 242
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-
Column1 int not null Primary key
Column2 int not null Unique Key

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

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


Group: General Forum Members
Last Login: Saturday, March 26, 2016 5:07 AM
Points: 14, Visits: 46
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
Senior SQL Server DBA / Data & Solutions Architect
678-414-0090 (Personal Cell)
Post #1265380
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse