You are on page 1of 22

WELCOME TO CLASS

DATA PROCESSING YEAR 11 CLASS


RELATIONAL MODEL II
LESSON OBJECTIVES
At the end of the lesson, I should be able to:

• Define database integrity

• Explain constraints and its types

• Mention types of Integrity constraints

• Explain how to enforcing integrity constraints


Database Integrity
• This refers to the accuracy and
consistency of stored data in a
database.
• It describes the correctness and
completeness of data stored in the
database.
• Integrity is usually expressed in terms
of constraints.
WHAT ARE CONSTRAINTS?

• Constraints are rules that are enforced on data in a table. They ensure accuracy and
reliability of data in a relational database.

• They are used to limit the type of data that can


go into a table.

• EXAMPLE- A blood group must be ‘A’ or ‘B’ or ‘AB’ or ‘O’ only (can not be any other value
else).
TYPES OF CONSTRAINTS
TYPES OF CONSTRAINTS

• (1) NOT NULL Constraint: Ensures that no column should have a null value

• (2) DEFAULT Constraint: This provides a default value for a column when no value is
specified.

• (3) UNIQUE Constraint: Ensures that all values in a column are different.

• (4) PRIMARY KEY Constraint: Ensures that the primary key uniquely identifies each
record in a table.
• (5) FOREIGN KEY Constraint: Ensures that the foreign key value has to be one of
the values contained in the parent table i.e the table with the primary key.

• (6) CHECK Constraint: It ensures that all values in a column satisfy certain
conditions.

• (7) INDEX Constraint: Used to retrieve data quickly from the database.
INTEGRITY CONSTRAINTS

• Integrity constraints are used to ensure accuracy and consistency in a relational


database.
• Thus, integrity constraints guard against accidental damage to the database.

• Data integrity is handled in relational database through the concept of


referential integrity (RI).
TYPES OF INTEGRITY CONSTRAINTS
Domain Constraints

• Domain constraints can be defined as the definition of a valid set of values for an
attribute.
• The data type of domain includes string, character, integer, time, date, currency, etc.
The value of the attribute must be available in the corresponding domain.
Entity Integrity Constraints

• The entity integrity constraint states that primary key value can't be null.

• A table can contain a null value other than the primary key field.
Example:
Referential Integrity Constraints

• A referential integrity constraint is


specified between two tables.
• In the Referential integrity
constraints, if a foreign key in
Table 1 refers to the Primary Key
of Table 2, then every value of the
Foreign Key in Table 1 must be
available in Table 2.
Key constraints

• Keys are the entity set that is used to identify an entity within its entity set uniquely.
• An entity set can have multiple keys, but out of which one key will be the primary key.
ENFORCING INTEGRITY CONSTRAINTS

We can enforce constraints in the following ways:

(1) Primary Key constraints:


Primary Keys must contain unique values.
The column cannot have NULL values.
A table can have only one primary key which may consist of single or multiple fields. When multiple fields
are used as a primary key, they are called a composite key.
PRIMARY KEY constraints guarantee unique data, they are frequently defined on an identity column.
ENFORCING INTEGRITY CONSTRAINTS

(2) FOREIGN KEY CONSTRAINT: Foreign Key is a column or combination of columns whose values
match a primary key in a different table. It links two tables together. It is also called referencing key. The
constraint enforces referential integrity by guaranteeing that changes cannot be made to data in the primary key
table if those changes invalidate the link to the data in the foreign key table

If an attempt is made to delete the row in a primary key table or to change a primary key value, the
action will fail when the deleted or changed primary key value corresponds to a value in the FOREIGN
KEY constraint of another table. To successfully change or delete a row in a FOREIGN KEY constraint,
you must first either delete the foreign key data in the foreign key table or change the foreign key data in
the foreign key table, which links the foreign key to different primary key data.
ENFORCING INTEGRITY CONSTRAINTS

• (3) UNIQUE CONSTRAINT: The unique constraint prevents two records from
having identical values in a particular column. In a ‘CUSTOMERS’ table, for example,
you might want to prevent two or more people from having identical age.
• You can use UNIQUE constraints to make sure that no duplicate values are
entered in specific columns that do not participate in a primary key. Although
both a UNIQUE constraint and a PRIMARY KEY constraint enforce uniqueness,
use a UNIQUE constraint instead of a PRIMARY KEY constraint when you want
to enforce the uniqueness of a column, or combination of columns, that is not the
primary key.
ENFORCING INTEGRITY CONSTRAINTS

• (4) CHECK CONSTRAINT: The check constraint enables a condition to check the value being
entered into a record. If the condition evaluates to false, the record violates the constraint and it isn’t
entered into the table.
• You can create a Check constraint with any logical expression that returns TRUE or FALSE based on
the logical operators.
• For example, the range of values for a salary column can be limited by creating a Check
constraint that allows for only data that ranges from $15,000 through $100,000. This prevents
salaries from being entered beyond the regular salary range.
ENFORCING INTEGRITY CONSTRAINTS

• (5) NOT NULL CONSTRAINT: By default, a column can hold null values. If you do not want a column to
have a null value then you need to specify a NOT NULL constraint on the column indicating that null (blank)
is not allowed for that column.

• (6) INDEX CONSTRAINT: The INDEX is used to create and retrieve data from the database very quickly.
When index is created, it is assigned a ROW_ID for each row before it sort out the data. You can create index
on single or multiple columns.

• (7) DEFAULT CONSTRAINT: The DEFAULT constraint provides a default value to a column when the
INSERT INTO statement does not provides a specific value.
CLASS DISCUSSION

• When multiple fields are used as a primary key, they are called …... key (a) Composite (b)
Foreign (c) Unique (d) Entity
• Foreign key is also called ...... key (a) Data (b) Referencing (c) column level (d) domain
• The …… constraint enables a condition to check the value being entered into a record. (a) Key
(b) Table (c) Check (d) SQL
• The …… constraint prevents two records from having identical values in a particular column.
(a) Unique (b) NULL (c) Record (d) Index
• Constraints are used to limit the type of data that can go into a ……. (a) Entity (b) Index (c)
Table (d) Set
CLASS INTERACTION

1. What is Database Integrity?


2. What is the purpose of Constraints?
3. Explain 3 types of constraints.
4. State 2 types of integrity constraints
THANK YOU

You might also like