What is the purpose of constraint naming

SqlDatabaseConstraintsNaming

Sql Problem Overview


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!

Sql Solutions


Solution 1 - Sql

Here's some pretty basic reasons.

(1) If a query (insert, update, delete) violates a constraint, 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.

Solution 2 - Sql

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.

Solution 3 - Sql

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

Solution 4 - Sql

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.

Solution 5 - Sql

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.

Solution 6 - Sql

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.

Solution 7 - Sql

By naming the constraints you can differentiate violations of them. This is not only useful for admins and developers, but your program can also use the constraint names. This is much more robust than trying to parse the error message. By using constraint names your program can react differently depending on which constraint was violated.

Constraint names are also very useful to display appropriate error messages in the user’s language mentioning which field caused a constraint violation instead of just forwarding a cryptic error message from the database server to the user.

See my answer on how to do this with PostgreSQL and Java.

Solution 8 - Sql

While the OP's example used a permanent table, just remember that named constraints on temp tables behave like named constraints on permanent tables (i.e. you can't have multiple sessions with the exact same code handling the temp table, without it generating an error because the constraints are named the same). Because named constraints must be unique, if you absolutely must name a constraint on a temp table try to do so with some sort of randomized GUID (like SELECT NEWID() ) on the end of it to ensure that it will uniquely-named across sessions.

Solution 9 - Sql

Another good reason to name constraints is if you are using version control on your database schema. In this case, if you have to drop and re-create a constraint using the default database naming (in my case SQL Server) then you will see differences between your committed version and the working copy because it will have a newly generated name. Giving an explicit name to the constraint will avoid this being flagged as a change.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrewView Question on Stackoverflow
Solution 1 - SqlPhilip KelleyView Answer on Stackoverflow
Solution 2 - SqlmmxView Answer on Stackoverflow
Solution 3 - Sqluser73774View Answer on Stackoverflow
Solution 4 - SqlTim HowlandView Answer on Stackoverflow
Solution 5 - SqlJames BlackView Answer on Stackoverflow
Solution 6 - SqlGG.View Answer on Stackoverflow
Solution 7 - SqlMartinView Answer on Stackoverflow
Solution 8 - SqlTim H.View Answer on Stackoverflow
Solution 9 - SqlskeletankView Answer on Stackoverflow