khajvah
khajvah

Reputation: 5090

Concerning Constraints

I am completely new to sql (have couple of days to get to know it) and have following question:

Here is a syntax for constraints:

While creating the table, I have this kind of line:

CONSTRAINT smth UNIQUE(name)

I understand that it puts constraint on column name for it to be unique but what is smth for? Why do we need to name the constraint? Is it used anywhere?

Upvotes: 1

Views: 40

Answers (3)

koushik veldanda
koushik veldanda

Reputation: 1107

CONSTRAINT constraint_name UNIQUE(column_name)

column_name: to which you are applying constraint

constraint_name: name of the constraint, which you are applying to column_name

It is used to identify the UNIQUE(present declared) constraint and can able to delete if not needed

Upvotes: 0

user_0
user_0

Reputation: 3363

Constraints have names.

It is useful. Just imagine:

  • when you need to drop a constraint
  • when you list constraints on a object
  • when a constraint fails, it will show name in error message.

Upvotes: 1

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239646

You name it (as with many things) so that you can perform maintenance in your database easily.

See ALTER TABLE ... DROP CONSTRAINT and note that you have to supply the name of the constraint there.

Also, it's helpful if the constraint is violated:

An optional name for a column or table constraint. If the constraint is violated, the constraint name is present in error messages, so constraint names like col must be positive can be used to communicate helpful constraint information to client applications.

Upvotes: 3

Related Questions