What is the purpose of constraint naming

后端 未结 7 1199
栀梦
栀梦 2020-11-28 05:33

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:

C         


        
相关标签:
7条回答
  • 2020-11-28 06:08

    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.

    0 讨论(0)
  • 2020-11-28 06:10

    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.

    0 讨论(0)
  • 2020-11-28 06:16

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

    0 讨论(0)
  • 2020-11-28 06:17

    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.

    0 讨论(0)
  • 2020-11-28 06:18

    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.

    0 讨论(0)
  • 2020-11-28 06:27

    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.
    0 讨论(0)
提交回复
热议问题