Chapter 2
Chapter 2
Database System Concepts - 7th Edition 2.2 ©Silberschatz, Korth and Sudarshan
Example of a Instructor Relation
attributes
(or columns)
tuples
(or rows)
Database System Concepts - 7th Edition 2.3 ©Silberschatz, Korth and Sudarshan
Relation Schema and Instance
Database System Concepts - 7th Edition 2.4 ©Silberschatz, Korth and Sudarshan
Attributes
§ The set of allowed values for each attribute is called the domain of the
attribute
§ Attribute values are (normally) required to be atomic; that is, indivisible
Book Author
Database System Concepts Silberschatz, Korth, Sudarshan
Book Author
Database System Concepts Silberschatz
Database System Concepts Korth
Database System Concepts Sudarshan
§ The special value null is a member of every domain. Indicated that the
value is “unknown”
§ The null value causes complications in the definition of many operations
Database System Concepts - 7th Edition 2.5 ©Silberschatz, Korth and Sudarshan
Relations are Unordered
Database System Concepts - 7th Edition 2.6 ©Silberschatz, Korth and Sudarshan
Database Schema
Database System Concepts - 7th Edition 2.7 ©Silberschatz, Korth and Sudarshan
Keys
§ Let K Í R
§ K is a superkey of R if values for K are sufficient to identify a unique tuple
of each possible relation r(R)
• Example: {ID} and {ID,name} are both superkeys of instructor.
§ Superkey K is a candidate key if K is minimal
Example: {ID} is a candidate key for Instructor
department
§ One of the candidate keys is selected to be the primary key.
• Which one? instructor
§ Foreign key constraint: Value in one relation must appear in another
• Referencing relation
• Referenced relation
• Example: dept_name in instructor
is a foreign key from instructor
referencing department
Database System Concepts - 7th Edition 2.8 ©Silberschatz, Korth and Sudarshan
Another example of primary key
Database System Concepts - 7th Edition 2.9 ©Silberschatz, Korth and Sudarshan
Schema Diagram for University Database
Database System Concepts - 7th Edition 2.10 ©Silberschatz, Korth and Sudarshan
Relational Query Languages
Database System Concepts - 7th Edition 2.11 ©Silberschatz, Korth and Sudarshan
Relational Algebra
Database System Concepts - 7th Edition 2.12 ©Silberschatz, Korth and Sudarshan
Select Operation
Database System Concepts - 7th Edition 2.13 ©Silberschatz, Korth and Sudarshan
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:
s dept_name=“Physics” Ù salary > 90,000 (instructor)
Database System Concepts - 7th Edition 2.14 ©Silberschatz, Korth and Sudarshan
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 Edition 2.15 ©Silberschatz, Korth and Sudarshan
Project Operation Example
Database System Concepts - 7th Edition 2.16 ©Silberschatz, Korth and Sudarshan
Project Operation Example
MySQL PostgreSQL
Database System Concepts - 7th Edition 2.17 ©Silberschatz, Korth and Sudarshan
Composition of Relational Operations
§ The result of a relational-algebra operation is relation and therefore
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.
Database System Concepts - 7th Edition 2.18 ©Silberschatz, Korth and Sudarshan
Cartesian-Product Operation
Database System Concepts - 7th Edition 2.19 ©Silberschatz, Korth and Sudarshan
The instructor X teaches table
Database System Concepts - 7th Edition 2.20 ©Silberschatz, Korth and Sudarshan
The instructor X teaches table
Database System Concepts - 7th Edition 2.21 ©Silberschatz, Korth and Sudarshan
Join Operation
§ 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:
s instructor.ID = teaches.ID (instructor x teaches ))
Database System Concepts - 7th Edition 2.22 ©Silberschatz, Korth and Sudarshan
Join Operation (Cont.)
Database System Concepts - 7th Edition 2.23 ©Silberschatz, Korth and Sudarshan
Join Operation (Cont.)
Database System Concepts - 7th Edition 2.24 ©Silberschatz, Korth and Sudarshan
Join Operation (Cont.)
§ Thus
s instructor.ID = teaches.ID (instructor x teaches )
Database System Concepts - 7th Edition 2.25 ©Silberschatz, Korth and Sudarshan
Union Operation
Database System Concepts - 7th Edition 2.26 ©Silberschatz, Korth and Sudarshan
Union Operation (Cont.)
§ Result of:
Õcourse_id (s semester=“Fall” Λ year=2017 (section)) È
Õcourse_id (s semester=“Spring” Λ year=2018 (section))
Database System Concepts - 7th Edition 2.27 ©Silberschatz, Korth and Sudarshan
Set-Intersection Operation
• Result
Database System Concepts - 7th Edition 2.28 ©Silberschatz, Korth and Sudarshan
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 (s semester=“Fall” Λ year=2017 (section)) −
Õcourse_id (s semester=“Spring” Λ year=2018 (section))
Database System Concepts - 7th Edition 2.29 ©Silberschatz, Korth and Sudarshan
The Assignment Operation
Database System Concepts - 7th Edition 2.30 ©Silberschatz, Korth and Sudarshan
The Assignment Operation
Database System Concepts - 7th Edition 2.31 ©Silberschatz, Korth and Sudarshan
The Rename Operation
Database System Concepts - 7th Edition 2.32 ©Silberschatz, Korth and Sudarshan
The Rename Operation
§ Find the ID and name of those instructors who earn more than the
instructor whose ID is 12121.
Database System Concepts - 7th Edition 2.33 ©Silberschatz, Korth and Sudarshan
The Rename Operation
Database System Concepts - 7th Edition 2.34 ©Silberschatz, Korth and Sudarshan
Equivalent Queries
§ Query 2
s dept_name=“Physics” (s 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 Edition 2.35 ©Silberschatz, Korth and Sudarshan
Equivalent Queries
§ Query 2
(sdept_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.
Database System Concepts - 7th Edition 2.36 ©Silberschatz, Korth and Sudarshan
Equivalent Queries
Database System Concepts - 7th Edition 2.37 ©Silberschatz, Korth and Sudarshan
Features of Good Relational Designs
(a slide from chapter 7)
Database System Concepts - 7th Edition 2.38 ©Silberschatz, Korth and Sudarshan
Decomposition
(a slide from chapter 7)
Database System Concepts - 7th Edition 2.39 ©Silberschatz, Korth and Sudarshan
End of Chapter 2
Database System Concepts - 7th Edition 2.40 ©Silberschatz, Korth and Sudarshan
Homework:
• Write and run those MySQL example codes in the slides.
Database System Concepts - 7th Edition 2.41 ©Silberschatz, Korth and Sudarshan