Relational Model
3.1
Introduction to Relational Model
3.2
Relation
Jones
Smith
Curry
Lindsay
customer-name
Main
North
North
Park
customer-street
Harrison
Rye
Rye
Pittsfield
customer-city
attributes
tuples
3.3
Attribute
3.4
Relation Schema and Instance
E.g. Customer-schema =� (customer-name, customer-street, customer-city)
3.5
Keys (1)
Let R be a relation schema, r(R) be any relation on R, and K ⊆ R
3.6
Keys (2)
3.7
Primary Key
A primary key is a column (or a set of columns) in a table that uniquely identifies each row in that table.
🔗 Foreign Key
A foreign key is a column (or a set of columns) in one table that refers to the primary key in another table. It creates a relationship between the two tables.
📘 Example: University Database
1. Students Table
StudentID (PK) Name Major
101 Alice CS
StudentID is the primary key.
2. Enrollments Table
EnrollmentID (PK) StudentID (FK) CourseCode
1 101 CS101
2 102 MATH201
3 101 CS102
StudentID here is a foreign key that references the StudentID in the Students table.
This means each enrollment must be linked to a valid student.
3.8
Integrity Constraints
Integrity constraints are used to ensure the correctness of the data
3.9
✅ 1. Type Constraint
Specifies the legal data type for an attribute.
Example:
CREATE TABLE Employee (EmpID INT, Name VARCHAR(100),
Salary DECIMAL(10, 2));
EmpID must be an integer.
Salary must be a decimal with up to 10 digits, 2 after the decimal point.
3.10
2. Attribute Constraint
Declares that an attribute must conform to a specific type or format.
CREATE TABLE Product ( ProductID INT, Price DECIMAL(8, 2)
CHECK (Price > 0));
Price must be a positive decimal.
3. Not-Null Constraint
Ensures that an attribute cannot be null.
Example:
CREATE TABLE Student (StudentID INT NOT NULL,
Name VARCHAR(50) NOT NULL );
Both StudentID and Name must have values.
3.11
4. Entity Integrity Constraint
Ensures that primary key values are never null.
Example:
CREATE TABLE Department (DeptID INT PRIMARY KEY,
DeptName VARCHAR(100));
DeptID must be unique and not null.
5. Key Constraint
Ensures that all tuples (rows) are distinct based on the primary key.
Example:
CREATE TABLE Course (CourseCode VARCHAR(10) PRIMARY KEY,
Title VARCHAR(100));
No two courses can have the same CourseCode.
3.12
6. Referential Integrity Constraint
Ensures that a foreign key value matches a primary key in another table or is null.
Example:
CREATE TABLE Enrollment
(
StudentID INT, CourseCode VARCHAR(10),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseCode) REFERENCES Course(CourseCode)
);
StudentID must exist in the Student table.
CourseCode must exist in the Course table.
3.13
1. INSERT Operation – Violation of Primary Key Constraint
Table: Students(StudentID, Name, Age)
Constraint: StudentID is a Primary Key (must be unique)
Existing Data:
StudentID | Name | Age
----------|--------|-----
101 | Anita | 20
Invalid Insert:
INSERT INTO Students (StudentID, Name, Age) VALUES (101, 'Ravi', 22);
Violation: Duplicate StudentID (101 already exists)
Error: "Duplicate entry for primary key 'StudentID'"
3.14
2. DELETE Operation – Violation of Referential Integrity
Tables:
Students(StudentID, Name)
Enrollments(StudentID, CourseID)
Constraint: Enrollments.StudentID is a Foreign Key referencing Students.StudentID
Existing Data:
Students:
StudentID | Name
----------|-----
101 | Anita
Enrollments:
StudentID | CourseID
----------|---------
101 | CSE101
3.15
Invalid Delete:
DELETE FROM Students WHERE StudentID = 101;
Violation: Enrollments still references StudentID = 101
Error: "Cannot delete or update a parent row: a foreign key constraint fails"
3. UPDATE Operation – Violation of Domain Constraint
Table: Students(StudentID, Name, Age)
Constraint: Age must be a positive integer
Invalid Update:
UPDATE Students SET Age = -5 WHERE StudentID = 101;
Violation: Age = -5 is outside the valid domain
Error: "Check constraint 'Age > 0' violated"
3.16
Database (1)
3.17
Database (2)
3.18
E-R Diagram for the Banking Enterprise
3.19
Schema Diagram for the Banking Enterprise
3.20
Relational Data Manipulation Languages
3.21
Relational Algebra
3.22
Select Operation – Example
A
B
C
D
α
α
β
β
α
β
β
β
1
5
12
23
7
7
3
10
A
B
C
D
α
β
α
β
1
23
7
10
3.23
Select Operation
σp(r) = {t | t ∈ r and p(t)}
Where p is a formula in propositional calculus consisting of terms connected by : ∧ (and), ∨ (or), ¬ (not)�Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, ≠, >, ≥. <. ≤
3.24
Project Operation – Example
A
B
C
α
α
β
β
10
20
30
40
1
1
1
2
A
C
α
α
β
β
1
1
1
2
=
A
C
α
β
β
1
1
2
3.25
Project Operation
where A1, A2 are attribute names and r is a relation name.
3.26
Union Operation – Example
A
B
α
α
β
1
2
1
A
B
α
β
2
3
r
s
A
B
α
α
β
β
1
2
1
3
3.27
Union Operation
r ∪ s = {t | t ∈ r or t ∈ s}
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (e.g., 2nd column � of r deals with the same type of values as does the 2nd � column of s)
3.28
Set Difference Operation – Example
r – s:
A
B
α
α
β
1
2
1
A
B
α
β
2
3
r
s
A
B
α
β
1
1
3.29
Set Difference Operation
r – s = {t | t ∈ r and t ∉ s}
3.30
Cartesian-Product Operation-Example
Relations r, s:
r x s:
A
B
α
β
1
2
A
B
α
α
α
α
β
β
β
β
1
1
1
1
2
2
2
2
C
D
α
β
β
γ
α
β
β
γ
10
10
20
10
10
10
20
10
E
a
a
b
b
a
a
b
b
C
D
α
β
β
γ
10
10
20
10
E
a
a
b
b
r
s
3.31
Cartesian-Product Operation
r x s = {t q | t ∈ r and q ∈ s}
3.32
Rename Operation
Example:
ρ x (E)
returns the expression E under the name X
If a relational-algebra expression E has arity n, then
ρx (A1, A2, …, An) (E)
returns the result of expression E under the name X, and with the
attributes renamed to A1, A2, …., An.
3.33
Composition of Operations
A
B
α
α
α
α
β
β
β
β
1
1
1
1
2
2
2
2
C
D
α
β
β
γ �α
β
β
γ
10
19
20
10
10
10
20
10
E
a
a
b
b
a
a
b
b
A
B
C
D
E
α
β
β
1
2
2
α
β
β
10
20
20
a
a
b
3.34
Expressions
3.35
Sample Relation: STUDENT
SID Name Age Dept Marks
S1 Alice 20 CSE 85
S2 Bob 21 ECE 78
S3 Charlie 22 CSE 90
S4 David 20 MECH 88
S5 Eva 21 ECE 92
Relational Algebra Operations and Queries
1. Selection (σ) — Select rows based on condition
Query: Students from CSE department
�Relational Algebra: σDept=′CSE′(STUDENT)
Output:
SID | Name | Age | Dept | Marks |
S1 | Alice | 20 | CSE | 85 |
S3 | Charlie | 22 | CSE | 90 |
3.36
2. Projection (π) — Select specific columns
Query: Names and Marks of all students
�Relational Algebra: πName,Marks(STUDENT)
Output:
Name | Marks |
Alice | 85 |
Bob | 78 |
Charlie | 90 |
David | 88 |
Eva | 92 |
3.37
3. Union (∪) — Combine two relations
Let’s define another relation:
Relation: TOPPER
SID | Name | Age | Dept | Marks |
S3 | Charlie | 22 | CSE | 90 |
S5 | Eva | 21 | ECE | 92 |
Output: (No duplicates)
SID Name Age Dept Marks
S1 Alice 20 CSE 85
S2 Bob 21 ECE 78
S3 Charlie 22 CSE 90
S4 David 20 MECH 88
S5 Eva 21 ECE 92
Query: All students and toppers
�Relational Algebra: STUDENT∪TOPPER
Sample Relation: STUDENT
SID Name Age Dept Marks
S1 Alice 20 CSE 85
S2 Bob 21 ECE 78
S3 Charlie 22 CSE 90
S4 David 20 MECH 88
S5 Eva 21 ECE 92
3.38
4. Set Difference (−) — Students who are not toppers
Relational Algebra: STUDENT−TOPPER
Output:
SID | Name | Age | Dept | Marks |
S1 | Alice | 20 | CSE | 85 |
S2 | Bob | 21 | ECE | 78 |
S4 | David | 20 | MECH | 88 |
Relation: TOPPER
SID | Name | Age | Dept | Marks |
S3 | Charlie | 22 | CSE | 90 |
S5 | Eva | 21 | ECE | 92 |
Sample Relation: STUDENT
SID Name Age Dept Marks
S1 Alice 20 CSE 85
S2 Bob 21 ECE 78
S3 Charlie 22 CSE 90
S4 David 20 MECH 88
S5 Eva 21 ECE 92
3.39
5. Rename (ρ) — Rename relation or attributes
Relational Algebra: ρS(STUDENT)
Renames STUDENT to S
Dept | HOD |
CSE | Dr. Rao |
ECE | Dr. Meena |
MECH | Dr. Kumar |
6. Join (⨝) — Combine related tuples from two relations
Let’s define another relation:
Relation: DEPARTMENT
3.40
Query: Join STUDENT with DEPARTMENT
�Relational Algebra:�STUDENT⋈STUDENT.Dept=DEPARTMENT.Dept DEPARTMENT
Output:
SID | Name | Age | Dept | Marks | HOD |
S1 | Alice | 20 | CSE | 85 | Dr. Rao |
S2 | Bob | 21 | ECE | 78 | Dr. Meena |
S3 | Charlie | 22 | CSE | 90 | Dr. Rao |
S4 | David | 20 | MECH | 88 | Dr. Kumar |
S5 | Eva | 21 | ECE | 92 | Dr. Meena |
Relation: STUDENT
SID Name Age Dept Marks
S1 Alice 20 CSE 85
S2 Bob 21 ECE 78
S3 Charlie 22 CSE 90
S4 David 20 MECH 88
S5 Eva 21 ECE 92
Dept | HOD |
CSE | Dr. Rao |
ECE | Dr. Meena |
MECH | Dr. Kumar |
Relation: DEPARTMENT
3.41
Natural-Join Operation
R = (A, B, C, D)
S = (E, B, D)
∏r.A, r.B, r.C, r.D, s.E (σr.B = s.B r.D = s.D (r x s))
3.42
Natural Join Operation – Example
A
B
α
β
γ
α
δ
1
2
4
1
2
C
D
α
γ
β
γ
β
a
a
b
a
b
B
1
3
1
2
3
D
a
a
a
b
b
E
α
β
γ
δ
∈
r
A
B
α
α
α
α
δ
1
1
1
1
2
C
D
α
α
γ
γ
β
a
a
a
a
b
E
α
γ
α
γ
δ
s
r s
Theta join operation
3.43
Types of Join Operations
Theta Join (θ-join)�Combines tuples from two relations based on a condition involving comparison operators like =, <, >, etc.
Equi Join�A special case of theta join where the condition is equality (=).
Natural Join�Automatically joins tables based on all common attributes (same name and domain), eliminating duplicate columns.
Outer Joins (Left, Right, Full)�These are not part of basic relational algebra but are used in extended relational algebra to include unmatched tuples.
3.44
3.45
3.46
Theta Join in Relational Algebra
A theta join (denoted as ⨝θ) is a type of join operation in relational algebra where two relations are joined based on a general condition (θ), which can include any comparison operator like:
• =, ≠, <, >, ≤, ≥
________________________________________
🔹 Syntax
If R and S are two relations, and θ is a condition involving attributes from both:
R ⨝θ S
This returns a relation consisting of all combinations of tuples from R and S that satisfy the condition θ.
3.47
Equi Join in Relational Algebra
An Equi Join is a special case of the theta join where the condition is equality
( = ) between attributes of two relations. It’s used to combine tuples from two relations that have matching values in specified attributes.
🔹 Syntax
If R and S are two relations:
R ⨝_{R.A = S.B} S
This joins R and S where attribute A in R equals attribute B in S.
3.48
Example
Let’s consider two relations:
Employee
EmpID Name DeptID
1 Alice 10
2 Bob 20
3 Carol 30
Department
DeptID DeptName
10 HR
20 IT
40 Finance
🔹 Equi Join: Employee ⨝_{Employee.DeptID = Department.DeptID} Department
This will join the two tables where DeptID matches:
Result:
EmpID Name DeptID DeptName
1 Alice 10 HR
2 Bob 20 IT
Carol is excluded because her DeptID = 30 does not match any in the Department table.
3.49
Outer Join in Relational Algebra (Extended)
An outer join is an extension of the basic join operations in relational algebra. Unlike inner joins (like theta or equi joins), outer joins include unmatched tuples from one or both relations, filling in missing values with NULL.
There are three types:
Left Outer Join (⟕) – Includes all tuples from the left relation and matched tuples from the right.
Right Outer Join (⟖) – Includes all tuples from the right relation and matched tuples from the left.
Full Outer Join (⟗) – Includes all tuples from both relations, matched and unmatched.
3.50
Example
Let’s use the same relations:
Employee
EmpID Name DeptID
1 Alice 10
2 Bob 20
3 Carol 30
Department
DeptID DeptName
10 HR
20 IT
40 Finance
3.51
Left Outer Join: Employee ⟕ Department
Includes all employees, even if their DeptID doesn’t match:
EmpID Name DeptID DeptName
1 Alice 10 HR
2 Bob 20 IT
3 Carol 30 NULL
🔹 Right Outer Join: Employee ⟖ Department
Includes all departments, even if no employee belongs to them:
EmpID Name DeptID DeptName
1 Alice 10 HR
2 Bob 20 IT
NULL NULL 40 Finance
🔹 Full Outer Join: Employee ⟗ Department
Includes all employees and all departments:
EmpID Name DeptID DeptName
1 Alice 10 HR
2 Bob 20 IT
3 Carol 30 NULL
NULL NULL 40 Finance
3.52
Additional Operators
We define additional operators that do not add any power to the relational algebra, but that simplify common queries. That’s to say, the additional operators can be expressed by the six basic operators
3.53
Set-Intersection Operation
3.54
Set-Intersection Operation - Example
A B
α
α
β
1
2
1
A B
α
β
2
3
r
s
A B
α 2
A B
α 2
3.55
Division Operation
The result of r ÷ s is a relation on schema
R – S = (A1, …, Am)
r ÷ s = { t | t ∈ ∏ R-S(r) ∧ ∀ u ∈ s ( tu ∈ r ) }
r ÷ s
3.56
Division Operation – Example
Relations r, s:
r ÷ s:
A
B
α
β
1
2
A
B
α
α
α
β
γ
δ
δ
δ
∈
∈
β
1
2
3
1
1
1
3
4
6
1
2
r
s
3.57
Another Division Example
A
B
α
α
α
β
β
γ
γ
γ
a
a
a
a
a
a
a
a
C
D
α
γ
γ
γ
γ
γ
γ
β
a
a
b
a
b
a
b
b
E
1
1
1
1
3
1
1
1
Relations r, s:
r ÷ s:
D
a
b
E
1
1
A
B
α
γ
a
a
C
γ
γ
r
s
3.58
Division Operation (Cont.)
r ÷ s = ∏R-S (r) –∏R-S ( (∏R-S (r) x s) – ∏R-S,S(r))�
To see why
3.59
In Relational Algebra, the division operation is used when you want to find tuples in one relation that are associated with all tuples in another relation. It’s particularly useful for queries involving phrases like “for all”.
Definition
If we have two relations:
R(A, B) — a relation with attributes A and B
S(B) — a relation with attribute B
Then the division of R by S, written as R ÷ S, returns a relation T(A) such that:
T contains all values of A from R such that for every B in S, the pair (A, B) is in R.
3.60
Example
Let’s say we have the following relations:
R(Student, Course)
Student Course
Alice DBMS
Alice OS
Bob DBMS
Bob OS
Bob Networks
Charlie DBMS
Charlie OS
S(Course)
Course
DBMS
OS
R ÷ S gives:
We want to find all students who have taken all the courses listed in S
(i.e., DBMS and OS).
"Find students who have enrolled in all mandatory courses."
"Find suppliers who supply all parts required."
3.61
Result:
Student
Alice
Bob
Charlie
All three students have taken both DBMS and OS.
If S had included "Networks", then only Bob would be in the result.
3.62
Assignment Operation
temp1 ← ∏R-S (r) � temp2 ← ∏R-S ((temp1 x s) – ∏R-S,S (r))� result = temp1 – temp2
account ← account – σ branch-name = “Perryridge” (account)
3.63
Customer_ID |
|
101 | Alice |
102 | Bob |
103 | Charlie |
Account_ID | Customer_ID | Balance |
A1 | 101 | 5000 |
A2 | 102 | 3000 |
A3 | 104 | 7000 |
Loan_ID | Customer_ID | Amount |
L1 | 101 | 10000 |
L2 | 103 | 15000 |
Sample Relations
CUSTOMER
ACCOUNT
LOAN
�
3.64
Customer_ID |
101 |
1. Set Intersection (∩)
Find customers who have both an account and a loan.
Result:
π Customer_ID (ACCOUNT) ∩ π Customer_ID (LOAN)
2. Natural Join (⨝)
Join CUSTOMER and ACCOUNT on Customer_ID.
CUSTOMER ⨝ ACCOUNT
Customer_ID | Name | Account_ID | Balance |
101 | Alice | A1 | 5000 |
102 | Bob | A2 | 3000 |
Result:
3.65
3. Division (÷)
Suppose we have:
ENROLLS
Student | Course |
S1 | C1 |
S1 | C2 |
S2 | C1 |
S2 | C2 |
S3 | C1 |
REQUIRED_COURSES
Course
C1
C2
To find students who have taken all required courses:
ENROLLS ÷ REQUIRED_COURSES
Result:
Student |
S1 |
S2 |
3.66
4. Assignment (←)
Store the result of a join in a temporary relation:
TEMP ← CUSTOMER ⨝ ACCOUNT
Now you can use TEMP in further operations like:
π Name (TEMP)
Result:
Name |
Alice |
Bob |
3.67
Extended Operators
3.68
Generalized Projection
∏customer-name, limit – credit-balance (credit-info)
3.69
Aggregate Functions and Operations
avg: average value� min: minimum value� max: maximum value� sum: sum of values� count: number of values
G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E)
3.70
Aggregate Operation – Example
A
B
α
α
β
β
α
β
β
β
C
7
7
3
10
g sum(c) (r)
sum-C
27
3.71
Aggregate Operation – Example
branch-name g sum(balance) (account)
branch-name
account-number
balance
Perryridge
Perryridge
Brighton
Brighton
Redwood
A-102
A-201
A-217
A-215
A-222
400
900
750
750
700
branch-name
balance
Perryridge
Brighton
Redwood
1300
1500
700
3.72
Aggregate Functions (Cont.)
branch-name g sum(balance) as sum-balance (account)
3.73
Null Values
3.74
Tuple Relational Calculus
{t | P (t) }
3.75
Predicate Calculus Formula
1. Set of attributes and constants
2. Set of comparison operators: (e.g., <, ≤, =, ≠, >, ≥)
3. Set of connectives: and (∧), or (v)‚ not (¬)
4. Implication (⇒): x ⇒ y, if x if true, then y is true
x ⇒ y ≡ ¬x v y
5. Set of quantifiers:
3.76
Sample Schema
Let’s assume the following relations:
TRC Query Examples
1. Get names of students who scored more than 80 marks
TRC Expression:
{ t.Name ∣ t∈Student ∧ t.Marks>80 }
2. Find students enrolled in course with CID = 'C101‘
TRC Expression:
{s.Name ∣ s∈Student ∧ ∃e( e∈Enrolled ∧ e.SID=s.SID ∧ e.CID=′C101′) }
3. Get names and ages of students younger than 20
TRC Expression:
{ t ∣ t∈Student ∧ t.Age<20}
3.77
4. Find students who are enrolled in at least one course
TRC Expression:
{ s.Name∣ s∈Student ∧ ∃e(e∈Enrolled ∧ e.SID=s.SID) }
Sample Schema
Let’s assume the following relations:
5. Get names of students who are not enrolled in any course
TRC Expression:
{s.Name∣ s∈Student ∧ ¬∃e(e∈Enrolled ∧ e.SID=s.SID)}
3.78
Domain Relational Calculus
{ < x1, x2, …, xn > | P(x1, x2, …, xn)}�
3.79
Sample Schema
Domain Relational Calculus Examples
DRC Expression:
{⟨name⟩∣ ∃sid,age,marks(Student(sid,name,age,marks) ∧ marks>80)}
2. Find names of students enrolled in course with CID = 'C101‘
DRC Expression:
{⟨name⟩∣ ∃sid,age,marks(Student(sid,name,age,marks) ∧ ∃cid(Enrolled(sid,cid) ∧ cid=′C101′))}
3.80
3. Get names and ages of students younger than 20
DRC Expression:
{⟨name,age⟩ ∣ ∃sid,marks(Student(sid,name,age,marks) ∧ age<20)}
4. Find names of students not enrolled in any course
DRC Expression:
{ ⟨name⟩∣ ∃sid,age,marks(Student(sid,name,age,marks) ∧ ¬∃cid(Enrolled(sid,cid)))}
5. Get names of students enrolled in at least one course
DRC Expression:
{⟨name⟩∣ ∃sid,age,marks(Student(sid,name,age,marks) ∧ ∃cid(Enrolled(sid,cid)))}
3.81
Example Queries
{< l, b, a > | < l, b, a > ∈ loan ∧ a > 1200}
{< c > | ∃ l, b, a (< c, l > ∈ borrower ∧ < l, b, a > ∈ loan ∧ a > 1200)}
{< c, a > | ∃ l (< c, l > ∈ borrower ∧ ∃b(< l, b, a > ∈ loan ∧
b = “Perryridge”))}
or {< c, a > | ∃ l (< c, l > ∈ borrower ∧ < l, “Perryridge”, a > ∈ loan)}
3.82
Example Queries
{< c > | ∃ l ({< c, l > ∈ borrower � ∧ ∃ b,a(< l, b, a > ∈ loan ∧ b = “Perryridge”))� ∨ ∃ a(< c, a > ∈ depositor� ∧ ∃ b,n(< a, b, n > ∈ account ∧ b = “Perryridge”))}
{< c > | ∃ n (< c, s, n > ∈ customer) ∧
∀ x,y,z(< x, y, z > ∈ branch ∧ y = “Brooklyn”) ⇒� ∃ a,b(< x, y, z > ∈ account ∧ < c,a > ∈ depositor)}
3.83
Banking Example
3.84
Example Queries
{t | t ∈ loan ∧ t [amount] > 1200}
or {t | loan (t) ∧ t .amount > 1200}
{t | ∃ s ∈ loan (t[loan-number] = s[loan-number]� ∧ s [amount] > 1200}
or {t.loan-number | t ∈ loan ∧ t [amount] > 1200}
Notice that a relation on schema [loan-number] is implicitly defined by the query
3.85
Example Queries
{t | ∃s ∈ borrower(t[customer-name] = s[customer-name])� ∨ ∃u ∈ depositor(t[customer-name] = u[customer-name])�
{t | ∃s ∈ borrower(t[customer-name] = s[customer-name])� ∧ ∃u ∈ depositor(t[customer-name] = u[customer-name])
3.86
Example Queries
{t | ∃s ∈ borrower(t[customer-name] = s[customer-name] � ∧ ∃u ∈ loan(u[branch-name] = “Perryridge”� ∧ u[loan-number] = s[loan-number]))}�
{t | ∃s ∈ borrower(t[customer-name] = s[customer-name]� ∧ ∃u ∈ loan(u[branch-name] = “Perryridge”� ∧ u[loan-number] = s[loan-number]))� ∧ not ∃v ∈ depositor (v[customer-name] = � t[customer-name]) }
3.87
Example Queries
{t | ∃s ∈ loan(s[branch-name] = “Perryridge”� ∧ ∃u ∈ borrower (u[loan-number] = s[loan-number]� ∧ t [customer-name] = u[customer-name])� ∧ ∃ v ∈ customer (u[customer-name] = v[customer-name]� ∧ t[customer-city] = v[customer-city])))}
3.88
Example Queries
{t | ∃ c ∈ customer (t[customer.name] = c[customer-name]) ∧
∀ s ∈ branch(s[branch-city] = “Brooklyn” ⇒ � ∃ u ∈ account ( s[branch-name] = u[branch-name]� ∧ ∃ m ∈ depositor ( t[customer-name] = m[customer-name]� ∧ m[account-number] = u[account-number] )) )}
3.89
Universal and Existential Quantifiers
3.90
Examples
{e.name | e ∈ employee and (not (∃d (d ∈ dependent and
e.ssn = d.essn))}
or
{e.name | e ∈ employee and ((∀ d (not (d ∈ dependent) or
not (e.ssn = d.essn)))}
(notice that:
∀ d (d ∈ dependent) ==> not (e.ssn = d.essn) )
3.91
Banking Example
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)
3.92
Example Queries
σamount > 1200 (loan)
∏loan-number (σamount > 1200 (loan))
3.93
Example Queries
∏customer-name (borrower) ∪ ∏customer-name (depositor)
∏customer-name (borrower) ∩ ∏customer-name (depositor)
3.94
Example Queries
∏customer-name (σbranch-name=“Perryridge”
(σborrower.loan-number = loan.loan-number(borrower x loan)))
∏customer-name (σbranch-name = “Perryridge”
(σborrower.loan-number = loan.loan-number(borrower x loan)))�� – ∏customer-name(depositor)
3.95
Example Queries
(σborrower.loan-number = loan.loan-number(borrower x loan)))
− Query 2
∏customer-name(σloan.loan-number = borrower.loan-number(� (σbranch-name = “Perryridge”(loan)) x� borrower)� )
3.96
Example Queries
Find the largest account balance
∏balance(account) - ∏account.balance
(σaccount.balance < d.balance (account x ρd (account)))
3.97
Example Queries
∏CN(σBN=“Downtown”(depositor account)) ∩
∏CN(σBN=“Uptown”(depositor account))
where CN denotes customer-name and BN denotes �branch-name.
∏customer-name, branch-name (depositor account)� ÷ ρtemp(branch-name) ({(“Downtown”), (“Uptown”)})
3.98
Example Queries
� ∏customer-name, branch-name (depositor account)� ÷ ∏branch-name (σbranch-city = “Brooklyn” (branch))
3.99
Outer Join
3.100
Outer Join – Example
loan-number
amount
L-170
L-230
L-260
3000
4000
1700
customer-name
loan-number
Jones
Smith
Hayes
L-170
L-230
L-155
branch-name
Downtown
Redwood
Perryridge
3.101
Outer Join – Example
loan borrower
loan-number
amount
L-170
L-230
3000
4000
customer-name
Jones
Smith
branch-name
Downtown
Redwood
loan-number
amount
L-170
L-230
L-260
3000
4000
1700
customer-name
Jones
Smith
null
branch-name
Downtown
Redwood
Perryridge
3.102
Outer Join – Example
loan borrower
loan-number
amount
L-170
L-230
L-155
3000
4000
null
customer-name
Jones
Smith
Hayes
loan-number
amount
L-170
L-230
L-260
L-155
3000
4000
1700
null
customer-name
Jones
Smith
null
Hayes
loan borrower
branch-name
Downtown
Redwood
null
branch-name
Downtown
Redwood
Perryridge
null
3.103
View Definition
create view v as <query expression>
where <query expression> is any legal relational algebra query expression. The view name is represented by v.
3.104
Views
∏customer-name, loan-number (borrower loan)
3.105
View Examples
create view all-customer as
∏branch-name, customer-name (depositor account)
∪ ∏branch-name, customer-name (borrower loan)
∏branch-name
(σbranch-name = “Perryridge” (all-customer))
3.106
Views Defined Using Other Views
3.107
Snapshots
create snapshot v as <query expression>
3.108