What is the purpose of constraint naming

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.

Leave a Comment