0% found this document useful (0 votes)
2 views

Unit 2 Lecture 2

The document discusses SQL constraints, which are rules for data in a table that can be applied during table creation or alteration. It categorizes constraints into inherent model-based, schema-based, and application-based, with schema-based constraints further divided into entity integrity, referential integrity, and domain integrity constraints. Examples of domain constraints include null, unique, check, and default constraints, each with specific syntactical definitions.

Uploaded by

ishita15205
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit 2 Lecture 2

The document discusses SQL constraints, which are rules for data in a table that can be applied during table creation or alteration. It categorizes constraints into inherent model-based, schema-based, and application-based, with schema-based constraints further divided into entity integrity, referential integrity, and domain integrity constraints. Examples of domain constraints include null, unique, check, and default constraints, each with specific syntactical definitions.

Uploaded by

ishita15205
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Unit-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).

• (dept_id) is the primary key of department relation therefore it will be unique


and not null for all tuples of the department relation
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
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:

CREATE TABLE Student

ID int(6) NOT NULL,

NAME varchar(10) NOT NULL,

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

You might also like