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
 Transact-SQL (2005)
 Naming scheme for FKs

Author  Topic 

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2010-08-10 : 10:39:37
Does anyone have any opinion on whether or not having a consistent naming scheme for Foreign Keys, Indexes and Check constraints is a good idea? Just that we don't seem to ever refer to these in our code or anywhere else.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-08-10 : 11:21:37
Having standards for everything is good IMHO.
Go to Top of Page

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2010-08-10 : 11:55:41
quote:
Originally posted by Lamprey

Having standards for everything is good IMHO.



I know what you are saying but an analogy with Applications is that things which aren't referred to in code may not be named. Such as when there are 40 Text Labels on a Form few if any will be named other than it's default name.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-10 : 13:00:53
We name all constraints etc. We have many clients / many databases running the same stuff. If we upgrade its a PITA to pre-delete an FK (programmatically) so that we can add a column, and recreated the FKey, if it doesn't have a consistent name - well, consistent doens't matter, it has the have the EXPECTED name!!

Taking that a step further ... if a DBA manually changes something on a client database which an upgrade script later wants to change, then if the DBA used the correct naming convention then the upgrade script will run as expected ... so that comes back to conforming to the naming convention in the first place
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-08-10 : 13:16:45
quote:
Originally posted by denis_the_thief

quote:
Originally posted by Lamprey

Having standards for everything is good IMHO.



I know what you are saying but an analogy with Applications is that things which aren't referred to in code may not be named. Such as when there are 40 Text Labels on a Form few if any will be named other than it's default name.

I guess it depends on your level of standards.. In all my applications ALL text labels are named and they are named according to a standard. Just like every database object is named. I do not allow any default names in my databases. But, for you or your enterprise it may be acceptable to allow that.
Go to Top of Page

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2010-08-10 : 13:27:32
quote:
Originally posted by Kristen

We name all constraints etc. We have many clients / many databases running the same stuff. If we upgrade its a PITA to pre-delete an FK (programmatically) so that we can add a column, and recreated the FKey, if it doesn't have a consistent name - well, consistent doens't matter, it has the have the EXPECTED name!!

Taking that a step further ... if a DBA manually changes something on a client database which an upgrade script later wants to change, then if the DBA used the correct naming convention then the upgrade script will run as expected ... so that comes back to conforming to the naming convention in the first place



If I understand you correctly: you are saying the same constraint must have the same name across all Databases.

But what if different Constraints use different naming schemes. Or if we just keep the name SQL server assigns as a default? We obviously wouldn't want did that with Tables (ie Table1, Table2...) since that would make out Stored Proc Code hard to read. But in the case of FKs etc..., at least ourselves, we don't use them in our Stored Procs anyhow.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-11 : 02:57:07
"you are saying the same constraint must have the same name across all Database"

Yes. So if we say "DROP Foo" then we won't get a "not found" error, and if we then say "DROP TABLE Bar" we won't get "Can't drop table, constraint Foo2 exists"

"if we just keep the name SQL server assigns as a default?"

IIRC SQL will assign a different name (based on a random number) each time ... if it did "DF_ColumnName" then you'd be fine, of course. (I'm vague on this, I can't remember which things it gave random number names too, and which it gave repeatable names too)
Go to Top of Page
   

- Advertisement -