Unit 2 Lecture 2
Unit 2 Lecture 2
Constraints
Constraints
• SQL constraints are used to specify rules for data in a table.
• Constraints can be specified when the table is created with the CREATE
TABLE statement, or after the table is created with the ALTER TABLE
statement.
• They are used to limit the type of data that can go into a table.
• It can be column level or table level.
7
Categories of Constraints
• Constraints on databases are divided into three main categories:
• Inherent model-based constraints or implicit constraints
• Schema-based constraints or explicit constraints or integrity
constraints
• Application-based constraints
8
Inherent model-based constraints or implicit constraints
The constraints that are implicit in a data model are called inherent
model-based constraints. These constraints are:
• Ordering of tuples in a relation: A relation is not sensitive to the
ordering of tuples.
• Values and NULLs in the Tuples: multi-valued attributes are not
allowed and special value, called NULL, is used in the cases
where values of attributes that may be unknown or may not apply to a
tuple
9
Schema-based constraints or explicit
constraints or integrity constraints
Integrity constraints are used to ensure accuracy and consistency of the
data in a relational database. These are further categorized as:
• Entity Integrity constraint or Key constraint
• Referential Integrity constraint
• Domain Integrity constraints
• Null constraint
• Unique constraint
• Check constraint
• Default constraint
10
Entity Integrity Constraint or Key Constraint
• It ensures that there are no duplicate tuples in a relation.
• Entity integrity constraint is based on the Primary Key (PK).
12
Domain constraint
• Domain constraints specify the set of possible values that may be
associated with an attribute. Some of the most commonly used domain
constraints are:
• Null constraint: It specifies whether null values are permitted for an attribute
• Unique constraint: It is a rule that forbids duplicate values in one or more
columns within a relation
• Check constraint: It is defined on a column for specifying the range of values
that can be inserted into it, using a predefined condition
• Default constraint: It is defined to provide a default value to a column if no other
value is provided while inserting a new record.
13
Null Constraint
If we specify a field in a table to be NOT NULL. Then the field will never accept null value
Syntax:
ADDRESS varchar(20)
);
14
Unique Constraint
This constraint helps to uniquely identify each row in the table.
Syntax:
CREATE TABLE Student
(
ID int(6) NOT NULL UNIQUE,
NAME varchar(10),
ADDRESS varchar(20)
);
15
Check Constraint
Using the CHECK constraint we can specify a condition for a field,
which should be satisfied at the time of entering values for this field.
Syntax:
CREATE TABLE Student
(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int NOT NULL CHECK (AGE >= 18)
);
11
Default Constraint
This constraint is used to provide a default value for the fields.
Syntax:
CREATE TABLE Student
(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int DEFAULT 18
);
12