LM 5 - Constraints Relational Algebra
LM 5 - Constraints Relational Algebra
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Course Outcome
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Syllabus
UNIT I - RELATIONAL DATABASES
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
TOPIC
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
• 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)
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Select Operation(Cont.)
• 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
• 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.
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
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
• 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,
Types
Conditional Join
Equi Join
Natural Join
Conditional join
• 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
• 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
• 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