Constraints Syntax
Constraints Syntax
Module Objective
To describe the structure of Relational Model .
To discuss different keys in relational database like Super Key , Candidate key ,Primary Key ,
Composite Key , Secondary Key or Alternate Key , Surrogate Key , Foreign and Unique Key
To explain different constraints like schema based or integrity , entity integrity or key ,
Referential integrity and domain constraints
2
Session Plan
Constraints
• Integrity Constraints
• Entity Integrity Constraints
• Referential Integrity Constraints
• Domain Constraints
3
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
10
Can you answer this ?
1 Which of the following is not a class of constraint in SQL Server?
A NOT NULL
B NULL
C CHECK
D UNIQUE
8
Can you answer this ?
2 To include integrity constraint in an existing relation use :
A Modify table
B Drop table
C Alter table
D Create table
9
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).
D Check constraint
11
Referential Integrity Constraints
• They are based on the concept of Foreign Key.
• Constraint: If a relation R2 has a foreign key attribute (FK) matching
the primary key attribute (PK) of other relation R1, then every value of
FK in R2 must either be equal to the value of PK in some tuple of R1
or the FK value must be null.
12
Can you answer this ?
1 Which of the following can be addressed by enforcing a referential integrity
constraint?
B Certain fields are required (such as the email address, or phone number) before the
record is accepted
C Information on the customer must be known before anything can be sold to that customer
D When entering an order quantity, the user must input a number and not some text
13
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)
);
17
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
);
18
Can you answer this ?
1 Point out the correct statement.
C In a UNIQUE constraint, no two rows in the table can have the same value for the columns
D All
19
Can you answer this ?
1 Point out the wrong statement.
A Table constraints must be used when more than one column must be included in a
constraint
B A column constraint is specified as part of a column definition and applies only to that
column
C A table constraint is declared independently from a column definition and can apply to
more than one column in a table
20
Review Questions
• What Are Constraints in relational model?
• What are the different levels of data integrity?
• Explain different constraints to maintain data integrity?
• If we need to restrict column "Name" to take only alphabets, what
kind of constraint we need to apply and how?
21
Thank You
22