0% found this document useful (0 votes)
18 views34 pages

Chapter3_RelationalModel+Chapter5_IntegrityConstraints

The document discusses integrity constraints in database management systems, emphasizing the importance of entity and referential integrity to maintain data accuracy and consistency. It also covers the relational model, detailing its structure, key terminologies, and operations such as selection, projection, and various types of joins in relational algebra. Additionally, it introduces advanced operations like outer joins and aggregate functions to enhance query capabilities.

Uploaded by

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

Chapter3_RelationalModel+Chapter5_IntegrityConstraints

The document discusses integrity constraints in database management systems, emphasizing the importance of entity and referential integrity to maintain data accuracy and consistency. It also covers the relational model, detailing its structure, key terminologies, and operations such as selection, projection, and various types of joins in relational algebra. Additionally, it introduces advanced operations like outer joins and aggregate functions to enhance query capabilities.

Uploaded by

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

DATABASE

MANAGEMENT
SYSTEM
Subash Manandhar

1
Chapter 5 : Integrity Constraints
• Database Constraints:
• While designing relational model, we define some conditions which must hold
for data present in database are called constraints.
• These constraints are checked before performing any operation in database.
• If there is violation of any constraints, operation will fail.
• Integrity Constraints:
• Integrity refers accuracy or correctness of data in database.
• Integrity constraints ensure no result in loss of data consistency although
changes are made to database by authorized users.
• Integrity constraints are defined by some key constraints like primary key,
foreign key.
• Two general integrity rules are:
• Entity Integrity
• Referential Integrity

Subash Manandhar 2
Chapter 5 : Integrity Constraints

• Entity Integrity:
• Ensures that there are no duplicate records in the table and the field that identifies
each record is UNIQUE and NOT NULL.
• It is a mechanism to provide and maintain primary key.
• Ensures two properties for primary key:
• Primary key for a row is unique.
• Primary key is not null.
• Referential Integrity:
• Is concerned with foreign key.
• It designates a column or combination of columns as a foreign key and establishes
relationship between primary key.
• It states that: if in two relation R and S, if an attribute A1 in relation R is primary key
and foreign key in S then the value of foreign key in a tuple in S must either be equal
to primary key of tuple R or be entirely NULL.
• Domain Constraints:
• Domain constraints can be defined as the definition of a valid set of values for an attribute.
• Is specified on the column of a relation, so that correct values can be entered in the column
for each record.

Subash Manandhar 3
Chapter 3 : Relational Model
3.1 Definitions and Terminology
• Relational Data Model:
• Is the primary data model widely used for data storage and processing.
• Was proposed by Dr. E.F. Codd of IBM in 1970.
• Data are organized in two dimensional tables called relations.
• tables are related to each other.
• DBMS based on relational model is called relational database management system
(RDBMS)
• Some terminologies for RDBMS
Record or relation oriented Table Oriented
Relation Table
Domain Set of permitted values
Tuple, Record Row
Attribute, Field Column
Cardinality No. of rows
Degree No. of columns
Primary Key Unique + NOT NULL
Subash Manandhar 4
Chapter 3 : Relational Model
3.1 Definitions and Terminology

Primary Key Attribute Domain or Domain of Name

Roll No. Name Address Phone


101 Ram Kathmandu 1234567890
Tuple
Cardinality 102 Sita Patan 5236981470
103 Hari Dharan 2510254325
Degree
FIG: Student Table or Student Relation
• For above e.g. • Relation is a set of tuples.
Degree of Student relation = 4 • t ϵ r denotes that tuple t is in relation r
Cardinality of Student relation = 3 • for relation r, domains of all attributes of r be
atomic.
Subash Manandhar 5
Chapter 3 : Relational Model
3.2 Structure of Relational Database
• A relation is used to show information about any entity and its
relationship with other entities.
• A relation comprises of a relation schema and relation instance.
• A relation schema shows the attributes of the tables.
• A relation instance is a two dimensional table with set of tuples.
• e.g.
• Relation schema
• employee (eid, name, address, job, salary )
• works (eid, cid)
• company (cid, name, address)

Subash Manandhar 6
Chapter 3 : Relational Model
3.2 Structure of Relational Database
• e.g.
• Relation instance

employee
eid name address job salary
101 Ram Kathmandu Manager 50000
102 Sita Dharan Designer 35000
103 Hari Patan Developer 40000
104 Gita Kathmandu Analyst 35000

company works
cid name address eid cid
201 Creative IT Ltd. Kathmandu 101 201
202 Imafly Solutions Dharan 102 201
103 202
Subash Manandhar 7
Chapter 3 : Relational Model
3.2 Structure of Relational Database
• Schema Diagram:
• Enable database developers to transpose ideas to papers.
• Provides an overview of entire database.
• In schema diagram, all database tables are designated with unique
columns and special features like primary key etc.
• e.g.
• employee (eid, name, address, job, salary )
• works (eid, cid)
• company (cid, name, address) employee
eid
company works name
cid eid address
name cid job
address salary

Subash Manandhar 8
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Relational algebra is a procedural query language, which takes instances
of relations as input and gives instances of relation as output.
• It helps to understand query execution and optimization in RDBMS.
• Basic operations of relational algebra are:
1. Selection , 2. Projection, 3. Union, 4. Set Difference, 5. Cartesian Product
• Selection:
• It selects a subset of rows from relation that satisfies some condition.
• It is denoted by ς.
• Schema of result is identical to schema of input relation.
• Result relation can be input for another relational algebra operation.
• Syntax:
• ςcondition(relation)
• example:
• ςsalary > 10000 (employee)
Subash Manandhar 9
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Projection:
• It deletes the attributes that are not in project list.
• It is denoted by π.
• Schema of result contains exactly the fields in projection list, with the same
names that they had in input relation.
• Syntax:
• πattributes list(relation)
• example:
• πname, job, salary (employee)

•πname, job, salary (ςsalary > 10000 (employee))

Subash Manandhar 10
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Union:
• It builds a relation from tuples appearing in either or both of the specified
relation.
• It eliminates duplicates.
• It is denoted by U.
• Two relations are union compatible if
• They have same no. of attributes.
• Attribute domains must be compatible.
• Syntax:
• πattributes list(relation) U πattributes list(relation)
• RUS=SUR

Subash Manandhar 11
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Set Difference:
• It results tuples present in first relation but not in second relation.
• It is denoted by .
• Union compatibility should exists.
• Syntax:
• πattributes list(relation) πattributes list(relation)
• R S=S R
Cartesian Product:
• it builds a relation with concatenation of every row in first relation with
every row in second relation.
• Also known as cross product or cross join.
• Is denoted by X.
• Syntax:
• RXS

Subash Manandhar 12
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Intersection:
• Results tuples that appear in both relations.
• It is denoted by Π.
• Relations must be union compatible.
• Syntax:
• πattributes list(relation) Π πattributes list(relation)
• RΠS=SΠR
• Rename :
• allows to rename the output relation.
• It is denoted by ρ.
• Syntax:
• ρx (E) where result of expression E is saved with name x.
• Example:
• ρemp (name, phone) (employee)

Subash Manandhar 13
Chapter 3 : Relational Model
3.3 The Relational Algebra
• e.g. eid name address job salary
101 Ram Kathmandu Manager 45000
102 Sita Patan Designer 30000
103 Hari Kathmandu Developer 45000
104 Gita Dharan Analyst 40000
105 Rita Pokhara Reception 20000
Relation : employee

ςsalary > 35000 (employee)

eid name address job salary


101 Ram Kathmandu Manager 45000
103 Hari Kathmandu Developer 45000
104 Gita Dharan Analyst 40000

Subash Manandhar 14
Chapter 3 : Relational Model
3.3 The Relational Algebra
name job salary
πname, job, salary(employee) Ram Manager 45000
Sita Designer 30000
Hari Developer 45000
Gita Analyst 40000
Rita Reception 20000

πname, job, salary (ςsalary > 35000(employee))

name job salary


Ram Manager 45000
Hari Developer 45000
σsalary > 35000(πname, job (employee)) = ? Gita Analyst 40000

Subash Manandhar 15
Chapter 3 : Relational Model
3.3 The Relational Algebra
• e.g. id name address id name address
101 Ram Kathmandu 101 Ram Kathmandu
102 Sita Pokhara 201 Shyam Patan
103 Gita Dharan 202 Hari Jhapa
relation1 relation2

relation1 U relation2 relation1 Π relation2

id name address
id name address 101 Ram Kathmandu
101 Ram Kathmandu
102 Sita Pokhara relation1 - relation2
103 Gita Dharan id name address
201 Shyam Patan 102 Sita Pokhara
202 Hari Jhapa 103 Gita Dharan
Subash Manandhar 16
Chapter 3 : Relational Model
3.3 The Relational Algebra
relation1 X relation2

id name address id name address


101 Ram Kathmandu 101 Ram Kathmandu
101 Ram Kathmandu 201 Shyam Patan
101 Ram Kathmandu 202 Hari Jhapa
102 Sita Pokhara 101 Ram Kathmandu
102 Sita Pokhara 201 Shyam Patan
102 Sita Pokhara 202 Hari Jhapa
103 Gita Dharan 101 Ram Kathmandu
103 Gita Dharan 201 Shyam Patan
103 Gita Dharan 202 Hari Jhapa

Subash Manandhar 17
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Join Operation( ):
• allows users to combine two relations in a specified way.
• The join operation can be stated in terms of a cartesian product followed
by a selection operation.
• Is very important as used frequently while specifying database queries.
• Three types of join:
• Theta join (condition join)
• Equi join (inner join)
• Natural join
• Condition join (Theta join):
• joins two relations together on basis of some comparision operator.
• θ is a predicate and consists of one of the comparision operator (=,<,>,<=,>=,<>) and
specifies a join condition.
• If θ is ‘=’, then it is called equi join.
• Defined as a cross product followed by selection.

Subash Manandhar 18
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Condition join (Theta join):
• For any two relations R and S, theta join is
R θ S = σθ (R X S)

sid bid date sid sname rating age


22 101 11/11/2020 22 Ram 7 45
58 103 15/11/2020 31 John 9 28
R1 58 Hari 8 30
S1
S1 S1.sid < R1.sid R1

sid sname rating age sid bid date


22 Ram 7 45 58 103 15/11/2020
31 John 9 28 58 103 15/11/2020
Subash Manandhar 19
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Equi join (Inner join):
• If θ is ‘=’, then join is called equi join.
• The = condition is performed by primary and foreign keys.
• The result must include two attributes with property that values of those
two attributes are equal every tuple in relation.
•R R.PK = S.FK S

R1 R1.sid = S1.sid S1

sid bid date sid sname rating age


22 101 11/11/2020 22 Ram 7 45
58 103 15/11/2020 58 Hari 8 30

Subash Manandhar 20
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Natural Join:
• Is represented by symbol
• only relates those rows that are equal on their common attribute names.
• Natural join can be defined formally as below:
• Let R and S be any two relations and R = {A,B,C,D,E} and S = {A,B,F,G} are
attributes of given relations , then their natural join is denoted by R S and is
defined as follows:
R S = πR.A,R.B,C,D,E,F,G (ςR.A = S.A ^ R.B = S.B (R X S))
So, natural join can be computed by using fundamental operations : Cartesian
product, Selection and Projection.

Subash Manandhar 21
Chapter 3 : Relational Model
3.3 The Relational Algebra
empid name dept-name dept-name manager
101 Ram IT IT Rita
102 Sita Finance Finance Shyam
103 Hari IT Marketing John
employee department

employee department

empid name dept-name manager


101 Ram IT Rita
102 Sita Finance Shyam
103 Hari IT Rita
Subash Manandhar 22
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Assignment operation:
• Provides a convenient way to express complex queries.
• Allows us to express large and complex queries in series of small and
simple queries so that they can be more easily understood.
• Syntax:
• Variable E where E is relational algebra expression.
• Extended Relational Algebra Expression:
• are used to express the request that cannot be performed with original
relational algebra operation.
• enhance expressive power of original relational algebra.
• Different operations are:
• Outer Join Operation
• Generalized Projection
• Aggregate Function

Subash Manandhar 23
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Outer Join Operation
• Natural join operation returns only those tuples that have matching value
in common attributes of both relations.
• It eliminates tuples that do not have matching value in common attribute of
another relation.
• Outer Join is used to display rows in the result that do not have matching
values in the join column.
• Different outer join operations are:
• Left Outer Join
• Right Outer Join
• Full Outer Join

Subash Manandhar 24
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Left Outer Join
• In left outer join of two relations R and S, matching tuples from both
relations as well as tuples from R that do not have matching values in
common in S are included in result relation.
• Denoted by

• Right Outer Join


• In right outer join of two relations R and S, matching tuples from both
relations as well as tuples from S that do not have matching values in
common in R are included in result relation.
• Denoted by

Subash Manandhar 25
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Full Outer Join
• Here matching tuples from both relations as well as tuples from both
relations (R and S) that do not have matching values in common are also
included in result relation.
• Denoted by
• e.g.
Rid Bid Rname Rating Bid Title Price Year
Ro1 B01 Ram 8 B01 DBMS 500 2018
Ro2 B02 Hari 9 B02 C++ 650 2011
R03 B03 Sita 8 B03 JAVA 800 2015
Ro4 NULL Gita 7 B04 SEP 700 2019
ReviewBy Book

Subash Manandhar 26
Chapter 3 : Relational Model
3.3 The Relational Algebra

• Book ReviewBy

Bid Title Price Year Rid Rname Rating


B01 DBMS 500 2018 R01 Ram 8
B02 C++ 650 2011 R02 Hari 9
B03 JAVA 800 2015 R03 Sita 8
B04 SEP 700 2019 NULL NULL NULL

Subash Manandhar 27
Chapter 3 : Relational Model
3.3 The Relational Algebra

• Book ReviewBy

Bid Title Price Year Rid Rname Rating


B01 DBMS 500 2018 R01 Ram 8
B02 C++ 650 2011 R02 Hari 9
B03 JAVA 800 2015 R03 Sita 8
NULL NULL NULL NULL Ro4 Gita 7

Subash Manandhar 28
Chapter 3 : Relational Model
3.3 The Relational Algebra

• Book ReviewBy

Bid Title Price Year Rid Rname Rating


B01 DBMS 500 2018 R01 Ram 8
B02 C++ 650 2011 R02 Hari 9
B03 JAVA 800 2015 R03 Sita 8
B04 SEP 700 2019 NULL NULL NULL
NULL NULL NULL NULL Ro4 Gita 7

Subash Manandhar 29
Chapter 3 : Relational Model
3.3 The Relational Algebra

• Generalized Projection:
• It extends the projection operation by allowing arithmetic functions to be
used in projection list .
• Syntax:
• ∏F1,F2…..Fn (E)
• Where F1…Fn represents arithmetic expressions and E represents relational algebra
expression.
• E.g. Display name, job and 10% of salary of all employees from employee
relation.
• ∏name,job, salary*0.1 (employee)

Subash Manandhar 30
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Aggregate Operation:
• SUM(). MAX(), MIN(), COUNT(), AVG() are aggregate functions used in
relational algebra.
• Syntax:
• g1,g2……gnĢ F1(A1), F2(A2)………….Fn(An)(E)
• Where E represents relational algebra expression, each Fi is aggregate function, each
Ai is an attribute value and g1……gn is a list of attributes on which to group(can be
empty)
• E.g. 1. Find Average salary of employee.
• Ģ AVG(salary)(employee)
• 2. Find Maximum salary of programmers.
• Ģ MAX(salary)(σ job = “Programmer”(employee))
• 3. Find Total no of employees jobwise.
• jobĢ COUNT(eid)(employee)

Subash Manandhar 31
Chapter 3 : Relational Model
3.3 The Relational Algebra
• Modification of Database:
• Mainly three operations for modification of database are insertion,
deletion and update.
• All these operations can be expressed using assignment operator.
INSERTION:
• Syntax:
• r  r U E where r represent relation and E represent constant tuple or relational
algebra expression.
• E.g. To insert a record {105,”Gita”,”Patan”,”Analyst”,35000} in employee
relation.
• employee  employee U {105,”Gita”,”Patan”,”Analyst”,35000}

Subash Manandhar 32
Chapter 3 : Relational Model
3.3 The Relational Algebra
DELETION:
• Syntax:
• r  r - E where r represent relation and E represent constant tuple or relational
algebra expression.
• E.g. To delete records of employee having salary less than 5000 from
employee relation.
• employee  employee – (σsalary < 5000 (employee))
UPDATE:
• Generalized projection is used.
• Syntax:
• r  ∏F1,F2…..Fn (E)
• r  ∏F1,F2…..Fn (σpredicate (r)) U σnot predicate (r)

Subash Manandhar 33
Chapter 3 : Relational Model
3.3 The Relational Algebra
UPDATE:
• Examples:
1. Update salary of all employee by 10%
• employee  ∏eid,name ,address,job,salary=salary+salary*0.1 (employee)
2. Update salary of programmer to 60000
• employee  ∏ eid,name ,address,job,salary=60000(σjob=“programmer”(employee)) U σjob!=“programmer”(employee)

Subash Manandhar 34

You might also like