Vous êtes sur la page 1sur 10

Data Integrity

Use of Constraints

In this Presentation..

Data Integrity and Constraints PRIMARY KEY UNIQUE KEY FOREGIN KEY CHECK Constraint DEFUALT Constraint NULL Constraint IDENTITY Property

Data Integrity

Data Integrity means reliability and accuracy of data. Integrity rules are designed to keep the data consistent and correct. These rules keep a check on the data that is being inserted into the tables.
Constraints are database objects that are used to enforce Data Integrity. Constraints are placed on columns in a table to check validity of data.

PRIMARY KEY

Primary Key is a column or a set of columns that is used to uniquely identify each row in a table :
CREATE TABLE <table-name> <column-definition> PRIMARY KEY

To add a Primary Key constraint to an existing table :


ALTER TABLE <table-name> ADD CONSTRAINT <constraint-name> PRIMARY KEY <column-name>

UNIQUE KEY
Unique

Key constraint specifies that all the values in a given column must be unique. The column must have a different value in every row of the table. A table can have multiple unique constraints.

Syntax :

CREATE TABLE <table-name>


<column-definition> UNIQUE KEY

FOREGIN KEY

Foreign Key is a column or a combination of columns used to create a link between the data present in two tables. The link is created between the two tables basing on a common field. A table can have multiple Foreign Keys. Foreign Key column can refer to Primary and Unique Key columns in the other table.

Syntax:

CREATE TABLE <table-name> <column-definition> FOREIGN KEY REFERENCES <primary-table (<column>)>

CHECK Constraint

CHECK Constraint is used to enforce integrity by limiting columns inserted in a table. This constraint is very fast when compared to other integrity constraints. Check constraints can also be used to limit data through a View.
Syntax :

CREATE TABLE <table-name> <column-definition> CHECK <condition>

DEFUALT Constraint

DEFAULT Constraints are used to populate column with default values whenever there is an act of insertion into the table data. DEFAULT constraint provides the value that will be stored in a column, in case the user does not specify any value for it. Syntax :
CREATE TABLE <table-name> <column-definition> DEFAULT <value>

NULL Constraint

NULL constraint is used to define the nullability of the column i.e. whether the column can accept null values or not. Default column definition is NULL. Nullability can be defined at a database level. Syntax :
CREATE TABLE <table-name> <column-definition> NULL | NOT NULL

IDENTITY Property
Identity Property is used to generate unique values automatically. These are a good candidates for Primary Key. We cannot directly insert values into identity columns manually. Syntax :

CREATE TABLE <table-name> <column-definition> IDENTITY [(seed, increment)]

Vous aimerez peut-être aussi