0% found this document useful (0 votes)
35 views47 pages

LM 5 - Constraints Relational Algebra

The document discusses database integrity constraints in the relational model. It describes the different types of integrity constraints including entity integrity, referential integrity and database integrity. It also provides examples to explain anomalies like insert, update and delete anomalies that can occur due to lack of integrity constraints.

Uploaded by

Bala Krish
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)
35 views47 pages

LM 5 - Constraints Relational Algebra

The document discusses database integrity constraints in the relational model. It describes the different types of integrity constraints including entity integrity, referential integrity and database integrity. It also provides examples to explain anomalies like insert, update and delete anomalies that can occur due to lack of integrity constraints.

Uploaded by

Bala Krish
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/ 47

KGiSL Institute of Technology

(Approved by AICTE, New Delhi; Affiliated to Anna University, Chennai)


Recognized by UGC, Accredited by NBA (IT)
365, KGiSL Campus, Thudiyalur Road, Saravanampatti, Coimbatore – 641035.

Department of Computer Science and Engineering

Name of the Faculty : Ms. Aruna T N

Subject Name & Code : CS3492/ Database Management Systems

Branch & Department : Computer Science and Engineering

Year & Semester : II / IV

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Course Outcome

CO 1 Demonstrate fundamentals of Data models and Relational


databases - K3 LEVEL

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Syllabus
UNIT I - RELATIONAL DATABASES

Purpose of Database System – Views of data – Data Models –


Database System Architecture – Introduction to relational databases
– Relational Model – Keys – Relational Algebra – SQL
fundamentals – Advanced SQL features – Embedded SQL–
Dynamic SQL

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
TOPIC

Database Integrity Constraints in


Relational Model

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Integrity Constraints
• Database Integrity means correctness or accuracy of data in the database.
• Eg: Employee ID must consists of two digits
• There are three main integrity constraints
• Entity Integrity Rule
• Referential Integrity Rule
• Database Integrity
Entity Integrity Rule
• “In a relation, the value of attribute of primary key cannot be NULL”
• NULL : Represents a value for an attribute that is currently unknown
or not applicable.
• Primary key helps in uniquely identifying every row in the table. If the
users want to retrieve any row from the table, then they must know the
value of the key for that row.
• Hence it is necessary that the primary key should not have NULL
value.
Referential Integrity Rule
• “Whenever a foreign key value is used it must reference a valid , existing primary key in
the parent table”
• It refers to the accuracy and consistency of data within a relationship.
• Eg: Two Tables: Employees and Managers
• Employee table : Foreign key attribute ManagedBy which points to the record for each
employee’s manager in the Managers table
You cannot add a record to the Employees table unless ManagedBy attribute points to a
valid record in the Managers table
If the primary key for a record in the Managers table changes, all corresponding records
in the Employees table are modified.
If a record in the Managers table is deleted, all corresponding records in the Employees
table are deleted.
Referential Integrity - Advantages

• Prevents the entry of duplicate data

• Prevents one table from pointing to a nonexistent field in another table

• Guaranteed consistency between partnered tables

• Prevents the deletion of a record that contains a value referred to by a foreign


key in another table
• Prevents the addition of a record to a table that contains a foreign key unless
there is a primary key in the linked table
Database Integrity
• The foreign key is a key in one table that refers to the primary key of
another table Customer Table
• Eg: CustID Name City
• CustID in the Customer
C101 AAA Chennai
table is the primary key
C102 BBB Coimbatore in the Customer table.
C103 CCC Mumbai
• CustID in the Order
table is a foreign key in
Order Table the Order table
OrderID Description CustID
111 Pen C101
222 Pencil C102
333 Eraser C103
Database Integrity

• The Foreign Key constraint is used to prevent actions that would


destroy links between tables.

• The Foreign key constraint also prevents invalid data from being
inserted into the foreign key column, because it has to be one of the
values contained in the table it points to
Other Constraints
Domain Constraints:
• These are attribute level constraints.
• An attribute can only take values which lie inside the domain range.
• Eg,; If a constraint AGE>0 is applied on STUDENT relation, inserting negative value
of AGE will result in failure.
Key Integrity:
• Every relation in the database should have at least one set of attributes which defines a tuple
uniquely.
• Those set of attributes is called key. Eg: ROLL_NO in STUDENT is a key.
• No two students can have same roll number.
• So a key has two properties:
• It should be unique for all tuples.
• It can’t have NULL value
Anomalies in Relational Model
⮚ Any unexpected behavior while working with the relational databases, there may
be a presence of too much redundancy in the data stored in the database.
⮚ This can cause anomalies in the DBMS
⮚ Database anomaly is a flaw in databases because of poor planning and storing
everything in a flat database.
⮚ Anomalies occur when there is too much redundancy in the database.
⮚ Types of Anomalies
1) Insert anomaly
2) Update anomaly
3) Delete anomaly
Anomalies in Relational Model
Example
Insert Anomaly
• If a tuple is inserted in referencing relation and referencing
attribute value is not present in referenced attribute, it will not
allow inserting in referencing relation.
Example:
• Assume that a new employee is joining the company under training
and not assigned to any department.
• Then, we would not insert the data into the table if the emp_dept
field doesn't allow nulls.
Update Anomaly
• If a tuple is inserted in referencing relation and referencing attribute value is
not present in referenced attribute, it will not allow inserting in referencing
relation.
Example:
• In the given table, we have two rows for an employee named Rick, and he
belongs to two different departments of the company. If we need to update
Rick's address, we must update the same address in two rows. Otherwise, the
data will become inconsistent.
• If, in some way, we can update the correct address in one department but not
the other, then according to the database, Rick will have two different
addresses, which is not correct and would lead to inconsistent data.
Delete Anomaly
• If a tuple is deleted from referenced relation and the referenced attribute value is
used by referencing attribute in referencing relation, it will not allow deleting the
tuple from referenced relation.

Example:

Assume that if the company closes the department D890, then deleting
the rows that have emp_dept as D890 would also delete the information of
employee Maggie since she is assigned only to this department.
TOPIC

Relational Algebra

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Relational Algebra
• There are two formal query languages associated with relational model , they
are, Relational Algebra and Relational Calculus.
• Relational Algebra: A procedural language consisting of a set of operations
that take one or two relations as input and produce a new relation as their
result.
• It is a procedural query language which is used to access database tables to
read data in different ways.
• The queries present in the relational algebra are denoted using Operators.
• Each relational algebra is procedural. (Step-by-step procedure for computing
the desired answer)
Relational Operations - Types
• Selection (  )
• Projection (  )
• Cartesian Product ( x )
• Set Operations
• Union (  )
• Intersection ( n )
• Set Difference ( – )
• Join Operations
• Conditional Join
• Equal Join
• Natural Join
Select Operation
• The select operation selects tuples that satisfy a given predicate.
• Notation:  p (r)
• p is called the selection predicate
• Eg: select those tuples of the instructor relation where the instructor is in the
“Physics” department.
Select Operation (Cont.)
• We allow comparisons using
=, , >, . <.  in the selection predicate.
• We can combine several predicates into a larger predicate by using the
connectives:
 (and),  (or),  (not)

• Eg: Find the instructors in Physics with a salary greater $90,000,


 dept_name=“Physics”  salary > 90,000 (instructor)

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Select Operation(Cont.)

• The select predicate may include comparisons between two attributes.

• Eg: find all departments whose name is the same as their building
name:
 dept_name =building (department)

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Projection Operation

• It is used to project only a certain set of attributes of a relation.

• Eg: If you want to see only the names of all the students in the Student
Table.

• Syntax:

C1, C2
Project Operation Example

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Cartesian - Product Operation
• The Cartesian-product operation (denoted by X) used to combine data from two
different relations into one and fetch data from the combined relation.

• Eg: the Cartesian product of the relations instructor and teaches is written as:

instructor X teaches

• Eg:

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Set Operation
• Various Set operations are
• Union (  )
• Intersection ( n )
• Set Difference ( – )
Union Operation
• The union operation allows us to combine two relations

• Notation: r  s

• For r  s to be valid.

1. r, s must have the same arity (same number of attributes / columns)


2. The attribute domains must be compatible (example: 2nd column of r deals
with the same type of values as does the 2nd column of s)

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Example
Student Book
S.No sname age Isbn bname Author
1 AAA 20 005 DBMS XYZ
2 BBB 15 006 OS ZZZ
3 CCC 10 007 ALGO PQR
4 DDD 12

We want to display both the student name and book names


from both the tables then,

 sname ( Student )   bname (Book)


Set-Intersection Operation
• The set-intersection operation allows us to find tuples that are in both the
input relations.
• To fetch data from both tables which is common in both the tables
• Notation: r  s
• Assume:
r, s have the same arity
attributes of r and s are compatible

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Example
Student Worker
Name Branch Name Salary
AAA CSE XYZ NULL
BBB IT BBB 35000
CCC Mech CCC 40000
DDD Civil PQR NULL

We want to find out the names of the students who are


working in a company then,
Name
 name ( Student ) n  name (Worker) BBB
CCC
Set Difference Operation
• The set-difference operation allows us to find tuples that are in one relation
but are not in another.

• Notation: r – s
• Set differences must be taken between compatible relations.
• r and s must have the same arity
• attribute domains of r and s must be compatible

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Example
• Two relations Full_Time_Employee and Employee, if we want to find
out all the employee working for fulltime, then the set difference
operator is used,

 Empname ( Full_Time_Employee ) -  Empname ( Employee )


The Rename Operation
• The results of relational-algebra expressions do not have a name that we can use to
refer to them. The rename operator,  , is provided for that purpose
• The expression:
x (E) returns the result of expression E under the name x
• Another form of the rename operation:

x(A1,A2, .. An) (E)


Eg: Query to rename the relation Student as Male Student and the attributes of
Student – RollNo, SName as (Sno, Name).
ρ MaleStudent(Sno, Name) πRollNo, SName(σCondition(Student))
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Join
• The join operation is used to combine information from two or more
relations.
• It can be defined as a cross product followed by selections and projections.
• The join operator is used as
• Inner Join
• Conditional join
• Equii Join
• Natural Join
• Outer Join
• Left Outer Join
• Right Outer Join
• Full Outer join
Inner Join

Types
Conditional Join
Equi Join
Natural Join
Conditional join

• It is an operation in which information from two tables is combined using some


condition using some condition that is specified with Join operator.

• Eg: If we want the names of students with Sid(Student) = Sid(Reserve) and isbn
= 005, then we can write it using Cartesian product as

Query: ( ((Student.Sid = Reserve.Sid) ^ (Reserve.(isbn) = 005))(Student


x Reserve))
• There are 2 conditions: i) Student.Sid = Reserve.Sid and ii) Reserve.(isbn) = 005
Example

Query using Conditional Join:


(Student Student.Sid = Reserve.Sid) ^ (Reserve.(isbn)
= 005) Reserve)
Equii Join

• There is a equality condition between two attributes (columns) of


relations(tables).

• Eg: If we have two tables, Book and Reserve , and we want to find the
book which is reserved by the student having isbn 005 and name of the
book is ‘DBMS’,
Example
Book Reserve
isbn bname Author Sid isbn Date
005 DBMS XYZ 1 005 15-03-24
006 OS PQR 2 006 16-03-24
007 ALGO ABC 3 007 17-03-24

 bname = ‘DBMS’ (Book (Book.isbn = Reserve.isbn)


Reserve)
Natural Join
• When there are common columns and we have to equate these
common columns then we use natural join.
• Eg:
Book Reserve
isbn bname Author Sid isbn Date
005 DBMS XYZ 1 005 15-03-24
006 OS PQR 2 006 16-03-24
007 ALGO ABC 3 007 17-03-24
If we want to list the books that are reserved , then that means we want to match
Books.isbn with Reserve.isbn. Then,
Books
Outer Join
• Left Outer Join
• Right Outer Join
• Full Outer join
Left Outer Join / Left Join
• This is a type of join in which all the records from left table are
returned and the matched records from the right table gets returned.
• The rest is NULL from the right side, if there is no match
• The symbol used is ⟕
Syntax:
Select table1.column1, table2.column2,….. From table1 LEFT
JOIN table2 ON table1.common_field = table2.common_field;
Left Outer Join - Example
Course
Student
Rollno CourseName
Rollno sname 1 DBMS
1 AAA
2 OS
2 BBB
3 ALGO
3 CCC
4 AI

Left Outer Join


Rollno Name CourseName
1 AAA DBMS
2 BBB OS
3 CCC ALGO
Right Outer Join / Right Join
• This is a type of join in which all the records from right table are
returned and the matched records from the left table gets returned.
• The rest is NULL from the left side, if there is no match
• The symbol used is ⟖
Syntax:
Select table1.column1, table2.column2,….. From table1 RIGHT
JOIN table2 ON table1.common_field = table2.common_field;
Right Outer Join - Example
Course
Student
Rollno CourseName
Rollno sname 1 DBMS
1 AAA
3 OS
2 BBB
5 ALGO
3 CCC
6 AI

Right Outer Join


Rollno Name CourseName
1 AAA DBMS
3 CCC OS
5 NULL ALGO
6 NULL AI
Full Outer Join
• In a full outer join, all tuples from both relations are included in the result,
irrespective of the matching condition.

• It is denoted by
Syntax:
Select table1.column1, table2.column2,….. From table1 FULL JOIN
table2 ON table1.common_field = table2.common_field;
Full Outer Join - Example
Student Course
Rollno sname Rollno CourseName
1 AAA 1 DBMS
2 BBB 3 OS
3 CCC 5 ALGO
6 AI
Rollno Name CourseName
1 AAA DBMS
2 BBB NULL
3 CCC OS
Full Outer Join
5 NULL ALGO
6 NULL AI

You might also like