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

IT 220 - 2018 - Unit - 4

Uploaded by

evanjames038
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

IT 220 - 2018 - Unit - 4

Uploaded by

evanjames038
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Unit 4

Integrity Constraints
Integrity Constraints
• Integrity constraints guard against accidental damage to the
database, by ensuring that authorized changes to the database
do not result in a loss of data consistency.

• A checking account must have a balance greater than $10,000.00

• A salary of a bank employee must be at least $4.00 an hour

• A customer must have a (non-null) phone number

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 2


Integrity Constraints on a Single Relation
• not null
• primary key
• unique
• check (P), where P is a predicate

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 3


Not Null and Unique Constraints
• not null
• Declare name and budget to be not null
name varchar(20) not null
budget numeric(12,2) not null
• unique ( A , A , …, A )
1 2 m
• The unique specification states that the attributes A1, A2, … Am
form a candidate key.
• Candidate keys are permitted to be null (in contrast to primary keys).

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 4


The check clause
• check (P)
where P is a predicate
Example: ensure that semester is one of fall, winter, spring
or summer:

create table section (


course_id varchar (8),
sec_id varchar (8),
semester varchar (6),
year numeric (4,0),
building varchar (15),
room_number varchar (7),
time slot id varchar (4),
primary key (course_id, sec_id, semester, year),
check (semester in (’Fall’, ’Winter’, ’Spring’, ’Summer’))
);

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 5


Referential Integrity
• Ensures that a value that appears in one relation for a given set of attributes also
appears for a certain set of attributes in another relation.
• Example: If “Biology” is a department name appearing in one of the tuples
in the instructor relation, then there exists a tuple in the department relation
for “Biology”.
• Let A be a set of attributes. Let R and S be two relations that contain attributes A
and where A is the primary key of S. A is said to be a foreign key of R if for any
values of A appearing in R these values also appear in S.

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 6


Cascading Actions in Referential Integrity
• create table course (
course_id char(5) primary key,
title varchar(20),
dept_name varchar(20) references department
)
• create table course (

dept_name varchar(20),
foreign key (dept_name) references department
on delete cascade
on update cascade,
...
)
• alternative actions to cascade: set null, set default

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 7


Integrity Constraint Violation During Transactions
• E.g.
create table person (
ID char(10),
name char(40),
mother char(10),
father char(10),
primary key ID,
foreign key father references person,
foreign key mother references person)
• How to insert a tuple without causing constraint violation ?
• insert father and mother of a person before inserting person
• OR, set father and mother to null initially, update after inserting all persons (not
possible if father and mother attributes declared to be not null)
• OR defer constraint checking (next slide)

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 8


Complex Check Clauses
• check (time_slot_id in (select time_slot_id from time_slot))
• why not use a foreign key here?
• Every section has at least one instructor teaching the section.
• how to write this?
• Unfortunately: subquery in check clause not supported by pretty much any database
• Alternative: triggers (later)
• create assertion <assertion-name> check <predicate>;
• Also not supported by anyone

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 9


Built-in Data Types in SQL

• date: Dates, containing a (4 digit) year, month and date


• Example: date ‘2005-7-27’
• time: Time of day, in hours, minutes and seconds.
• Example: time ‘09:00:30’ time ‘09:00:30.75’
• timestamp: date plus time of day
• Example: timestamp ‘2005-7-27 09:00:30.75’
• interval: period of time
• Example: interval ‘1’ day
• Subtracting a date/time/timestamp value from another gives an interval value
• Interval values can be added to date/time/timestamp values

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 10


Index Creation
• create table student
(ID varchar (5),
name varchar (20) not null,
dept_name varchar (20),
tot_cred numeric (3,0) default 0,
primary key (ID))
• create index studentID_index on student(ID)
• Indices are data structures used to speed up access to records with specified values for index
attributes
• e.g. select *
from student
where ID = ‘12345’
can be executed by using the index to find the required record, without looking at all records of
student
More on indices in Chapter 11

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 11


User-Defined Types
• create type construct in SQL creates user-defined type

create type Dollars as numeric (12,2) final

• create table department


(dept_name varchar (20),
building varchar (15),
budget Dollars);

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 12


Domains
• create domain construct in SQL-92 creates user-defined domain types

create domain person_name char(20) not null

• Types and domains are similar. Domains can have constraints, such as not null, specified on
them.
• create domain degree_level varchar(10)
constraint degree_level_test
check (value in (’Bachelors’, ’Masters’, ’Doctorate’));

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 13


Types of Integrity Constraints
I. Domain Constraint:
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.

II. Entity Integrity Constraint


The entity integrity constraint states that primary key value can't be
null.
This is because the primary key value is used to identify individual rows
in relation and if the primary key has a null value, then we can't identify
those rows.
6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 14
Cont..
III. Referential Integrity Constraint
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
null or be available in Table 2.
IV. Key Constraint
A Key Constraint is a statement that a certain minimal subset of the fields of a
relation is a unique identifier for a tuple. The types of key constraints
• Primary key constraints
• Unique key constraints
• Foreign Key constraints
• NOT NULL constraints
• Check constraints
6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 15
Creating Constraint
CONSTRAINT constraint_name CHECK (address in(‘KTM’,
‘BRT’, ‘BRJ’)) (Check Constraint)

CONSTRAINT constraint_name FOREIGN KEY (key_name)


REFERENCES table_name(key_name) (Foreign Key
Constraint)

CONSTRAINT constraint_name PRIMARY KEY (key_name))


(Primary Key Constraint)

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 16


Dropping Constraint
ALTER TABLE table_name DROP CONSTRAINT constraint_name
-- mssql

ALTER TABLE test1 DROP FOREIGN KEY


constraint_name; -- mysql

ALTER TABLE table_name ADD CONSTRAINT


constraint_name FOREIGN KEY (ClientID) REFERENCES
Client(ID) --

6/7/2018 Prepared By Kul P. Paudel, Integrity Constraints 17

You might also like