0% found this document useful (0 votes)
26 views30 pages

Ch2 Relational Model

Uploaded by

bhanuprasadk19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views30 pages

Ch2 Relational Model

Uploaded by

bhanuprasadk19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter 2: Intro to Relational Model

Database System Concepts, 7th Ed.


©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Recap: Example of University Database

classroom(building, room number, capacity)


department(dept name, building, budget)
course(course id, title, dept name, credits)
instructor(ID, name, dept name, salary)
section(course id, sec id, semester, year, building, room number, time slot id)
teaches(ID, course id, sec id, semester, year)
student(ID, name, dept name, tot cred)
takes(ID, course id, sec id, semester, year, grade)
advisor(s_ID, i_ID)
time slot(time slot id, day, start time, end time)
prereq(course id, prereq id)

Database System Concepts - 7th 2.2 ©Silberschatz, Korth and


Schema Diagram for University Database

Database System Concepts - 7th 2.3 ©Silberschatz, Korth and


Schema Diagrams Summary

• Database schema is the logical view of the entire database


• Database schema + Primary key + Foreign key dependencies
• Schema diagrams with relation name and attribute names.
• Primary key – Underlined
• Foreign key – Arrows
• Other constraints are not explicitly shown in schema diagrams
• Entity-Relationship(E-R) diagrams let up represent several kind of
constraints.

Database System Concepts - 7th 2.4 ©Silberschatz, Korth and


Relational Query Languages

Database System Concepts - 7th 2.5 ©Silberschatz, Korth and


Relational Query Languages

▪ Query language: user request information from the database


▪ Higher level than that of a standard Programming Language
▪ Categories: Procedural and Non-procedural
▪ “Pure” languages:
• Relational algebra
• Tuple relational calculus
• Domain relational calculus
▪ The above 3 pure languages are equivalent in computing power
▪ We will concentrate in this chapter on relational algebra
• Consists of 6 basic operations

Database System Concepts - 7th 2.6 ©Silberschatz, Korth and


Procedural and Non-procedural
Query Language
▪ Procedural Query Language
• A sequence of operations on the database
• Compute the desired result
• What data is required?
• How to retrieve the required data?
• Example: Relational Algebra

▪ Non-procedural Query Language


• User describes only the desired information
• What data is required?
• No need to describe how to retrieve those data
• Example: Relational Calculus

Database System Concepts - 7th 2.7 ©Silberschatz, Korth and


Basic of 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(output).
▪ It provides a theoretical foundation for relational databases.
▪ Six basic fundamental operators
• select: σ
• project: ∏
• union: ∪
• set difference: –
• Cartesian product: x
• rename: ρ

Database System Concepts - 7th 2.8 ©Silberschatz, Korth and


Select Operation

▪ The select operation(Greek letter sigma) selects tuples that satisfy a


given predicate.
▪ Notation: σ p (Relation)
▪ p is called the selection predicate( or condition)
▪ Example: select those tuples of the instructor relation where the
instructor is in the “Physics” department.
• Query
σ dept_name=“Physics” (instructor)
• Result

Database System Concepts - 7th 2.9 ©Silberschatz, Korth and


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)
▪ Example: Find the instructors in Physics with a salary greater $90,000, we
write:
σ dept_name=“Physics” ∧ salary > 90,000 (instructor)

▪ The select predicate may include comparisons between two attributes.


• Example, find all departments whose name is the same as their
building name:
• σ dept_name=building (department)

Database System Concepts - 7th 2.10 ©Silberschatz, Korth and


Project Operation

▪ A unary operation that returns its argument relation, with certain attributes
left out.
▪ Notation:
∏ A1,A2,A3 ….Ak (r)
where A1, A2, …, Ak are attribute names and r is a relation name.
▪ The result is defined as the relation of k columns obtained by erasing the
columns that are not listed
▪ Duplicate rows removed from result, since relations are sets

Database System Concepts - 7th 2.11 ©Silberschatz, Korth and


Project Operation Example

▪ Example: eliminate the dept_name attribute of instructor


▪ Query:
∏ID, name, salary (instructor)
▪ Result:

Database System Concepts - 7th 2.12 ©Silberschatz, Korth and


Composition of Relational Operations

▪ The result of a relational-algebra


operation is relation and therefore of
relational-algebra operations can be
composed together into a relational-
algebra expression.
▪ Consider the query -- Find the names of
all instructors in the Physics department.

∏name(σ dept_name =“Physics” (instructor))

▪ Instead of giving the name of a relation


as the argument of the projection
operation, we give an expression that
evaluates to a relation.

Database System Concepts - 7th 2.13 ©Silberschatz, Korth and


Union Operation

▪ The union operation allows us to combine two relations


▪ It is a set of all objects that are a member of A, or B, or both.
▪ Notation: r ∪ s
▪ For r ∪ s to be valid.
1. r, s must have the same arity (same number of attributes)
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)
▪ Example: to find all courses taught in the Fall 2017 semester, or in the
Spring 2018 semester, or in both
∏course_id (σ semester=“Fall” Λ year=2017 (section)) ∪
∏course_id (σ semester=“Spring” Λ year=2018 (section))

Database System Concepts - 7th 2.14 ©Silberschatz, Korth and


Union Operation (Cont.)

▪ Result of:
∏course_id (σ semester=“Fall” Λ year=2017 (section)) ∪
∏course_id (σ semester=“Spring” Λ year=2018 (section))

Database System Concepts - 7th 2.15 ©Silberschatz, Korth and


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
▪ Example: to find all courses taught in the Fall 2017 semester, but not in the
Spring 2018 semester
∏course_id (σ semester=“Fall” Λ year=2017 (section)) −
∏course_id (σ semester=“Spring” Λ year=2018 (section))

Database System Concepts - 7th 2.16 ©Silberschatz, Korth and


Cartesian-Product Operation

▪ The Cartesian-product operation (denoted by X) allows us to combine


information from any two relations.
▪ Example: the Cartesian product of the relations instructor and teaches is
written as:
instructor X teaches
▪ We construct a tuple of the result out of each possible pair of tuples: one
from the instructor relation and one from the teaches relation (see next
slide)
▪ Since the instructor ID appears in both relations we distinguish between
these attribute by attaching to the attribute the name of the relation from
which the attribute originally came.
• instructor.ID
• teaches.ID
Example: Find the names of all instructors in the Physics department
together with the course id of all courses they taught.

Database System Concepts - 7th 2.17 ©Silberschatz, Korth and


The instructor X teaches table

Step 3
Name Course_ID
Einstein PHY-101
Step 1

Step 2

Database System Concepts - 7th 2.18 ©Silberschatz, Korth and


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, ρ (lowercase Greek
letter rho) , is provided for that purpose
▪ The expression:
ρx (E)
returns the result of expression E under the name x
▪ The same rename operation can be applied to a relation r to get the same
relation under a new name
▪ Another form of the rename operation, we can rename the attribute
names using this syntax ρx(A1,A2, .. An) (E)
▪ Example: Rename the result relation as Ins_Phy from The instructor X
teaches table:
ρ ins_phy (Result Expression)
Rename the Instructor X Teaches Table as Output:
ρ output (Instructor X Teaches)

Database System Concepts - 7th 2.19 ©Silberschatz, Korth and


Additional Operations

▪ To provide more functionality for manipulating and querying relational


databases
▪ All additional Operators can be achieved by fundamental operations.
▪ Intersection (∩)
▪ Join(⨝)
▪ Assignment operation (= or ← )
▪ Division Operation (÷)

Database System Concepts - 7th 2.20 ©Silberschatz, Korth and


Set-Intersection Operation

▪ Any relation is a set


▪ The set-intersection operation allows us to find tuples that are in both
the input relations.
▪ Notation: r ∩ s
▪ Assume:
• r, s have the same arity
• attributes of r and s are compatible
▪ Example: Find the set of all courses taught in both the Fall 2017 and the
Spring 2018 semesters.
∏course_id (σ semester=“Fall” Λ year=2017 (section)) ∩
∏course_id (σ semester=“Spring” Λ year=2018 (section))

• Result

• R ∩ S= R-(R-S)

Database System Concepts - 7th 2.21 ©Silberschatz, Korth and


The Assignment Operation

▪ It is convenient at times to write a relational-algebra expression by


assigning parts of it to temporary relation variables.
▪ The assignment operation is denoted by ← or = and works like
assignment in a programming language.
▪ Notation: P = R ∩ S, Relation P is a temporary relation variable, R ∩S is a
temporary relation.
▪ Example: Find all instructor in the “Physics” and Music department.

Physics ← σ dept_name=“Physics” (instructor)


Music ← σ dept_name=“Music” (instructor)
Physics ∪ Music

▪ With the assignment operation, a query can be written as a sequential


program consisting of a series of assignments followed by an expression
whose value is displayed as the result of the query.

Database System Concepts - 7th 2.22 ©Silberschatz, Korth and


Join Operation

▪ The Join Operation(⨝): R ⨝<join condition> S


▪ The Cartesian-Product
instructor X teaches
associates every tuple of instructor with every tuple of teaches.
• Most of the resulting rows have information about instructors who
did NOT teach a particular course.
▪ To get only those tuples of “instructor X teaches “ that pertain to
instructors and the courses that they taught, we write:
σ instructor.id = teaches.id (instructor x teaches ))

• We get only those tuples of “instructor X teaches” that pertain to


instructors and the courses that they taught.
▪ The result of this expression, shown in the next slide

Database System Concepts - 7th 2.23 ©Silberschatz, Korth and


Join Operation (Cont.)

▪ The table corresponding to:


σ instructor.id = teaches.id (instructor x teaches))

Database System Concepts - 7th 2.24 ©Silberschatz, Korth and


Join Operation (Cont.)

Database System Concepts - 7th 2.25 ©Silberschatz, Korth and


The Division Operation

▪ Notation: R ÷ S
▪ Example: To retrieve the student ID of the Student enrolled all courses:
▪ Student: Courses:
S_ID C_ID C_ID

1001 1 1

1002 1 2

1002 2

1003 2

▪ Result = Student÷ Courses


S_ID

1002

Database System Concepts - 7th 2.26 ©Silberschatz, Korth and


Equivalent Queries

▪ There is more than one way to write a query in relational algebra.


▪ Example: Find information about courses taught by instructors in the
Physics department with salary greater than 90,000
▪ Query 1
σ dept_name=“Physics” ∧ salary > 90,000 (instructor)

▪ 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.

Database System Concepts - 7th 2.27 ©Silberschatz, Korth and


Exercise

Q1. Write an RA expression to find all the


instructors whose salary between 70000 and
80000.

Q2. Find the name of all instructors in the


Computer Science department.

Q3. Find the set of all instructors’ ID who taught


courses in Fall 2019, or Spring 2020, or both.
(Find the appropriate R )

Database System Concepts - 7th 2.28 ©Silberschatz, Korth and


Exercise

Q4: List all the instructor’s ID who are not advisors.

Q5. List all the instructors’ ID who taught in Spring 2020 but not advisors.

Q6: Find the names of all instructors in the Physics department together with
the course id of all courses they taught (3 steps). Is there an Equivalent
Queries for the result? Please specify it.

Database System Concepts - 7th 2.29 ©Silberschatz, Korth and


End of Chapter 2

Database System Concepts - 7th 2.30 ©Silberschatz, Korth and

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy