0% found this document useful (0 votes)
19 views92 pages

Unit - 2

The document provides an overview of database management systems, focusing on key concepts such as primary keys, foreign keys, and various types of keys in relational databases. It discusses pitfalls in database design, including data redundancy and normalization issues, and offers strategies for mitigating these problems. Additionally, it introduces Tuple Relational Calculus (TRC) as a non-procedural query language for relational databases, explaining its syntax and logical operators.
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)
19 views92 pages

Unit - 2

The document provides an overview of database management systems, focusing on key concepts such as primary keys, foreign keys, and various types of keys in relational databases. It discusses pitfalls in database design, including data redundancy and normalization issues, and offers strategies for mitigating these problems. Additionally, it introduces Tuple Relational Calculus (TRC) as a non-procedural query language for relational databases, explaining its syntax and logical operators.
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/ 92

21CSC205P - DATABASE

MANAGEMENT SYSTEMS

PROFESSIONAL CORE – 4 Credit


Units
2
Units
3
4

 Thus, in the relational model the term relation is used


to refer to a table, while the term tuple is used to refer
to a row. Similarly, the term attribute refers to a
column of a table.
Keys
5

 Primary Key (PK)


 A primary key is a unique identifier for a table.
 It ensures that each record in a table is unique and cannot
contain NULL values.
 A table can have only one primary key.
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT );
Keys
6

 Candidate Key
 A candidate key is a set of attributes that can uniquely
identify a record.
 A table may have multiple candidate keys, but only one can

be chosen as the primary key.


 Example: In a Students table:

StudentID (unique)
Email (unique)
Both are candidate keys, but only one becomes the
primary key.
Keys
7

 Super Key
 A super key is a set of one or more attributes that uniquely
identifies a row.
 A candidate key is a minimal super key (i.e., no
unnecessary attributes).
 Example:
{StudentID, Name}
{StudentID, Email, PhoneNumber}
{StudentID} (Minimal, so it’s also a candidate key)
Keys
8

 Foreign Key (FK)


 A foreign key is a column that establishes a relationship
between two tables.
 It references the primary key in another table to enforce

referential integrity.
 Example:

CREATE TABLE Orders ( OrderID INT PRIMARY KEY,


StudentID_1 INT, FOREIGN KEY (StudentID_1) REFERENCES
Students(StudentID) );
 Here, StudentID_1 in Orders references StudentID in

Students.
Keys
9

 Alternate Key
 Any candidate key that is not chosen as the primary key
is an alternate key.
 Example:

If StudentID is the primary key, then Email (which was


a candidate key) becomes an alternate key.
Keys
10

 Unique Key
 A unique key ensures that all values in a column are
unique, but unlike a primary key, it can contain NULL
values.
 Example:

CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY,


Email VARCHAR(100) UNIQUE );
Here, Email is a unique key, meaning no two
employees can have the same email.
Pitfalls in relational database systems
11

 Data redundancy leading to anomalies (insertion,


update, deletion),
 Poor database design with unnecessary joins,
 Inadequate indexing causing slow queries,
 Improper normalization,
 Lack of data integrity constraints,
 Poor naming conventions,
 Insufficient documentation, and
 Potential scalability issues when dealing with large data
volumes;
12

 Data redundancy:
 When the same data is stored in multiple places within
the database, causing issues with updating data
consistently and increasing storage requirements.
 Anomalies:
 These are errors that can occur when data is inserted,
updated, or deleted due to redundancy, leading to
incomplete or inaccurate information.
Pitfalls in relational database systems
13

 Poor database design:


 Improper structuring of tables and relationships can
lead to complex queries with many joins, impacting
performance.
 Insufficient indexing:
 Without proper indexes on frequently queried columns,
database lookups can become significantly slower.
 Normalization issues:
 Not properly normalizing data can lead to redundancy
and potential data inconsistencies.
Pitfalls in relational database systems
14

 Lack of data integrity constraints:


 Not defining appropriate constraints (like primary keys,
foreign keys, unique constraints) can allow invalid data
to be entered into the database.
 Poor naming conventions:
 Using unclear or inconsistent naming for tables,
columns, and other database objects can make it
difficult to understand and maintain the database.
Pitfalls in relational database systems
15

 Lack of documentation:
 Not documenting database structure, design decisions,
and data definitions can lead to confusion and
problems when managing the system.
 Scalability issues:
 Relational databases can struggle to handle large data
volumes efficiently, especially when complex queries
are involved.
To mitigate these pitfalls
16

 Proper database design:


 Carefully plan the database structure, considering
normalization to minimize redundancy and optimize
queries.
 Use appropriate data types:
 Choose the right data types for each column to ensure
data integrity and efficiency.
 Implement data integrity constraints:
 Utilize primary keys, foreign keys, and other constraints
to enforce data rules.
To mitigate these pitfalls
17

 Indexing strategies:
 Create indexes on frequently queried columns to
improve query performance.
 Naming conventions:
 Adhere to consistent naming standards for tables,
columns, and other database objects.
 Thorough documentation:
 Maintain clear and updated documentation about the
database structure and design decisions.
Tuple Relational Calculus (TRC) in
18
DBMS
 Tuple Relational Calculus (TRC) is a non-procedural
query language used in relational databases.
 TRC is a declarative language, meaning that it specifies
what data is required from the database, rather
than how to retrieve it.
 TRC queries are expressed as logical formulas that
describe the desired tuples.
Tuple Relational Calculus (TRC)
19

 Understanding TRC Syntax


 A TRC query has the general form:
{t ∣ P(t)}
 t represents a tuple variable.
 P(t) is a logical condition (predicate) that the tuple
must satisfy.
 The result consists of all tuples t from a relation that
satisfy P(t).
Tuple Relational Calculus (TRC)
20

 Consider a relational schema for a STUDENT table:


RollNo Name Age Dept
101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Query 1: Find all students in the CSE department


 SELECT * FROM STUDENT WHERE Dept = 'CSE';
 {t ∣ t ∈ STUDENT ∧ t.Dept = ′CSE′}
t is a tuple from the STUDENT relation.
 The condition t.Dept = 'CSE' filters out students from other
departments.
Tuple Relational Calculus (TRC)
21

 Consider a relational schema for a STUDENT table:


RollNo Name Age Dept
101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Query 2: Find names of students who are 20 years old


 SELECT Name FROM STUDENT WHERE Age = 20;
 {t.Name ∣ t ∈ STUDENT ∧ t.Age=20}
 We retrieve only t.Name instead of the full tuple.
 The condition t.Age = 20 ensures only students aged 20 are
selected.
Tuple Relational Calculus (TRC)
22

 Logical Operators:
 AND (∧) → Both conditions must be true.
 OR (∨) → At least one condition must be true.
 NOT (¬) → The condition must be false.
 Implication (→) → If the first condition is true, then the
second must also be true.
 Existential (∃) → There exists at least one tuple satisfying
a condition.
 Universal (∀) → The condition must be true for all tuples.
Tuple Relational Calculus (TRC)
23

 Consider a relational schema for a STUDENT table:


RollNo Name Age Dept
101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Query 3: Find students who are in CSE or ECE


 SELECT * FROM STUDENT WHERE Dept = 'CSE' OR Dept =
'ECE';
 {t ∣ t ∈ STUDENT ∧ (t.Dept=′CSE′ ∨ t.Dept=′ECE′)}
Tuple Relational Calculus (TRC)
24

 Consider a relational schema for a STUDENT table:


RollNo Name Age Dept
101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Query 4: Find students who are not in the ME


department
 SELECT * FROM STUDENT WHERE Dept != 'ME';
 {t ∣ t ∈ STUDENT ∧ ¬(t.Dept=′ME′)}
Existence
25

 Understanding Existence (∃) and Implication (→)


in Detail with Examples
 In Tuple Relational Calculus (TRC) and First-Order
Logic, two important logical concepts are:
 Existence (∃) → "There exists at least one..."
 Implication (→) → "If condition A is true, then
condition B must be true."
Existence
26

 Existence (∃) – "There Exists"


 The existential quantifier (∃) means that at least
one object satisfies a given condition.
 General Form:
 ∃x ∈ X (Condition(x))
 This means "there exists at least one x in X that
satisfies the given condition."
 Example 1: "There Exists a CSE Student Older
than 20"
 ∃s∈STUDENT (s.Dept=′CSE′ ∧ s.Age>20)
27

RollNo Name Age Dept


101 Alice 20 CSE
102 Bob 19 ECE
103 Charlie 22 CSE

Charlie (22, CSE) satisfies the condition → The statement is


TRUE!
Implication
28

 Implication (→) – "If... Then..."


 The implication operator (→) represents a logical
condition:
 A→B
 This means:
 If A is true, then B must also be true.
 If A is false, then B doesn’t matter (the whole statement
A (Condition) B (Result) A → B (Overall Result)
is true by default). True
True ✅ True
True False ❌ False
False True ✅ True
False False ✅ True
29

 If a Student is from CSE, Then Their Age Must be Above


18
 ∀s∈STUDENT (s.Dept=′CSE′→s.Age>18)
 Breaking it Down:
 For every student s in the STUDENT table:
 If s.Dept = 'CSE' (student is from CSE),
 Then s.Age > 18 (the student must be older than 18).
Check (CSE → Age
RollNo Name Age Dept
> 18)
101 Alice 20 CSE ✅ (20 > 18) True
✅ (Not CSE → Doesn't
102 Bob 19 ECE
matter) True
103 Charlie 22 CSE ✅ (22 > 18) True
30

 If a Student is from CSE, They Must Have Age > 21


 ∀s∈STUDENT (s.Dept=′CSE′→s.Age>21)

Check (CSE →
RollNo Name Age Dept
Age > 21)
❌ (20 is NOT >
101 Alice 20 CSE
21) False
✅ (Not CSE →
102 Bob 19 ECE Doesn't matter)
True
103 Charlie 22 CSE ✅ (22 > 21) True
Tuple Relational Calculus (TRC)
31

RollNo Name Age Dept


101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 22 CSE
104 David 23 ME

 Find students who are older than at least one student in


the CSE department
 SELECT * FROM STUDENT WHERE Age > ANY (SELECT Age FROM
STUDENT WHERE Dept = 'CSE‘) AND Dept <> 'CSE';
 {t ∣ t∈STUDENT ∧ (∃s∈STUDENT (s.Dept=′CSE′ ∧ t.Age>s.Age))}
 {t ∣ t∈STUDENT ∧ t.Dept ≠ ′CSE′ ∧ (∃s∈STUDENT (s.Dept=′CSE
′∧t.Age>s.Age))}
32

RollNo Name Age Dept


101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Find students who are older than all students in CSE


 SELECT * FROM STUDENT WHERE Age > ALL (SELECT Age FROM
STUDENT WHERE Dept = 'CSE');
 {t ∣ t∈STUDENT∧(∀s∈STUDENT (s.Dept=′CSE′→t.Age>s.Age))}
 The universal quantifier (∀) ensures that t.Age is greater than the age of
every CSE student.
33

RollNo Name Age Dept


101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Find students in the CSE department who are at least 21


years old
 SELECT * FROM STUDENT WHERE Dept = 'CSE' AND Age >= 21;
 {t ∣ t∈STUDENT∧(t.Dept=′CSE′∧t.Age≥21)}
34

RollNo Name Age Dept


101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Retrieve Students with Age Less than 21


 SELECT * FROM STUDENT WHERE Age < 21;
 {t ∣ t∈STUDENT∧t.Age<21}
35

RollNo Name Age Dept


101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Retrieve Students Who Are Older Than Bob


 SELECT * FROM STUDENT WHERE Age > (SELECT Age FROM
STUDENT WHERE Name = 'Bob');
 {t ∣ t∈STUDENT∧(∃s∈STUDENT (s.Name=′Bob′ ∧ t.Age>s.Age))}
36

RollNo Name Age Dept


101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Find Students Who Have the Same Age as Any Student in


CSE
 SELECT * FROM STUDENT WHERE Age IN (SELECT Age FROM
STUDENT WHERE Dept = 'CSE');
 {t ∣ t∈STUDENT∧(∃s∈STUDENT (s.Dept=′CSE′∧t.Age=s.Age))}
37

RollNo Name Age Dept


101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Find Students in CSE Who Are the Youngest in Their


Department
 SELECT * FROM STUDENT WHERE Dept = 'CSE' AND Age =
(SELECT MIN(Age) FROM STUDENT WHERE Dept = 'CSE');
 {t ∣ t∈STUDENT ∧ t.Dept=′CSE′ ∧ (∀s∈STUDENT (s.Dept=′CSE
′→t.Age≤s.Age))}
38

RollNo Name Age Dept


101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Find Students Who Have the Same Age and Department


as Another Student
 SELECT * FROM STUDENT s1 WHERE EXISTS (SELECT * FROM
STUDENT s2 WHERE s1.Age = s2.Age AND s1.Dept = s2.Dept
AND s1.RollNo != s2.RollNo);
 {t ∣ t∈STUDENT ∧ (∃s∈STUDENT (t.Age=s.Age ∧ t.Dept=s.Dept
∧ t.RollNo≠s.RollNo))}
39

 t∈STUDENT
 t represents an element (a student) from the STUDENT
relation/table.
 t.Dept=′CSE′
 This means we are only considering students from the CSE
(Computer Science & Engineering) department.
 ∀s∈STUDENT (s.Dept=′CSE′→t.Age≤s.Age)
 For every student s in the STUDENT table,
 If sss is from CSE (s.Dept=′CSE′),
 Then the condition t.Age≤s.Age must hold.
 This means t is the student with the minimum age
40

RollNo Name Age Dept


101 Alice 20 CSE
102 Bob 21 ECE
103 Charlie 20 CSE
104 David 22 ME

 Find Students Whose Age is Greater Than the Average


Age
 SELECT * FROM STUDENT WHERE Age > (SELECT AVG(Age)
FROM STUDENT);
 {t ∣ t∈STUDENT∧(∃s1​,s2​,...,sn​∈STUDENT (t.Age>1/n∑ (i=1)(n)​si​
.Age))}
41

 Example: Find all students who are younger than


at least one student in the 'CSE' department.
SID Name Dept Age
1 Alice CSE 20
2 Bob ECE 22
3 Charlie CSE 19
4 David ME 21

 { t | t ∈ STUDENT ∧ (∃s ∈ STUDENT (s.Dept = 'CSE' ∧


t.Age < s.Age)) }
42

 Understand the Components


 { t | ... }:
 The query returns a set of tuples t that satisfy the condition.

 t ∈ STUDENT:
 t is a tuple variable ranging over the STUDENT relation.

 ∃s ∈ STUDENT:
 There exists a tuple s in the STUDENT relation.

 s.Dept = 'CSE':
 The student s must belong to the 'CSE' department.

 t.Age < s.Age:


 The age of student t must be less than the age of student s.
43

 Evaluating the Query


 Let’s evaluate the query for each student:
 Alice (SID: 1, Dept: CSE, Age: 20):
 Check if there exists a student s in 'CSE' such that Alice is
younger than s.
 Alice is in 'CSE', but there is no student in 'CSE' older than Alice.
 Result: Exclude Alice.
 Bob (SID: 2, Dept: ECE, Age: 22):
 Check if there exists a student s in 'CSE' such that Bob is
younger than s.
 Alice (Age: 20) and Charlie (Age: 19) are in 'CSE', but Bob (Age:
22) is older than both.
44

 Charlie (SID: 3, Dept: CSE, Age: 19):


 Check if there exists a student s in 'CSE' such that Charlie is
younger than s.
 Alice (Age: 20) is in 'CSE', and Charlie (Age: 19) is younger than
Alice.
 Result: Include Charlie.
 David (SID: 4, Dept: ME, Age: 21):
 Check if there exists a student s in 'CSE' such that David is
younger than s.
 Alice (Age: 20) is in 'CSE', but David (Age: 21) is older than Alice.
 Result: Exclude David.
45

 Step 4: Final Result


 The query returns the following student:

SID Name Dept Age


3 Charlie CSE 19
46

 Find all employees who earn more than at least


one employee in the 'Sales' department.

EmpID Name Dept Salary


1 Alice Sales 50000
2 Bob HR 60000
3 Charlie Sales 45000
4 David IT 70000

 { t | t ∈ Employee ∧ (∃s ∈ Employee (s.Dept = 'Sales'


∧ t.Salary > s.Salary)) }
47

 Understand the Components


 { t | ... }:
 The query returns a set of tuples t that satisfy the condition.

 t ∈ Employee:
 t is a tuple variable ranging over the Employee relation.

 ∃s ∈ Employee:
 There exists a tuple s in the Employee relation.

 s.Dept = 'Sales':
 The employee s must belong to the 'Sales' department.

 t.Salary > s.Salary:


 The salary of employee t must be greater than the salary of

employee s.
48

 Evaluating the Query


 Alice (EmpID: 1, Dept: Sales, Salary: 50000):
 Check if there exists an employee s in 'Sales' such that Alice
earns more than s.
 Charlie (Salary: 45000) is in 'Sales', and Alice (Salary: 50000)
earns more than Charlie.
 Result: Include Alice.
 Bob (EmpID: 2, Dept: HR, Salary: 60000):
 Check if there exists an employee s in 'Sales' such that Bob
earns more than s.
 Alice (Salary: 50000) and Charlie (Salary: 45000) are in 'Sales',
and Bob (Salary: 60000) earns more than both.
49

 Charlie (EmpID: 3, Dept: Sales, Salary: 45000):


 Check if there exists an employee s in 'Sales' such that
Charlie earns more than s.
 No employee in 'Sales' earns less than Charlie.
 Result: Exclude Charlie.
 David (EmpID: 4, Dept: IT, Salary: 70000):
 Check if there exists an employee s in 'Sales' such that
David earns more than s.
 Alice (Salary: 50000) and Charlie (Salary: 45000) are in
'Sales', and David (Salary: 70000) earns more than both.
 Result: Include David.
50

EmpID Name Dept Salary


1 Alice Sales 50000
2 Bob HR 60000
4 David IT 70000
51

 Find all employees who work in the same


department as at least one employee with a
salary greater
EmpID Name
than $60,000.
Dept Salary
1 Alice Sales 50000
2 Bob HR 65000
3 Charlie Sales 45000
4 David HR 70000
5 Eve IT 55000

 { t | t ∈ Employee ∧ (∃s ∈ Employee (s.Salary > 60000


∧ t.Dept = s.Dept)) }
52

 Evaluating the Query


 Alice (EmpID: 1, Dept: Sales, Salary: 50000):
 Check if there exists an employee s in 'Sales' with a salary >
$60,000.
 No employee in 'Sales' earns more than $60,000.
 Result: Exclude Alice.
 Bob (EmpID: 2, Dept: HR, Salary: 65000):
 Check if there exists an employee s in 'HR' with a salary >
$60,000.
 Bob (Salary: 65000) and David (Salary: 70000) are in 'HR', and
both earn more than $60,000.
 Result: Include Bob.
53

 Charlie (EmpID: 3, Dept: Sales, Salary: 45000):


 Check if there exists an employee s in 'Sales' with a salary > $60,000.

 No employee in 'Sales' earns more than $60,000.

 Result: Exclude Charlie.

 David (EmpID: 4, Dept: HR, Salary: 70000):


 Check if there exists an employee s in 'HR' with a salary > $60,000.

 Bob (Salary: 65000) and David (Salary: 70000) are in 'HR', and both

earn more than $60,000.


 Result: Include David.

 Eve (EmpID: 5, Dept: IT, Salary: 55000):


 Check if there exists an employee s in 'IT' with a salary > $60,000.

 No employee in 'IT' earns more than $60,000.

 Result: Exclude Eve.


54

EmpID Name Dept Salary


2 Bob HR 65000
4 David HR 70000
55

 Find all employees who have the same salary as


at least one employee in the 'HR' department.
EmpID Name Dept Salary
1 Alice Sales 50000
2 Bob HR 65000
3 Charlie Sales 55000
4 David HR 70000
5 Eve IT 65000

 { t | t ∈ Employee ∧ (∃s ∈ Employee (s.Dept =


'HR' ∧ t.Salary = s.Salary)) }
56

 Evaluating the Query


 Let’s evaluate the query for each employee:
 Alice (EmpID: 1, Dept: Sales, Salary: 50000):
 Check if there exists an employee s in 'HR' with a salary =
$50,000.
 No employee in 'HR' earns $50,000.
 Result: Exclude Alice.
 Bob (EmpID: 2, Dept: HR, Salary: 65000):
 Check if there exists an employee s in 'HR' with a salary =
$65,000.
 Bob himself is in 'HR' and earns $65,000.
57

 Charlie (EmpID: 3, Dept: Sales, Salary: 55000):


 Check if there exists an employee s in 'HR' with a salary = $55,000.
 No employee in 'HR' earns $55,000.
 Result: Exclude Charlie.
 David (EmpID: 4, Dept: HR, Salary: 70000):
 Check if there exists an employee s in 'HR' with a salary = $70,000.
 David himself is in 'HR' and earns $70,000.
 Result: Include David.
 Eve (EmpID: 5, Dept: IT, Salary: 65000):
 Check if there exists an employee s in 'HR' with a salary = $65,000.
 Bob (Salary: 65000) is in 'HR', and Eve earns the same as Bob.
 Result: Include Eve.
58

EmpID Name Dept Salary


2 Bob HR 65000
4 David HR 70000
5 Eve IT 65000
59

 Find all employees who work in a department


located in 'New York'.
Employee(EmpID, Name, DeptID, Salary)

 Department(DeptID, DeptName, Location)


EmpID Name DeptID Salary
1 Alice 101 50000
2 Bob 102 60000
3 Charlie 101 55000
4 David 103 70000

DeptID DeptName Location


101 Sales New York
102 HR Chicago
103 IT New York
60

 { t | t ∈ Employee ∧ (∃d ∈ Department (d.DeptID =


t.DeptID ∧ d.Location = 'New York')) }
 Alice (EmpID: 1, DeptID: 101):
 Check if there exists a department d with DeptID =

101 and Location = 'New York'.


 Department 101 (Sales) is located in 'New York'.

 Result: Include Alice.

 Bob (EmpID: 2, DeptID: 102):


 Check if there exists a department d with DeptID =

102 and Location = 'New York'.


 Department 102 (HR) is located in 'Chicago'.

 Result: Exclude Bob.


61

 Charlie (EmpID: 3, DeptID: 101):


 Check if there exists a department d with DeptID =
101 and Location = 'New York'.
 Department 101 (Sales) is located in 'New York'.
 Result: Include Charlie.
 David (EmpID: 4, DeptID: 103):
 Check if there exists a department d with DeptID =
103 and Location = 'New York'.
 Department 103 (IT) is located in 'New York'.
 Result: Include David.
62

EmpID Name DeptID Salary


1 Alice 101 50000
3 Charlie 101 55000
4 David 103 70000
63

 Find all employees who are working on at least


one project with a budget greater than $100,000.
 Employee(EmpID, Name, DeptID, Salary)
 Project(ProjectID, ProjectName, Budget, EmpID)

EmpI DeptI ProjectNa


Name Salary ProjectID Budget EmpID
D D me

1 Alice 101 50000 P1 ProjectA 120000 1

2 Bob 102 60000 P2 ProjectB 80000 2

3 Charlie 101 55000 P3 ProjectC 150000 3

4 David 103 70000 P4 ProjectD 90000 4

{ t | t ∈ Employee ∧ (∃p ∈ Project (p.EmpID = t.EmpID ∧


p.Budget > 100000)) }
64

 Alice (EmpID: 1):


 Check if there exists a project p with EmpID = 1 and Budget
> 100000.
 Project P1 (Budget: 120000) is assigned to Alice.
 Result: Include Alice.
 Bob (EmpID: 2):
 Check if there exists a project p with EmpID = 2 and Budget
> 100000.
 Project P2 (Budget: 80000) is assigned to Bob, but its
budget is not greater than $100,000.
 Result: Exclude Bob.
65

 Charlie (EmpID: 3):


 Check if there exists a project p with EmpID = 3 and Budget
> 100000.
 Project P3 (Budget: 150000) is assigned to Charlie.
 Result: Include Charlie.
 David (EmpID: 4):
 Check if there exists a project p with EmpID = 4 and Budget
> 100000.
 Project P4 (Budget: 90000) is assigned to David, but its
budget is not greater than $100,000.
 Result: Exclude David.
66

EmpID Name DeptID Salary


1 Alice 101 50000
3 Charlie 101 55000
67

 Find all employees who work in a department located in


'New York' and are assigned to at least one project with a
budget greater than $100,000. Projec
Projec Budg EmpI
 Employee(EmpID, Name, DeptID, Salary) tNam
tID et D
 Department(DeptID, DeptName, Location) e
Projec 1200
EmpI
 Project(ProjectID,
DeptI ProjectName, Budget, EmpID) P1 1
Name Salary DeptNam tA 00
D D DeptID Location
e Projec 8000
1 Alice 101 50000 P2 2
101 Sales New York tB 0
2 Bob 102 60000 102 HR Chicago Projec 1500
P3 3
Charli tC 00
3 101 55000 103 IT New York
e Projec 9000
P4 4
4 David 103 70000 tD 0
 { t | t ∈ Employee ∧
(∃d ∈ Department (d.DeptID = t.DeptID ∧ d.Location = 'New York')) ∧
(∃p ∈ Project (p.EmpID = t.EmpID ∧ p.Budget > 100000)) }
68

 Alice (EmpID: 1, DeptID: 101):


 Check if there exists a department d with DeptID =
101 and Location = 'New York'.
 Department 101 (Sales) is located in 'New York'.
 Check if there exists a project p with EmpID = 1 and Budget >
100000.
 Project P1 (Budget: 120000) is assigned to Alice.
 Result: Include Alice.
 Bob (EmpID: 2, DeptID: 102):
 Check if there exists a department d with DeptID =
102 and Location = 'New York'.
 Department 102 (HR) is located in 'Chicago'.

69

 Charlie (EmpID: 3, DeptID: 101):


 Check if there exists a department d with DeptID =
101 and Location = 'New York'.
 Department 101 (Sales) is located in 'New York'.
 Check if there exists a project p with EmpID = 3 and Budget >
100000.
 Project P3 (Budget: 150000) is assigned to Charlie.
 Result: Include Charlie.
 David (EmpID: 4, DeptID: 103):
 Check if there exists a department d with DeptID =
103 and Location = 'New York'.
 Department 103 (IT) is located in 'New York'.

70

EmpID Name DeptID Salary


1 Alice 101 50000
3 Charlie 101 55000
71

 Finding Employees with the Highest Salary


EID Name Salary
E1 Alice 5000
E2 Bob 7000
E3 Charlie 6000
E4 David 7000

 {e ∣ Employee(e)∧(∀x∈Employee, x.Salary≤e.Salary)}
72

EID Name Salary


E2 Bob 7000
E4 David 7000
73

 Finding Products with the Highest Price

PID Product_Name Price


P1 Laptop 1000
P2 Phone 700
P3 Tablet 900
P4 Desktop 1000

 {p ∣ Product(p)∧(∀x∈Product, x.Price≤p.Price)}
74

PID Product_Name Price


P1 Laptop 1000
P4 Desktop 1000

•∀ (Universal Quantifier) checks all tuples in a relation.


•It is commonly used to find maximum/minimum values (e.g., highest salary, highest marks, most expensive
product).
•Multiple entities can satisfy the condition if there are ties.
75

 Find employees who have both the highest salary and


the most experience.

EID Name Salary Experience (Years)


E1 Alice 5000 5
E2 Bob 7000 7
E3 Charlie 7000 10
E4 David 6000 12

 {e ∣ Employee(e)∧(∀x∈Employee, x.Salary≤e.Salary)∧(
∀x∈Employee, x.Experience≤e.Experience)}
76

 Employees with the Highest Salary OR Most Experience

 {e ∣ Employee(e)∧(∀x∈Employee, x.Salary≤e.Salary)∨(
∀x∈Employee, x.Experience≤e.Experience)}
77

EID Name Salary Experience


E2 Bob 7000 7
E3 Charlie 7000 10
E4 David 6000 12
78

 Find Employees Who Earn More Than Every IT


Employee
EID Name Salary Dept
E1 Alice 5000 IT
E2 Bob 7000 IT
E3 Charlie 8000 HR
E4 David 9000 Finance

 {e∣e∈EMPLOYEE ∧ (∀x∈EMPLOYEE (x.Dept=′IT


′→e.Salary>x.Salary))}
79

EID Name Salary Dept


E3 Charlie 8000 HR
E4 David 9000 Finance
80

 Find Products That Are Cheaper Than All Electronics

PID Product Price Category


P1 Laptop 1000 Electronics
P2 Phone 800 Electronics
P3 Chair 500 Furniture
P4 Table 600 Furniture

 {p∣p∈PRODUCT∧(∀x∈PRODUCT (x.Category=
′Electronics′→p.Price<x.Price))}
81

PID Product Price Category


P3 Chair 500 Furniture
P4 Table 600 Furniture
82

 Find Employees With More Experience Than All HR


Employees

EID Name Experience (Years) Dept


E1 Alice 5 HR
E2 Bob 7 HR
E3 Charlie 10 IT
E4 David 8 Finance

 {e∣e∈EMPLOYEE∧(∀x∈EMPLOYEE (x.Dept=′HR
′→e.Experience>x.Experience))}
83

EID Name Experience (Years) Dept


E3 Charlie 10 IT
E4 David 8 Finance
84

 Find all departments where every employee


earns more than $50,000.
 We have the following two tables:
 Employee(EmpID, Name, DeptID, Salary)
 Department(DeptID, DeptName)
EmpI DeptI Salar
Name
D D y DeptID DeptName
5000 101 Sales
1 Alice 101
0 102 HR
6500
2 Bob 102
0
Charli 5500
3 101
e 0
85

 Sales (DeptID: 101):


 Employees in 'Sales': Alice (50000) and Charlie (55000).
 Alice earns exactly $50,000, which does not satisfy t.Salary
> 50000.
 Result: Exclude 'Sales'.
 HR (DeptID: 102):
 Employees in 'HR': Bob (65000) and David (70000).
 Both Bob and David earn more than $50,000.
 Result: Include 'HR'.
86

 { d | d ∈ Department ∧ (∀t ∈ Employee (t.DeptID =


d.DeptID → t.Salary > 50000)) }

DeptID DeptName
102 HR

 Find all employees who work in a department


where every employee earns more than $60,000.
87

 Alice (EmpID: 1, DeptID: 101):


 Employees in 'Sales': Alice (65000) and Bob (70000).
 Both Alice and Bob earn more than $60,000.
 Result: Include Alice.
 Bob (EmpID: 2, DeptID: 101):
 Employees in 'Sales': Alice (65000) and Bob (70000).
 Both Alice and Bob earn more than $60,000.
 Result: Include Bob.
88

 Charlie (EmpID: 3, DeptID: 102):


 Employees in 'HR': Charlie (55000) and David (60000).
 Charlie earns $55,000, which does not satisfy s.Salary >
60000.
 Result: Exclude Charlie.
 David (EmpID: 4, DeptID: 102):
 Employees in 'HR': Charlie (55000) and David (60000).
 Charlie earns $55,000, which does not satisfy s.Salary >
60000.
 Result: Exclude David.
89

 { t | t ∈ Employee ∧ (∀s ∈ Employee (s.DeptID =


t.DeptID → s.Salary > 60000)) }

EmpID Name DeptID Salary


1 Alice 101 65000
2 Bob 101 70000
90

 Find all departments where every employee is


assigned to at least one project.
 Employee(EmpID, Name, DeptID)
 Assignment(EmpID, ProjectID)
 Department(DeptID, DeptName)

EmpID Name DeptID


EmpID ProjectID
1 Alice 101 DeptID DeptName
1 P1
2 Bob 101 101 Sales
2 P2
3 Charlie 102 102 HR
3 P3
4 David 102
91

 { d | d ∈ Department ∧ (∀e ∈ Employee (e.DeptID =


d.DeptID → ∃p ∈ projecr (p.EmpID = e.EmpID)) }
 Sales (DeptID: 101):
 Employees in 'Sales': Alice (EmpID: 1) and Bob (EmpID: 2).
 Alice is assigned to P1, and Bob is assigned to P2.
 Result: Include 'Sales'.
 HR (DeptID: 102):
 Employees in 'HR': Charlie (EmpID: 3) and David (EmpID: 4).
 Charlie is assigned to P3, but David is not assigned to any
project.
 Result: Exclude 'HR'.
92

DeptID DeptName
101 Sales

You might also like