SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Changing Foreign key names to standard naming convention across the DB


Changing Foreign key names to standard naming convention across the DB

Author
Message
vidya_pande
vidya_pande
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1097 Visits: 242
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.



hfreeman
hfreeman
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 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
hfreeman@msn.com
678-414-0090 (Personal Cell)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search