DBMSSQL

Integrity Constraints in SQL

Integrity Constraints

While creating a table you can place certain Constraints/limitations on the values stored in the table. 

Different constrains applicable to SQL are:

NOT NULL.

UNIQUE.

PRIMARY KEY.

CHECK.

DEFAULT.

REFERENCES.

All the above mentioned constraints are further elaborated in the following sub-sections: 

Not Null

NOT NULL constraint prevents a column from accepting null values. If You try to insert a null value in such a column, it will be rejected. Note that NULL value does not mean a zero value. It just means absence of any dat, in that column.

UNIQUE

UNIQUE constraint ensures that values entered into a column are all different, i.e. unique. A column with this constraint will not accept any duplicate values.

PRIMARY KEY 

In order to declare a column as  primary key of the table, use the PRIMARY KEY constraint. There can be only one primary key in a table.

CHECK

CHECK constraint is used to control the values entered into a field. A condition is specified along with the CHECK constraint which must be satisfied by all the values being entered into the column otherwise the value will be rejected.

DEFAULT

DEFAULT constraint is used to assign default values to a column before any values is assigned to it. If user does not enter any value in that field default value will be assigned. 

REFERENCES

REFERENCES A foreign key has values which form the primary key in another table. The two tables thus get related by using the foreign key. Columns which are chosen as a foreign key should not have values other than that present in the primary key of a related table. This referential integrity is implemented using the REFERENCE constrain.

REFERENCE constraint is followed by the name of the related table and its primary key.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button