ritesh gyanchandani ritesh gyanchandani - 7 months ago 10
SQL Question

What is the use of uc after constraint?

CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT **uc**_PersonID UNIQUE (P_Id,LastName)
)

Answer

It is absolutely good practice to name your constraints (otherwise SQL Server will name them with a random name, which makes it really difficult to upgrade more than one system with a general upgrade script).

It is good practice to use a prefix to see what type of constraint this is.

Common are

  • UQ, UC or UK for unique constraint / unique key
  • FK for foreign key
  • PK for the primary key
  • CK for a CHECK constraint

UPDATE

And it is good practice to add the table's name to the constraint (to avoid ambiguities). In your case this was:

CONSTRAINT uc_Persons_PersonID UNIQUE (P_Id,LastName)

Btw (Naming convention): It is quite common to use table names in singular form (Person instead of Persons). Read here: Table Naming Dilemma: Singular vs. Plural Names

Comments