CH 2
CH 2
Database System Concepts - 7th Edition 2.2 ©Silberschatz, Korth and Sudarshan
Data model
A data model is a collection of conceptual tools for describing
data,
data relationships,
data semantics, and
consistency constraints.
The relational model uses a collection of tables to represent both data
and the relationships among those data.
Its conceptual simplicity has led to its widespread adoption; today a
vast majority of database products are based on the relational model.
The relational model describes data at the logical and view levels,
abstracting away low-level details of data storage.
It’s the primary data model for commercial data-processing
applications. It attained its primary position because of
its simplicity,
eases the job of the programmer, compared to earlier data
models such as the network model or the hierarchical model.
Database System Concepts - 7th Edition 2.3 ©Silberschatz, Korth and Sudarshan
Example of a Instructor Relation
A relational database consists of a collection of tables, each of which is assigned a unique
name.
Stores information about instructors.
The table has four column headers: ID, name, dept name, and salary
Each row of this table records information about an instructor, consisting of the instructor’s ID,
name, dept name, and salary attributes
(or columns)
Database System Concepts - 7th Edition 2.5 ©Silberschatz, Korth and Sudarshan
Example of a Course Relation and Prereq relation
Database System Concepts - 7th Edition 2.6 ©Silberschatz, Korth and Sudarshan
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
We use the term relation instance to refer to a specific instance of a relation, that is,
containing a specific set of rows.
The instance of instructor shown in Figure 2.1 has 12 tuples, corresponding to 12
instructors.
Database System Concepts - 7th Edition 2.7 ©Silberschatz, Korth and Sudarshan
Attributes
The set of allowed values for each attribute is called the domain of the attribute
For each attribute of a relation, there is a set of permitted values, called the
domain of that attribute.
the domain of the salary attribute of the instructor relation is the set of all
possible salary values,
while the domain of the name attribute is the set of all possible instructor
names.
We require that, for all relations r, the domains of all attributes of r be atomic.
A domin is atomic if elements of the domain are considered to be indivisible
units.
– For example, suppose the table instructor had an attribute phone number,
which can store a set of phone numbers corresponding to the instructor.
– Then the domain of phone number would not be atomic, since an
element of the domain is a set of phone numbers, and it has subparts,
namely, the individual phone numbers in the set.
Attribute values are (normally) required to be atomic; that is, indivisible
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.8 ©Silberschatz, Korth and Sudarshan
Relations are Unordered
Database System Concepts - 7th Edition 2.9 ©Silberschatz, Korth and Sudarshan
Database Schema
Database schema -- is the logical structure (design) of the database.
consists of a list of attributes and their corresponding domains
Database instance -- is a snapshot of the data in the database at a given
instant in time.
Example: The concept of a
relation instance
schema: instructor (ID, name, dept_name, salary) corresponds to the
Instance: programming-language
notion of a value of a
variable.
Superkey K is a candidate key if K is minimal; {ID, name} is not a candidate key since for
Example: {ID} alone is a candidate key for Instructor
One of the candidate keys is selected to be the primary key.
Which one? (The primary key should be chosen such that its attribute values are never, or
are very rarely, changed)
Foreign key constraint: Value in one relation must appear in another
Referencing relation Example: dept_name in instructor is a foreign key from
instructor referencing department
Referenced relation department is called referenced relation
Database System Concepts - 7th Edition 2.11 ©Silberschatz, Korth and Sudarshan
Schema Diagram for University Database
Database System Concepts - 7th Edition 2.12 ©Silberschatz, Korth and Sudarshan
Schema Diagram for University Database
a two-headed arrow from time slot id in the section relation to time slot id in the
time slot relation represents the referential integrity constraint from section.time slot
id to time slot.time slot id.
Database System Concepts - 7th Edition 2.13 ©Silberschatz, Korth and Sudarshan
Relational Query Languages
Procedural versus non-procedural, or declarative
declarative query language the user describes the desired information without
giving a specific sequence of steps or function calls for obtaining that
information;
the desired information is typically described using some form of
mathematical logic.
It is the job of the database system to figure out how to obtain the desired
information.
“Pure” languages:
Relational algebra: forms the theoretical basis of the SQL query language.
Tuple relational calculus: declarative
Domain relational calculus: declarative
The above 3 pure languages are equivalent in computing power
We will concentrate in this chapter on relational algebra
Not Turing-machine equivalent
Consists of 6 basic operations
Database System Concepts - 7th Edition 2.14 ©Silberschatz, Korth and Sudarshan
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.
Six basic operators
select:
project:
union:
set difference: –
Cartesian product: x
rename:
, , and are unary operations because they operate
on one relation
, – , and x are binary operations because they
operate on pairs of relations
Database System Concepts - 7th Edition 2.15 ©Silberschatz, Korth and Sudarshan
Select Operation
dept_name=“Physics” (instructor)
Result
Database System Concepts - 7th Edition 2.16 ©Silberschatz, Korth and Sudarshan
Select Operation (Cont.)
Database System Concepts - 7th Edition 2.17 ©Silberschatz, Korth and Sudarshan
Project Operation
A unary operation that returns its argument relation, with certain attributes
left out.
Notation:
Database System Concepts - 7th Edition 2.18 ©Silberschatz, Korth and Sudarshan
Project Operation Example
Example: eliminate the dept_name attribute of instructor
Query:
ID, name, salary (instructor)
Result:
Database System Concepts - 7th Edition 2.19 ©Silberschatz, Korth and Sudarshan
Composition of Relational Operations
Database System Concepts - 7th Edition 2.20 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 7th Edition 2.21 ©Silberschatz, Korth and Sudarshan
The instructor X teaches table
Database System Concepts - 7th Edition 2.22 ©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:
instructor.id = teaches.id (instructor x teaches ))
Database System Concepts - 7th Edition 2.23 ©Silberschatz, Korth and Sudarshan
Join Operation (Cont.)
The table corresponding to:
instructor.id = teaches.id (instructor x teaches))
Database System Concepts - 7th Edition 2.24 ©Silberschatz, Korth and Sudarshan
Join Operation (Cont.)
Thus
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 ( semester=“Fall” Λ year=2017 (section))
course_id ( semester=“Spring” Λ year=2018 (section))
Database System Concepts - 7th Edition 2.27 ©Silberschatz, Korth and Sudarshan
Set-Intersection Operation
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
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 ( semester=“Fall” Λ year=2017 (section)) −
course_id ( semester=“Spring” Λ year=2018 (section))
Database System Concepts - 7th Edition 2.29 ©Silberschatz, Korth and Sudarshan
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 and works like assignment in
a programming language.
Example: Find all instructor in the “Physics” and Music department.
Database System Concepts - 7th Edition 2.30 ©Silberschatz, Korth and Sudarshan
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, , is provided for that
purpose
The expression:
x (E)
returns the result of expression E under the name x
Another form of the rename operation:
x(A1,A2, .. An) (E)
Database System Concepts - 7th Edition 2.31 ©Silberschatz, Korth and Sudarshan
Equivalent Queries
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 Edition 2.32 ©Silberschatz, Korth and Sudarshan
Equivalent Queries
Query 2
(dept_name=“Physics” (instructor))instructor.ID = teaches.IDteaches
The two queries are not identical; they are, however, equivalent -- they
give the same result on any database.
Database System Concepts - 7th Edition 2.33 ©Silberschatz, Korth and Sudarshan
Instructor Relation
In the instance of instructor shown in Fig. 2.1, no two instructors have the
same name. From this, can we conclude that name can be used as a
superkey (or primary key) of instructor?
• Answer: No. For this possible instance of the instructor table the
names are unique, but in general this may not be always the case
(unless the university has a rule that two instructors cannot have the
same name, which is a rather unlikey scenario).
Database System Concepts - 7th Edition 2.34 ©Silberschatz, Korth and Sudarshan
Course Relation
Database System Concepts - 7th Edition 2.35 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 2.36 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 2.37 ©Silberschatz, Korth and Sudarshan
Example
Consider the foreign key constraint from the dept name attribute of
instructor to the department relation.
• Give examples of inserts and deletes to these relations, which can
cause a violation of the foreign key constraint.
• Answer:
Inserting a tuple: (10111, Ostrom, Economics, 110,000) into the
instructor table, where the department table does not have the
department Economics, would violate the foreign key constraint
Database System Concepts - 7th Edition 2.38 ©Silberschatz, Korth and Sudarshan
Example 2
Consider the following expressions, which use the result of a relational algebra
operation as the input to another operation. For each expression, explain in
words what the expression does?
Note that
a) For each student who takes at least one course in 2009, display the
students information along with the information about what courses the
student took. The attributes in the result are:
• ID, name, dept name, tot cred, course id, section id, semester, year,
grade
b) The result is similar to (a); the selection can be done before the join operation
as well.
c) Provide a list of consisting of ID, name, course id of all students who took any
course in the university.
Database System Concepts - 7th Edition 2.39 ©Silberschatz, Korth and Sudarshan
Example 3
Consider the relational database of the following relations. What are the
appropriate primary keys?
Database System Concepts - 7th Edition 2.40 ©Silberschatz, Korth and Sudarshan
Example 4
Consider the following employee database.
• employee (person_name, street, city)
• Works(person_name, company_name, salary)
• Company(company_name, city)
Give an expression in the relational algebra to express each of the
following queries:
• Find the name of each employee who lives in city “Miami”.
• Find the name of each employee who lives in “Miami” and whose
salary is greater than $100000.
Database System Concepts - 7th Edition 2.41 ©Silberschatz, Korth and Sudarshan
Exercises
1. Find the name of each employee who does not work for “BigBank”.
2. Find the name of each employee who works for “BigBank”.
3. Find the name, and city of residence of each employee who works for
“BigBank”.
4. Find the name, street address, and city of residence of each employee who
works for “BigBank” and earns more than $10000.
5. Find the name of each employee in this database who lives in the same city as
the company for which she or he works.
Database System Concepts - 7th Edition 2.42 ©Silberschatz, Korth and Sudarshan
Example 4
branch(branch_name, branch city, assets)
customer (ID, customer_name, customer_street, customer city)
loan (loan_number, branch name, amount)
borrower (ID, loan_number)
account (account_number, branch_name, balance)
depositor (ID, account_number)
Consider the previous bank database. Give an expression in the relational algebra
for each of the following queries.
a) Find the names of all branches located in “Chicago”.
b) Find the names of all borrowers who have a loan in branch “Downtown”.
Database System Concepts - 7th Edition 2.43 ©Silberschatz, Korth and Sudarshan
End of Chapter 2
Database System Concepts - 7th Edition 2.44 ©Silberschatz, Korth and Sudarshan