3cs518ic24 Dbms Minor - Unit 2 (A)
3cs518ic24 Dbms Minor - Unit 2 (A)
Model
Unit-2
Cardinality = No of
Rows or h Record (Tuple): The horizontal
Tuples or component of a table, consisting of a
103 Mayu CE 3 6
Records sequence of values, one for each
r
tuples (7)
(7) column of the table. It is also known as
104 Niles EE 3 9 row.
h
A database consists of a collection of
105 Hites CI 3 7 tables (relations), each having a unique
h name.
Degree = No of
Domain 106is a setTarun ME
columns
of all 3 unique 8
(5)
possible values for
a specific107
column.Sures CE 3 9
Domain of Branch h attribute is (CE, CI, ME, EE)
(DBMS) Unit 2 – Introduction to Relational Model 3
Relation Schema and Instance
A1, A2, …, An are attributes
R = (A1, A2, …, An ) is a relation schema
Example:
instructor = (ID, name, dept_name, salary)
A relation instance r defined over schema R is denoted by r
(R).
The current values a relation are specified by a table
An element t of relation r is called a tuple and is
represented by a row
in a table
Relation=table
Tuple=row
Attribute=column
Instance
Relational Database schema(related
relations)
• student (ID, name, dept name, tot cred)
• advisor (s id, i id)
• takes (ID, course id, sec id, semester,
year, grade)
• classroom (building, room number,
capacity)
• time slot (time slot id, day, start time,
end time)
Roll Branc Se SP B
EnrollNo Name
No h m I L
• Adding zero or more attributes
190540107 101 CE 3 8 Raju 0 to candidate key generates
001 super key.
190540107 102 CE 3 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 r
190540106 102 CI 3 7 Niles 1
002 h
180540107 101 CE 5 7 Niles 1
001 h
(DBMS) Unit 2 – Introduction to Relational Model 9
Candidate Key
A candidate key is a subset of a super key.
The minimal set of attribute which can uniquely identify a tuple is known as candidate
key. E.g. EnrollNo
The value of Candidate Key is unique and non-null for every tuple.
There can be more than one candidate key in a relation
Every candidate key is a super key but every super key is not a candidate key.
The candidate keyCandidate Candidate
can be simple (having Key
only one attribute) or composite as well
Key (RollNo, Branch,
EnrollNo Sem)
Roll Branc Se SP B
EnrollNo Name
No h m I L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 (DBMS) r Model
Unit 2 – Introduction to Relational 10
Primary Key
A primary key is a candidate key that is chosen by database
designer to identify tuples uniquely in a relation (table).
Primary
Key
Candidate Candidate Key
Key (RollNo, Branch,
EnrollNo Sem)
Roll Branc Se SP B
EnrollNo Name
No h m I L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 (DBMS) r Model
Unit 2 – Introduction to Relational 11
Alternate Key
The candidate key other than the primary key is called an alternate
key.
An alternate key is a candidate key that is not chosen by database
designer to identify tuples uniquely in a relation.
Primary Alternate
Key Key
Candidate Candidate Key
Key (RollNo, Branch,
EnrollNo Sem)
Roll Branc Se SP B
EnrollNo Name
No h m I L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 (DBMS) r Model
Unit 2 – Introduction to Relational 12
Primary Key rules
A primary key may have one or more attributes.
There is only one primary key in the relation (table).
A primary key attribute value cannot be NULL.
Generally, the value of a primary key attribute does not change.
Stud Proje
ent Nam Branc Se ct
EnrollNo Projec
e h m Title EnrollNo
tID
190540107 Raju CE 3 101 Bank 190540107
001 001
190540107 Mites CE 3 102 College 190540107
002 h 002
190540107 Niles CE 3 103 School 190540107
003 h (DBMS) Unit 2 – Introduction to Relational Model003 14
Foreign Key: If an attribute can only take the values which are present as
values of some other attribute.
The relation which is being referenced is called referenced relation and the
corresponding attribute is called referenced attribute and the relation
which refers to the referenced relation is called referencing relation and
the corresponding attribute is called referencing attribute.
The referenced attribute of the referenced relation should be the primary
key for it. For Example, EnrollNO in Project is a foreign key of the
relation.
It may be worth noting that unlike, Primary Key of any given relation,
Foreign Key can be NULL as well as may contain duplicate tuples i.e. it
need not follow uniqueness constraint.
For Example, EnrollNO in Project relation is not unique. It has been
repeated for the first and fourth tuple. However, the EnrollNO in Student
relation is a primary key and it needs to be always unique and it cannot be
null.
(DBMS) Unit 2 – Introduction to Relational Model 15
1. Candidate Key: are individual columns in a table that qualifies for uniqueness of all the rows.
Here in Employee table EmployeeID & SSN are Candidate keys.
2. Primary Key: is the columns you choose to maintain uniqueness in a table. Here in Employee
table you can choose either EmployeeID or SSN columns.
3. Super Key: If you add any other column/attribute to a Primary Key(candidate key) then it become
a super key, like EmployeeID + FullName is a Super Key.
4. Alternate Key: Candidate column other the Primary column, like if SSN is PK then EmployeeID
would be the Alternate key.
This relation satisfies the tuple uniqueness constraint since here all the
tuples are unique.
The second relation does not satisfy the tuple uniqueness constraint since
here all the tuples are not
(DBMS)unique.
Unit 2 – Introduction to Relational Model 19
3. Key Constraint-
Key constraint specifies that in any relation-
All the values of key must be unique.
This relation does not satisfy the key constraint as here all the values of primary key are
not unique.
This relation does not satisfy the entity integrity constraint as here the
primary key contains a NULL value.
(DBMS) Unit 2 – Introduction to Relational Model 21
5. Referential Integrity Constraint-
This constraint is enforced when a foreign key references the primary key of a
relation.
It specifies that all the values taken by the foreign key must either be available in the
relation of the primary key or be null.
The following two important results emerges out due to referential integrity
constraint-
We can not insert a record into a referencing relation if the corresponding record
does not exist in the referenced relation.
We can not delete or update a record of the referenced relation if the corresponding
record exists in the referencing relation.
a. DELETION :
ON DELETE NO ACTION : If referential integrity violation has occurred, the deletion would not
be allowed.
ON DELETE CASCADE : In the case of referential integrity violation, the tuple would be deleted
from both the relation i.e. Referenced as well as Referencing Relation. Whenever Primary Key
is deleted, the corresponding tuple in the referencing relation is also deleted.
ON DELETE SET NULL : In the case of violation, then the values of FOREIGN KEY are set to
NULL. If the FOREIGN KEY is a NOT NULL attribute , then ON DELETE SET NULL = ON DELETE
NO ACTION. (DBMS) Unit 2 – Introduction to Relational Model 24
b. UPDATION :
ON UPDATE NO ACTION : Updation would be restricted
ON UPDATE CASCADE : Updation would be performed in both the referenced as well as
referencing relation.
ON UPDATE SET NULL : Referencing attribute tried to set NULL, if its a success , updation of
referenced relation is allowed.
2. Referencing Relation :
INSERTION : Violation might occur. If the violation occurs, then insertion gets
restricted.
DELETION : No violation occurs.
UPDATION : Referencing attribute may cause violation, in that case updation is not
allowed.
a) Set union:
b) Set intersection : ∩
c) Set difference: --
d) Division: /
e) rename:
f) Assignment:
(DBMS) Unit 2 – Introduction to Relational Model 29
Relational Algebra Operations
Operator Description
Selection Display particular rows/records/tuples from a relation
Projection Display particular columns from a relation
Cross Product Multiply each tuples of both relations
Combine data or records from two or more tables
1. Natural Join / Inner Join
Joins 2. Outer Join
1. Left Outer Join 2. Right Outer Join 3. Full Outer Join
Exam Display the detail of students belongs to “CE” Branch and having
ple SPI more than 8.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
er (Student)
Outp
ut
Roll Nam Branc SP
No e h I
104 Meet CE 9
Stud Outp
ent
Roll Nam Branc SP ut
Roll Nam Branc
No e h I No e h
101 Raju CE 8 101 Raju CE
102 Mites ME 9 102 Mites ME
h h
103 Niles CI 9 103 Niles CI
h h
104 Meet CE 9 (DBMS) Unit 1 – Overview and Architecture of104 Meet CE 36
Exercise
Write down the relational algebra for the student Stud
ent
table. Roll Nam Branc SP
No e h I
Display RollNo, Name and SPI of all students.
101 Raj CE 6
Display Name and SPI of all students.
Display the Name of all students. 102 Meet ME 8
Display the Name of all branches. 103 Hars EE 7
h
104 Punit CE 9
Write down the Emplo
relational algebra for the yee
employee table. EmpI Nam Sala
Dept
D e ry
Display EmpID with Name of all employee.
101 Niles Sales 1000
Display Name and Salary of all employee. h 0
Display the Name of all employee. 102 Mayu HR 2500
Display the Name of all departments. r 0
103 Hardi HR 1500
k 0
104 Ajay Admi 2000
(DBMS) Unit 2 – Introduction to Relational Model n 0
37
2.5 Combined Projection & Selection
Operation
Exam Display RollNo, Name & Branch of “ME”
ple Branch students.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σ CE
Branch=‘ME’ 7 Answ ∏ RollNo, Name, Branch (σBranch=‘ME’
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Branc SP t-2
Roll Nam Branc
No e h I No e h
102 Mites ME 9 102 Mites ME
h h
(DBMS) Unit 1 – Overview and Architecture of
38
Combined Projection & Selection Operation
Exam Display Name, Branch and SPI of students whose SPI is
ple more than 8.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σSPI>8CE 7 Answ ∏ Name, Branch, SPI (σSPI>8
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Branc SP t-2
Nam Branc
SPI
No e h I e h
102 Mites ME 9 Mites ME 9
h h
103 Niles CI 9 (DBMS) Unit 1 – OverviewNiles CI
and Architecture of 9 39
Combined Projection & Selection Operation
Exam Display Name, Branch and SPI of students who belongs to “CE” Branch and
ple SPI is more than 7.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σBranch=‘CE’
CE 7
Λ SPI>7 Answ ∏ Name, Branch, SPI (σBranch=‘CE’ Λ SPI>8
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Branc SP t-2
Nam Branc
SPI
No e h I e h
101 Raju CE 8 Raju CE 8
Steps performed in
Natural Description
Steps Join
Step – 1 It performs Cartesian Product
Step – 2 Then it deletes inconsistent tuples
Then it removes an attribute from duplicate
Step – 3
attributes
Example:
R = (A, B, C, D)
S = (E, B, D)
Result schema = (A, B, C, D, E)
r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B(DBMS)
r.D =s.D (r1 –xOverview
Unit s)) and Architecture of
47
Relations r, s:
A B C D B D E
1 a 1 a
2 a 3 a
4 b 1 a
1 a 2 b
2 b 3 b
r s
r s
A B C D E
1 a
1 a
1 a
1 a
2 b
Exam List the name of students with their department name and SPI of all student
ple belong to “CE” department.
Answ ∏ Sname, Dname, (σDname=‘CE’ (Student (Department
er SPI Academic)))
Exam Display the name of students with their project name whose guide
ple is “Anil”.
Answ ∏ Sname, (σFname=‘Anil’ (Student (Guide
er Pname Faculty)))
(DBMS) Unit 2 – Introduction to Relational Model 51
Exercise: Write down relational algebra for the following tables/relations
Relations
Student (Rno, Sname, Address, City, Mobile)
Department (Did, Dname)
Academic (Rno, Did, SPI, CPI, Backlog)
Guide (Rno, PName, Fid)
Faculty (Fid, Fname, Subject, Did, Salary)
List the name of students with their department name having backlog 0.
List the name of faculties with their department name and salary having salary
more than 25000 and belongs to “CE” department.
List the name of all faculties of “CE” and “ME” department whose salary is more
than 50000.
Display the students name with their project name of all “CE” department’s
students whose guide is “Z.Z. Patel”.
Display the name of faculties with their department name who belongs to “CE”
department and tough “CPU” subject having salary more than 25000.
List the name of students(DBMS)
with their department name doing project “Hackathon”
Unit 1 – Overview and Architecture of
52
4(a)Theta Join Operation
The join operation allows us to combine a select operation and a
Cartesian-Product operation into a single operation.
Consider relations r (R) and s (S)
Let “theta” be a predicate on attributes in the schema R “union” S. The
join operation r s is defined as follows:
Thus
instructor.id = teaches.id (instructor x teaches ))
Can equivalently be written as
Three types of
Outer Join Sym
Sr. Outer Join
1 Left Outer Join bol
2 Right Outer Join
3 Full Outer Join
Exerci Is there any difference in the output if we swap the tables in Intersection.
se (Employee) ∩ (Customer).
(DBMS) Unit 1 – Overview and Architecture of
65
6(c) Minus/ Set difference Operator
Symbol: −
Notation: Relation-1 (R1) − Relation-2 (R2) OR Algebra-1 − Algebra-2
Operation:
It displays all the tuples/records belonging to the first relation (left relation) but not in
the second relation (right relation).
Exerci Is there any difference in the output if we swap the tables in Set difference.
se (Employee) − (Customer).
(DBMS) Unit 1 – Overview and Architecture of
66
Union Operators Example
A B B B B
Sn 1 2 3 4
PNo PNo PNo PNo PNo
o
P2 P2 P1 P2
S1 P1
P4 P2 P5
S1 P2
P4
S1 P3
Alge (A) ÷ Alge (A) ÷ Alge (A) ÷ Alge (A) ÷
S1 P4
bra (B1) bra (B2) bra (B3) bra (B4)
S2 P1 Outp Outp Outp Outp
ut ut ut ut
S2 P2 SNo SNo SNo SNo
S3 P2 S1 S1 S1
S4 P2 S2 S4
S4 P4 S3
S5 P4 S4
Operation:
The rename operation is used to rename the output relation.
The result of rename operator are also relations with new name.
The original relation name can not be changed when we perform rename
operation on any relation.
How to use:
ρ x (E)
Returns a relation E under a new name X.
ρ A1, A2. …,An (E)
Returns a relation E with the attributes renamed to A1, A2, …., An.
ρ x(A1, A2. …,An) (E)
Returns a relation E under a new name X with the attributes renamed to A1, A2,
…., An.
(DBMS) Unit 1 – Overview and Architecture of
77
Rename Operator Example
Exam Rename Exam Rename
ple table ple attributes
Stud Stud
ent ent
Rn Na CP
RN Na CP
o me I o me I
10 Raj 8 10 Raj 8
1 1
10 Meet 9 10 Meet 9
2 2
AlgeJay ρPerson
10 7 AlgeJay ρ(RollNo,
10 7 StudentName, SPI)
3
bra (Student) 3
bra (Student)
Perso Stud
n ent
Roll StudentN SP
RN Na CP
o me I No ame I
10 Raj 8 101 Raj 8
1
102 Meet 9
10 Meet 9
2 103 Jay 7
10 Jay 7 (DBMS) Unit 2 – Introduction to Relational Model 78
Rename Operator Example
Exam Rename table and Exam Rename particular
ple attributes both ple attributes
Stud Stud
ent
Rn Na CP ent
Rn Na CP
o me I o me I
10 Raj 8 10 Raj 8
1 1
10 Meet 9 10 Meet 9
2 2
AlgeJay ρPerson
10 7 (RollNo, StudentName) (∏ RNo, Name AlgeJay ρStudentName
10 7 / Name
3
bra (Student)) 3
bra (Student)
Perso Stud
n
Roll StudentN ent
Rn StudentN CP
No ame o ame I
101 Raj 10 Raj 8
102 Meet 1
10 Meet 9
103 Jay 2
(DBMS) Unit 2 – Introduction to Relational Model 79
Rename Operator Example
Exam Find out maximum CPI from Step- ρA (Student) X ρB
ple student table. 1 (Student)
Stud Outpu
ent
Rn Na CP t-1
A.Rn A.Na A.C B.Rn B.Na B.C
o me I o me PI o me PI
10 Raj 8 101 Raj 8 101 Raj 8
1
101 Raj 8 102 Meet 9
10 Meet 9
2 101 Raj 8 103 Jay 7
10 Jay
Step- 7σ
A.CPI<B.CPI (ρA (Student) X ρB 102 Meet 9 101 Raj 8
3
2 (Student)) 102 Meet 9 102 Meet 9
Outpu
t-2 102 Meet 9 103 Jay 7
A.Rn A.Na A.C B.Rn B.Na B.C
o me PI o me PI 103 Jay 7 101 Raj 8
101 Raj 8 102 Meet 9 103 Jay 7 102 Meet 9
103 Jay 7 101 Raj 8 103 Jay 7 103 Jay 7
103 Jay 7 102 Meet 9
(DBMS) Unit 2 – Introduction to Relational Model 80
Rename Operator Example
Exam Find out maximum CPI from
ple student table.
Stud
Step- ∏ A.CPI (σA.CPI<B.CPI (ρA (Student) X ρB
ent
Rn Na CP 3 (Student)))
o me I Outpu
10 Raj 8 t-3
A.CPI
1
8
10 Meet 9
2 7
10 Jay
Step- 7σ
A.CPI<B.CPI (ρA (Student) X ρB
3
2 (Student))
Outpu
t-2
A.Rn A.Na A.C B.Rn B.Na B.C
o me PI o me PI
101 Raj 8 102 Meet 9
103 Jay 7 101 Raj 8
103 Jay 7 102 Meet 9
(DBMS) Unit 1 – Overview and Architecture of
81
Rename Operator Example
Exam Find out maximum CPI from
ple student table.
Stud
Step- ∏ A.CPI (σA.CPI<B.CPI (ρA (Student) X ρB
ent
Rn Na CP 3 (Student)))
o me I Outpu
10 Raj 8 t-3
A.CPI
1
8
10 Meet 9
2 7
10 Jay 7
Step-
3 ∏ CPI (Student) − ∏ A.CPI (σA.CPI<B.CPI (ρ A (Student) X ρB
4
(Student)))
Stud Outpu Outpu
ent t-3 t
CPI A.CPI CPI
8 − 8 = 9
9 7
7
(DBMS) Unit 1 – Overview and Architecture of
82
Aggregate Functions
Symbol: g or G
Notation: g (Relation)
function-name(column), function-name(column), …, function-name(column)
Operation:
It takes a more than one value as input and returns a single value as output
(result).
Aggregate functions are:
Sum (It returns the sum (addition) of the values of a column.)
Max (It returns the maximum value for a column.)
Min (It returns the minimum value for a column.)
Avg (It returns the average of the values for a column.)
Count (It returns total number of values in a given column.)
Query 2
dept_name=“Physics” ( salary > 90.000 (instructor))
The two queries are not identical; they are, however, equivalent -- they give
the same result on any database.
Query 2
(dept_name=“Physics” (instructor)) instructor.ID = teaches.ID teaches
The two queries are not identical; they are, however, equivalent -- they
give the same result on any database.
2.Find the names of all employees whose salary is greater than $100,000.
3.Find the names of all employees who live in “Miami” and whose salary is greater than
$100,000.
• Find the names of all borrowers who have a loan in branch “Downtown”.
Find the loan number for each loan of an amount loan-number (amount > 1200 (loan))
greater than $1200
Find the names of all customers who have a loan, an account, or both,
from the bank
customer-name (borrower) customer-name (depositor)
(DBMS) Unit 2 – Introduction to Relational Model 93
branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
Find the names of all customers who have a loan and an account at bank .
Find the names of all customers who have a loan at the Perryridge branch.
1 A 50 1 A 50
0 0
2 B 30 2 B 30
0 0
3 account
C 70 3d(account)
C 70
0 0 1 A 50 1 A 50
1 A 50 1 A 50
0 0 0 0
1 A 50 2 B 30 1 A 50 2 B 30
0 0 0 0
1 A 50 3 C 70 1 A 50 3 C 70
0 0 0 0
account x d
2 B 30 1 A 50 2 B 30 1 A 50
0 0 0 0
2 B 30 2 B 30 2 B 30 2 B 30
0 0 0 0
2 B 30 3 C 70 2 B 30 3 C 70
0 0 0 0
3 C 70 1 A 50 3 C 70 1 A 50
0 (DBMS)
0 Unit 2 – Introduction to Relational Model 0 0 95
Example 4
Suppliers(sid, sname, address)
Parts(pid, pname, color)
Catalog(sid, pid, cost)