views:

249

answers:

6

What is the purpose of naming your constraints (unique, primary key, foreign key)?

Say I have a table which is using natural keys as a primary key:

CREATE TABLE Order
(
    LoginName        VARCHAR(50)    NOT NULL,
    ProductName      VARCHAR(50)    NOT NULL,
    NumberOrdered    INT            NOT NULL,
    OrderDateTime    DATETIME       NOT NULL,
    PRIMARY KEY(LoginName, OrderDateTime)
);

What benefits (if any) does naming my PK bring?

Eg. Replace:

    PRIMARY KEY(LoginName, OrderDateTime)

With:

    CONSTRAINT Order_PK PRIMARY KEY(LoginName, OrderDateTime)

Sorry if my data model is not the best, I'm new to this!

+3  A: 

To identify the constraint in the future (e.g. you want to drop it in the future), it should have a unique name. If you don't specify a name for it, the database engine will probably assign a weird name (e.g. containing random stuff to ensure uniqueness) for you.

Mehrdad Afshari
Does this mean that naming it is only used so that people can identify a constraint more easily? In other words, it doesn't matter to (or effect) the DBMS in any way whether you name a constraint or not? You can't use it in code for some purpose? Sorry if that was unclear.
Andrew
It's just a name. The name doesn't make a functional difference. If you want to reference it in code in the future, the name matters, of course. It's like say, a variable name in code.
Mehrdad Afshari
Just like column *names* don't make a difference. If they describe what they are ("ProductId" instead of "BJZ0_340" or "Fred"), they're much more useful.
Philip Kelley
@Merhdad - Ah, I see, thanks for that.
Andrew
Could you use a constraints name in a REFERENCES statement?
Andrew
No. You can use it in `ALTER TABLE DROP CONSTRAINT [name]` for instance.
Mehrdad Afshari
+1  A: 

It helps someone to know quickly what constraints are doing without having to look at the actual constraint, as the name gives you all the info you need.

So, I know if it is a primary key, unique key or default key, as well as the table and possibly columns involved.

James Black
+1  A: 

It keeps the DBAs happy, so they let your schema definition into the production database.

mtnygard
Haha...yes, I guess this is the most important reason :P
Andrew
+3  A: 

When your code randomly violates some foreign key constraint, it sure as hell saves time on debugging to figure out which one it was. Naming them greatly simplifies debugging your inserts and your updates.

Tim Howland
+13  A: 

Here's some pretty basic reasons.

(1) If a query (insert, update, delete) violates a constraing, SQL will generate an error message that will contain the constraint name. If the constraint name is clear and descriptive, the error message will be easier to understand; if the constraint name is a random guid-based name, it's a lot less clear. Particulary for end-users, who will (ok, might) phone you up and ask what "FK__B__B_COL1__75435199" means.

(2) If a constraint needs to be modified in the future (yes, it happens), it's very hard to do if you don't know what it's named. (ALTER TABLE MyTable drop CONSTRAINT um...) And if you create more than one instance of the database "from scratch" and use system-generated default names, no two names will ever match.

(3) If the person who gets to support your code (aka a DBA) has to waste a lot of pointless time dealing with case (1) or case (2) at 3am on Sunday, they're quite probably in a position to identify where the code came from and be able to react accordingly.

Philip Kelley
+1 covers all bases
gbn
A: 

By correctly naming all constraints, You can quickly associate a particular constraint with our data model. This gives us two real advantages:

  1. We can quickly identify and fix any errors.
  2. We can reliably modify or drop constraints.
GG