A constraint is a property assigned to a column or the set of columns in a table that prevents to inconsistent values in
the column(s) and ensures the accuracy and reliability of the data in the database . Constraints are used to enforce
the data integrity. The following categories of the data integrity exist:
Entity Integrity ensures that there are no duplicate rows in a table.
Domain Integrity enforces valid entries for a given column by restricting the type, the format, or the range of
possible values.
Referential integrity ensures that rows cannot be deleted, which are used by other records (for example,
corresponding data values between tables will be vital).
User-Defined Integrity enforces some specific business rules that do not fall into entity, domain, or referential
integrity categories.
Each of these categories of the data integrity can be enforced by the appropriate constraints. Microsoft SQL Server supports the
following constraints:
1. PRIMARY KEY constraints
2. UNIQUE constraints
3. FOREIGN KEY constraints
4. CHECK constraints
5. NOT NULL constraints
6. DEFAULT constraints
1
(
aid int CONSTRAINT u_aid unique,
aName varchar(50)
)
B.Table Level
Example:
create table t_test
(
aid int ,
aName varchar(50),
More than one
CONSTRAINT u_aid unique(aid),
unique
CONSTRAINT u_aName unique(aName) constraint
)
C. Add a constraint to an existing table
Example:
ALTER TABLE T_TEST
ADD CONSTRAINT u_aid unique(aid)
2
4- CHECK CONSTRAINTS
They are similar to FOREIGN KEY constraints in that they control the values that are put in a column. You can create
a CHECK constraint with any logical (Boolean) expression that returns TRUE or FALSE based on the logical operators. The
check constraints are used to enforce domain integrity.
Note: - In a foreign key validates data from another table but in a check constraint validate data in a column values.
Even with the constraint on salary (Salary > 0) in place, the INSERT is successful. A NULL value makes the
expression logically unknown. A CHECK constraint will only fail an INSERT or UPDATE if the expression in
the constraint explicitly returns false. An expression returning true, or a logically unknown expression will
let the command succeed.
6- DEFAULT CONSTRAINTS
4
Each column in a record must contain a value, even if that value is NULL. If a column does not allow for null
values and does not have a DEFAULT definition, you must explicitly specify a value for the column, or the Database
Engine returns an error that states that the column does not allow null values.
i. Create table Statement to create DEFAULT.
a. Column Level
Example:
CREATE TABLE employee
(
EmployeeId INT ,
Name VARCHAR(30),
HireDate DATETIME NOT NULL default getdate()
)
b.Table Level
Example:
CREATE TABLE employee
(
EmployeeId INT ,
Name VARCHAR(30),
HireDate DATETIME NOT NULL
constraint HireDate default getdate()
)
c. Add a constraint to an existing table
Example 1:
alter table employee
add constraint d_HireDate default getdate() for HireDate
Example 2:
alter table employee
alter column HireDate default getdate()