Академический Документы
Профессиональный Документы
Культура Документы
Database System Concepts - 6th Edition 1.2 ©Silberschatz, Korth and Sudarshan
Database Management System (DBMS)
Database System Concepts - 6th Edition 1.3 ©Silberschatz, Korth and Sudarshan
University Database Example
Application program examples
Add new students, instructors, and courses
Register students for courses, and generate class rosters
Assign grades to students, compute grade point averages
(GPA) and generate transcripts
In the early days, database applications were built directly on
top of file systems
Database System Concepts - 6th Edition 1.4 ©Silberschatz, Korth and Sudarshan
Drawbacks of using file systems to store data
Database System Concepts - 6th Edition 1.5 ©Silberschatz, Korth and Sudarshan
Drawbacks of using file systems to store data (Cont.)
Atomicity of updates
Failures may leave database in an inconsistent state with partial
updates carried out
Example: Transfer of funds from one account to another should
either complete or not happen at all
Concurrent access by multiple users
Concurrent access needed for performance
Uncontrolled concurrent accesses can lead to inconsistencies
Example: Two people reading a balance (say 100) and
updating it by withdrawing money (say 50 each) at the same
time
Security problems
Hard to provide user access to some, but not all, data
Database System Concepts - 6th Edition 1.6 ©Silberschatz, Korth and Sudarshan
Levels of Abstraction
Physical level: describes how a record (e.g., instructor) is stored.
Logical level: describes data stored in database, and the relationships
among the data.
type instructor = record
ID : string;
name : string;
dept_name : string;
salary : integer;
end;
View level: application programs hide details of data types. Views can
also hide information (such as an employee’s salary) for security
purposes.
Database System Concepts - 6th Edition 1.7 ©Silberschatz, Korth and Sudarshan
View of Data
Database System Concepts - 6th Edition 1.8 ©Silberschatz, Korth and Sudarshan
Instances and Schemas
Similar to types and variables in programming languages
Logical Schema – the overall logical structure of the database
Example: The database consists of information about a set of
customers and accounts in a bank and the relationship between them
Analogous to type information of a variable in a program
Physical schema– the overall physical structure of the database
Instance – the actual content of the database at a particular point in time
Analogous to the value of a variable
Physical Data Independence – the ability to modify the physical schema
without changing the logical schema
Applications depend on the logical schema
In general, the interfaces between the various levels and components
should be well defined so that changes in some parts do not seriously
influence others.
Database System Concepts - 6th Edition 1.9 ©Silberschatz, Korth and Sudarshan
Data Models
A collection of tools for describing
Data
Data relationships
Data semantics
Data constraints
Relational model
Entity-Relationship data model (mainly for database design)
Object-based data models (Object-oriented and Object-relational)
Semistructured data model (XML)
Other older models:
Network model
Hierarchical model
Database System Concepts - 6th Edition 1.10 ©Silberschatz, Korth and Sudarshan
Relational Model
All the data is stored in various tables.
Example of tabular data in the relational model Columns
Rows
Database System Concepts - 6th Edition 1.11 ©Silberschatz, Korth and Sudarshan
A Sample Relational Database
Database System Concepts - 6th Edition 1.12 ©Silberschatz, Korth and Sudarshan
Data Definition Language (DDL)
Specification notation for defining the database schema
Example: create table instructor (
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))
DDL compiler generates a set of table templates stored in a data dictionary
Data dictionary contains metadata (i.e., data about data)
Database schema
Integrity constraints
Primary key (ID uniquely identifies instructors)
Authorization
Who can access what
Database System Concepts - 6th Edition 1.13 ©Silberschatz, Korth and Sudarshan
Data Manipulation Language (DML)
Language for accessing and manipulating the data organized
by the appropriate data model
DML also known as query language
Two classes of languages
Pure – used for proving properties about computational
power and for optimization
Relational Algebra
Tuple relational calculus
Domain relational calculus
Commercial – used in commercial systems
SQL is the most widely used commercial language
Database System Concepts - 6th Edition 1.14 ©Silberschatz, Korth and Sudarshan
SQL
Database System Concepts - 6th Edition 1.15 ©Silberschatz, Korth and Sudarshan
Database Design
The process of designing the general structure of the database:
Database System Concepts - 6th Edition 1.16 ©Silberschatz, Korth and Sudarshan
Database Design (Cont.)
Is there any problem with this relation?
Database System Concepts - 6th Edition 1.17 ©Silberschatz, Korth and Sudarshan
Design Approaches
Need to come up with a methodology to ensure that each of the
relations in the database is “good”
Two ways of doing so:
Entity Relationship Model (Chapter 7)
Models an enterprise as a collection of entities and
relationships
Represented diagrammatically by an entity-relationship
diagram:
Normalization Theory (Chapter 8)
Formalize what designs are bad, and test for them
Database System Concepts - 6th Edition 1.18 ©Silberschatz, Korth and Sudarshan
Object-Relational Data Models
Relational model: flat, “atomic” values
Object Relational Data Models
Extend the relational data model by including object orientation
and constructs to deal with added data types.
Allow attributes of tuples to have complex types, including non-
atomic values such as nested relations.
Preserve relational foundations, in particular the declarative
access to data, while extending modeling power.
Provide upward compatibility with existing relational languages.
Database System Concepts - 6th Edition 1.19 ©Silberschatz, Korth and Sudarshan
XML: Extensible Markup Language
Defined by the WWW Consortium (W3C)
Originally intended as a document markup language not a
database language
The ability to specify new tags, and to create nested tag structures
made XML a great way to exchange data, not just documents
XML has become the basis for all new generation data interchange
formats.
A wide variety of tools is available for parsing, browsing and
querying XML documents/data
Database System Concepts - 6th Edition 1.20 ©Silberschatz, Korth and Sudarshan
Database Engine
Storage manager
Query processing
Transaction manager
Database System Concepts - 6th Edition 1.21 ©Silberschatz, Korth and Sudarshan
Storage Management
Storage manager is a program module that provides the interface
between the low-level data stored in the database and the application
programs and queries submitted to the system.
The storage manager is responsible to the following tasks:
Interaction with the OS file manager
Efficient storing, retrieving and updating of data
Issues:
Storage access
File organization
Indexing and hashing
Database System Concepts - 6th Edition 1.22 ©Silberschatz, Korth and Sudarshan
Query Processing
1. Parsing and translation
2. Optimization
3. Evaluation
Database System Concepts - 6th Edition 1.23 ©Silberschatz, Korth and Sudarshan
Query Processing (Cont.)
Alternative ways of evaluating a given query
Equivalent expressions
Different algorithms for each operation
Cost difference between a good and a bad way of evaluating a
query can be enormous
Need to estimate the cost of operations
Depends critically on statistical information about relations
which the database must maintain
Need to estimate statistics for intermediate results to compute
cost of complex expressions
Database System Concepts - 6th Edition 1.24 ©Silberschatz, Korth and Sudarshan
Transaction Management
What if the system fails?
What if more than one user is concurrently updating the same
data?
A transaction is a collection of operations that performs a single
logical function in a database application
Transaction-management component ensures that the
database remains in a consistent (correct) state despite system
failures (e.g., power failures and operating system crashes) and
transaction failures.
Concurrency-control manager controls the interaction among
the concurrent transactions, to ensure the consistency of the
database.
Database System Concepts - 6th Edition 1.25 ©Silberschatz, Korth and Sudarshan
Database Users and Administrators
Database
Database System Concepts - 6th Edition 1.26 ©Silberschatz, Korth and Sudarshan
Database System Internals
Database System Concepts - 6th Edition 1.27 ©Silberschatz, Korth and Sudarshan
Database Architecture
Database System Concepts - 6th Edition 1.28 ©Silberschatz, Korth and Sudarshan
History of Database Systems
1950s and early 1960s:
Data processing using magnetic tapes for storage
Tapes provided only sequential access
Punched cards for input
Late 1960s and 1970s:
Hard disks allowed direct access to data
Network and hierarchical data models in widespread use
Ted Codd defines the relational data model
Would win the ACM Turing Award for this work
IBM Research begins System R prototype
UC Berkeley begins Ingres prototype
High-performance (for the era) transaction processing
Database System Concepts - 6th Edition 1.29 ©Silberschatz, Korth and Sudarshan
History (cont.)
1980s:
Research relational prototypes evolve into commercial systems
SQL becomes industrial standard
Parallel and distributed database systems
Object-oriented database systems
1990s:
Large decision support and data-mining applications
Large multi-terabyte data warehouses
Emergence of Web commerce
Early 2000s:
XML and XQuery standards
Automated database administration
Later 2000s:
Giant data storage systems
Google BigTable, Yahoo PNuts, Amazon, ..
Database System Concepts - 6th Edition 1.30 ©Silberschatz, Korth and Sudarshan
End of Chapter 1
Database System Concepts - 6th Edition 1.31 ©Silberschatz, Korth and Sudarshan
Chapter 2: Intro to Relational Model
attributes
(or columns)
tuples
(or rows)
Database System Concepts - 6th Edition 2.2 ©Silberschatz, Korth and Sudarshan
Attribute Types
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
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 - 6th Edition 2.3 ©Silberschatz, Korth and Sudarshan
Relation Schema and Instance
A1, A2, …, An are attributes
Database System Concepts - 6th Edition 2.4 ©Silberschatz, Korth and Sudarshan
Relations are Unordered
Database System Concepts - 6th Edition 2.5 ©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
One of the candidate keys is selected to be the primary key.
which one?
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 - 6th Edition 2.6 ©Silberschatz, Korth and Sudarshan
Schema Diagram for University Database
Database System Concepts - 6th Edition 2.7 ©Silberschatz, Korth and Sudarshan
Relational Query Languages
Procedural vs .non-procedural, or declarative
“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
Not turning-machine equivalent
consists of 6 basic operations
Database System Concepts - 6th Edition 2.8 ©Silberschatz, Korth and Sudarshan
Select Operation – selection of rows (tuples)
Relation r
Database System Concepts - 6th Edition 2.9 ©Silberschatz, Korth and Sudarshan
Project Operation – selection of columns (Attributes)
Relation r:
A,C (r)
Database System Concepts - 6th Edition 2.10 ©Silberschatz, Korth and Sudarshan
Union of two relations
Relations r, s:
r s:
Database System Concepts - 6th Edition 2.11 ©Silberschatz, Korth and Sudarshan
Set difference of two relations
Relations r, s:
r – s:
Database System Concepts - 6th Edition 2.12 ©Silberschatz, Korth and Sudarshan
Set intersection of two relations
Relation r, s:
rs
Note: r s = r – (r – s)
Database System Concepts - 6th Edition 2.13 ©Silberschatz, Korth and Sudarshan
joining two relations -- Cartesian-product
Relations r, s:
r x s:
Database System Concepts - 6th Edition 2.14 ©Silberschatz, Korth and Sudarshan
Cartesian-product – naming issue
Relations r, s: B
r x s: r.B s.B
Database System Concepts - 6th Edition 2.15 ©Silberschatz, Korth and Sudarshan
Renaming a Table
Allows us to refer to a relation, (say E) by more than one name.
x (E)
Relations r
Database System Concepts - 6th Edition 2.16 ©Silberschatz, Korth and Sudarshan
Composition of Operations
Can build expressions using multiple operations
Example: A=C (r x s)
rxs
A=C (r x s)
Database System Concepts - 6th Edition 2.17 ©Silberschatz, Korth and Sudarshan
Joining two relations – Natural Join
Database System Concepts - 6th Edition 2.18 ©Silberschatz, Korth and Sudarshan
Natural Join Example
Relations r, s:
Natural Join
r s
Database System Concepts - 6th Edition 2.19 ©Silberschatz, Korth and Sudarshan
Notes about Relational Languages
Each Query input is a table (or set of tables)
Each query output is a table.
All data in the output table appears in one of the input tables
Relational Algebra is not Turning complete
Can we compute:
SUM
AVG
MAX
MIN
Database System Concepts - 6th Edition 2.20 ©Silberschatz, Korth and Sudarshan
Summary of Relational Algebra Operators
Symbol (Name) Example of Use
σ
(Selection) σ salary > = 85000 (instructor)
Return rows of the input relation that satisfy the predicate.
Π
(Projection) Π ID, salary (instructor)
Output specified attributes from all rows of the input relation. Remove
duplicate tuples from the output.
x
(Cartesian Product) instructor x department
Output pairs of rows from the two input relations that have the same value on
all attributes that have the same name.
∪
(Union) Π name (instructor) ∪ Π name (student)
Output the set difference of tuples from the two input relations.
⋈
(Natural Join) instructor ⋈ department
Output pairs of rows from the two input relations that have the same value on
all attributes that have the same name.
Database System Concepts - 6th Edition 2.21 ©Silberschatz, Korth and Sudarshan
End of Chapter 2
E.g. Customer-schema =
(customer-name, customer-street, customer-city)
r(R) is a relation on the relation schema R
attributes
(or columns)
customer-name customer-street customer-city
customer
• Relation r A B C D
1 7
5 7
12 3
23 10
1 7
23 10
Notation: p(r)
p is called the selection predicate
Defined as:
Relation r: A B C
10 1
20 1
30 1
40 2
A,C (r) A C A C
1 1
1 = 1
1 2
2
Relations r, s:
A B A B
1 2
2 3
1 s
r
r s: A B
1
2
1
3
r s = {t | t r or t s}
For r s to be valid.
Relations r, s:
A B A B
1 2
2 3
1 s
r
r – s: A B
1
1
r – s = {t | t r and t 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
Relations r, s: A B C D E
1 10 a
10 a
2 20 b
r 10 b
s
r x s:
A B C D E
1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
r x s = {t q | t r and q s}
Assume that attributes of r(R) and s(S) are disjoint. (That is,
R S = ).
If attributes of r(R) and s(S) are not disjoint, then renaming must
be used.
1 10 a
2 20 a
2 20 b
Database System Concepts 3.27 ©Silberschatz, Korth and Sudarshan
Rename Operation
Allows us to name, and therefore to refer to, the results of
relational-algebra expressions.
Allows us to refer to a relation by more than one name.
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.
Find the loan number for each loan of an amount greater than
$1200
loan-number (amount > 1200 (loan))
account at bank.
customer-name (branch-name=“Perryridge”
Query 2
customer-name(loan.loan-number = borrower.loan-number(
(branch-name = “Perryridge”(loan)) x borrower))
balance(account) - account.balance
Set intersection
Natural join
Division
Assignment
r s
rs
A B
2
Example:
R = (A, B, C, D)
S = (E, B, D)
Result schema = (A, B, C, D, E)
r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
A B C D B D E
1 a 1 a
2 a 3 a
4 b 1 a
1 a 2 b
2 b 3 b
r s
r s
A B C D E
1 a
1 a
1 a
1 a
2 b
rs
Suited to queries that include the phrase “for all”.
Let r and s be relations on schemas R and S respectively
where
R = (A1, …, Am, B1, …, Bn)
S = (B1, …, Bn)
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 ) }
Relations r, s: A B B
1
1
2
3 2
1 s
1
1
3
4
6
1
2
r s: A r
Relations r, s:
A B C D E D E
a a 1 a 1
a a 1 b 1
a b 1 s
a a 1
a b 3
a a 1
a b 1
a b 1
r
r s: A B C
a
a
Property
Let q – r s
Then q is the largest relation satisfying q x s r
Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S R
To see why
R-S,S(r) simply reorders attributes of r
CN(BN=“Downtown”(depositor account))
CN(BN=“Uptown”(depositor account))
Query 2
Generalized Projection
Outer Join
Aggregate Functions
7
7
3
10
sum-C
g sum(c) (r)
27
Relation loan
Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
loan Borrower
rr–E
where r is a relation and E is a relational algebra query.
r r E
where r is a relation and E is a relational algebra expression.
The insertion of a single tuple is expressed by letting E be a
constant relation containing one tuple.
customer-name
{t | P (t) }
It is the set of all tuples t such that predicate P is true for t
t is a tuple variable, t[A] denotes the value of tuple t on attribute A
t r denotes that tuple t is in relation r
P is a formula similar to that of the predicate calculus
Find the loan number for each loan of an amount greater than $1200
Find the names of all customers who have a loan and an account
at the bank
{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 loan(u[branch-name] = “Perryridge”
u[loan-number] = s[loan-number]))}
{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])))}
Find the names of all customers who have a loan of over $1200
Find the names of all customers who have a loan from the
Perryridge branch and the loan amount:
{ 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 | s, n ( c, s, n customer)
x,y,z( x, y, z branch y = “Brooklyn”)
a,b( x, y, z account c,a depositor)}
Relational Algebra
Tuple Relational Calculus
Domain Relational Calculus
Database System Concepts - 6th Edition 6.2 ©Silberschatz, Korth and Sudarshan
Relational Algebra
Procedural language
Six basic operators
select:
project:
union:
set difference: –
Cartesian product: x
rename:
The operators take one or two relations as inputs and produce a new
relation as a result.
Database System Concepts - 6th Edition 6.3 ©Silberschatz, Korth and Sudarshan
Select Operation
Notation: p(r)
p is called the selection predicate
Defined as:
Example of selection:
dept_name=“Physics”(instructor)
Database System Concepts - 6th Edition 6.4 ©Silberschatz, Korth and Sudarshan
Project Operation
Notation:
A1 , A2 ,, Ak (r )
where A1, A2 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
Example: To eliminate the dept_name attribute of instructor
Database System Concepts - 6th Edition 6.5 ©Silberschatz, Korth and Sudarshan
Union Operation
Notation: r s
Defined as:
r s = {t | t r or t 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 2 nd
column of s)
Example: to find all courses taught in the Fall 2009 semester, or in the
Spring 2010 semester, or in both
Database System Concepts - 6th Edition 6.6 ©Silberschatz, Korth and Sudarshan
Set Difference Operation
Notation r – s
Defined as:
r – s = {t | t r and t s}
Database System Concepts - 6th Edition 6.7 ©Silberschatz, Korth and Sudarshan
Set-Intersection Operation
Notation: r s
Defined as:
r s = { t | t r and t s }
Assume:
r, s have the same arity
attributes of r and s are compatible
Note: r s = r – (r – s)
Database System Concepts - 6th Edition 6.8 ©Silberschatz, Korth and Sudarshan
Cartesian-Product Operation
Notation r x s
Defined as:
r x s = {t q | t r and q s}
Database System Concepts - 6th Edition 6.9 ©Silberschatz, Korth and Sudarshan
Rename Operation
Allows us to name, and therefore to refer to, the results of relational-
algebra expressions.
Allows us to refer to a relation by more than one name.
Example:
x (E)
x ( A1 , A2 ,...,An ) ( E )
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
Database System Concepts - 6th Edition 6.10 ©Silberschatz, Korth and Sudarshan
Formal Definition
A basic expression in the relational algebra consists of either one of the
following:
A relation in the database
A constant relation
Let E1 and E2 be relational-algebra expressions; the following are all
relational-algebra expressions:
E1 E2
E1 – E2
E1 x E2
Database System Concepts - 6th Edition 6.11 ©Silberschatz, Korth and Sudarshan
Tuple Relational Calculus
Database System Concepts - 6th Edition 6.12 ©Silberschatz, Korth and Sudarshan
Tuple Relational Calculus
Database System Concepts - 6th Edition 6.13 ©Silberschatz, Korth and Sudarshan
Predicate Calculus Formula
Database System Concepts - 6th Edition 6.14 ©Silberschatz, Korth and Sudarshan
Example Queries
Find the ID, name, dept_name, salary for instructors whose salary
is greater than $80,000
Database System Concepts - 6th Edition 6.15 ©Silberschatz, Korth and Sudarshan
Example Queries
Find the set of all courses taught in the Fall 2009 semester, or in
the Spring 2010 semester, or both
Database System Concepts - 6th Edition 6.16 ©Silberschatz, Korth and Sudarshan
Example Queries
Find the set of all courses taught in the Fall 2009 semester, and in
the Spring 2010 semester
Find the set of all courses taught in the Fall 2009 semester, but not in
the Spring 2010 semester
Database System Concepts - 6th Edition 6.17 ©Silberschatz, Korth and Sudarshan
Universal Quantification
Find all students who have taken all courses offered in the
Biology department
{t | r student (t [ID] = r [ID])
( u course (u [dept_name]=“Biology”
s takes (t [ID] = s [ID ]
s [course_id] = u [course_id]))}
Database System Concepts - 6th Edition 6.18 ©Silberschatz, Korth and Sudarshan
Safety of Expressions
Database System Concepts - 6th Edition 6.19 ©Silberschatz, Korth and Sudarshan
Safety of Expressions (Cont.)
Consider again that query to find all students who have taken
all courses offered in the Biology department
{t | r student (t [ID] = r [ID])
( u course (u [dept_name]=“Biology”
s takes (t [ID] = s [ID ]
s [course_id] = u [course_id]))}
Without the existential quantification on student, the above
query would be unsafe if the Biology department has not
offered any courses.
Database System Concepts - 6th Edition 6.20 ©Silberschatz, Korth and Sudarshan
Domain Relational Calculus
Database System Concepts - 6th Edition 6.21 ©Silberschatz, Korth and Sudarshan
Domain Relational Calculus
Database System Concepts - 6th Edition 6.22 ©Silberschatz, Korth and Sudarshan
Example Queries
Find the ID, name, dept_name, salary for instructors whose salary is
greater than $80,000
{< i, n, d, s> | < i, n, d, s> instructor s 80000}
As in the previous query, but output only the ID attribute value
{< i> | < i, n, d, s> instructor s 80000}
Find the names of all instructors whose department is in the Watson
building
{< n > | i, d, s (< i, n, d, s > instructor
b, a (< d, b, a> department b = “Watson” ))}
Database System Concepts - 6th Edition 6.23 ©Silberschatz, Korth and Sudarshan
Example Queries
Find the set of all courses taught in the Fall 2009 semester, or in
the Spring 2010 semester, or both
{<c> | a, s, y, b, r, t ( <c, a, s, y, b, r, t > section
s = “Fall” y = 2009 )
v a, s, y, b, r, t ( <c, a, s, y, b, r, t > section ]
s = “Spring” y = 2010)}
This case can also be written as
{<c> | a, s, y, b, r, t ( <c, a, s, y, b, r, t > section
( (s = “Fall” y = 2009 ) v (s = “Spring” y = 2010))}
Find the set of all courses taught in the Fall 2009 semester, and in
the Spring 2010 semester
Database System Concepts - 6th Edition 6.24 ©Silberschatz, Korth and Sudarshan
Safety of Expressions
The expression:
{ x1, x2, …, xn | P (x1, x2, …, xn )}
Database System Concepts - 6th Edition 6.25 ©Silberschatz, Korth and Sudarshan
Universal Quantification
Find all students who have taken all courses offered in the Biology
department
{< i > | n, d, tc ( < i, n, d, tc > student
( ci, ti, dn, cr ( < ci, ti, dn, cr > course dn =“Biology”
si, se, y, g ( <i, ci, si, se, y, g> takes ))}
Note that without the existential quantification on student, the
above query would be unsafe if the Biology department has not
offered any courses.
Database System Concepts - 6th Edition 6.26 ©Silberschatz, Korth and Sudarshan
End of Chapter 6
Design Process
Modeling
Constraints
E-R Diagram
Design Issues
Weak Entity Sets
Extended E-R Features
Design of the Bank Database
Reduction to Relation Schemas
Database Design
UML
Attribute types:
Simple and composite attributes.
Single-valued and multivalued attributes
Example: multivalued attribute: phone_numbers
Derived attributes
Can be computed from other attributes
Example: age, given date_of_birth
Domain – the set of permitted values for each attribute
A weak entity set becomes a table that includes a column for the
primary key of the identifying strong entity set
section ( course_id, sec_id, sem, year )
Method 1:
Form a schema for the higher-level entity
Form a schema for each lower-level entity set, include primary
key of higher-level entity set and local attributes
schema attributes
person ID, name, street, city
student ID, tot_cred
employee ID, salary
Method 2:
Form a schema for each entity set with all local and inherited
attributes
schema attributes
person ID, name, street, city
student ID, name, street, city, tot_cred
employee ID, name, street, city, salary
Database System Concepts - 6th Edition 3.2 ©Silberschatz, Korth and Sudarshan
History
IBM Sequel language developed as part of System R project at the
IBM San Jose Research Laboratory
Renamed Structured Query Language (SQL)
ANSI and ISO standard SQL:
SQL-86
SQL-89
SQL-92
SQL:1999 (language name became Y2K compliant!)
SQL:2003
Commercial systems offer most, if not all, SQL-92 features, plus
varying feature sets from later standards and special proprietary
features.
Not all examples here may work on your particular system.
Database System Concepts - 6th Edition 3.3 ©Silberschatz, Korth and Sudarshan
Data Definition Language
The SQL data-definition language (DDL) allows the specification
of information about relations, including:
The schema for each relation.
The domain of values associated with each attribute.
Integrity constraints
And as we will see later, also other information such as
The set of indices to be maintained for each relations.
Security and authorization information for each relation.
The physical storage structure of each relation on disk.
Database System Concepts - 6th Edition 3.4 ©Silberschatz, Korth and Sudarshan
Domain Types in SQL
char(n). Fixed length character string, with user-specified length n.
varchar(n). Variable length character strings, with user-specified
maximum length n.
int. Integer (a finite subset of the integers that is machine-dependent).
smallint. Small integer (a machine-dependent subset of the integer
domain type).
numeric(p,d). Fixed point number, with user-specified precision of p
digits, with d digits to the right of decimal point. (ex., numeric(3,1),
allows 44.5 to be stores exactly, but not 444.5 or 0.32)
real, double precision. Floating point and double-precision floating
point numbers, with machine-dependent precision.
float(n). Floating point number, with user-specified precision of at least
n digits.
More are covered in Chapter 4.
Database System Concepts - 6th Edition 3.5 ©Silberschatz, Korth and Sudarshan
Create Table Construct
An SQL relation is defined using the create table command:
create table r (A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))
r is the name of the relation
each Ai is an attribute name in the schema of relation r
Di is the data type of values in the domain of attribute Ai
Example:
create table instructor (
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))
Database System Concepts - 6th Edition 3.6 ©Silberschatz, Korth and Sudarshan
Integrity Constraints in Create Table
not null
primary key (A1, ..., An )
foreign key (Am, ..., An ) references r
Example:
Database System Concepts - 6th Edition 3.7 ©Silberschatz, Korth and Sudarshan
And a Few More Relation Definitions
create table student (
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0),
primary key (ID),
foreign key (dept_name) references department);
Database System Concepts - 6th Edition 3.8 ©Silberschatz, Korth and Sudarshan
And more still
create table course (
course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0),
primary key (course_id),
foreign key (dept_name) references department);
Database System Concepts - 6th Edition 3.9 ©Silberschatz, Korth and Sudarshan
Updates to tables
Insert
insert into instructor values (‘10211’, ’Smith’, ’Biology’, 66000);
Delete
Remove all tuples from the student relation
delete from student
Drop Table
drop table r
Alter
alter table r add A D
where A is the name of the attribute to be added to relation r
and D is the domain of A.
All exiting tuples in the relation are assigned null as the value for
the new attribute.
alter table r drop A
where A is the name of an attribute of relation r
Dropping of attributes not supported by many databases.
Database System Concepts - 6th Edition 3.10 ©Silberschatz, Korth and Sudarshan
Basic Query Structure
A typical SQL query has the form:
Ai represents an attribute
Ri represents a relation
P is a predicate.
The result of an SQL query is a relation.
Database System Concepts - 6th Edition 3.11 ©Silberschatz, Korth and Sudarshan
The select Clause
The select clause lists the attributes desired in the result of a query
corresponds to the projection operation of the relational algebra
Example: find the names of all instructors:
select name
from instructor
NOTE: SQL names are case insensitive (i.e., you may use upper- or
lower-case letters.)
E.g., Name ≡ NAME ≡ name
Some people use upper case wherever we use bold font.
Database System Concepts - 6th Edition 3.12 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
SQL allows duplicates in relations as well as in query results.
To force the elimination of duplicates, insert the keyword distinct
after select.
Find the department names of all instructors, and remove duplicates
select distinct dept_name
from instructor
The keyword all specifies that duplicates should not be removed.
Database System Concepts - 6th Edition 3.13 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
An asterisk in the select clause denotes “all attributes”
select *
from instructor
An attribute can be a literal with no from clause
select ‘437’
Results is a table with one column and a single row with value “437”
Can give the column a name using:
select ‘437’ as FOO
An attribute can be a literal with from clause
select ‘A’
from instructor
Result is a table with one column and N rows (number of tuples in the
instructors table), each row with value “A”
Database System Concepts - 6th Edition 3.14 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
The select clause can contain arithmetic expressions involving the
operation, +, –, , and /, and operating on constants or attributes of
tuples.
The query:
select ID, name, salary/12
from instructor
would return a relation that is the same as the instructor relation,
except that the value of the attribute salary is divided by 12.
Can rename “salary/12” using the as clause:
select ID, name, salary/12 as monthly_salary
Database System Concepts - 6th Edition 3.15 ©Silberschatz, Korth and Sudarshan
The where Clause
The where clause specifies conditions that the result must satisfy
Corresponds to the selection predicate of the relational algebra.
To find all instructors in Comp. Sci. dept
select name
from instructor
where dept_name = ‘Comp. Sci.'
Comparison results can be combined using the logical connectives
and, or, and not
To find all instructors in Comp. Sci. dept with salary > 80000
select name
from instructor
where dept_name = ‘Comp. Sci.' and salary > 80000
Database System Concepts - 6th Edition 3.16 ©Silberschatz, Korth and Sudarshan
The from Clause
The from clause lists the relations involved in the query
Corresponds to the Cartesian product operation of the relational
algebra.
Find the Cartesian product instructor X teaches
select
from instructor, teaches
generates every possible instructor – teaches pair, with all attributes
from both relations.
For common attributes (e.g., ID), the attributes in the resulting table
are renamed using the relation name (e.g., instructor.ID)
Cartesian product not very useful directly, but useful combined with
where-clause condition (selection operation in relational algebra).
Database System Concepts - 6th Edition 3.17 ©Silberschatz, Korth and Sudarshan
Cartesian Product
instructor teaches
Database System Concepts - 6th Edition 3.18 ©Silberschatz, Korth and Sudarshan
Examples
Find the names of all instructors who have taught some course and the
course_id
select name, course_id
from instructor , teaches
where instructor.ID = teaches.ID
Find the names of all instructors in the Art department who have taught
some course and the course_id
select name, course_id
from instructor , teaches
where instructor.ID = teaches.ID and instructor. dept_name = ‘Art’
Database System Concepts - 6th Edition 3.19 ©Silberschatz, Korth and Sudarshan
The Rename Operation
The SQL allows renaming relations and attributes using the as clause:
old-name as new-name
Find the names of all instructors who have a higher salary than
some instructor in ‘Comp. Sci’.
select distinct T.name
from instructor as T, instructor as S
where T.salary > S.salary and S.dept_name = ‘Comp. Sci.’
Database System Concepts - 6th Edition 3.20 ©Silberschatz, Korth and Sudarshan
Self Join Example
Relation emp-super
person
supervisor
Bob Alice
Mary Susan
Alice David
David
Mary
Find the supervisor of “Bob”
Find the supervisor of the supervisor of “Bob”
Find ALL the supervisors (direct and indirect) of “Bob
Database System Concepts - 6th Edition 3.21 ©Silberschatz, Korth and Sudarshan
String Operations
SQL includes a string-matching operator for comparisons on character
strings. The operator like uses patterns that are described using two
special characters:
percent ( % ). The % character matches any substring.
underscore ( _ ). The _ character matches any character.
Find the names of all instructors whose name includes the substring
“dar”.
select name
from instructor
where name like '%dar%'
Match the string “100%”
like ‘100 \%' escape '\'
in that above we use backslash (\) as the escape character.
Database System Concepts - 6th Edition 3.22 ©Silberschatz, Korth and Sudarshan
String Operations (Cont.)
Patterns are case sensitive.
Pattern matching examples:
‘Intro%’ matches any string beginning with “Intro”.
‘%Comp%’ matches any string containing “Comp” as a substring.
‘_ _ _’ matches any string of exactly three characters.
‘_ _ _ %’ matches any string of at least three characters.
Database System Concepts - 6th Edition 3.23 ©Silberschatz, Korth and Sudarshan
Ordering the Display of Tuples
List in alphabetic order the names of all instructors
select distinct name
from instructor
order by name
We may specify desc for descending order or asc for ascending
order, for each attribute; ascending order is the default.
Example: order by name desc
Can sort on multiple attributes
Example: order by dept_name, name
Database System Concepts - 6th Edition 3.24 ©Silberschatz, Korth and Sudarshan
Where Clause Predicates
SQL includes a between comparison operator
Example: Find the names of all instructors with salary between $90,000
and $100,000 (that is, $90,000 and $100,000)
select name
from instructor
where salary between 90000 and 100000
Tuple comparison
select name, course_id
from instructor, teaches
where (instructor.ID, dept_name) = (teaches.ID, ’Biology’);
Database System Concepts - 6th Edition 3.25 ©Silberschatz, Korth and Sudarshan
Duplicates
In relations with duplicates, SQL can define how many copies of tuples
appear in the result.
Multiset versions of some of the relational algebra operators – given
multiset relations r1 and r2:
Database System Concepts - 6th Edition 3.26 ©Silberschatz, Korth and Sudarshan
Duplicates (Cont.)
Example: Suppose multiset relations r1 (A, B) and r2 (C) are as
follows:
r1 = {(1, a) (2,a)} r2 = {(2), (3), (3)}
Then B(r1) would be {(a), (a)}, while B(r1) x r2 would be
{(a,2), (a,2), (a,3), (a,3), (a,3), (a,3)}
SQL duplicate semantics:
select A1,, A2, ..., An
from r1, r2, ..., rm
where P
is equivalent to the multiset version of the expression:
Database System Concepts - 6th Edition 3.27 ©Silberschatz, Korth and Sudarshan
Set Operations
(select course_id from section where sem = ‘Fall’ and year = 2009)
intersect
(select course_id from section where sem = ‘Spring’ and year = 2010)
Find courses that ran in Fall 2009 but not in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)
except
(select course_id from section where sem = ‘Spring’ and year = 2010)
Database System Concepts - 6th Edition 3.28 ©Silberschatz, Korth and Sudarshan
Set Operations (Cont.)
Find the salaries of all instructors that are less than the largest salary.
select distinct T.salary
from instructor as T, instructor as S
where T.salary < S.salary
Database System Concepts - 6th Edition 3.29 ©Silberschatz, Korth and Sudarshan
Set Operations (Cont.)
Set operations union, intersect, and except
Each of the above operations automatically eliminates duplicates
To retain all duplicates use the corresponding multiset versions union
all, intersect all and except all.
Database System Concepts - 6th Edition 3.30 ©Silberschatz, Korth and Sudarshan
Null Values
It is possible for tuples to have a null value, denoted by null, for
some of their attributes
null signifies an unknown value or that a value does not exist.
The result of any arithmetic expression involving null is null
Example: 5 + null returns null
The predicate is null can be used to check for null values.
Example: Find all instructors whose salary is null.
select name
from instructor
where salary is null
Database System Concepts - 6th Edition 3.31 ©Silberschatz, Korth and Sudarshan
Null Values and Three Valued Logic
Three values – true, false, unknown
Any comparison with null returns unknown
Example: 5 < null or null <> null or null = null
Three-valued logic using the value unknown:
OR: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown
AND: (true and unknown) = unknown,
(false and unknown) = false,
(unknown and unknown) = unknown
NOT: (not unknown) = unknown
“P is unknown” evaluates to true if predicate P evaluates to
unknown
Result of where clause predicate is treated as false if it evaluates to
unknown
Database System Concepts - 6th Edition 3.32 ©Silberschatz, Korth and Sudarshan
Aggregate Functions
These functions operate on the multiset of values of a column of
a relation, and return a value
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Database System Concepts - 6th Edition 3.33 ©Silberschatz, Korth and Sudarshan
Aggregate Functions (Cont.)
Find the average salary of instructors in the Computer Science
department
select avg (salary)
from instructor
where dept_name= ’Comp. Sci.’;
Find the total number of instructors who teach a course in the Spring
2010 semester
select count (distinct ID)
from teaches
where semester = ’Spring’ and year = 2010;
Find the number of tuples in the course relation
select count (*)
from course;
Database System Concepts - 6th Edition 3.34 ©Silberschatz, Korth and Sudarshan
Aggregate Functions – Group By
Find the average salary of instructors in each department
select dept_name, avg (salary) as avg_salary
from instructor
group by dept_name;
avg_salary
Database System Concepts - 6th Edition 3.35 ©Silberschatz, Korth and Sudarshan
Aggregation (Cont.)
Attributes in select clause outside of aggregate functions must appear
in group by list
/* erroneous query */
select dept_name, ID, avg (salary)
from instructor
group by dept_name;
Database System Concepts - 6th Edition 3.36 ©Silberschatz, Korth and Sudarshan
Aggregate Functions – Having Clause
Database System Concepts - 6th Edition 3.37 ©Silberschatz, Korth and Sudarshan
Null Values and Aggregates
Total all salaries
select sum (salary )
from instructor
Above statement ignores null amounts
Result is null if there is no non-null amount
All aggregate operations except count(*) ignore tuples with null values
on the aggregated attributes
What if collection has only null values?
count returns 0
all other aggregates return null
Database System Concepts - 6th Edition 3.38 ©Silberschatz, Korth and Sudarshan
Nested Subqueries
SQL provides a mechanism for the nesting of subqueries. A subquery
is a select-from-where expression that is nested within another query.
The nesting can be done in the following SQL query
as follows:
Ai can be replaced be a subquery that generates a single value.
ri can be replaced by any valid subquery
P can be replaced with an expression of the form:
B <operation> (subquery)
Where B is an attribute and <operation> to be defined later.
Database System Concepts - 6th Edition 3.39 ©Silberschatz, Korth and Sudarshan
Subqueries in the Where Clause
Database System Concepts - 6th Edition 3.40 ©Silberschatz, Korth and Sudarshan
Subqueries in the Where Clause
A common use of subqueries is to perform tests:
For set membership
For set comparisons
For set cardinality.
Database System Concepts - 6th Edition 3.41 ©Silberschatz, Korth and Sudarshan
Set Membership
Find courses offered in Fall 2009 and in Spring 2010
Database System Concepts - 6th Edition 3.42 ©Silberschatz, Korth and Sudarshan
Set Membership (Cont.)
Find the total number of (distinct) students who have taken course
sections taught by the instructor with ID 10101
Database System Concepts - 6th Edition 3.43 ©Silberschatz, Korth and Sudarshan
Set Comparison – “some” Clause
Find names of instructors with salary greater than that of some (at
least one) instructor in the Biology department.
select name
from instructor
where salary > some (select salary
from instructor
where dept name = ’Biology’);
Database System Concepts - 6th Edition 3.44 ©Silberschatz, Korth and Sudarshan
Definition of “some” Clause
0
(5 < some 5 ) = true
(read: 5 < some tuple in the relation)
6
0
(5 < some 5 ) = false
0
(5 = some 5 ) = true
0
(5 some 5 ) = true (since 0 5)
(= some) in
However, ( some) not in
Database System Concepts - 6th Edition 3.45 ©Silberschatz, Korth and Sudarshan
Set Comparison – “all” Clause
Find the names of all instructors whose salary is greater than the
salary of all instructors in the Biology department.
select name
from instructor
where salary > all (select salary
from instructor
where dept name = ’Biology’);
Database System Concepts - 6th Edition 3.46 ©Silberschatz, Korth and Sudarshan
Definition of “all” Clause
F <comp> all r t r (F <comp> t)
0
(5 < all 5 ) = false
6
6
(5 < all 10 ) = true
4
(5 = all 5 ) = false
4
(5 all 6 ) = true (since 5 4 and 5 6)
( all) not in
However, (= all) in
Database System Concepts - 6th Edition 3.47 ©Silberschatz, Korth and Sudarshan
Test for Empty Relations
The exists construct returns the value true if the argument
subquery is nonempty.
exists r r Ø
not exists r r = Ø
Database System Concepts - 6th Edition 3.48 ©Silberschatz, Korth and Sudarshan
Use of “exists” Clause
Yet another way of specifying the query “Find all courses taught in
both the Fall 2009 semester and in the Spring 2010 semester”
select course_id
from section as S
where semester = ’Fall’ and year = 2009 and
exists (select *
from section as T
where semester = ’Spring’ and year= 2010
and S.course_id = T.course_id);
Database System Concepts - 6th Edition 3.49 ©Silberschatz, Korth and Sudarshan
Use of “not exists” Clause
Find all students who have taken all courses offered in the Biology
department.
Database System Concepts - 6th Edition 3.50 ©Silberschatz, Korth and Sudarshan
Test for Absence of Duplicate Tuples
The unique construct tests whether a subquery has any
duplicate tuples in its result.
The unique construct evaluates to “true” if a given subquery
contains no duplicates .
Find all courses that were offered at most once in 2009
select T.course_id
from course as T
where unique (select R.course_id
from section as R
where T.course_id= R.course_id
and R.year = 2009);
Database System Concepts - 6th Edition 3.51 ©Silberschatz, Korth and Sudarshan
Subqueries in the Form Clause
Database System Concepts - 6th Edition 3.52 ©Silberschatz, Korth and Sudarshan
Subqueries in the Form Clause
SQL allows a subquery expression to be used in the from clause
Find the average instructors’ salaries of those departments where the
average salary is greater than $42,000.”
select dept_name, avg_salary
from (select dept_name, avg (salary) as avg_salary
from instructor
group by dept_name)
where avg_salary > 42000;
Note that we do not need to use the having clause
Another way to write above query
select dept_name, avg_salary
from (select dept_name, avg (salary)
from instructor
group by dept_name) as dept_avg (dept_name, avg_salary)
where avg_salary > 42000;
Database System Concepts - 6th Edition 3.53 ©Silberschatz, Korth and Sudarshan
With Clause
The with clause provides a way of defining a temporary relation
whose definition is available only to the query in which the with
clause occurs.
Find all departments with the maximum budget
Database System Concepts - 6th Edition 3.54 ©Silberschatz, Korth and Sudarshan
Complex Queries using With Clause
Find all departments where the total salary is greater than the
average of the total salary at all departments
Database System Concepts - 6th Edition 3.55 ©Silberschatz, Korth and Sudarshan
Subqueries in the Select Clause
Database System Concepts - 6th Edition 3.56 ©Silberschatz, Korth and Sudarshan
Scalar Subquery
Scalar subquery is one which is used where a single value is
expected
List all departments along with the number of instructors in each
department
select dept_name,
(select count(*)
from instructor
where department.dept_name = instructor.dept_name)
as num_instructors
from department;
Runtime error if subquery returns more than one result tuple
Database System Concepts - 6th Edition 3.57 ©Silberschatz, Korth and Sudarshan
Modification of the Database
Database System Concepts - 6th Edition 3.58 ©Silberschatz, Korth and Sudarshan
Deletion
Database System Concepts - 6th Edition 3.59 ©Silberschatz, Korth and Sudarshan
Deletion (Cont.)
Delete all instructors whose salary is less than the average salary of
instructors
Database System Concepts - 6th Edition 3.60 ©Silberschatz, Korth and Sudarshan
Insertion
Add a new tuple to course
insert into course
values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
or equivalently
Database System Concepts - 6th Edition 3.61 ©Silberschatz, Korth and Sudarshan
Insertion (Cont.)
Add all instructors to the student relation with tot_creds set to 0
insert into student
select ID, name, dept_name, 0
from instructor
The select from where statement is evaluated fully before any of its
results are inserted into the relation.
Otherwise queries like
insert into table1 select * from table1
would cause problem
Database System Concepts - 6th Edition 3.62 ©Silberschatz, Korth and Sudarshan
Updates
Increase salaries of instructors whose salary is over $100,000
by 3%, and all others by a 5%
Write two update statements:
update instructor
set salary = salary * 1.03
where salary > 100000;
update instructor
set salary = salary * 1.05
where salary <= 100000;
The order is important
Can be done better using the case statement (next slide)
Database System Concepts - 6th Edition 3.63 ©Silberschatz, Korth and Sudarshan
Case Statement for Conditional Updates
Same query as before but with case statement
update instructor
set salary = case
when salary <= 100000 then salary * 1.05
else salary * 1.03
end
Database System Concepts - 6th Edition 3.64 ©Silberschatz, Korth and Sudarshan
Updates with Scalar Subqueries
Recompute and update tot_creds value for all students
update student S
set tot_cred = (select sum(credits)
from takes, course
where takes.course_id = course.course_id and
S.ID= takes.ID.and
takes.grade <> ’F’ and
takes.grade is not null);
Sets tot_creds to null for students who have not taken any course
Instead of sum(credits), use:
case
when sum(credits) is not null then sum(credits)
else 0
end
Database System Concepts - 6th Edition 3.65 ©Silberschatz, Korth and Sudarshan
End of Chapter 3
Database System Concepts - 6th Edition 4.2 ©Silberschatz, Korth and Sudarshan
Joined Relations
Database System Concepts - 6th Edition 4.3 ©Silberschatz, Korth and Sudarshan
Join operations – Example
Relation course
Relation prereq
Observe that
prereq information is missing for CS-315 and
course information is missing for CS-437
Database System Concepts - 6th Edition 4.4 ©Silberschatz, Korth and Sudarshan
Outer Join
Database System Concepts - 6th Edition 4.5 ©Silberschatz, Korth and Sudarshan
Left Outer Join
Database System Concepts - 6th Edition 4.6 ©Silberschatz, Korth and Sudarshan
Right Outer Join
Database System Concepts - 6th Edition 4.7 ©Silberschatz, Korth and Sudarshan
Joined Relations
Join operations take two relations and return as a result
another relation.
These additional operations are typically used as subquery
expressions in the from clause
Join condition – defines which tuples in the two relations
match, and what attributes are present in the result of the join.
Join type – defines how tuples in each relation that do not
match any tuple in the other relation (based on the join
condition) are treated.
Database System Concepts - 6th Edition 4.8 ©Silberschatz, Korth and Sudarshan
Full Outer Join
Database System Concepts - 6th Edition 4.9 ©Silberschatz, Korth and Sudarshan
Joined Relations – Examples
course inner join prereq on
course.course_id = prereq.course_id
Database System Concepts - 6th Edition 4.10 ©Silberschatz, Korth and Sudarshan
Joined Relations – Examples
course natural right outer join prereq
Database System Concepts - 6th Edition 4.11 ©Silberschatz, Korth and Sudarshan
Views
In some cases, it is not desirable for all users to see the
entire logical model (that is, all the actual relations stored in
the database.)
Consider a person who needs to know an instructors name
and department, but not the salary. This person should see a
relation described, in SQL, by
Database System Concepts - 6th Edition 4.12 ©Silberschatz, Korth and Sudarshan
View Definition
A view is defined using the create view statement which has
the form
Database System Concepts - 6th Edition 4.13 ©Silberschatz, Korth and Sudarshan
Example Views
A view of instructors without their salary
create view faculty as
select ID, name, dept_name
from instructor
Find all instructors in the Biology department
select name
from faculty
where dept_name = ‘Biology’
Create a view of department salary totals
create view departments_total_salary(dept_name, total_salary) as
select dept_name, sum (salary)
from instructor
group by dept_name;
Database System Concepts - 6th Edition 4.14 ©Silberschatz, Korth and Sudarshan
Views Defined Using Other Views
create view physics_fall_2009 as
select course.course_id, sec_id, building, room_number
from course, section
where course.course_id = section.course_id
and course.dept_name = ’Physics’
and section.semester = ’Fall’
and section.year = ’2009’;
create view physics_fall_2009_watson as
select course_id, room_number
from physics_fall_2009
where building= ’Watson’;
Database System Concepts - 6th Edition 4.15 ©Silberschatz, Korth and Sudarshan
View Expansion
Expand use of a view in a query/another view
Database System Concepts - 6th Edition 4.16 ©Silberschatz, Korth and Sudarshan
Views Defined Using Other Views
One view may be used in the expression defining another view
A view relation v1 is said to depend directly on a view relation
v2 if v2 is used in the expression defining v1
A view relation v1 is said to depend on view relation v2 if either
v1 depends directly to v2 or there is a path of dependencies
from v1 to v2
A view relation v is said to be recursive if it depends on itself.
Database System Concepts - 6th Edition 4.17 ©Silberschatz, Korth and Sudarshan
View Expansion
A way to define the meaning of views defined in terms of other
views.
Let view v1 be defined by an expression e1 that may itself
contain uses of view relations.
View expansion of an expression repeats the following
replacement step:
repeat
Find any view relation vi in e1
Replace the view relation vi by the expression defining vi
until no more view relations are present in e1
As long as the view definitions are not recursive, this loop will
terminate
Database System Concepts - 6th Edition 4.18 ©Silberschatz, Korth and Sudarshan
Update of a View
Add a new tuple to faculty view which we defined earlier
Database System Concepts - 6th Edition 4.19 ©Silberschatz, Korth and Sudarshan
Some Updates cannot be Translated Uniquely
create view instructor_info as
select ID, name, building
from instructor, department
where instructor.dept_name= department.dept_name;
insert into instructor_info values (’69987’, ’White’, ’Taylor’);
which department, if multiple departments in Taylor?
what if no department is in Taylor?
Most SQL implementations allow updates only on simple views
The from clause has only one database relation.
The select clause contains only attribute names of the
relation, and does not have any expressions, aggregates, or
distinct specification.
Any attribute not listed in the select clause can be set to null
The query does not have a group by or having clause.
Database System Concepts - 6th Edition 4.20 ©Silberschatz, Korth and Sudarshan
And Some Not at All
create view history_instructors as
select *
from instructor
where dept_name= ’History’;
What happens if we insert (’25566’, ’Brown’, ’Biology’, 100000)
into history_instructors?
Database System Concepts - 6th Edition 4.21 ©Silberschatz, Korth and Sudarshan
Materialized Views
Materializing a view: create a physical table containing all the tuples
in the result of the query defining the view
If relations used in the query are updated, the materialized view result
becomes out of date
Need to maintain the view, by updating the view whenever the
underlying relations are updated.
Database System Concepts - 6th Edition 4.22 ©Silberschatz, Korth and Sudarshan
Transactions
Unit of work
Atomic transaction
either fully executed or rolled back as if it never occurred
Isolation from concurrent transactions
Transactions begin implicitly
Ended by commit work or rollback work
But default on most databases: each SQL statement commits
automatically
Can turn off auto commit for a session (e.g. using API)
In SQL:1999, can use: begin atomic …. end
Not supported on most databases
Database System Concepts - 6th Edition 4.23 ©Silberschatz, Korth and Sudarshan
Integrity Constraints
Integrity constraints guard against accidental damage to the
database, by ensuring that authorized changes to the
database do not result in a loss of data consistency.
A checking account must have a balance greater than
$10,000.00
A salary of a bank employee must be at least $4.00 an
hour
A customer must have a (non-null) phone number
Database System Concepts - 6th Edition 4.24 ©Silberschatz, Korth and Sudarshan
Integrity Constraints on a Single Relation
not null
primary key
unique
check (P), where P is a predicate
Database System Concepts - 6th Edition 4.25 ©Silberschatz, Korth and Sudarshan
Not Null and Unique Constraints
not null
Declare name and budget to be not null
name varchar(20) not null
budget numeric(12,2) not null
unique ( A1, A2, …, Am)
The unique specification states that the attributes A1, A2, …
Am
form a candidate key.
Candidate keys are permitted to be null (in contrast to primary
keys).
Database System Concepts - 6th Edition 4.26 ©Silberschatz, Korth and Sudarshan
The check clause
check (P)
where P is a predicate
Database System Concepts - 6th Edition 4.28 ©Silberschatz, Korth and Sudarshan
Cascading Actions in Referential Integrity
Database System Concepts - 6th Edition 4.29 ©Silberschatz, Korth and Sudarshan
Integrity Constraint Violation During
Transactions
E.g.
Database System Concepts - 6th Edition 4.30 ©Silberschatz, Korth and Sudarshan
Complex Check Clauses
check (time_slot_id in (select time_slot_id from time_slot))
why not use a foreign key here?
Every section has at least one instructor teaching the section.
how to write this?
Unfortunately: subquery in check clause not supported by
pretty much any database
Alternative: triggers (later)
create assertion <assertion-name> check <predicate>;
Also not supported by anyone
Database System Concepts - 6th Edition 4.31 ©Silberschatz, Korth and Sudarshan
Built-in Data Types in SQL
date: Dates, containing a (4 digit) year, month and date
Example: date ‘2005-7-27’
time: Time of day, in hours, minutes and seconds.
Example: time ‘09:00:30’ time ‘09:00:30.75’
timestamp: date plus time of day
Example: timestamp ‘2005-7-27 09:00:30.75’
interval: period of time
Example: interval ‘1’ day
Subtracting a date/time/timestamp value from another gives
an interval value
Interval values can be added to date/time/timestamp values
Database System Concepts - 6th Edition 4.32 ©Silberschatz, Korth and Sudarshan
Index Creation
create table student
(ID varchar (5),
name varchar (20) not null,
dept_name varchar (20),
tot_cred numeric (3,0) default 0,
primary key (ID))
create index studentID_index on student(ID)
Indices are data structures used to speed up access to records with
specified values for index attributes
e.g. select *
from student
where ID = ‘12345’
can be executed by using the index to find the required record,
without looking at all records of student
More on indices in Chapter 11
Database System Concepts - 6th Edition 4.33 ©Silberschatz, Korth and Sudarshan
User-Defined Types
create type construct in SQL creates user-defined type
Database System Concepts - 6th Edition 4.34 ©Silberschatz, Korth and Sudarshan
Domains
create domain construct in SQL-92 creates user-defined
domain types
Database System Concepts - 6th Edition 4.35 ©Silberschatz, Korth and Sudarshan
Large-Object Types
Large objects (photos, videos, CAD files, etc.) are stored as a
large object:
blob: binary large object -- object is a large collection of
uninterpreted binary data (whose interpretation is left to an
application outside of the database system)
clob: character large object -- object is a large collection of
character data
When a query returns a large object, a pointer is returned
rather than the large object itself.
Database System Concepts - 6th Edition 4.36 ©Silberschatz, Korth and Sudarshan
Authorization
Database System Concepts - 6th Edition 4.37 ©Silberschatz, Korth and Sudarshan
Authorization Specification in SQL
The grant statement is used to confer authorization
Database System Concepts - 6th Edition 4.38 ©Silberschatz, Korth and Sudarshan
Privileges in SQL
select: allows read access to relation,or the ability to query
using the view
Example: grant users U1, U2, and U3 select
authorization on the instructor relation:
grant select on instructor to U1, U2, U3
insert: the ability to insert tuples
update: the ability to update using the SQL update
statement
delete: the ability to delete tuples.
all privileges: used as a short form for all the allowable
privileges
Database System Concepts - 6th Edition 4.39 ©Silberschatz, Korth and Sudarshan
Revoking Authorization in SQL
The revoke statement is used to revoke authorization.
Database System Concepts - 6th Edition 4.40 ©Silberschatz, Korth and Sudarshan
Roles
create role instructor;
grant instructor to Amit;
Privileges can be granted to roles:
grant select on takes to instructor;
Roles can be granted to users, as well as to other roles
create role teaching_assistant
grant teaching_assistant to instructor;
Instructor inherits all privileges of teaching_assistant
Chain of roles
create role dean;
grant instructor to dean;
grant dean to Satoshi;
Database System Concepts - 6th Edition 4.41 ©Silberschatz, Korth and Sudarshan
Authorization on Views
create view geo_instructor as
(select *
from instructor
where dept_name = ’Geology’);
grant select on geo_instructor to geo_staff
Suppose that a geo_staff member issues
select *
from geo_instructor;
What if
geo_staff does not have permissions on instructor?
creator of view did not have some permissions on
instructor?
Database System Concepts - 6th Edition 4.42 ©Silberschatz, Korth and Sudarshan
Other Authorization Features
references privilege to create foreign key
grant reference (dept_name) on department to Mariano;
why is this required?
transfer of privileges
grant select on department to Amit with grant option;
revoke select on department from Amit, Satoshi cascade;
revoke select on department from Amit, Satoshi restrict;
Etc. read Section 4.6 for more details we have omitted here.
Database System Concepts - 6th Edition 4.43 ©Silberschatz, Korth and Sudarshan
End of Chapter 4
Database System Concepts - 6th Edition 4.45 ©Silberschatz, Korth and Sudarshan
Figure 4.02
Database System Concepts - 6th Edition 4.46 ©Silberschatz, Korth and Sudarshan
Figure 4.03
Database System Concepts - 6th Edition 4.47 ©Silberschatz, Korth and Sudarshan
Figure 4.04
Database System Concepts - 6th Edition 4.48 ©Silberschatz, Korth and Sudarshan
Figure 4.05
Database System Concepts - 6th Edition 4.49 ©Silberschatz, Korth and Sudarshan
Figure 4.07
Taylor
Database System Concepts - 6th Edition 4.50 ©Silberschatz, Korth and Sudarshan
Figure 4.06
Database System Concepts - 6th Edition 4.51 ©Silberschatz, Korth and Sudarshan
Figure 4.03
Database System Concepts - 6th Edition 4.52 ©Silberschatz, Korth and Sudarshan
Chapter 5: Advanced SQL
Database System Concepts - 6th Edition 5.2 ©Silberschatz, Korth and Sudarshan
Accessing SQL From a Programming Language
Database System Concepts - 6th Edition 5.3 ©Silberschatz, Korth and Sudarshan
Accessing SQL From a Programming Language
Database System Concepts - 6th Edition 5.4 ©Silberschatz, Korth and Sudarshan
ODBC
Database System Concepts - 6th Edition 5.5 ©Silberschatz, Korth and Sudarshan
JDBC
JDBC is a Java API for communicating with database systems
supporting SQL.
JDBC supports a variety of features for querying and updating data,
and for retrieving query results.
JDBC also supports metadata retrieval, such as querying about
relations present in the database and the names and types of
relation attributes.
Model for communicating with the database:
Open a connection
Create a “statement” object
Execute queries using the Statement object to send queries and
fetch results
Exception mechanism to handle errors
Database System Concepts - 6th Edition 5.6 ©Silberschatz, Korth and Sudarshan
JDBC Code
public static void JDBCexample(String dbid, String userid, String passwd)
{
try (Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@db.yale.edu:2000:univdb", userid, passwd);
Statement stmt = conn.createStatement();
)
{
… Do Actual Work ….
}
catch (SQLException sqle) {
System.out.println("SQLException : " + sqle);
}
}
Database System Concepts - 6th Edition 5.7 ©Silberschatz, Korth and Sudarshan
JDBC Code for
Older Versions of Java/JDBC
public static void JDBCexample(String dbid, String userid, String passwd)
{
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@db.yale.edu:2000:univdb", userid, passwd);
Statement stmt = conn.createStatement();
… Do Actual Work ….
stmt.close();
conn.close();
}
catch (SQLException sqle) {
System.out.println("SQLException : " + sqle);
}
}
NOTE: Classs.forName is not required from JDBC 4 onwards. The try with
resources syntax in prev slide is preferred for Java 7 onwards.
Database System Concepts - 6th Edition 5.8 ©Silberschatz, Korth and Sudarshan
JDBC Code (Cont.)
Update to database
try {
stmt.executeUpdate(
"insert into instructor values(’77987’, ’Kim’, ’Physics’,
98000)");
} catch (SQLException sqle)
{
System.out.println("Could not insert tuple. " + sqle);
}
Execute query and fetch and print results
ResultSet rset = stmt.executeQuery(
"select dept_name, avg (salary)
from instructor
group by dept_name");
while (rset.next()) {
System.out.println(rset.getString("dept_name") + " " +
rset.getFloat(2));
}
Database System Concepts - 6th Edition 5.9 ©Silberschatz, Korth and Sudarshan
JDBC Code Details
Database System Concepts - 6th Edition 5.10 ©Silberschatz, Korth and Sudarshan
Prepared Statement
PreparedStatement pStmt = conn.prepareStatement(
"insert into instructor values(?,?,?,?)");
pStmt.setString(1, "88877");
pStmt.setString(2, "Perry");
pStmt.setString(3, "Finance");
pStmt.setInt(4, 125000);
pStmt.executeUpdate();
pStmt.setString(1, "88878");
pStmt.executeUpdate();
WARNING: always use prepared statements when taking an input
from the user and adding it to a query
NEVER create a query by concatenating strings
"insert into instructor values(’ " + ID + " ’, ’ " + name + " ’, " + " ’
+ dept name + " ’, " ’ balance + ")“
What if name is “D’Souza”?
Database System Concepts - 6th Edition 5.11 ©Silberschatz, Korth and Sudarshan
SQL Injection
Suppose query is constructed using
"select * from instructor where name = ’" + name + "’"
Suppose the user, instead of entering a name, enters:
X’ or ’Y’ = ’Y
then the resulting statement becomes:
"select * from instructor where name = ’" + "X’ or ’Y’ = ’Y" + "’"
which is:
select * from instructor where name = ’X’ or ’Y’ = ’Y’
Database System Concepts - 6th Edition 5.12 ©Silberschatz, Korth and Sudarshan
Metadata Features
ResultSet metadata
E.g.after executing query to get a ResultSet rs:
ResultSetMetaData rsmd = rs.getMetaData();
for(int i = 1; i <= rsmd.getColumnCount(); i++) {
System.out.println(rsmd.getColumnName(i));
System.out.println(rsmd.getColumnTypeName(i));
}
How is this useful?
Database System Concepts - 6th Edition 5.13 ©Silberschatz, Korth and Sudarshan
Metadata (Cont)
Database metadata
DatabaseMetaData dbmd = conn.getMetaData();
// Arguments to getColumns: Catalog, Schema-pattern, Table-pattern,
// and Column-Pattern
// Returns: One row for each column; row has a number of attributes
// such as COLUMN_NAME, TYPE_NAME
// The value null indicates all Catalogs/Schemas.
// The value “” indicates current catalog/schema
// The value “%” has the same meaning as SQL like clause
ResultSet rs = dbmd.getColumns(null, "univdb", "department", "%");
while( rs.next()) {
System.out.println(rs.getString("COLUMN_NAME"),
rs.getString("TYPE_NAME");
}
And where is this useful?
Database System Concepts - 6th Edition 5.14 ©Silberschatz, Korth and Sudarshan
Metadata (Cont)
Database metadata
DatabaseMetaData dbmd = conn.getMetaData();
// Arguments to getTables: Catalog, Schema-pattern, Table-pattern,
// and Table-Type
// Returns: One row for each table; row has a number of attributes
// such as TABLE_NAME, TABLE_CAT, TABLE_TYPE, ..
// The value null indicates all Catalogs/Schemas.
// The value “” indicates current catalog/schema
// The value “%” has the same meaning as SQL like clause
// The last attribute is an array of types of tables to return.
// TABLE means only regular tables
ResultSet rs = dbmd.getTables (“”, "", “%", new String[] {“TABLES”});
while( rs.next()) {
System.out.println(rs.getString(“TABLE_NAME“));
}
And where is this useful?
Database System Concepts - 6th Edition 5.15 ©Silberschatz, Korth and Sudarshan
Finding Primary Keys
DatabaseMetaData dmd = connection.getMetaData();
while(rs.next()){
// KEY_SEQ indicates the position of the attribute in
// the primary key, which is required if a primary key has multiple
// attributes
System.out.println(rs.getString(“KEY_SEQ”),
rs.getString("COLUMN_NAME");
}
Database System Concepts - 6th Edition 5.16 ©Silberschatz, Korth and Sudarshan
Transaction Control in JDBC
Database System Concepts - 6th Edition 5.17 ©Silberschatz, Korth and Sudarshan
Other JDBC Features
Calling functions and procedures
CallableStatement cStmt1 = conn.prepareCall("{? = call some
function(?)}");
CallableStatement cStmt2 = conn.prepareCall("{call some
procedure(?,?)}");
Handling large object types
getBlob() and getClob() that are similar to the getString() method,
but return objects of type Blob and Clob, respectively
get data from these objects by getBytes()
associate an open stream with Java Blob or Clob object to update
large objects
blob.setBlob(int parameterIndex, InputStream inputStream).
Database System Concepts - 6th Edition 5.18 ©Silberschatz, Korth and Sudarshan
JDBC Resources
JDBC Basics Tutorial
https://docs.oracle.com/javase/tutorial/jdbc/index.html
Database System Concepts - 6th Edition 5.19 ©Silberschatz, Korth and Sudarshan
SQLJ
JDBC is overly dynamic, errors cannot be caught by compiler
SQLJ: embedded SQL in Java
#sql iterator deptInfoIter ( String dept name, int avgSal);
deptInfoIter iter = null;
#sql iter = { select dept_name, avg(salary) from instructor
group by dept name };
while (iter.next()) {
String deptName = iter.dept_name();
int avgSal = iter.avgSal();
System.out.println(deptName + " " + avgSal);
}
iter.close();
Database System Concepts - 6th Edition 5.20 ©Silberschatz, Korth and Sudarshan
Embedded SQL
Database System Concepts - 6th Edition 5.21 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
Before executing any SQL statements, the program must first connect
to the database. This is done using:
EXEC-SQL connect to server user user-name using password;
Here, server identifies the server to which a connection is to be
established.
Variables of the host language can be used within embedded SQL
statements. They are preceded by a colon (:) to distinguish from
SQL variables (e.g., :credit_amount )
Variables used as above must be declared within DECLARE section,
as illustrated below. The syntax for declaring the variables, however,
follows the usual host language syntax.
EXEC-SQL BEGIN DECLARE SECTION}
int credit-amount ;
EXEC-SQL END DECLARE SECTION;
Database System Concepts - 6th Edition 5.22 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
Database System Concepts - 6th Edition 5.23 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
Example:
From within a host language, find the ID and name of
students who have completed more than the number of
credits stored in variable credit_amount in the host langue
Specify the query in SQL as follows:
EXEC SQL
declare c cursor for
select ID, name
from student
where tot_cred > :credit_amount
END_EXEC
The variable c (used in the cursor declaration) is used to
identify the query
Database System Concepts - 6th Edition 5.24 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
Database System Concepts - 6th Edition 5.25 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
Database System Concepts - 6th Edition 5.26 ©Silberschatz, Korth and Sudarshan
Updates Through Embedded SQL
Database System Concepts - 6th Edition 5.27 ©Silberschatz, Korth and Sudarshan
Extensions to SQL
Database System Concepts - 6th Edition 5.28 ©Silberschatz, Korth and Sudarshan
Functions and Procedures
Database System Concepts - 6th Edition 5.29 ©Silberschatz, Korth and Sudarshan
SQL Functions
Define a function that, given the name of a department, returns the
count of the number of instructors in that department.
create function dept_count (dept_name varchar(20))
returns integer
begin
declare d_count integer;
select count (* ) into d_count
from instructor
where instructor.dept_name = dept_name
return d_count;
end
The function dept_count can be used to find the department names
and budget of all departments with more that 12 instructors.
select dept_name, budget
from department
where dept_count (dept_name ) > 12
Database System Concepts - 6th Edition 5.30 ©Silberschatz, Korth and Sudarshan
SQL functions (Cont.)
Database System Concepts - 6th Edition 5.31 ©Silberschatz, Korth and Sudarshan
Table Functions
SQL:2003 added functions that return a relation as a result
Example: Return all instructors in a given department
create function instructor_of (dept_name char(20))
returns table (
ID varchar(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))
return table
(select ID, name, dept_name, salary
from instructor
where instructor.dept_name = instructor_of.dept_name)
Usage
select *
from table (instructor_of (‘Music’))
Database System Concepts - 6th Edition 5.32 ©Silberschatz, Korth and Sudarshan
SQL Procedures
The dept_count function could instead be written as procedure:
create procedure dept_count_proc (in dept_name varchar(20),
out d_count integer)
begin
select count(*) into d_count
from instructor
where instructor.dept_name = dept_count_proc.dept_name
end
Procedures can be invoked either from an SQL procedure or from
embedded SQL, using the call statement.
declare d_count integer;
call dept_count_proc( ‘Physics’, d_count);
Procedures and functions can be invoked also from dynamic SQL
SQL:1999 allows more than one function/procedure of the same name
(called name overloading), as long as the number of
arguments differ, or at least the types of the arguments differ
Database System Concepts - 6th Edition 5.33 ©Silberschatz, Korth and Sudarshan
Language Constructs for Procedures & Functions
SQL supports constructs that gives it almost all the power of a general-
purpose programming language.
Warning: most database systems implement their own variant of the
standard syntax below.
Compound statement: begin … end,
May contain multiple SQL statements between begin and end.
Local variables can be declared within a compound statements
While and repeat statements:
while boolean expression do
sequence of statements ;
end while
repeat
sequence of statements ;
until boolean expression
end repeat
Database System Concepts - 6th Edition 5.34 ©Silberschatz, Korth and Sudarshan
Language Constructs (Cont.)
For loop
Permits iteration over all results of a query
Example: Find the budget of all departments
Database System Concepts - 6th Edition 5.35 ©Silberschatz, Korth and Sudarshan
Language Constructs (Cont.)
Database System Concepts - 6th Edition 5.36 ©Silberschatz, Korth and Sudarshan
External Language Routines
Database System Concepts - 6th Edition 5.37 ©Silberschatz, Korth and Sudarshan
External Language Routines
Database System Concepts - 6th Edition 5.38 ©Silberschatz, Korth and Sudarshan
External Language Routines (Cont.)
Database System Concepts - 6th Edition 5.39 ©Silberschatz, Korth and Sudarshan
Security with External Language Routines
Database System Concepts - 6th Edition 5.40 ©Silberschatz, Korth and Sudarshan
Triggers
Database System Concepts - 6th Edition 5.41 ©Silberschatz, Korth and Sudarshan
Triggers
Database System Concepts - 6th Edition 5.42 ©Silberschatz, Korth and Sudarshan
Triggering Events and Actions in SQL
Triggering event can be insert, delete or update
Triggers on update can be restricted to specific attributes
For example, after update of takes on grade
Values of attributes before and after an update can be referenced
referencing old row as : for deletes and updates
referencing new row as : for inserts and updates
Triggers can be activated before an event, which can serve as
extra constraints. For example, convert blank grades to null.
Database System Concepts - 6th Edition 5.43 ©Silberschatz, Korth and Sudarshan
Trigger to Maintain credits_earned value
create trigger credits_earned after update of takes on (grade)
referencing new row as nrow
referencing old row as orow
for each row
when nrow.grade <> ’F’ and nrow.grade is not null
and (orow.grade = ’F’ or orow.grade is null)
begin atomic
update student
set tot_cred= tot_cred +
(select credits
from course
where course.course_id= nrow.course_id)
where student.id = nrow.id;
end;
Database System Concepts - 6th Edition 5.44 ©Silberschatz, Korth and Sudarshan
Statement Level Triggers
Database System Concepts - 6th Edition 5.45 ©Silberschatz, Korth and Sudarshan
When Not To Use Triggers
Triggers were used earlier for tasks such as
Maintaining summary data (e.g., total salary of each
department)
Replicating databases by recording changes to special
relations (called change or delta relations) and having a
separate process that applies the changes over to a replica
There are better ways of doing these now:
Databases today provide built in materialized view facilities
to maintain summary data
Databases provide built-in support for replication
Encapsulation facilities can be used instead of triggers in many
cases
Define methods to update fields
Carry out actions as part of the update methods instead of
through a trigger
Database System Concepts - 6th Edition 5.46 ©Silberschatz, Korth and Sudarshan
When Not To Use Triggers (Cont.)
Risk of unintended execution of triggers, for example, when
Loading data from a backup copy
Replicating updates at a remote site
Trigger execution can be disabled before such actions.
Other risks with triggers:
Error leading to failure of critical transactions that set off
the trigger
Cascading execution
Database System Concepts - 6th Edition 5.47 ©Silberschatz, Korth and Sudarshan
Recursive Queries
Database System Concepts - 6th Edition 5.48 ©Silberschatz, Korth and Sudarshan
Recursion in SQL
SQL:1999 permits recursive view definition
Example: find which courses are a prerequisite, whether
directly or indirectly, for a specific course
with recursive rec_prereq(course_id, prereq_id) as (
select course_id, prereq_id
from prereq
union
select rec_prereq.course_id, prereq.prereq_id,
from rec_rereq, prereq
where rec_prereq.prereq_id = prereq.course_id
)
select ∗
from rec_prereq;
This example view, rec_prereq, is called the transitive closure
of the prereq relation
Note: 1st printing of 6th ed erroneously used c_prereq in place of
rec_prereq in some places
Database System Concepts - 6th Edition 5.49 ©Silberschatz, Korth and Sudarshan
The Power of Recursion
Database System Concepts - 6th Edition 5.50 ©Silberschatz, Korth and Sudarshan
The Power of Recursion
Database System Concepts - 6th Edition 5.51 ©Silberschatz, Korth and Sudarshan
Example of Fixed-Point Computation
Database System Concepts - 6th Edition 5.52 ©Silberschatz, Korth and Sudarshan
Advanced Aggregation Features
Database System Concepts - 6th Edition 5.53 ©Silberschatz, Korth and Sudarshan
Ranking
Ranking is done in conjunction with an order by specification.
Suppose we are given a relation
student_grades(ID, GPA)
giving the grade-point average of each student
Find the rank of each student.
select ID, rank() over (order by GPA desc) as s_rank
from student_grades
An extra order by clause is needed to get them in sorted order
select ID, rank() over (order by GPA desc) as s_rank
from student_grades
order by s_rank
Ranking may leave gaps: e.g. if 2 students have the same top GPA,
both have rank 1, and the next rank is 3
dense_rank does not leave gaps, so next dense rank would be 2
Database System Concepts - 6th Edition 5.54 ©Silberschatz, Korth and Sudarshan
Ranking
Ranking can be done using basic SQL aggregation, but
resultant query is very inefficient
select ID, (1 + (select count(*)
from student_grades B
where B.GPA > A.GPA)) as s_rank
from student_grades A
order by s_rank;
Database System Concepts - 6th Edition 5.55 ©Silberschatz, Korth and Sudarshan
Ranking (Cont.)
Ranking can be done within partition of the data.
“Find the rank of students within each department.”
select ID, dept_name,
rank () over (partition by dept_name order by GPA desc)
as dept_rank
from dept_grades
order by dept_name, dept_rank;
Multiple rank clauses can occur in a single select clause.
Ranking is done after applying group by clause/aggregation
Can be used to find top-n results
More general than the limit n clause supported by many
databases, since it allows top-n within each partition
Database System Concepts - 6th Edition 5.56 ©Silberschatz, Korth and Sudarshan
Ranking (Cont.)
Other ranking functions:
percent_rank (within partition, if partitioning is done)
cume_dist (cumulative distribution)
fraction of tuples with preceding values
row_number (non-deterministic in presence of duplicates)
SQL:1999 permits the user to specify nulls first or nulls last
select ID,
rank ( ) over (order by GPA desc nulls last) as s_rank
from student_grades
Database System Concepts - 6th Edition 5.57 ©Silberschatz, Korth and Sudarshan
Ranking (Cont.)
For a given constant n, the ranking the function ntile(n) takes
the tuples in each partition in the specified order, and divides
them into n buckets with equal numbers of tuples.
E.g.,
select ID, ntile(4) over (order by GPA desc) as quartile
from student_grades;
Database System Concepts - 6th Edition 5.58 ©Silberschatz, Korth and Sudarshan
Windowing
Used to smooth out random variations.
E.g., moving average: “Given sales values for each date, calculate
for each date the average of the sales on that day, the previous day,
and the next day”
Window specification in SQL:
Given relation sales(date, value)
select date, sum(value) over
(order by date between rows 1 preceding and 1 following)
from sales
Database System Concepts - 6th Edition 5.59 ©Silberschatz, Korth and Sudarshan
Windowing
Examples of other window specifications:
between rows unbounded preceding and current
rows unbounded preceding
range between 10 preceding and current row
All rows with values between current row value –10 to
current value
range interval 10 day preceding
Not including current row
Database System Concepts - 6th Edition 5.60 ©Silberschatz, Korth and Sudarshan
Windowing (Cont.)
Can do windowing within partitions
E.g., Given a relation transaction (account_number, date_time,
value), where value is positive for a deposit and negative for a
withdrawal
“Find total balance of each account after each transaction
on the account”
select account_number, date_time,
sum (value) over
(partition by account_number
order by date_time
rows unbounded preceding)
as balance
from transaction
order by account_number, date_time
Database System Concepts - 6th Edition 5.61 ©Silberschatz, Korth and Sudarshan
OLAP
Database System Concepts - 6th Edition 5.62 ©Silberschatz, Korth and Sudarshan
Data Analysis and OLAP
Online Analytical Processing (OLAP)
Interactive analysis of data, allowing data to be summarized and
viewed in different ways in an online fashion (with negligible
delay)
Data that can be modeled as dimension attributes and measure
attributes are called multidimensional data.
Measure attributes
measure some value
can be aggregated upon
e.g., the attribute number of the sales relation
Dimension attributes
define the dimensions on which measure attributes (or
aggregates thereof) are viewed
e.g., attributes item_name, color, and size of the sales relation
Database System Concepts - 6th Edition 5.63 ©Silberschatz, Korth and Sudarshan
Example sales relation
Database System Concepts - 6th Edition 5.66 ©Silberschatz, Korth and Sudarshan
Cross Tabulation With Hierarchy
Database System Concepts - 6th Edition 5.68 ©Silberschatz, Korth and Sudarshan
Relational Representation of Cross-tabs
Database System Concepts - 6th Edition 5.69 ©Silberschatz, Korth and Sudarshan
Extended Aggregation to Support OLAP
The cube operation computes union of group by’s on every subset of the
specified attributes
Example relation for this section
sales(item_name, color, clothes_size, quantity)
E.g. consider the query
select item_name, color, size, sum(number)
from sales
group by cube(item_name, color, size)
This computes the union of eight different groupings of the sales relation:
{ (item_name, color, size), (item_name, color),
(item_name, size), (color, size),
(item_name), (color),
(size), ()}
where ( ) denotes an empty group by list.
For each grouping, the result contains the null value
for attributes not present in the grouping.
Database System Concepts - 6th Edition 5.70 ©Silberschatz, Korth and Sudarshan
Online Analytical Processing Operations
Relational representation of cross-tab that we saw earlier, but with
null in place of all, can be computed by
select item_name, color, sum(number)
from sales
group by cube(item_name, color)
The function grouping() can be applied on an attribute
Returns 1 if the value is a null value representing all, and returns
0 in all other cases.
select item_name, color, size, sum(number),
grouping(item_name) as item_name_flag,
grouping(color) as color_flag,
grouping(size) as size_flag,
from sales
group by cube(item_name, color, size)
Database System Concepts - 6th Edition 5.71 ©Silberschatz, Korth and Sudarshan
Online Analytical Processing Operations
Can use the function decode() in the select clause to replace
such nulls by a value such as all
E.g., replace item_name in first query by
decode( grouping(item_name), 1, ‘all’, item_name)
Database System Concepts - 6th Edition 5.72 ©Silberschatz, Korth and Sudarshan
Extended Aggregation (Cont.)
The rollup construct generates union on every prefix of specified list
of attributes
E.g.,
select item_name, color, size, sum(number)
from sales
group by rollup(item_name, color, size)
Generates union of four groupings:
{ (item_name, color, size), (item_name, color), (item_name), ( ) }
Rollup can be used to generate aggregates at multiple levels of a
hierarchy.
E.g., suppose table itemcategory(item_name, category) gives the
category of each item. Then
select category, item_name, sum(number)
from sales, itemcategory
where sales.item_name = itemcategory.item_name
group by rollup(category, item_name)
would give a hierarchical summary by item_name and by category.
Database System Concepts - 6th Edition 5.73 ©Silberschatz, Korth and Sudarshan
Extended Aggregation (Cont.)
Database System Concepts - 6th Edition 5.74 ©Silberschatz, Korth and Sudarshan
Online Analytical Processing Operations
Database System Concepts - 6th Edition 5.75 ©Silberschatz, Korth and Sudarshan
OLAP Implementation
Database System Concepts - 6th Edition 5.76 ©Silberschatz, Korth and Sudarshan
OLAP Implementation (Cont.)
Early OLAP systems precomputed all possible aggregates in order to
provide online response
Space and time requirements for doing so can be very high
2
n combinations of group by
Database System Concepts - 6th Edition 8.2 ©Silberschatz, Korth and Sudarshan
Combine Schemas?
Suppose we combine instructor and department into inst_dept
(No connection to relationship set inst_dept)
Result is possible repetition of information
Database System Concepts - 6th Edition 8.3 ©Silberschatz, Korth and Sudarshan
A Combined Schema Without Repetition
Consider combining relations
sec_class(sec_id, building, room_number) and
section(course_id, sec_id, semester, year)
into one relation
section(course_id, sec_id, semester, year,
building, room_number)
No repetition in this case
Database System Concepts - 6th Edition 8.4 ©Silberschatz, Korth and Sudarshan
What About Smaller Schemas?
Suppose we had started with inst_dept. How would we know to split up
(decompose) it into instructor and department?
Write a rule “if there were a schema (dept_name, building, budget), then
dept_name would be a candidate key”
Denote as a functional dependency:
dept_name building, budget
In inst_dept, because dept_name is not a candidate key, the building
and budget of a department may have to be repeated.
This indicates the need to decompose inst_dept
Not all decompositions are good. Suppose we decompose
employee(ID, name, street, city, salary) into
employee1 (ID, name)
employee2 (name, street, city, salary)
The next slide shows how we lose information -- we cannot reconstruct
the original employee relation -- and so, this is a lossy decomposition.
Database System Concepts - 6th Edition 8.5 ©Silberschatz, Korth and Sudarshan
A Lossy Decomposition
Database System Concepts - 6th Edition 8.6 ©Silberschatz, Korth and Sudarshan
Example of Lossless-Join Decomposition
A B C A B B C
1 A 1 1 A
2 B 2 2 B
r A,B(r) B,C(r)
A B C
A (r) B (r)
1 A
2 B
Database System Concepts - 6th Edition 8.7 ©Silberschatz, Korth and Sudarshan
First Normal Form
Domain is atomic if its elements are considered to be indivisible units
Examples of non-atomic domains:
Set of names, composite attributes
Identification numbers like CS101 that can be broken up into
parts
A relational schema R is in first normal form if the domains of all
attributes of R are atomic
Non-atomic values complicate storage and encourage redundant
(repeated) storage of data
Example: Set of accounts stored with each customer, and set of
owners stored with each account
We assume all relations are in first normal form (and revisit this in
Chapter 22: Object Based Databases)
Database System Concepts - 6th Edition 8.8 ©Silberschatz, Korth and Sudarshan
First Normal Form (Cont’d)
Atomicity is actually a property of how the elements of the domain are
used.
Example: Strings would normally be considered indivisible
Suppose that students are given roll numbers which are strings of
the form CS0012 or EE1127
If the first two characters are extracted to find the department, the
domain of roll numbers is not atomic.
Doing so is a bad idea: leads to encoding of information in
application program rather than in the database.
Database System Concepts - 6th Edition 8.9 ©Silberschatz, Korth and Sudarshan
Goal — Devise a Theory for the Following
Database System Concepts - 6th Edition 8.10 ©Silberschatz, Korth and Sudarshan
Functional Dependencies
Constraints on the set of legal relations.
Require that the value for a certain set of attributes determines
uniquely the value for another set of attributes.
A functional dependency is a generalization of the notion of a key.
Database System Concepts - 6th Edition 8.11 ©Silberschatz, Korth and Sudarshan
Functional Dependencies (Cont.)
Let R be a relation schema
R and R
The functional dependency
holds on R if and only if for any legal relations r(R), whenever any
two tuples t1 and t2 of r agree on the attributes , they also agree
on the attributes . That is,
t1[] = t2 [] t1[ ] = t2 [ ]
Example: Consider r(A,B ) with the following instance of r.
1 4
1 5
3 7
On this instance, A B does NOT hold, but B A does hold.
Database System Concepts - 6th Edition 8.12 ©Silberschatz, Korth and Sudarshan
Functional Dependencies (Cont.)
K is a superkey for relation schema R if and only if K R
K is a candidate key for R if and only if
K R, and
for no K, R
Functional dependencies allow us to express constraints that cannot be
expressed using superkeys. Consider the schema:
inst_dept (ID, name, salary, dept_name, building, budget ).
We expect these functional dependencies to hold:
dept_name building
and ID building
but would not expect the following to hold:
dept_name salary
Database System Concepts - 6th Edition 8.13 ©Silberschatz, Korth and Sudarshan
Use of Functional Dependencies
Database System Concepts - 6th Edition 8.14 ©Silberschatz, Korth and Sudarshan
Functional Dependencies (Cont.)
A functional dependency is trivial if it is satisfied by all instances of a
relation
Example:
ID, name ID
name name
In general, is trivial if
Database System Concepts - 6th Edition 8.15 ©Silberschatz, Korth and Sudarshan
Closure of a Set of Functional
Dependencies
Database System Concepts - 6th Edition 8.16 ©Silberschatz, Korth and Sudarshan
Boyce-Codd Normal Form
is trivial (i.e., )
is a superkey for R
Database System Concepts - 6th Edition 8.17 ©Silberschatz, Korth and Sudarshan
Decomposing a Schema into BCNF
Database System Concepts - 6th Edition 8.18 ©Silberschatz, Korth and Sudarshan
BCNF and Dependency Preservation
Constraints, including functional dependencies, are costly to check in
practice unless they pertain to only one relation
If it is sufficient to test only those dependencies on each individual
relation of a decomposition in order to ensure that all functional
dependencies hold, then that decomposition is dependency
preserving.
Because it is not always possible to achieve both BCNF and
dependency preservation, we consider a weaker normal form, known
as third normal form.
Database System Concepts - 6th Edition 8.19 ©Silberschatz, Korth and Sudarshan
Third Normal Form
A relation schema R is in third normal form (3NF) if for all:
in F+
at least one of the following holds:
is trivial (i.e., )
is a superkey for R
Each attribute A in – is contained in a candidate key for R.
(NOTE: each attribute may be in a different candidate key)
If a relation is in BCNF it is in 3NF (since in BCNF one of the first two
conditions above must hold).
Third condition is a minimal relaxation of BCNF to ensure dependency
preservation (will see why later).
Database System Concepts - 6th Edition 8.20 ©Silberschatz, Korth and Sudarshan
Goals of Normalization
Database System Concepts - 6th Edition 8.21 ©Silberschatz, Korth and Sudarshan
How good is BCNF?
There are database schemas in BCNF that do not seem to be
sufficiently normalized
Consider a relation
inst_info (ID, child_name, phone)
where an instructor may have more than one phone and can have
multiple children
ID child_name phone
99999 512-555-1234
David
512-555-4321
99999 David
512-555-1234
99999 William
512-555-4321
99999 Willian
inst_info
Database System Concepts - 6th Edition 8.22 ©Silberschatz, Korth and Sudarshan
How good is BCNF? (Cont.)
Database System Concepts - 6th Edition 8.23 ©Silberschatz, Korth and Sudarshan
How good is BCNF? (Cont.)
ID child_name
ID phone
99999 512-555-1234
inst_phone 512-555-4321
99999 512-555-1234
99999 512-555-4321
99999
This suggests the need for higher normal forms, such as Fourth
Normal Form (4NF), which we shall see later.
Database System Concepts - 6th Edition 8.24 ©Silberschatz, Korth and Sudarshan
Functional-Dependency Theory
We now consider the formal theory that tells us which functional
dependencies are implied logically by a given set of functional
dependencies.
We then develop algorithms to generate lossless decompositions into
BCNF and 3NF
We then develop algorithms to test if a decomposition is dependency-
preserving
Database System Concepts - 6th Edition 8.25 ©Silberschatz, Korth and Sudarshan
Closure of a Set of Functional
Dependencies
Database System Concepts - 6th Edition 8.26 ©Silberschatz, Korth and Sudarshan
Closure of a Set of Functional
Dependencies
Database System Concepts - 6th Edition 8.27 ©Silberschatz, Korth and Sudarshan
Example
R = (A, B, C, G, H, I)
F={ AB
AC
CG H
CG I
B H}
some members of F+
AH
by transitivity from A B and B H
AG I
by augmenting A C with G, to get AG CG
and then transitivity with CG I
CG HI
by augmenting CG I to infer CG CGI,
and augmenting of CG H to infer CGI HI,
and then transitivity
Database System Concepts - 6th Edition 8.28 ©Silberschatz, Korth and Sudarshan
Procedure for Computing F+
To compute the closure of a set of functional dependencies F:
F+=F
repeat
for each functional dependency f in F+
apply reflexivity and augmentation rules on f
add the resulting functional dependencies to F +
for each pair of functional dependencies f1and f2 in F +
if f1 and f2 can be combined using transitivity
then add the resulting functional dependency to F +
until F + does not change any further
Database System Concepts - 6th Edition 8.29 ©Silberschatz, Korth and Sudarshan
Closure of Functional Dependencies
(Cont.)
Additional rules:
If holds and holds, then holds (union)
If holds, then holds and holds
(decomposition)
If holds and holds, then holds
(pseudotransitivity)
The above rules can be inferred from Armstrong’s axioms.
Database System Concepts - 6th Edition 8.30 ©Silberschatz, Korth and Sudarshan
Closure of Attribute Sets
Given a set of attributes , define the closure of under F (denoted
by +) as the set of attributes that are functionally determined by
under F
result := ;
while (changes to result) do
for each in F do
begin
if result then result := result
end
Database System Concepts - 6th Edition 8.31 ©Silberschatz, Korth and Sudarshan
Example of Attribute Set Closure
R = (A, B, C, G, H, I)
F = {A B
AC
CG H
CG I
B H}
(AG)+
1. result = AG
2. result = ABCG (A C and A B)
3. result = ABCGH (CG H and CG AGBC)
4. result = ABCGHI (CG I and CG AGBCH)
Is AG a candidate key?
1. Is AG a super key?
Does AG R? == Is (AG)+ R
1.
2. Is any subset of AG a superkey?
1. Does A R? == Is (A)+ R
2. Does G R? == Is (G)+ R
Database System Concepts - 6th Edition 8.32 ©Silberschatz, Korth and Sudarshan
Uses of Attribute Closure
There are several uses of the attribute closure algorithm:
Testing for superkey:
To test if is a superkey, we compute +, and check if + contains
all attributes of R.
Testing functional dependencies
To check if a functional dependency holds (or, in other
words, is in F+), just check if +.
That is, we compute + by using attribute closure, and then check
if it contains .
Is a simple and cheap test, and very useful
Computing closure of F
For each R, we find the closure +, and for each S +, we
output a functional dependency S.
Database System Concepts - 6th Edition 8.33 ©Silberschatz, Korth and Sudarshan
Canonical Cover
Sets of functional dependencies may have redundant dependencies
that can be inferred from the others
For example: A C is redundant in: {A B, B C, A C}
Parts of a functional dependency may be redundant
E.g.: on RHS: {A B, B C, A CD} can be simplified
to
{A B, B C, A D}
E.g.: on LHS: {A B, B C, AC D} can be simplified
to
{A B, B C, A D}
Intuitively, a canonical cover of F is a “minimal” set of functional
dependencies equivalent to F, having no redundant dependencies or
redundant parts of dependencies
Database System Concepts - 6th Edition 8.34 ©Silberschatz, Korth and Sudarshan
Extraneous Attributes
Database System Concepts - 6th Edition 8.35 ©Silberschatz, Korth and Sudarshan
Testing if an Attribute is Extraneous
Database System Concepts - 6th Edition 8.36 ©Silberschatz, Korth and Sudarshan
Canonical Cover
Database System Concepts - 6th Edition 8.37 ©Silberschatz, Korth and Sudarshan
Computing a Canonical Cover
R = (A, B, C)
F = {A BC
BC
AB
AB C}
Combine A BC and A B into A BC
Set is now {A BC, B C, AB C}
A is extraneous in AB C
Check if the result of deleting A from AB C is implied by the other
dependencies
Yes: in fact, B C is already present!
Set is now {A BC, B C}
C is extraneous in A BC
Check if A C is logically implied by A B and the other dependencies
Yes: using transitivity on A B and B C.
– Can use attribute closure of A in more complex cases
The canonical cover is: AB
BC
Database System Concepts - 6th Edition 8.38 ©Silberschatz, Korth and Sudarshan
Lossless-join Decomposition
For the case of R = (R1, R2), we require that for all possible relations r
on schema R
r = R1 (r ) R2 (r )
A decomposition of R into R1 and R2 is lossless join if at least one of
the following dependencies is in F+:
R1 R2 R1
R1 R2 R2
The above functional dependencies are a sufficient condition for
lossless join decomposition; the dependencies are a necessary
condition only if all constraints are functional dependencies
Database System Concepts - 6th Edition 8.39 ©Silberschatz, Korth and Sudarshan
Example
R = (A, B, C)
F = {A B, B C)
Can be decomposed in two different ways
R1 = (A, B), R2 = (B, C)
Lossless-join decomposition:
R1 R2 = {B} and B BC
Dependency preserving
R1 = (A, B), R2 = (A, C)
Lossless-join decomposition:
R1 R2 = {A} and A AB
Not dependency preserving
(cannot check B C without computing R1 R2)
Database System Concepts - 6th Edition 8.40 ©Silberschatz, Korth and Sudarshan
Dependency Preservation
Database System Concepts - 6th Edition 8.41 ©Silberschatz, Korth and Sudarshan
Testing for Dependency Preservation
Database System Concepts - 6th Edition 8.42 ©Silberschatz, Korth and Sudarshan
Example
R = (A, B, C )
F = {A B
B C}
Key = {A}
R is not in BCNF
Decomposition R1 = (A, B), R2 = (B, C)
R1 and R2 in BCNF
Lossless-join decomposition
Dependency preserving
Database System Concepts - 6th Edition 8.43 ©Silberschatz, Korth and Sudarshan
Testing for BCNF
To check if a non-trivial dependency causes a violation of BCNF
1. compute + (the attribute closure of ), and
2. verify that it includes all attributes of R, that is, it is a superkey of R.
Simplified test: To check if a relation schema R is in BCNF, it suffices
to check only the dependencies in the given set F for violation of BCNF,
rather than checking all dependencies in F+.
If none of the dependencies in F causes a violation of BCNF, then
none of the dependencies in F+ will cause a violation of BCNF
either.
However, simplified test using only F is incorrect when testing a
relation in a decomposition of R
Consider R = (A, B, C, D, E), with F = { A B, BC D}
Decompose R into R1 = (A,B) and R2 = (A,C,D, E)
Database System Concepts - 6th Edition 8.44 ©Silberschatz, Korth and Sudarshan
Testing Decomposition for BCNF
Database System Concepts - 6th Edition 8.45 ©Silberschatz, Korth and Sudarshan
BCNF Decomposition Algorithm
result := {R };
done := false;
compute F +;
while (not done) do
if (there is a schema Ri in result that is not in BCNF)
then begin
let be a nontrivial functional dependency that
holds on Ri such that Ri is not in F +,
and = ;
result := (result – Ri ) (Ri – ) (, );
end
else done := true;
Database System Concepts - 6th Edition 8.46 ©Silberschatz, Korth and Sudarshan
Example of BCNF Decomposition
R = (A, B, C )
F = {A B
B C}
Key = {A}
R is not in BCNF (B C but B is not superkey)
Decomposition
R1 = (B, C)
R2 = (A,B)
Database System Concepts - 6th Edition 8.47 ©Silberschatz, Korth and Sudarshan
Example of BCNF Decomposition
class (course_id, title, dept_name, credits, sec_id, semester, year,
building, room_number, capacity, time_slot_id)
Functional dependencies:
course_id→ title, dept_name, credits
building, room_number→capacity
course_id, sec_id, semester, year→building, room_number,
time_slot_id
A candidate key {course_id, sec_id, semester, year}.
BCNF Decomposition:
course_id→ title, dept_name, credits holds
but course_id is not a superkey.
We replace class by:
course(course_id, title, dept_name, credits)
class-1 (course_id, sec_id, semester, year, building,
room_number, capacity, time_slot_id)
Database System Concepts - 6th Edition 8.48 ©Silberschatz, Korth and Sudarshan
BCNF Decomposition (Cont.)
course is in BCNF
How do we know this?
building, room_number→capacity holds on class-1
but {building, room_number} is not a superkey for class-1.
We replace class-1 by:
classroom (building, room_number, capacity)
section (course_id, sec_id, semester, year, building,
room_number, time_slot_id)
classroom and section are in BCNF.
Database System Concepts - 6th Edition 8.49 ©Silberschatz, Korth and Sudarshan
BCNF and Dependency Preservation
R = (J, K, L )
F = {JK L
LK}
Two candidate keys = JK and JL
R is not in BCNF
Any decomposition of R will fail to preserve
JK L
This implies that testing for JK L requires a join
Database System Concepts - 6th Edition 8.50 ©Silberschatz, Korth and Sudarshan
Third Normal Form: Motivation
There are some situations where
BCNF is not dependency preserving, and
efficient checking for FD violation on updates is important
Solution: define a weaker normal form, called Third
Normal Form (3NF)
Allows some redundancy (with resultant problems; we will
see examples later)
But functional dependencies can be checked on individual
relations without computing a join.
There is always a lossless-join, dependency-preserving
decomposition into 3NF.
Database System Concepts - 6th Edition 8.51 ©Silberschatz, Korth and Sudarshan
3NF Example
Relation dept_advisor:
dept_advisor (s_ID, i_ID, dept_name)
F = {s_ID, dept_name i_ID, i_ID dept_name}
Two candidate keys: s_ID, dept_name, and i_ID, s_ID
R is in 3NF
s_ID, dept_name i_ID s_ID
– dept_name is a superkey
i_ID dept_name
– dept_name is contained in a candidate key
Database System Concepts - 6th Edition 8.52 ©Silberschatz, Korth and Sudarshan
Redundancy in 3NF
There is some redundancy in this schema
Example of problems due to redundancy in 3NF
R = (J, K, L)
F = {JK L, L K } J L K
j1 l1 k1
j2 l1 k1
j3 l1 k1
null l2 k2
Database System Concepts - 6th Edition 8.53 ©Silberschatz, Korth and Sudarshan
Testing for 3NF
Optimization: Need to check only FDs in F, need not check all FDs in
F+.
Use attribute closure to check for each dependency , if is a
superkey.
If is not a superkey, we have to verify if each attribute in is
contained in a candidate key of R
this test is rather more expensive, since it involve finding
candidate keys
testing for 3NF has been shown to be NP-hard
Interestingly, decomposition into third normal form (described
shortly) can be done in polynomial time
Database System Concepts - 6th Edition 8.54 ©Silberschatz, Korth and Sudarshan
3NF Decomposition Algorithm
Database System Concepts - 6th Edition 8.55 ©Silberschatz, Korth and Sudarshan
3NF Decomposition Algorithm (Cont.)
Above algorithm ensures:
each relation schema Ri is in 3NF
decomposition is dependency preserving and lossless-join
Proof of correctness is at end of this presentation (click here)
Database System Concepts - 6th Edition 8.56 ©Silberschatz, Korth and Sudarshan
3NF Decomposition: An Example
Relation schema:
cust_banker_branch = (customer_id, employee_id, branch_name, type )
The functional dependencies for this relation schema are:
1. customer_id, employee_id branch_name, type
2. employee_id branch_name
3. customer_id, branch_name employee_id
We first compute a canonical cover
branch_name is extraneous in the r.h.s. of the 1st dependency
No other attribute is extraneous, so we get F C =
customer_id, employee_id type
employee_id branch_name
customer_id, branch_name employee_id
Database System Concepts - 6th Edition 8.57 ©Silberschatz, Korth and Sudarshan
3NF Decompsition Example (Cont.)
The for loop generates following 3NF schema:
(customer_id, employee_id, type )
(employee_id, branch_name)
(customer_id, branch_name, employee_id)
Observe that (customer_id, employee_id, type ) contains a
candidate key of the original schema, so no further relation schema
needs be added
At end of for loop, detect and delete schemas, such as (employee_id,
branch_name), which are subsets of other schemas
result will not depend on the order in which FDs are considered
The resultant simplified 3NF schema is:
(customer_id, employee_id, type)
(customer_id, branch_name, employee_id)
Database System Concepts - 6th Edition 8.58 ©Silberschatz, Korth and Sudarshan
Comparison of BCNF and 3NF
It is always possible to decompose a relation into a set of relations
that are in 3NF such that:
the decomposition is lossless
the dependencies are preserved
It is always possible to decompose a relation into a set of relations
that are in BCNF such that:
the decomposition is lossless
it may not be possible to preserve dependencies.
Database System Concepts - 6th Edition 8.59 ©Silberschatz, Korth and Sudarshan
Design Goals
Goal for a relational database design is:
BCNF.
Lossless join.
Dependency preservation.
If we cannot achieve this, we accept one of
Lack of dependency preservation
Redundancy due to use of 3NF
Interestingly, SQL does not provide a direct way of specifying functional
dependencies other than superkeys.
Can specify FDs using assertions, but they are expensive to test, (and
currently not supported by any of the widely used databases!)
Even if we had a dependency preserving decomposition, using SQL we
would not be able to efficiently test a functional dependency whose left
hand side is not a key.
Database System Concepts - 6th Edition 8.60 ©Silberschatz, Korth and Sudarshan
Multivalued Dependencies
Suppose we record names of children, and phone numbers for
instructors:
inst_child(ID, child_name)
inst_phone(ID, phone_number)
If we were to combine these schemas to get
inst_info(ID, child_name, phone_number)
Example data:
(99999, David, 512-555-1234)
(99999, David, 512-555-4321)
(99999, William, 512-555-1234)
(99999, William, 512-555-4321)
This relation is in BCNF
Why?
Database System Concepts - 6th Edition 8.61 ©Silberschatz, Korth and Sudarshan
Multivalued Dependencies (MVDs)
Database System Concepts - 6th Edition 8.62 ©Silberschatz, Korth and Sudarshan
MVD (Cont.)
Tabular representation of
Database System Concepts - 6th Edition 8.63 ©Silberschatz, Korth and Sudarshan
Example
Let R be a relation schema with a set of attributes that are partitioned
into 3 nonempty subsets.
Y, Z, W
We say that Y Z (Y multidetermines Z )
if and only if for all possible relations r (R )
< y1, z1, w1 > r and < y1, z2, w2 > r
then
< y1, z1, w2 > r and < y1, z2, w1 > r
Note that since the behavior of Z and W are identical it follows that
Y Z if Y W
Database System Concepts - 6th Edition 8.64 ©Silberschatz, Korth and Sudarshan
Example (Cont.)
In our example:
ID child_name
ID phone_number
The above formal definition is supposed to formalize the notion that given
a particular value of Y (ID) it has associated with it a set of values of Z
(child_name) and a set of values of W (phone_number), and these two
sets are in some sense independent of each other.
Note:
If Y Z then Y Z
Indeed we have (in above notation) Z1 = Z2
The claim follows.
Database System Concepts - 6th Edition 8.65 ©Silberschatz, Korth and Sudarshan
Use of Multivalued Dependencies
We use multivalued dependencies in two ways:
1. To test relations to determine whether they are legal under a
given set of functional and multivalued dependencies
2. To specify constraints on the set of legal relations. We shall
thus concern ourselves only with relations that satisfy a given
set of functional and multivalued dependencies.
If a relation r fails to satisfy a given multivalued dependency, we can
construct a relations r that does satisfy the multivalued
dependency by adding tuples to r.
Database System Concepts - 6th Edition 8.66 ©Silberschatz, Korth and Sudarshan
Theory of MVDs
From the definition of multivalued dependency, we can derive the
following rule:
If , then
That is, every functional dependency is also a multivalued dependency
The closure D+ of D is the set of all functional and multivalued
dependencies logically implied by D.
We can compute D+ from D, using the formal definitions of
functional dependencies and multivalued dependencies.
We can manage with such reasoning for very simple multivalued
dependencies, which seem to be most common in practice
For complex dependencies, it is better to reason about sets of
dependencies using a system of inference rules (see Appendix C).
Database System Concepts - 6th Edition 8.67 ©Silberschatz, Korth and Sudarshan
Fourth Normal Form
Database System Concepts - 6th Edition 8.68 ©Silberschatz, Korth and Sudarshan
Restriction of Multivalued Dependencies
The restriction of D to Ri is the set Di consisting of
All functional dependencies in D + that include only attributes of Ri
All multivalued dependencies of the form
( Ri)
where Ri and is in D+
Database System Concepts - 6th Edition 8.69 ©Silberschatz, Korth and Sudarshan
4NF Decomposition Algorithm
result: = {R};
done := false;
compute D+;
Let Di denote the restriction of D+ to Ri
while (not done)
if (there is a schema Ri in result that is not in 4NF) then
begin
let be a nontrivial multivalued dependency that holds
on Ri such that Ri is not in Di, and ;
result := (result - Ri) (Ri - ) (, );
end
else done:= true;
Note: each Ri is in 4NF, and decomposition is lossless-join
Database System Concepts - 6th Edition 8.70 ©Silberschatz, Korth and Sudarshan
Example
R =(A, B, C, G, H, I)
F ={ A B
B HI
CG H }
R is not in 4NF since A B and A is not a superkey for R
Decomposition
a) R1 = (A, B) (R1 is in 4NF)
b) R2 = (A, C, G, H, I) (R2 is not in 4NF, decompose into R3 and R4)
c) R3 = (C, G, H) (R3 is in 4NF)
d) R4 = (A, C, G, I) (R4 is not in 4NF, decompose into R5 and R6)
A B and B HI A HI, (MVD transitivity), and
and hence A I (MVD restriction to R4)
e) R5 = (A, I) (R5 is in 4NF)
f)R6 = (A, C, G) (R6 is in 4NF)
Database System Concepts - 6th Edition 8.71 ©Silberschatz, Korth and Sudarshan
Further Normal Forms
Join dependencies generalize multivalued dependencies
lead to project-join normal form (PJNF) (also called fifth normal
form)
A class of even more general constraints, leads to a normal form
called domain-key normal form.
Problem with these generalized constraints: are hard to reason with,
and no set of sound and complete set of inference rules exists.
Hence rarely used
Database System Concepts - 6th Edition 8.72 ©Silberschatz, Korth and Sudarshan
Overall Database Design Process
Database System Concepts - 6th Edition 8.73 ©Silberschatz, Korth and Sudarshan
ER Model and Normalization
Database System Concepts - 6th Edition 8.74 ©Silberschatz, Korth and Sudarshan
Denormalization for Performance
Database System Concepts - 6th Edition 8.75 ©Silberschatz, Korth and Sudarshan
Other Design Issues
Some aspects of database design are not caught by normalization
Examples of bad database design, to be avoided:
Instead of earnings (company_id, year, amount ), use
earnings_2004, earnings_2005, earnings_2006, etc., all on the
schema (company_id, earnings).
Above are in BCNF, but make querying across years difficult and
needs new table each year
company_year (company_id, earnings_2004, earnings_2005,
earnings_2006)
Also in BCNF, but also makes querying across years difficult and
requires new attribute each year.
Is an example of a crosstab, where values for one attribute
become column names
Used in spreadsheets, and in data analysis tools
Database System Concepts - 6th Edition 8.76 ©Silberschatz, Korth and Sudarshan
Modeling Temporal Data
Temporal data have an association time interval during which the
data are valid.
A snapshot is the value of the data at a particular point in time
Several proposals to extend ER model by adding valid time to
attributes, e.g., address of an instructor at different points in time
entities, e.g., time duration when a student entity exists
relationships, e.g., time during which
t an instructor was associated
with a student as an advisor.
But no accepted standard
Adding a temporal component results in functional dependencies like
ID street, city
not to hold, because the address varies over time
A temporal functional dependency X Y holds on schema R if the
functional dependency X Y holds on all snapshots for all legal
instances r (R).
Database System Concepts - 6th Edition 8.77 ©Silberschatz, Korth and Sudarshan
Modeling Temporal Data (Cont.)
In practice, database designers may add start and end time attributes
to relations
E.g., course(course_id, course_title) is replaced by
course(course_id, course_title, start, end)
Constraint: no two tuples can have overlapping valid times
– Hard to enforce efficiently
Foreign key references may be to current version of data, or to data at
a point in time
E.g., student transcript should refer to course information at the
time the course was taken
Database System Concepts - 6th Edition 8.78 ©Silberschatz, Korth and Sudarshan
End of Chapter
Database System Concepts - 6th Edition 8.81 ©Silberschatz, Korth and Sudarshan
Correctness of 3NF Decomposition
Algorithm (Cont’d.)
Database System Concepts - 6th Edition 8.82 ©Silberschatz, Korth and Sudarshan
Correctness of 3NF Decomposition
(Cont’d.)
Case 1: If B in :
If is a superkey, the 2nd condition of 3NF is satisfied
Otherwise must contain some attribute not in
Since B is in F+ it must be derivable from Fc, by using attribute
closure on .
Attribute closure not have used . If it had been used, must
be contained in the attribute closure of , which is not possible, since
we assumed is not a superkey.
Now, using (- {B}) and B, we can derive B
(since , and B since B is non-trivial)
Then, B is extraneous in the right-hand side of ; which is not
possible since is in Fc.
Thus, if B is in then must be a superkey, and the second
condition of 3NF must be satisfied.
Database System Concepts - 6th Edition 8.83 ©Silberschatz, Korth and Sudarshan
Correctness of 3NF Decomposition
(Cont’d.)
Case 2: B is in .
Since is a candidate key, the third alternative in the definition of
3NF is trivially satisfied.
In fact, we cannot show that is a superkey.
This shows exactly why the third alternative is present in the
definition of 3NF.
Q.E.D.
Database System Concepts - 6th Edition 8.84 ©Silberschatz, Korth and Sudarshan
Figure 8.02
Database System Concepts - 6th Edition 8.85 ©Silberschatz, Korth and Sudarshan
Figure 8.03
Database System Concepts - 6th Edition 8.86 ©Silberschatz, Korth and Sudarshan
Figure 8.04
Database System Concepts - 6th Edition 8.87 ©Silberschatz, Korth and Sudarshan
Figure 8.05
Database System Concepts - 6th Edition 8.88 ©Silberschatz, Korth and Sudarshan
Figure 8.06
Database System Concepts - 6th Edition 8.89 ©Silberschatz, Korth and Sudarshan
Figure 8.14
Database System Concepts - 6th Edition 8.90 ©Silberschatz, Korth and Sudarshan
Figure 8.15
Database System Concepts - 6th Edition 8.91 ©Silberschatz, Korth and Sudarshan
Figure 8.17
Database System Concepts - 6th Edition 8.92 ©Silberschatz, Korth and Sudarshan
Chapter 10: Storage and File Structure
Database System Concepts - 6th Edition 10.2 ©Silberschatz, Korth and Sudarshan
Classification of Physical Storage Media
Database System Concepts - 6th Edition 10.3 ©Silberschatz, Korth and Sudarshan
Physical Storage Media
Database System Concepts - 6th Edition 10.4 ©Silberschatz, Korth and Sudarshan
Physical Storage Media (Cont.)
Flash memory
Data survives power failure
Data can be written at a location only once, but location can be
erased and written to again
Can support only a limited number (10K – 1M) of write/erase
cycles.
Erasing of memory has to be done to an entire bank of
memory
Reads are roughly as fast as main memory
But writes are slow (few microseconds), erase is slower
Widely used in embedded devices such as digital cameras,
phones, and USB keys
Database System Concepts - 6th Edition 10.5 ©Silberschatz, Korth and Sudarshan
Physical Storage Media (Cont.)
Magnetic-disk
Data is stored on spinning disk, and read/written magnetically
Primary medium for the long-term storage of data; typically stores entire
database.
Data must be moved from disk to main memory for access, and written
back for storage
Much slower access than main memory (more on this later)
direct-access – possible to read data on disk in any order, unlike
magnetic tape
Capacities range up to roughly 1.5 TB as of 2009
Much larger capacity and cost/byte than main memory/flash memory
Growing constantly and rapidly with technology improvements (factor
of 2 to 3 every 2 years)
Survives power failures and system crashes
disk failure can destroy data, but is rare
Database System Concepts - 6th Edition 10.6 ©Silberschatz, Korth and Sudarshan
Physical Storage Media (Cont.)
Optical storage
non-volatile, data is read optically from a spinning disk using
a laser
CD-ROM (640 MB) and DVD (4.7 to 17 GB) most popular
forms
Blu-ray disks: 27 GB to 54 GB
Write-one, read-many (WORM) optical disks used for archival
storage (CD-R, DVD-R, DVD+R)
Multiple write versions also available (CD-RW, DVD-RW,
DVD+RW, and DVD-RAM)
Reads and writes are slower than with magnetic disk
Juke-box systems, with large numbers of removable disks, a
few drives, and a mechanism for automatic loading/unloading
of disks available for storing large volumes of data
Database System Concepts - 6th Edition 10.7 ©Silberschatz, Korth and Sudarshan
Physical Storage Media (Cont.)
Tape storage
non-volatile, used primarily for backup (to recover from disk
failure), and for archival data
sequential-access – much slower than disk
very high capacity (40 to 300 GB tapes available)
tape can be removed from drive storage costs much
cheaper than disk, but drives are expensive
Tape jukeboxes available for storing massive amounts of
data
hundreds of terabytes (1 terabyte = 109 bytes) to even
multiple petabytes (1 petabyte = 1012 bytes)
Database System Concepts - 6th Edition 10.8 ©Silberschatz, Korth and Sudarshan
Storage Hierarchy
Database System Concepts - 6th Edition 10.9 ©Silberschatz, Korth and Sudarshan
Storage Hierarchy (Cont.)
Database System Concepts - 6th Edition 10.10 ©Silberschatz, Korth and Sudarshan
Magnetic Hard Disk Mechanism
NOTE: Diagram is schematic, and simplifies the structure of actual disk drives
Database System Concepts - 6th Edition 10.11 ©Silberschatz, Korth and Sudarshan
Magnetic Disks
Read-write head
Positioned very close to the platter surface (almost touching it)
Reads or writes magnetically encoded information.
Surface of platter divided into circular tracks
Over 50K-100K tracks per platter on typical hard disks
Each track is divided into sectors.
A sector is the smallest unit of data that can be read or written.
Sector size typically 512 bytes
Typical sectors per track: 500 to 1000 (on inner tracks) to 1000 to 2000 (on
outer tracks)
To read/write a sector
disk arm swings to position head on right track
platter spins continually; data is read/written as sector passes under head
Head-disk assemblies
multiple disk platters on a single spindle (1 to 5 usually)
one head per platter, mounted on a common arm.
Cylinder i consists of ith track of all the platters
Database System Concepts - 6th Edition 10.12 ©Silberschatz, Korth and Sudarshan
Magnetic Disks (Cont.)
Database System Concepts - 6th Edition 10.13 ©Silberschatz, Korth and Sudarshan
Disk Subsystem
Database System Concepts - 6th Edition 10.14 ©Silberschatz, Korth and Sudarshan
Disk Subsystem
Disks usually connected directly to computer system
In Storage Area Networks (SAN), a large number of disks are
connected by a high-speed network to a number of servers
In Network Attached Storage (NAS) networked storage provides a
file system interface using networked file system protocol, instead of
providing a disk system interface
Database System Concepts - 6th Edition 10.15 ©Silberschatz, Korth and Sudarshan
Performance Measures of Disks
Access time – the time it takes from when a read or write request is issued to
when data transfer begins. Consists of:
Seek time – time it takes to reposition the arm over the correct track.
Average seek time is 1/2 the worst case seek time.
– Would be 1/3 if all tracks had the same number of sectors, and we
ignore the time to start and stop arm movement
4 to 10 milliseconds on typical disks
Rotational latency – time it takes for the sector to be accessed to appear
under the head.
Average latency is 1/2 of the worst case latency.
4 to 11 milliseconds on typical disks (5400 to 15000 r.p.m.)
Data-transfer rate – the rate at which data can be retrieved from or stored to
the disk.
25 to 100 MB per second max rate, lower for inner tracks
Multiple disks may share a controller, so rate that controller can handle is
also important
E.g. SATA: 150 MB/sec, SATA-II 3Gb (300 MB/sec)
Ultra 320 SCSI: 320 MB/s, SAS (3 to 6 Gb/sec)
Fiber Channel (FC2Gb or 4Gb): 256 to 512 MB/s
Database System Concepts - 6th Edition 10.16 ©Silberschatz, Korth and Sudarshan
Performance Measures (Cont.)
Database System Concepts - 6th Edition 10.17 ©Silberschatz, Korth and Sudarshan
Optimization of Disk-Block Access
Block – a contiguous sequence of sectors from a single track
data is transferred between disk and main memory in blocks
sizes range from 512 bytes to several kilobytes
Smaller blocks: more transfers from disk
Larger blocks: more space wasted due to partially filled blocks
Typical block sizes today range from 4 to 16 kilobytes
Disk-arm-scheduling algorithms order pending accesses to tracks so
that disk arm movement is minimized
elevator algorithm:
R6 R3 R1 R5 R2 R4
Database System Concepts - 6th Edition 10.18 ©Silberschatz, Korth and Sudarshan
Optimization of Disk Block Access (Cont.)
Database System Concepts - 6th Edition 10.19 ©Silberschatz, Korth and Sudarshan
Optimization of Disk Block Access (Cont.)
Database System Concepts - 6th Edition 10.20 ©Silberschatz, Korth and Sudarshan
Flash Storage
NOR flash vs NAND flash
NAND flash
used widely for storage, since it is much cheaper than NOR flash
requires page-at-a-time read (page: 512 bytes to 4 KB)
transfer rate around 20 MB/sec
solid state disks: use multiple flash storage devices to provide
higher transfer rate of 100 to 200 MB/sec
erase is very slow (1 to 2 millisecs)
erase block contains multiple pages
remapping of logical page addresses to physical page addresses
avoids waiting for erase
– translation table tracks mapping
» also stored in a label field of flash page
– remapping carried out by flash translation layer
after 100,000 to 1,000,000 erases, erase block becomes
unreliable and cannot be used
– wear leveling
Database System Concepts - 6th Edition 10.21 ©Silberschatz, Korth and Sudarshan
RAID
RAID: Redundant Arrays of Independent Disks
disk organization techniques that manage a large numbers of disks,
providing a view of a single disk of
high capacity and high speed by using multiple disks in parallel,
high reliability by storing data redundantly, so that data can be
recovered even if a disk fails
The chance that some disk out of a set of N disks will fail is much higher than
the chance that a specific single disk will fail.
E.g., a system with 100 disks, each with MTTF of 100,000 hours (approx.
11 years), will have a system MTTF of 1000 hours (approx. 41 days)
Techniques for using redundancy to avoid data loss are critical with large
numbers of disks
Originally a cost-effective alternative to large, expensive disks
I in RAID originally stood for ``inexpensive’’
Today RAIDs are used for their higher reliability and bandwidth.
The “I” is interpreted as independent
Database System Concepts - 6th Edition 10.22 ©Silberschatz, Korth and Sudarshan
Improvement of Reliability via Redundancy
Database System Concepts - 6th Edition 10.25 ©Silberschatz, Korth and Sudarshan
RAID Levels (Cont.)
RAID Level 2: Memory-Style Error-Correcting-Codes (ECC) with bit
striping.
RAID Level 3: Bit-Interleaved Parity
a single parity bit is enough for error correction, not just
detection, since we know which disk has failed
When writing data, corresponding parity bits must also be
computed and written to a parity bit disk
To recover data in a damaged disk, compute XOR of bits
from other disks (including parity bit disk)
Database System Concepts - 6th Edition 10.26 ©Silberschatz, Korth and Sudarshan
RAID Levels (Cont.)
RAID Level 3 (Cont.)
Faster data transfer than with a single disk, but fewer I/Os per
second since every disk has to participate in every I/O.
Subsumes Level 2 (provides all its benefits, at lower cost).
RAID Level 4: Block-Interleaved Parity; uses block-level striping,
and keeps a parity block on a separate disk for corresponding
blocks from N other disks.
When writing data block, corresponding block of parity bits must
also be computed and written to parity disk
To find value of a damaged block, compute XOR of bits from
corresponding blocks (including parity block) from other disks.
Database System Concepts - 6th Edition 10.27 ©Silberschatz, Korth and Sudarshan
RAID Levels (Cont.)
RAID Level 4 (Cont.)
Provides higher I/O rates for independent block reads than Level 3
block read goes to a single disk, so blocks stored on different
disks can be read in parallel
Provides high transfer rates for reads of multiple blocks than no-
striping
Before writing a block, parity data must be computed
Can be done by using old parity block, old value of current block
and new value of current block (2 block reads + 2 block writes)
Or by recomputing the parity value using the new values of
blocks corresponding to the parity block
– More efficient for writing large amounts of data sequentially
Parity block becomes a bottleneck for independent block writes
since every block write also writes to parity disk
Database System Concepts - 6th Edition 10.28 ©Silberschatz, Korth and Sudarshan
RAID Levels (Cont.)
RAID Level 5: Block-Interleaved Distributed Parity; partitions data and
parity among all N + 1 disks, rather than storing data in N disks and
parity in 1 disk.
E.g., with 5 disks, parity block for nth set of blocks is stored on disk
(n mod 5) + 1, with the data blocks stored on the other 4 disks.
Database System Concepts - 6th Edition 10.29 ©Silberschatz, Korth and Sudarshan
RAID Levels (Cont.)
RAID Level 5 (Cont.)
Higher I/O rates than Level 4.
Block writes occur in parallel if the blocks and their parity
blocks are on different disks.
Subsumes Level 4: provides same benefits, but avoids bottleneck
of parity disk.
RAID Level 6: P+Q Redundancy scheme; similar to Level 5, but
stores extra redundant information to guard against multiple disk
failures.
Better reliability than Level 5 at a higher cost; not used as widely.
Database System Concepts - 6th Edition 10.30 ©Silberschatz, Korth and Sudarshan
Choice of RAID Level
Factors in choosing RAID level
Monetary cost
Performance: Number of I/O operations per second, and
bandwidth during normal operation
Performance during failure
Performance during rebuild of failed disk
Including time taken to rebuild failed disk
RAID 0 is used only when data safety is not important
E.g. data can be recovered quickly from other sources
Level 2 and 4 never used since they are subsumed by 3 and 5
Level 3 is not used anymore since bit-striping forces single block
reads to access all disks, wasting disk arm movement, which
block striping (level 5) avoids
Level 6 is rarely used since levels 1 and 5 offer adequate safety
for most applications
Database System Concepts - 6th Edition 10.31 ©Silberschatz, Korth and Sudarshan
Choice of RAID Level (Cont.)
Level 1 provides much better write performance than level 5
Level 5 requires at least 2 block reads and 2 block writes to write
a single block, whereas Level 1 only requires 2 block writes
Level 1 preferred for high update environments such as log disks
Level 1 had higher storage cost than level 5
disk drive capacities increasing rapidly (50%/year) whereas disk
access times have decreased much less (x 3 in 10 years)
I/O requirements have increased greatly, e.g. for Web servers
When enough disks have been bought to satisfy required rate of
I/O, they often have spare storage capacity
so there is often no extra monetary cost for Level 1!
Level 5 is preferred for applications with low update rate,
and large amounts of data
Level 1 is preferred for all other applications
Database System Concepts - 6th Edition 10.32 ©Silberschatz, Korth and Sudarshan
Hardware Issues
Database System Concepts - 6th Edition 10.33 ©Silberschatz, Korth and Sudarshan
Hardware Issues (Cont.)
Latent failures: data successfully written earlier gets damaged
can result in data loss even if only one disk fails
Data scrubbing:
continually scan for latent failures, and recover from copy/parity
Hot swapping: replacement of disk while system is running, without power
down
Supported by some hardware RAID systems,
reduces time to recovery, and improves availability greatly
Many systems maintain spare disks which are kept online, and used as
replacements for failed disks immediately on detection of failure
Reduces time to recovery greatly
Many hardware RAID systems ensure that a single point of failure will not
stop the functioning of the system by using
Redundant power supplies with battery backup
Multiple controllers and multiple interconnections to guard against
controller/interconnection failures
Database System Concepts - 6th Edition 10.34 ©Silberschatz, Korth and Sudarshan
Optical Disks
Compact disk-read only memory (CD-ROM)
Removable disks, 640 MB per disk
Seek time about 100 msec (optical read head is heavier and slower)
Higher latency (3000 RPM) and lower data-transfer rates (3-6 MB/s)
compared to magnetic disks
Digital Video Disk (DVD)
DVD-5 holds 4.7 GB , and DVD-9 holds 8.5 GB
DVD-10 and DVD-18 are double sided formats with capacities of 9.4
GB and 17 GB
Blu-ray DVD: 27 GB (54 GB for double sided disk)
Slow seek time, for same reasons as CD-ROM
Record once versions (CD-R and DVD-R) are popular
data can only be written once, and cannot be erased.
high capacity and long lifetime; used for archival storage
Multi-write versions (CD-RW, DVD-RW, DVD+RW and DVD-RAM)
also available
Database System Concepts - 6th Edition 10.35 ©Silberschatz, Korth and Sudarshan
Magnetic Tapes
Database System Concepts - 6th Edition 10.36 ©Silberschatz, Korth and Sudarshan
File Organization, Record Organization
and Storage Access
Database System Concepts - 6th Edition 10.37 ©Silberschatz, Korth and Sudarshan
File Organization
Database System Concepts - 6th Edition 10.38 ©Silberschatz, Korth and Sudarshan
Fixed-Length Records
Simple approach:
Store record i starting from byte n (i – 1), where n is the size of
each record.
Record access is simple but records may cross blocks
Modification: do not allow records to cross block boundaries
Deletion of record i:
alternatives:
move records i + 1, . . ., n
to i, . . . , n – 1
move record n to i
do not move records, but
link all free records on a
free list
Database System Concepts - 6th Edition 10.39 ©Silberschatz, Korth and Sudarshan
Deleting record 3 and compacting
Database System Concepts - 6th Edition 10.40 ©Silberschatz, Korth and Sudarshan
Deleting record 3 and moving last record
Database System Concepts - 6th Edition 10.41 ©Silberschatz, Korth and Sudarshan
Free Lists
Store the address of the first deleted record in the file header.
Use this first record to store the address of the second deleted record,
and so on
Can think of these stored addresses as pointers since they “point” to
the location of a record.
More space efficient representation: reuse space for normal attributes
of free records to store pointers. (No pointers stored in in-use records.)
Database System Concepts - 6th Edition 10.42 ©Silberschatz, Korth and Sudarshan
Variable-Length Records
Database System Concepts - 6th Edition 10.43 ©Silberschatz, Korth and Sudarshan
Variable-Length Records: Slotted Page Structure
Database System Concepts - 6th Edition 10.44 ©Silberschatz, Korth and Sudarshan
Organization of Records in Files
Database System Concepts - 6th Edition 10.45 ©Silberschatz, Korth and Sudarshan
Sequential File Organization
Suitable for applications that require sequential processing of
the entire file
The records in the file are ordered by a search-key
Database System Concepts - 6th Edition 10.46 ©Silberschatz, Korth and Sudarshan
Sequential File Organization (Cont.)
Deletion – use pointer chains
Insertion –locate the position where the record is to be inserted
if there is free space insert there
if no free space, insert the record in an overflow block
In either case, pointer chain must be updated
Need to reorganize the file
from time to time to restore
sequential order
Database System Concepts - 6th Edition 10.47 ©Silberschatz, Korth and Sudarshan
Multitable Clustering File Organization
Store several relations in one file using a multitable clustering
file organization
department
instructor
multitable clustering
of department and
instructor
Database System Concepts - 6th Edition 10.48 ©Silberschatz, Korth and Sudarshan
Multitable Clustering File Organization (cont.)
Database System Concepts - 6th Edition 10.49 ©Silberschatz, Korth and Sudarshan
Data Dictionary Storage
The Data dictionary (also called system catalog) stores
metadata; that is, data about data, such as
Information about relations
names of relations
names, types and lengths of attributes of each relation
names and definitions of views
integrity constraints
User and accounting information, including passwords
Statistical and descriptive data
number of tuples in each relation
Physical file organization information
How relation is stored (sequential/hash/…)
Physical location of relation
Information about indices (Chapter 11)
Database System Concepts - 6th Edition 10.50 ©Silberschatz, Korth and Sudarshan
Relational Representation of System Metadata
Relational
representation on
disk
Specialized data
structures
designed for
efficient access, in
memory
Database System Concepts - 6th Edition 10.51 ©Silberschatz, Korth and Sudarshan
Storage Access
Database System Concepts - 6th Edition 10.52 ©Silberschatz, Korth and Sudarshan
Buffer Manager
Programs call on the buffer manager when they need a block
from disk.
1. If the block is already in the buffer, buffer manager returns
the address of the block in main memory
2. If the block is not in the buffer, the buffer manager
1. Allocates space in the buffer for the block
1. Replacing (throwing out) some other block, if required,
to make space for the new block.
2. Replaced block written back to disk only if it was
modified since the most recent time that it was written
to/fetched from the disk.
2. Reads the block from the disk to the buffer, and returns
the address of the block in main memory to requester.
Database System Concepts - 6th Edition 10.53 ©Silberschatz, Korth and Sudarshan
Buffer-Replacement Policies
Most operating systems replace the block least recently used
(LRU strategy)
Idea behind LRU – use past pattern of block references as a
predictor of future references
Queries have well-defined access patterns (such as sequential
scans), and a database system can use the information in a user’s
query to predict future references
LRU can be a bad strategy for certain access patterns involving
repeated scans of data
For example: when computing the join of 2 relations r and s
by a nested loops
for each tuple tr of r do
for each tuple ts of s do
if the tuples tr and ts match …
Mixed strategy with hints on replacement strategy provided
by the query optimizer is preferable
Database System Concepts - 6th Edition 10.54 ©Silberschatz, Korth and Sudarshan
Buffer-Replacement Policies (Cont.)
Pinned block – memory block that is not allowed to be written
back to disk.
Toss-immediate strategy – frees the space occupied by a block
as soon as the final tuple of that block has been processed
Most recently used (MRU) strategy – system must pin the
block currently being processed. After the final tuple of that block
has been processed, the block is unpinned, and it becomes the
most recently used block.
Buffer manager can use statistical information regarding the
probability that a request will reference a particular relation
E.g., the data dictionary is frequently accessed. Heuristic:
keep data-dictionary blocks in main memory buffer
Buffer managers also support forced output of blocks for the
purpose of recovery (more in Chapter 16)
Database System Concepts - 6th Edition 10.55 ©Silberschatz, Korth and Sudarshan
End of Chapter 10
Database System Concepts - 6th Edition 10.57 ©Silberschatz, Korth and Sudarshan
Figure 10.18
Database System Concepts - 6th Edition 10.58 ©Silberschatz, Korth and Sudarshan
Figure in-10.1
Database System Concepts - 6th Edition 10.59 ©Silberschatz, Korth and Sudarshan
CS204 (DBMS) Assignment
Q.1) Explain the terms seek time, rotational delay, and transfer time.
Q.2) Consider a disk with a sector size of 512 bytes, 2,000 tracks per surface, 50 sectors per track, 5
double-sided platters, average seek time of 10 msec.
1. What is the capacity of a track in bytes? What is the capacity of each surface? What is the
capacity of the disk?
2. How many cylinders does the disk have?
3. Give examples of valid block sizes. Is 256 bytes a valid block size? 2,048? 51,200?
4. If the disk platters rotate at 5,400 rpm (revolutions per minute), what is the maximum
rotational delay?
5. Assuming that one track of data can be transferred per revolution, what is the transfer
rate?
Q.3) Explain what the buffer manager must do to process a read request for a page. What happens if
the requested page is in the pool but not pinned?
Q.4)What is the sequential file organization. What is the importance of such type of file
organization.
Q.5) Explain the file organization that stores related records of two or more relations within a block.
In what condition, such type of file organization is more suitable.
Q.6) Modern disk drives store more sectors on the outer tracks than the inner tracks. Since the
rotation speed is constant, the sequential data transfer rate is also higher on the outer tracks. The
seek time and rotational delay are unchanged.
Considering this information, explain good strategies for placing files with the following kinds of
access patterns:
1. Frequent, random accesses to a small file (e.g., catalog relations).
2. Sequential scans of a large file (e.g., selection from a relation with no index).
3. Random accesses to a large file via an index (e.g., selection from a relation via the
index).
4. Sequential scans of a small file.
CS204 DBMS ASSIGNMENT
Q.1) Explain the terms seek time, rotational delay, and transfer time.
Q.2) Consider a disk with a sector size of 512 bytes, 2,000 tracks per surface, 50 sectors per track, 5
1. What is the capacity of a track in bytes? What is the capacity of each surface? What is the
3. Give examples of valid block sizes. Is 256 bytes a valid block size? 2,048? 51,200?
4. If the disk platters rotate at 5,400 rpm (revolutions per minute), what is the maximum
rotational delay?
5. Assuming that one track of data can be transferred per revolution, what is the transfer
rate?
Q.3) Explain what the buffer manager must do to process a read request for a page. What happens
if the requested page is in the pool but not pinned?
Q.4) What is the sequential file organization. What is the importance of such type of file
organization?
Q.5) Explain the file organization that stores related records of two or more relations within a
block. In what condition, such type of file organization is more suitable.
Q.6) Modern disk drives store more sectors on the outer tracks than the inner tracks. Since the
rotation speed is constant, the sequential data transfer rate is also higher on the outer tracks. The
Considering this information, explain good strategies for placing files with the following kinds of
access patterns:
2. Sequential scans of a large file (e.g., selection from a relation with no index).
3. Random accesses to a large file via an index (e.g., selection from a relation via the
index).
Database System Concepts - 6th Edition 11.2 ©Silberschatz, Korth and Sudarshan
Basic Concepts
Indexing mechanisms used to speed up access to desired data.
E.g., author catalog in library
Search Key - attribute to set of attributes used to look up records in a
file.
An index file consists of records (called index entries) of the form
search-key pointer
Index files are typically much smaller than the original file
Two basic kinds of indices:
Ordered indices: search keys are stored in sorted order
Hash indices: search keys are distributed uniformly across
“buckets” using a “hash function”.
Database System Concepts - 6th Edition 11.3 ©Silberschatz, Korth and Sudarshan
Index Evaluation Metrics
Access types supported efficiently. E.g.,
records with a specified value in the attribute
or records with an attribute value falling in a specified range of
values.
Access time
Insertion time
Deletion time
Space overhead
Database System Concepts - 6th Edition 11.4 ©Silberschatz, Korth and Sudarshan
Ordered Indices
In an ordered index, index entries are stored sorted on the search key
value. E.g., author catalog in library.
Primary index: in a sequentially ordered file, the index whose search
key specifies the sequential order of the file.
Also called clustering index
The search key of a primary index is usually but not necessarily the
primary key.
Secondary index: an index whose search key specifies an order
different from the sequential order of the file. Also called
non-clustering index.
Index-sequential file: ordered sequential file with a primary index.
Database System Concepts - 6th Edition 11.5 ©Silberschatz, Korth and Sudarshan
Dense Index Files
Dense index — Index record appears for every search-key
value in the file.
E.g. index on ID attribute of instructor relation
Database System Concepts - 6th Edition 11.6 ©Silberschatz, Korth and Sudarshan
Dense Index Files (Cont.)
Dense index on dept_name, with instructor file sorted on
dept_name
Database System Concepts - 6th Edition 11.7 ©Silberschatz, Korth and Sudarshan
Sparse Index Files
Sparse Index: contains index records for only some search-key
values.
Applicable when records are sequentially ordered on search-key
To locate a record with search-key value K we:
Find index record with largest search-key value < K
Search file sequentially starting at the record to which the index
record points
Database System Concepts - 6th Edition 11.8 ©Silberschatz, Korth and Sudarshan
Sparse Index Files (Cont.)
Compared to dense indices:
Less space and less maintenance overhead for insertions and
deletions.
Generally slower than dense index for locating records.
Good tradeof: sparse index with an index entry for every block in file,
corresponding to least search-key value in the block.
Database System Concepts - 6th Edition 11.9 ©Silberschatz, Korth and Sudarshan
Secondary Indices Example
Database System Concepts - 6th Edition 11.10 ©Silberschatz, Korth and Sudarshan
Primary and Secondary Indices
Indices offer substantial benefits when searching for records.
BUT: Updating indices imposes overhead on database
modification --when a file is modified, every index on the file
must be updated,
Sequential scan using primary index is efficient, but a
sequential scan using a secondary index is expensive
Each record access may fetch a new block from disk
Block fetch requires about 5 to 10 milliseconds, versus
about 100 nanoseconds for memory access
Database System Concepts - 6th Edition 11.11 ©Silberschatz, Korth and Sudarshan
Multilevel Index
If primary index does not fit in memory, access becomes
expensive.
Solution: treat primary index kept on disk as a sequential file
and construct a sparse index on it.
outer index – a sparse index of primary index
inner index – the primary index file
If even outer index is too large to fit in main memory, yet
another level of index can be created, and so on.
Indices at all levels must be updated on insertion or deletion
from the file.
Database System Concepts - 6th Edition 11.12 ©Silberschatz, Korth and Sudarshan
Multilevel Index (Cont.)
Database System Concepts - 6th Edition 11.13 ©Silberschatz, Korth and Sudarshan
Index Update: Deletion
Database System Concepts - 6th Edition 11.15 ©Silberschatz, Korth and Sudarshan
Secondary Indices
Frequently, one wants to find all the records whose values in
a certain field (which is not the search-key of the primary
index) satisfy some condition.
Example 1: In the instructor relation stored sequentially by
ID, we may want to find all instructors in a particular
department
Example 2: as above, but where we want to find all
instructors with a specified salary or with salary in a
specified range of values
We can have a secondary index with an index record for each
search-key value
Database System Concepts - 6th Edition 11.16 ©Silberschatz, Korth and Sudarshan
B+-Tree Index Files
Database System Concepts - 6th Edition 11.17 ©Silberschatz, Korth and Sudarshan
Example of B+-Tree
Database System Concepts - 6th Edition 11.18 ©Silberschatz, Korth and Sudarshan
B+-Tree Index Files (Cont.)
Database System Concepts - 6th Edition 11.19 ©Silberschatz, Korth and Sudarshan
B+-Tree Node Structure
Typical node
Database System Concepts - 6th Edition 11.20 ©Silberschatz, Korth and Sudarshan
Leaf Nodes in B+-Trees
Database System Concepts - 6th Edition 11.21 ©Silberschatz, Korth and Sudarshan
Non-Leaf Nodes in B+-Trees
Non leaf nodes form a multi-level sparse index on the leaf
nodes. For a non-leaf node with m pointers:
All the search-keys in the subtree to which P1 points are
less than K1
For 2 i n – 1, all the search-keys in the subtree to
which Pi points have values greater than or equal to Ki–1 and
less than Ki
All the search-keys in the subtree to which Pn points have
values greater than or equal to Kn–1
Database System Concepts - 6th Edition 11.22 ©Silberschatz, Korth and Sudarshan
Example of B+-tree
Database System Concepts - 6th Edition 11.23 ©Silberschatz, Korth and Sudarshan
Observations about B+-trees
Since the inter-node connections are done by pointers, “logically”
close blocks need not be “physically” close.
The non-leaf levels of the B+-tree form a hierarchy of sparse indices.
The B+-tree contains a relatively small number of levels
Level below root has at least 2* n/2 values
Next level has at least 2* n/2 * n/2 values
.. etc.
If there are K search-key values in the file, the tree height is no
more than logn/2(K)
thus searches can be conducted efficiently.
Insertions and deletions to the main file can be handled efficiently,
as the index can be restructured in logarithmic time (as we shall
see).
Database System Concepts - 6th Edition 11.24 ©Silberschatz, Korth and Sudarshan
Queries on B+-Trees
Find record with search-key value V.
1. C=root
2. While C is not a leaf node {
1. Let i be least value s.t. V Ki.
2. If no such exists, set C = last non-null pointer in C
3. Else { if (V= Ki ) Set C = Pi +1 else set C = Pi}
}
3. Let i be least value s.t. Ki = V
4. If there is such a value i, follow pointer Pi to the desired record.
5. Else no record with search-key value k exists.
Database System Concepts - 6th Edition 11.25 ©Silberschatz, Korth and Sudarshan
Handling Duplicates
With duplicate search keys
In both leaf and internal nodes,
we cannot guarantee that K1 < K2 < K3 < . . . < Kn–1
but can guarantee K1 K2 K3 . . . Kn–1
Search-keys in the subtree to which Pi points
are Ki,, but not necessarily < Ki,
To see why, suppose same search key value V is present
in two leaf node Li and Li+1. Then in parent node Ki must
be equal to V
Database System Concepts - 6th Edition 11.26 ©Silberschatz, Korth and Sudarshan
Handling Duplicates
Database System Concepts - 6th Edition 11.27 ©Silberschatz, Korth and Sudarshan
Queries on B+-Trees (Cont.)
If there are K search-key values in the file, the height of the tree is no
more than logn/2(K).
A node is generally the same size as a disk block, typically 4 kilobytes
and n is typically around 100 (40 bytes per index entry).
With 1 million search key values and n = 100
at most log50(1,000,000) = 4 nodes are accessed in a lookup.
Contrast this with a balanced binary tree with 1 million search key
values — around 20 nodes are accessed in a lookup
above difference is significant since every node access may need
a disk I/O, costing around 20 milliseconds
Database System Concepts - 6th Edition 11.28 ©Silberschatz, Korth and Sudarshan
Updates on B+-Trees: Insertion
1. Find the leaf node in which the search-key value would appear
2. If the search-key value is already present in the leaf node
1. Add record to the file
2. If necessary add a pointer to the bucket.
3. If the search-key value is not present, then
1. add the record to the main file (and create a bucket if
necessary)
2. If there is room in the leaf node, insert (key-value, pointer)
pair in the leaf node
3. Otherwise, split the node (along with the new (key-value,
pointer) entry) as discussed in the next slide.
Database System Concepts - 6th Edition 11.29 ©Silberschatz, Korth and Sudarshan
Updates on B+-Trees: Insertion (Cont.)
Splitting a leaf node:
take the n (search-key value, pointer) pairs (including the one
being inserted) in sorted order. Place the first n/2 in the original
node, and the rest in a new node.
let the new node be p, and let k be the least key value in p. Insert
(k,p) in the parent of the node being split.
If the parent is full, split it and propagate the split further up.
Splitting of nodes proceeds upwards till a node that is not full is found.
In the worst case the root node may be split increasing the height
of the tree by 1.
Result of splitting node containing Brandt, Califieri and Crick on inserting Adams
Next step: insert entry with (Califieri,pointer-to-new-node) into parent
Database System Concepts - 6th Edition 11.30 ©Silberschatz, Korth and Sudarshan
B+-Tree Insertion
Database System Concepts - 6th Edition 11.31 ©Silberschatz, Korth and Sudarshan
B+-Tree Insertion
Database System Concepts - 6th Edition 11.32 ©Silberschatz, Korth and Sudarshan
Insertion in B+-Trees (Cont.)
Splitting a non-leaf node: when inserting (k,p) into an already full
internal node N
Copy N to an in-memory area M with space for n+1 pointers and n
keys
Insert (k,p) into M
Copy P1,K1, …, K n/2-1,P n/2 from M back into node N
Copy Pn/2+1,K n/2+1,…,Kn,Pn+1 from M into newly allocated node N’
Insert (K n/2,N’) into parent N
Read pseudocode in book!
Califieri
Database System Concepts - 6th Edition 11.33 ©Silberschatz, Korth and Sudarshan
Examples of B+-Tree Deletion
Database System Concepts - 6th Edition 11.34 ©Silberschatz, Korth and Sudarshan
Examples of B+-Tree Deletion (Cont.)
Database System Concepts - 6th Edition 11.35 ©Silberschatz, Korth and Sudarshan
Example of B+-tree Deletion (Cont.)
Node with Gold and Katz became underfull, and was merged with its sibling
Parent node becomes underfull, and is merged with its sibling
Value separating two nodes (at the parent) is pulled down when merging
Root node then has only one child, and is deleted
Database System Concepts - 6th Edition 11.36 ©Silberschatz, Korth and Sudarshan
Updates on B+-Trees: Deletion
Find the record to be deleted, and remove it from the main file and from
the bucket (if present)
Remove (search-key value, pointer) from the leaf node if there is no
bucket or if the bucket has become empty
If the node has too few entries due to the removal, and the entries in
the node and a sibling fit into a single node, then merge siblings:
Insert all the search-key values in the two nodes into a single node
(the one on the left), and delete the other node.
Delete the pair (Ki–1, Pi), where Pi is the pointer to the deleted node,
from its parent, recursively using the above procedure.
Database System Concepts - 6th Edition 11.37 ©Silberschatz, Korth and Sudarshan
Updates on B+-Trees: Deletion
Otherwise, if the node has too few entries due to the removal, but the
entries in the node and a sibling do not fit into a single node, then
redistribute pointers:
Redistribute the pointers between the node and a sibling such that
both have more than the minimum number of entries.
Update the corresponding search-key value in the parent of the
node.
The node deletions may cascade upwards till a node which has n/2
or more pointers is found.
If the root node has only one pointer after deletion, it is deleted and the
sole child becomes the root.
Database System Concepts - 6th Edition 11.38 ©Silberschatz, Korth and Sudarshan
Non-Unique Search Keys
Alternatives to scheme described earlier
Buckets on separate block (bad idea)
List of tuple pointers with each key
Extra code to handle long lists
Deletion of a tuple can be expensive if there are many
duplicates on search key (why?)
Low space overhead, no extra cost for queries
Make search key unique by adding a record-identifier
Extra storage overhead for keys
Simpler code for insertion/deletion
Widely used
Database System Concepts - 6th Edition 11.39 ©Silberschatz, Korth and Sudarshan
B+-Tree File Organization
Index file degradation problem is solved by using B+-Tree indices.
Data file degradation problem is solved by using B+-Tree File
Organization.
The leaf nodes in a B+-tree file organization store records, instead of
pointers.
Leaf nodes are still required to be half full
Since records are larger than pointers, the maximum number of
records that can be stored in a leaf node is less than the number of
pointers in a nonleaf node.
Insertion and deletion are handled in the same way as insertion and
deletion of entries in a B+-tree index.
Database System Concepts - 6th Edition 11.40 ©Silberschatz, Korth and Sudarshan
B+-Tree File Organization (Cont.)
Database System Concepts - 6th Edition 11.41 ©Silberschatz, Korth and Sudarshan
Other Issues in Indexing
Record relocation and secondary indices
If a record moves, all secondary indices that store record pointers
have to be updated
Node splits in B+-tree file organizations become very expensive
Solution: use primary-index search key instead of record pointer in
secondary index
Extra traversal of primary index to locate record
– Higher cost for queries, but node splits are cheap
Add record-id if primary-index search key is non-unique
Database System Concepts - 6th Edition 11.42 ©Silberschatz, Korth and Sudarshan
Indexing Strings
Variable length strings as keys
Variable fanout
Use space utilization as criterion for splitting, not number of
pointers
Prefix compression
Key values at internal nodes can be prefixes of full key
Keep enough characters to distinguish entries in the
subtrees separated by the key value
– E.g. “Silas” and “Silberschatz” can be separated by
“Silb”
Keys in leaf node can be compressed by sharing common
prefixes
Database System Concepts - 6th Edition 11.43 ©Silberschatz, Korth and Sudarshan
Bulk Loading and Bottom-Up Build
Inserting entries one-at-a-time into a B +-tree requires 1 IO per entry
assuming leaf level does not fit in memory
can be very inefficient for loading a large number of entries at a time (bulk
loading)
Efficient alternative 1:
sort entries first (using efficient external-memory sort algorithms
discussed later in Section 12.4)
insert in sorted order
insertion will go to existing page (or cause a split)
much improved IO performance, but most leaf nodes half full
Efficient alternative 2: Bottom-up B+-tree construction
As before sort entries
And then create tree layer-by-layer, starting with leaf level
details as an exercise
Implemented as part of bulk-load utility by most database systems
Database System Concepts - 6th Edition 11.44 ©Silberschatz, Korth and Sudarshan
B-Tree Index Files
Similar to B+-tree, but B-tree allows search-key values to
appear only once; eliminates redundant storage of search
keys.
Search keys in nonleaf nodes appear nowhere else in the B-
tree; an additional pointer field for each search key in a
nonleaf node must be included.
Generalized B-tree leaf node
Database System Concepts - 6th Edition 11.45 ©Silberschatz, Korth and Sudarshan
B-Tree Index File Example
Database System Concepts - 6th Edition 11.46 ©Silberschatz, Korth and Sudarshan
B-Tree Index Files (Cont.)
Advantages of B-Tree indices:
May use less tree nodes than a corresponding B+-Tree.
Sometimes possible to find search-key value before reaching leaf
node.
Disadvantages of B-Tree indices:
Only small fraction of all search-key values are found early
Non-leaf nodes are larger, so fan-out is reduced. Thus, B-Trees
typically have greater depth than corresponding B+-Tree
Insertion and deletion more complicated than in B+-Trees
Implementation is harder than B+-Trees.
Typically, advantages of B-Trees do not out weigh disadvantages.
Database System Concepts - 6th Edition 11.47 ©Silberschatz, Korth and Sudarshan
Multiple-Key Access
Use multiple indices for certain types of queries.
Example:
select ID
from instructor
where dept_name = “Finance” and salary = 80000
Possible strategies for processing query using indices on single
attributes:
1. Use index on dept_name to find instructors with department
name Finance; test salary = 80000
2. Use index on salary to find instructors with a salary of $80000;
test dept_name = “Finance”.
3. Use dept_name index to find pointers to all records pertaining
to the “Finance” department. Similarly use index on salary.
Take intersection of both sets of pointers obtained.
Database System Concepts - 6th Edition 11.48 ©Silberschatz, Korth and Sudarshan
Indices on Multiple Keys
Composite search keys are search keys containing more than
one attribute
E.g. (dept_name, salary)
Lexicographic ordering: (a1, a2) < (b1, b2) if either
a1 < b1, or
a1=b1 and a2 < b2
Database System Concepts - 6th Edition 11.49 ©Silberschatz, Korth and Sudarshan
Indices on Multiple Attributes
Database System Concepts - 6th Edition 11.50 ©Silberschatz, Korth and Sudarshan
Other Features
Covering indices
Add extra attributes to index so (some) queries can avoid fetching
the actual records
Particularly useful for secondary indices
– Why?
Can store extra attributes only at leaf
Database System Concepts - 6th Edition 11.51 ©Silberschatz, Korth and Sudarshan
Hashing
Database System Concepts - 6th Edition 11.53 ©Silberschatz, Korth and Sudarshan
Example of Hash File Organization
Database System Concepts - 6th Edition 11.54 ©Silberschatz, Korth and Sudarshan
Example of Hash File Organization
Database System Concepts - 6th Edition 11.56 ©Silberschatz, Korth and Sudarshan
Handling of Bucket Overflows
Bucket overflow can occur because of
Insufficient buckets
Skew in distribution of records. This can occur due to two
reasons:
multiple records have same search-key value
chosen hash function produces non-uniform distribution of key
values
Although the probability of bucket overflow can be reduced, it cannot
be eliminated; it is handled by using overflow buckets.
Database System Concepts - 6th Edition 11.57 ©Silberschatz, Korth and Sudarshan
Handling of Bucket Overflows (Cont.)
Database System Concepts - 6th Edition 11.58 ©Silberschatz, Korth and Sudarshan
Hash Indices
Hashing can be used not only for file organization, but also for index-
structure creation.
A hash index organizes the search keys, with their associated record
pointers, into a hash file structure.
Strictly speaking, hash indices are always secondary indices
if the file itself is organized using hashing, a separate primary hash
index on it using the same search-key is unnecessary.
However, we use the term hash index to refer to both secondary
index structures and hash organized files.
Database System Concepts - 6th Edition 11.59 ©Silberschatz, Korth and Sudarshan
Example of Hash Index
Database System Concepts - 6th Edition 11.60 ©Silberschatz, Korth and Sudarshan
Deficiencies of Static Hashing
In static hashing, function h maps search-key values to a fixed set of B
of bucket addresses. Databases grow or shrink with time.
If initial number of buckets is too small, and file grows, performance
will degrade due to too much overflows.
If space is allocated for anticipated growth, a significant amount of
space will be wasted initially (and buckets will be underfull).
If database shrinks, again space will be wasted.
One solution: periodic re-organization of the file with a new hash
function
Expensive, disrupts normal operations
Better solution: allow the number of buckets to be modified dynamically.
Database System Concepts - 6th Edition 11.61 ©Silberschatz, Korth and Sudarshan
Dynamic Hashing
Good for database that grows and shrinks in size
Allows the hash function to be modified dynamically
Extendable hashing – one form of dynamic hashing
Hash function generates values over a large range — typically b-bit
integers, with b = 32.
At any time use only a prefix of the hash function to index into a
table of bucket addresses.
Let the length of the prefix be i bits, 0 i 32.
Bucket address table size = 2i. Initially i = 0
Value of i grows and shrinks as the size of the database grows
and shrinks.
Multiple entries in the bucket address table may point to a bucket
(why?)
Thus, actual number of buckets is < 2i
The number of buckets also changes dynamically due to
coalescing and splitting of buckets.
Database System Concepts - 6th Edition 11.62 ©Silberschatz, Korth and Sudarshan
General Extendable Hash Structure
Database System Concepts - 6th Edition 11.63 ©Silberschatz, Korth and Sudarshan
Use of Extendable Hash Structure
Each bucket j stores a value ij
All the entries that point to the same bucket have the same values on
the first ij bits.
To locate the bucket containing search-key Kj:
1. Compute h(Kj) = X
2. Use the first i high order bits of X as a displacement into bucket
address table, and follow the pointer to appropriate bucket
To insert a record with search-key value Kj
follow same procedure as look-up and locate the bucket, say j.
If there is room in the bucket j insert record in the bucket.
Else the bucket must be split and insertion re-attempted (next slide.)
Overflow buckets used instead in some cases (will see shortly)
Database System Concepts - 6th Edition 11.64 ©Silberschatz, Korth and Sudarshan
Insertion in Extendable Hash Structure (Cont)
To split a bucket j when inserting record with search-key value Kj:
Database System Concepts - 6th Edition 11.66 ©Silberschatz, Korth and Sudarshan
Use of Extendable Hash Structure: Example
Database System Concepts - 6th Edition 11.67 ©Silberschatz, Korth and Sudarshan
Example (Cont.)
Database System Concepts - 6th Edition 11.68 ©Silberschatz, Korth and Sudarshan
Example (Cont.)
Database System Concepts - 6th Edition 11.69 ©Silberschatz, Korth and Sudarshan
Example (Cont.)
Database System Concepts - 6th Edition 11.70 ©Silberschatz, Korth and Sudarshan
Example (Cont.)
Hash structure after insertion of Gold and El Said records
Database System Concepts - 6th Edition 11.71 ©Silberschatz, Korth and Sudarshan
Example (Cont.)
Hash structure after insertion of Katz record
Database System Concepts - 6th Edition 11.72 ©Silberschatz, Korth and Sudarshan
Example (Cont.)
Database System Concepts - 6th Edition 11.73 ©Silberschatz, Korth and Sudarshan
Example (Cont.)
Database System Concepts - 6th Edition 11.74 ©Silberschatz, Korth and Sudarshan
Extendable Hashing vs. Other Schemes
Benefits of extendable hashing:
Hash performance does not degrade with growth of file
Minimal space overhead
Disadvantages of extendable hashing
Extra level of indirection to find desired record
Bucket address table may itself become very big (larger than memory)
Cannot allocate very large contiguous areas on disk either
Solution: B+-tree structure to locate desired record in bucket
address table
Changing size of bucket address table is an expensive operation
Linear hashing is an alternative mechanism
Allows incremental growth of its directory (equivalent to bucket
address table)
At the cost of more bucket overflows
Database System Concepts - 6th Edition 11.75 ©Silberschatz, Korth and Sudarshan
Comparison of Ordered Indexing and Hashing
Database System Concepts - 6th Edition 11.76 ©Silberschatz, Korth and Sudarshan
Bitmap Indices
Bitmap indices are a special type of index designed for efficient
querying on multiple keys
Records in a relation are assumed to be numbered sequentially
from, say, 0
Given a number n it must be easy to retrieve record n
Particularly easy if records are of fixed size
Applicable on attributes that take on a relatively small number
of distinct values
E.g. gender, country, state, …
E.g. income-level (income broken up into a small number of
levels such as 0-9999, 10000-19999, 20000-50000,
50000- infinity)
A bitmap is simply an array of bits
Database System Concepts - 6th Edition 11.77 ©Silberschatz, Korth and Sudarshan
Bitmap Indices (Cont.)
In its simplest form a bitmap index on an attribute has a bitmap for
each value of the attribute
Bitmap has as many bits as records
In a bitmap for value v, the bit for a record is 1 if the record has the
value v for the attribute, and is 0 otherwise
Database System Concepts - 6th Edition 11.78 ©Silberschatz, Korth and Sudarshan
Bitmap Indices (Cont.)
Bitmap indices are useful for queries on multiple attributes
not particularly useful for single attribute queries
Queries are answered using bitmap operations
Intersection (and)
Union (or)
Complementation (not)
Each operation takes two bitmaps of the same size and applies the
operation on corresponding bits to get the result bitmap
E.g. 100110 AND 110011 = 100010
100110 OR 110011 = 110111
NOT 100110 = 011001
Males with income level L1: 10010 AND 10100 = 10000
Can then retrieve required tuples.
Counting number of matching tuples is even faster
Database System Concepts - 6th Edition 11.79 ©Silberschatz, Korth and Sudarshan
Bitmap Indices (Cont.)
Bitmap indices generally very small compared with relation size
E.g. if record is 100 bytes, space for a single bitmap is 1/800 of space
used by relation.
If number of distinct attribute values is 8, bitmap is only 1% of
relation size
Deletion needs to be handled properly
Existence bitmap to note if there is a valid record at a record location
Needed for complementation
not(A=v): (NOT bitmap-A-v) AND ExistenceBitmap
Should keep bitmaps for all values, even null value
To correctly handle SQL null semantics for NOT(A=v):
intersect above result with (NOT bitmap-A-Null)
Database System Concepts - 6th Edition 11.80 ©Silberschatz, Korth and Sudarshan
Efficient Implementation of Bitmap Operations
Bitmaps are packed into words; a single word and (a basic CPU
instruction) computes and of 32 or 64 bits at once
E.g. 1-million-bit maps can be and-ed with just 31,250 instruction
Counting number of 1s can be done fast by a trick:
Use each byte to index into a precomputed array of 256 elements
each storing the count of 1s in the binary representation
Can use pairs of bytes to speed up further at a higher memory
cost
Add up the retrieved counts
Bitmaps can be used instead of Tuple-ID lists at leaf levels of
B+-trees, for values that have a large number of matching records
Worthwhile if > 1/64 of the records have that value, assuming a
tuple-id is 64 bits
Above technique merges benefits of bitmap and B+-tree indices
Database System Concepts - 6th Edition 11.81 ©Silberschatz, Korth and Sudarshan
Index Definition in SQL
Create an index
create index <index-name> on <relation-name>
(<attribute-list>)
E.g.: create index b-index on branch(branch_name)
Use create unique index to indirectly specify and enforce the
condition that the search key is a candidate key is a candidate key.
Not really required if SQL unique integrity constraint is supported
To drop an index
drop index <index-name>
Most database systems allow specification of type of index, and
clustering.
Database System Concepts - 6th Edition 11.82 ©Silberschatz, Korth and Sudarshan
End of Chapter
Database System Concepts - 6th Edition 11.84 ©Silberschatz, Korth and Sudarshan
Figure 11.15
Database System Concepts - 6th Edition 11.85 ©Silberschatz, Korth and Sudarshan
Partitioned Hashing
Hash values are split into segments that depend on each
attribute of the search-key.
(A1, A2, . . . , An) for n attribute search-key
Example: n = 2, for customer, search-key being
(customer-street, customer-city)
search-key value hash value
(Main, Harrison) 101 111
(Main, Brooklyn) 101 001
(Park, Palo Alto) 010 010
(Spring, Brooklyn) 001 001
(Alma, Palo Alto) 110 010
To answer equality query on single attribute, need to look up
multiple buckets. Similar in effect to grid files.
Database System Concepts - 6th Edition 11.86 ©Silberschatz, Korth and Sudarshan
Grid Files
Structure used to speed the processing of general multiple search-
key queries involving one or more comparison operators.
The grid file has a single grid array and one linear scale for each
search-key attribute. The grid array has number of dimensions
equal to number of search-key attributes.
Multiple cells of grid array can point to same bucket
To find the bucket for a search-key value, locate the row and column
of its cell using the linear scales and follow pointer
Database System Concepts - 6th Edition 11.87 ©Silberschatz, Korth and Sudarshan
Example Grid File for account
Database System Concepts - 6th Edition 11.88 ©Silberschatz, Korth and Sudarshan
Queries on a Grid File
A grid file on two attributes A and B can handle queries of all following
forms with reasonable efficiency
(a1 A a2)
(b1 B b2)
(a1 A a2 b1 B b2),.
E.g., to answer (a1 A a2 b1 B b2), use linear scales to find
corresponding candidate grid array cells, and look up all the buckets
pointed to from those cells.
Database System Concepts - 6th Edition 11.89 ©Silberschatz, Korth and Sudarshan
Grid Files (Cont.)
During insertion, if a bucket becomes full, new bucket can be created
if more than one cell points to it.
Idea similar to extendable hashing, but on multiple dimensions
If only one cell points to it, either an overflow bucket must be
created or the grid size must be increased
Linear scales must be chosen to uniformly distribute records across
cells.
Otherwise there will be too many overflow buckets.
Periodic re-organization to increase grid size will help.
But reorganization can be very expensive.
Space overhead of grid array can be high.
R-trees (Chapter 23) are an alternative
Database System Concepts - 6th Edition 11.90 ©Silberschatz, Korth and Sudarshan
Q.1) When is it preferable to use a dense index rather than a sparse index. Explain
your answer.
Q.3) What is the difference between primary index and secondary index.
Q.4) What are the various problems in static hashing and how these are resolved by
dynamic hashing.
Q.5) Consider two relations R(A, B) and S(B, C) stored as sequential files on A and B
respectively. There are two types of most frequently asked queries as follows.
query1: select * from R where R.A > a1 and R.A < a2
query2: select * from R, S where R.B = S.B
Q.6) Consider a dynamic hash structure where buckets can hold up to three records.
Initially the structure is empty. Then we insert the following records, in the order
below, where we indicate the hashed key in parenthesis (in binary):
a [010000]
b [011010]
c [111100]
d [001110]
e [010111]
f [011010]
g [101001]
h [010111]
i [000110]
j [101001]
a) Show the extensible hash structure after these records have been inserted.
Q.7) What is the order n of a B + -tree? Describe the structure of both internal and
leaf nodes of a B + -tree.
Q.8) How does multilevel indexing improve the efficiency of searching an index file?
Q.9) Why can we have at most one primary or clustering index on a file, but several
secondary indexes?
Q.11) Static hashing assumes that a large contiguous stretch of disk blocks can be
allocated to a static hash table. Suppose you can allocate only C contiguous blocks.
Suggest how to implement the hash table, if it can be much larger than C blocks.
Access to a block should still be efficient.
Q.12) A student file with student# as the key field includes records with the following
student# values: 23, 65, 37, 60, 46, 92, 48, 71, 56, 59, 18, 21, 10, 74, 78, 15, 16, 20,
24, 28, 39, 43, 47, 50, 69, 75, 8, 49, 33, 38. Suppose that the search field values are
inserted in the given order in a B + -tree of order n = 4. Show how the tree will
expand and what the final tree will look like.
Q.13) Suppose that the following search field values are deleted, in the given order,
from
the B+-tree of above exercise. Show how the tree will shrink and show the final tree.
The deleted values are 65, 75, 43, 18, 20, 92, 59, and 37.
Q.14) Use the Hash function: Kmod 9 and place the following keys in a hash
table:"12","44","13","88","23","94","11","45","19","63","31","39","20","16","5"
Q.15) Use the Hash function: Kmod17 and place the following keys in a hash
table:"12","44","13","88","23","94","11","45","19","63","31","39","20","16","5"
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Chapter 12: Query Processing
Overview
Measures of Query Cost
Selection Operation
Sorting
Join Operation
Other Operations
Evaluation of Expressions
Database System Concepts - 6th Edition 12.2 ©Silberschatz, Korth and Sudarshan
Basic Steps in Query Processing
1. Parsing and translation
2. Optimization
3. Evaluation
Database System Concepts - 6th Edition 12.3 ©Silberschatz, Korth and Sudarshan
Basic Steps in Query Processing
(Cont.)
Parsing and translation
translate the query into its internal form. This is then
translated into relational algebra.
Parser checks syntax, verifies relations
Evaluation
The query-execution engine takes a query-evaluation plan,
executes that plan, and returns the answers to the query.
Database System Concepts - 6th Edition 12.4 ©Silberschatz, Korth and Sudarshan
Basic Steps in Query Processing :
Optimization
A relational algebra expression may have many equivalent
expressions
E.g., salary75000(salary(instructor)) is equivalent to
salary(salary75000(instructor))
Each relational algebra operation can be evaluated using one of
several different algorithms
Correspondingly, a relational-algebra expression can be
evaluated in many ways.
Annotated expression specifying detailed evaluation strategy is
called an evaluation-plan.
E.g., can use an index on salary to find instructors with salary <
75000,
or can perform complete relation scan and discard instructors
with salary 75000
Database System Concepts - 6th Edition 12.5 ©Silberschatz, Korth and Sudarshan
Basic Steps: Optimization (Cont.)
Query Optimization: Amongst all equivalent evaluation plans
choose the one with lowest cost.
Cost is estimated using statistical information from the
database catalog
e.g. number of tuples in each relation, size of tuples, etc.
In this chapter we study
How to measure query costs
Algorithms for evaluating relational algebra operations
How to combine algorithms for individual operations in
order to evaluate a complete expression
In Chapter 14
We study how to optimize queries, that is, how to find an
evaluation plan with lowest estimated cost
Database System Concepts - 6th Edition 12.6 ©Silberschatz, Korth and Sudarshan
Measures of Query Cost
Cost is generally measured as total elapsed time for answering
query
Many factors contribute to time cost
disk accesses, CPU, or even network communication
Typically disk access is the predominant cost, and is also
relatively easy to estimate. Measured by taking into account
Number of seeks * average-seek-cost
Number of blocks read * average-block-read-cost
Number of blocks written * average-block-write-cost
Cost to write a block is greater than cost to read a block
– data is read back after being written to ensure that the
write was successful
Database System Concepts - 6th Edition 12.7 ©Silberschatz, Korth and Sudarshan
Measures of Query Cost (Cont.)
For simplicity we just use the number of block transfers from disk
and the number of seeks as the cost measures
tT – time to transfer one block
tS – time for one seek
Cost for b block transfers plus S seeks
b * tT + S * tS
We ignore CPU costs for simplicity
Real systems do take CPU cost into account
We do not include cost to writing output to disk in our cost formulae
Database System Concepts - 6th Edition 12.8 ©Silberschatz, Korth and Sudarshan
Measures of Query Cost (Cont.)
Database System Concepts - 6th Edition 12.9 ©Silberschatz, Korth and Sudarshan
Selection Operation
File scan
Algorithm A1 (linear search). Scan each file block and test all
records to see whether they satisfy the selection condition.
Cost estimate = br block transfers + 1 seek
br denotes number of blocks containing records from relation r
If selection is on a key attribute, can stop on finding record
cost = (br /2) block transfers + 1 seek
Linear search can be applied regardless of
selection condition or
ordering of records in the file, or
availability of indices
Note: binary search generally does not make sense since data is not
stored consecutively
except when there is an index available,
and binary search requires more seeks than index search
Database System Concepts - 6th Edition 12.10 ©Silberschatz, Korth and Sudarshan
Selections Using Indices
Database System Concepts - 6th Edition 12.11 ©Silberschatz, Korth and Sudarshan
Selections Using Indices
Database System Concepts - 6th Edition 12.12 ©Silberschatz, Korth and Sudarshan
Selections Involving Comparisons
Can implement selections of the form AV (r) or A V(r) by using
a linear file scan,
or by using indices in the following ways:
A5 (primary index, comparison). (Relation is sorted on A)
For A V(r) use index to find first tuple v and scan relation
sequentially from there
For AV (r) just scan relation sequentially till first tuple > v; do not
use index
A6 (secondary index, comparison).
For A V(r) use index to find first index entry v and scan index
sequentially from there, to find pointers to records.
For AV (r) just scan leaf pages of index finding pointers to
records, till first entry > v
In either case, retrieve records that are pointed to
– requires an I/O for each record
– Linear file scan may be cheaper
Database System Concepts - 6th Edition 12.13 ©Silberschatz, Korth and Sudarshan
Implementation of Complex Selections
Disjunction:1 2 . . . n (r).
A10 (disjunctive selection by union of identifiers).
Applicable if all conditions have available indices.
Otherwise use linear scan.
Use corresponding index for each condition, and take union
of all the obtained sets of record pointers.
Then fetch records from file
Negation: (r)
Use linear scan on file
If very few records satisfy , and an index is applicable to
Find satisfying records using index and fetch from file
Database System Concepts - 6th Edition 12.15 ©Silberschatz, Korth and Sudarshan
Sorting
We may build an index on the relation, and then use the index
to read the relation in sorted order. May lead to one disk block
access for each tuple.
For relations that fit in memory, techniques like quicksort can
be used. For relations that don’t fit in memory, external
sort-merge is a good choice.
Database System Concepts - 6th Edition 12.16 ©Silberschatz, Korth and Sudarshan
External Sort-Merge
Let M denote memory size (in pages).
1. Create sorted runs. Let i be 0 initially.
Repeatedly do the following till the end of the relation:
(a) Read M blocks of relation into memory
(b) Sort the in-memory blocks
(c) Write sorted data to run Ri; increment i.
Let the final value of i be N
2. Merge the runs (next slide)…..
Database System Concepts - 6th Edition 12.17 ©Silberschatz, Korth and Sudarshan
External Sort-Merge (Cont.)
2. Merge the runs (N-way merge). We assume (for now) that N <
M.
1. Use N blocks of memory to buffer input runs, and 1 block to
buffer output. Read the first block of each run into its buffer
page
2. repeat
1. Select the first record (in sort order) among all buffer
pages
2. Write the record to the output buffer. If the output buffer
is full write it to disk.
3. Delete the record from its input buffer page.
If the buffer page becomes empty then
read the next block (if any) of the run into the buffer.
3. until all input buffer pages are empty:
Database System Concepts - 6th Edition 12.18 ©Silberschatz, Korth and Sudarshan
External Sort-Merge (Cont.)
Database System Concepts - 6th Edition 12.19 ©Silberschatz, Korth and Sudarshan
Example: External Sorting Using Sort-Merge
a 19 a 19
g 24 d 31 a 14
b 14
a 19 g 24 a 19
c 33
d 31 b 14
b 14 d 31
c 33 c 33
c 33 e 16
b 14 d 7
e 16 g 24
e 16 d 21
r 16 d 21 d 31
a 14
d 21 m 3 e 16
d 7
m 3 r 16 g 24
d 21
p 2 m 3
m 3
d 7 a 14 p 2
p 2
a 14 d 7 r 16
r 16
p 2
initial sorted
relation runs runs output
create merge merge
runs pass–1 pass–2
Database System Concepts - 6th Edition 12.20 ©Silberschatz, Korth and Sudarshan
External Merge Sort (Cont.)
Cost analysis:
1 block per run leads to too many seeks during merge
Instead use bb buffer blocks per run
read/write bb blocks at a time
Can merge M/bb–1 runs in one pass
Total number of merge passes required: log M/bb–1(br/M).
Block transfers for initial run creation as well as in each pass is 2br
for final pass, we don’t count write cost
– we ignore final write cost for all operations since the output
of an operation may be sent to the parent operation without
being written to disk
Thus total number of block transfers for external sorting:
br ( 2 log M/bb–1 (br / M) + 1)
Database System Concepts - 6th Edition 12.21 ©Silberschatz, Korth and Sudarshan
External Merge Sort (Cont.)
Cost of seeks
During run generation: one seek to read each run and one
seek to write each run
2 br / M
During the merge phase
Need 2 br / bb seeks for each merge pass
– except the final one which does not require a write
Total number of seeks:
2 br / M + br / bb (2 logM/bb–1(br / M) -1)
Database System Concepts - 6th Edition 12.22 ©Silberschatz, Korth and Sudarshan
Join Operation
Several different algorithms to implement joins
Nested-loop join
Block nested-loop join
Indexed nested-loop join
Merge-join
Hash-join
Choice based on cost estimate
Examples use the following information
Number of records of student: 5,000 takes: 10,000
Number of blocks of student: 100 takes: 400
Database System Concepts - 6th Edition 12.23 ©Silberschatz, Korth and Sudarshan
Nested-Loop Join
Database System Concepts - 6th Edition 12.24 ©Silberschatz, Korth and Sudarshan
Nested-Loop Join (Cont.)
In the worst case, if there is enough memory only to hold one block of each
relation, the estimated cost is
nr bs + br block transfers, plus
nr + br seeks
If the smaller relation fits entirely in memory, use that as the inner relation.
Reduces cost to br + bs block transfers and 2 seeks
Assuming worst case memory availability cost estimate is
with student as outer relation:
5000 400 + 100 = 2,000,100 block transfers,
5000 + 100 = 5100 seeks
with takes as the outer relation
10000 100 + 400 = 1,000,400 block transfers and 10,400 seeks
If smaller relation (student) fits entirely in memory, the cost estimate will be
500 block transfers.
Block nested-loops algorithm (next slide) is preferable.
Database System Concepts - 6th Edition 12.25 ©Silberschatz, Korth and Sudarshan
Block Nested-Loop Join
Variant of nested-loop join in which every block of inner
relation is paired with every block of outer relation.
for each block Br of r do begin
for each block Bs of s do begin
for each tuple tr in Br do begin
for each tuple ts in Bs do begin
Check if (tr,ts) satisfy the join condition
if they do, add tr • ts to the result.
end
end
end
end
Database System Concepts - 6th Edition 12.26 ©Silberschatz, Korth and Sudarshan
Block Nested-Loop Join (Cont.)
For each tuple tr in the outer relation r, use the index to look up
tuples in s that satisfy the join condition with tuple tr.
Worst case: buffer has space for only one page of r, and, for each
tuple in r, we perform an index lookup on s.
Cost of the join: br (tT + tS) + nr c
Where c is the cost of traversing index and fetching all matching s
tuples for one tuple or r
c can be estimated as cost of a single selection on s using the join
condition.
If indices are available on join attributes of both r and s,
use the relation with fewer tuples as the outer relation.
Database System Concepts - 6th Edition 12.28 ©Silberschatz, Korth and Sudarshan
Example of Nested-Loop Join Costs
Compute student takes, with student as the outer relation.
Let takes have a primary B+-tree index on the attribute ID, which
contains 20 entries in each index node.
Since takes has 10,000 tuples, the height of the tree is 4, and one
more access is needed to find the actual data
student has 5000 tuples
Cost of block nested loops join
400*100 + 100 = 40,100 block transfers + 2 * 100 = 200 seeks
assuming worst case memory
may be significantly less with more memory
Cost of indexed nested loops join
100 + 5000 * 5 = 25,100 block transfers and seeks.
CPU cost likely to be less than that for block nested loops join
Database System Concepts - 6th Edition 12.29 ©Silberschatz, Korth and Sudarshan
Merge-Join
1. Sort both relations on their join attribute (if not already sorted on
the join attributes).
2. Merge the sorted relations to join them
1. Join step is similar to the merge stage of the sort-merge
algorithm.
2. Main difference is handling of duplicate values in join
attribute — every pair with same value on join attribute must
be matched
3. Detailed algorithm in book
Database System Concepts - 6th Edition 12.30 ©Silberschatz, Korth and Sudarshan
Merge-Join (Cont.)
Database System Concepts - 6th Edition 12.31 ©Silberschatz, Korth and Sudarshan
Hash-Join
Applicable for equi-joins and natural joins.
A hash function h is used to partition tuples of both relations
h maps JoinAttrs values to {0, 1, ..., n}, where JoinAttrs denotes the
common attributes of r and s used in the natural join.
r0, r1, . . ., rn denote partitions of r tuples
Each tuple tr r is put in partition ri where i = h(tr [JoinAttrs]).
r0,, r1. . ., rn denotes partitions of s tuples
Database System Concepts - 6th Edition 12.32 ©Silberschatz, Korth and Sudarshan
Hash-Join (Cont.)
Database System Concepts - 6th Edition 12.33 ©Silberschatz, Korth and Sudarshan
Hash-Join (Cont.)
r tuples in ri need only to be compared with s tuples in si
Need not be compared with s tuples in any other partition,
since:
an r tuple and an s tuple that satisfy the join condition
will have the same value for the join attributes.
If that value is hashed to some value i, the r tuple has
to be in ri and the s tuple in si.
Database System Concepts - 6th Edition 12.34 ©Silberschatz, Korth and Sudarshan
Hash-Join Algorithm
Relation s is called the build input and r is called the probe input.
Database System Concepts - 6th Edition 12.35 ©Silberschatz, Korth and Sudarshan
Hash-Join algorithm (Cont.)
The value n and the hash function h is chosen such that each
si should fit in memory.
Typically n is chosen as bs/M * f where f is a “fudge
factor”, typically around 1.2
The probe relation partitions si need not fit in memory
Recursive partitioning required if number of partitions n is
greater than number of pages M of memory.
instead of partitioning n ways, use M – 1 partitions for s
Further partition the M – 1 partitions using a different hash
function
Use same partitioning method on r
Rarely required: e.g., with block size of 4 KB, recursive
partitioning not needed for relations of < 1GB with memory
size of 2MB, or relations of < 36 GB with memory of 12 MB
Database System Concepts - 6th Edition 12.36 ©Silberschatz, Korth and Sudarshan
Handling of Overflows
Database System Concepts - 6th Edition 12.37 ©Silberschatz, Korth and Sudarshan
Cost of Hash-Join
Database System Concepts - 6th Edition 12.38 ©Silberschatz, Korth and Sudarshan
Example of Cost of Hash-Join
instructor teaches
Database System Concepts - 6th Edition 12.39 ©Silberschatz, Korth and Sudarshan
Hybrid Hash–Join
Useful when memory sized are relatively large, and the build
input is bigger than memory.
Main feature of hybrid hash join:
Keep the first partition of the build relation in memory.
E.g. With memory size of 25 blocks, instructor can be partitioned
into five partitions, each of size 20 blocks.
Division of memory:
The first partition occupies 20 blocks of memory
1 block is used for input, and 1 block each for buffering the other
4 partitions.
teaches is similarly partitioned into five partitions each of size 80
the first is used right away for probing, instead of being written out
Cost of 3(80 + 320) + 20 +80 = 1300 block transfers for
hybrid hash join, instead of 1500 with plain hash-join.
Hybrid hash-join most useful if M >> bs
Database System Concepts - 6th Edition 12.40 ©Silberschatz, Korth and Sudarshan
Complex Joins
r 1 2 ... n s
Either use nested loops/block nested loops, or
Compute as the union of the records in individual joins r i s:
(r 1 s) (r 2 s) . . . (r n s)
Database System Concepts - 6th Edition 12.41 ©Silberschatz, Korth and Sudarshan
Other Operations
Database System Concepts - 6th Edition 12.42 ©Silberschatz, Korth and Sudarshan
Other Operations : Aggregation
Aggregation can be implemented in a manner similar to duplicate
elimination.
Sorting or hashing can be used to bring tuples in the same
group together, and then the aggregate functions can be
applied on each group.
Optimization: combine tuples in the same group during run
generation and intermediate merges, by computing partial
aggregate values
For count, min, max, sum: keep aggregate values on tuples
found so far in the group.
– When combining partial aggregate for count, add up the
aggregates
For avg, keep sum and count, and divide sum by count at
the end
Database System Concepts - 6th Edition 12.43 ©Silberschatz, Korth and Sudarshan
Other Operations : Set Operations
Set operations (, and ): can either use variant of merge-join
after sorting, or variant of hash-join.
E.g., Set operations using hashing:
1. Partition both relations using the same hash function
2. Process each partition i as follows.
1. Using a different hashing function, build an in-memory hash
index on ri.
2. Process si as follows
r s:
1. Add tuples in si to the hash index if they are not
already in it.
2. At end of si add the tuples in the hash index to the
result.
Database System Concepts - 6th Edition 12.44 ©Silberschatz, Korth and Sudarshan
Other Operations : Set Operations
Database System Concepts - 6th Edition 12.45 ©Silberschatz, Korth and Sudarshan
Other Operations : Outer Join
Outer join can be computed either as
A join followed by addition of null-padded non-participating
tuples.
by modifying the join algorithms.
Modifying merge join to compute r s
In r s, non participating tuples are those in r – R(r s)
Modify merge-join to compute r s:
During merging, for every tuple tr from r that do not match
any tuple in s, output tr padded with nulls.
Right outer-join and full outer-join can be computed similarly.
Database System Concepts - 6th Edition 12.46 ©Silberschatz, Korth and Sudarshan
Other Operations : Outer Join
Database System Concepts - 6th Edition 12.47 ©Silberschatz, Korth and Sudarshan
Evaluation of Expressions
So far: we have seen algorithms for individual operations
Alternatives for evaluating an entire expression tree
Materialization: generate results of an expression whose
inputs are relations or are already computed, materialize
(store) it on disk. Repeat.
Pipelining: pass on tuples to parent operations even as an
operation is being executed
We study above alternatives in more detail
Database System Concepts - 6th Edition 12.48 ©Silberschatz, Korth and Sudarshan
Materialization
Materialized evaluation: evaluate one operation at a time,
starting at the lowest-level. Use intermediate results materialized
into temporary relations to evaluate next-level operations.
E.g., in figure below, compute and store
building"Watson" (department)
then compute the store its join with instructor, and finally compute
the projection on name.
Database System Concepts - 6th Edition 12.49 ©Silberschatz, Korth and Sudarshan
Materialization (Cont.)
Materialized evaluation is always applicable
Cost of writing results to disk and reading them back can be
quite high
Our cost formulas for operations ignore cost of writing
results to disk, so
Overall cost = Sum of costs of individual operations +
cost of writing intermediate results to disk
Double buffering: use two output buffers for each operation,
when one is full write it to disk while the other is getting filled
Allows overlap of disk writes with computation and reduces
execution time
Database System Concepts - 6th Edition 12.50 ©Silberschatz, Korth and Sudarshan
Pipelining
Database System Concepts - 6th Edition 12.51 ©Silberschatz, Korth and Sudarshan
Pipelining (Cont.)
Database System Concepts - 6th Edition 12.52 ©Silberschatz, Korth and Sudarshan
Pipelining (Cont.)
Implementation of demand-driven pipelining
Each operation is implemented as an iterator implementing the
following operations
open()
– E.g. file scan: initialize file scan
» state: pointer to beginning of file
– E.g.merge join: sort relations;
» state: pointers to beginning of sorted relations
next()
– E.g. for file scan: Output next tuple, and advance and store
file pointer
– E.g. for merge join: continue with merge from earlier state
till
next output tuple is found. Save pointers as iterator state.
close()
Database System Concepts - 6th Edition 12.53 ©Silberschatz, Korth and Sudarshan
Evaluation Algorithms for Pipelining
Some algorithms are not able to output results even as they get
input tuples
E.g. merge join, or hash join
intermediate results written to disk and then read back
Algorithm variants to generate (at least some) results on the fly, as
input tuples are read in
E.g. hybrid hash join generates output tuples even as probe relation
tuples in the in-memory partition (partition 0) are read in
Double-pipelined join technique: Hybrid hash join, modified to
buffer partition 0 tuples of both relations in-memory, reading them as
they become available, and output results of any matches between
partition 0 tuples
When a new r0 tuple is found, match it with existing s 0 tuples,
output matches, and save it in r0
Symmetrically for s0 tuples
Database System Concepts - 6th Edition 12.54 ©Silberschatz, Korth and Sudarshan
End of Chapter
Database System Concepts - 6th Edition 12.56 ©Silberschatz, Korth and Sudarshan
Selection Operation (Cont.)
Old-A2 (binary search). Applicable if selection is an equality
comparison on the attribute on which file is ordered.
Assume that the blocks of a relation are stored contiguously
Cost estimate (number of disk blocks to be scanned):
cost of locating the first tuple by a binary search on the
blocks
– log2(br) * (tT + tS)
If there are multiple records satisfying selection
– Add transfer cost of the number of blocks containing
records that satisfy selection condition
– Will see how to estimate this cost in Chapter 13
Database System Concepts - 6th Edition 12.57 ©Silberschatz, Korth and Sudarshan
Chapter 13: Query Optimization
Database System Concepts - 6th Edition 1.2 ©Silberschatz, Korth and Sudarshan
Introduction
Alternative ways of evaluating a given query
Equivalent expressions
Different algorithms for each operation
Database System Concepts - 6th Edition 1.3 ©Silberschatz, Korth and Sudarshan
Introduction (Cont.)
Find out how to view query execution plans on your favorite database
Database System Concepts - 6th Edition 1.4 ©Silberschatz, Korth and Sudarshan
Introduction (Cont.)
Database System Concepts - 6th Edition 1.5 ©Silberschatz, Korth and Sudarshan
Generating Equivalent Expressions
Database System Concepts - 6th Edition 1.7 ©Silberschatz, Korth and Sudarshan
Equivalence Rules
Database System Concepts - 6th Edition 1.8 ©Silberschatz, Korth and Sudarshan
Equivalence Rules (Cont.)
5. Theta-join operations (and natural joins) are commutative.
E1 E2 = E2 E1
6. (a) Natural join operations are associative:
(E1 E2) E3 = E1 (E2 E3)
Database System Concepts - 6th Edition 1.9 ©Silberschatz, Korth and Sudarshan
Pictorial Depiction of Equivalence Rules
Database System Concepts - 6th Edition 1.10 ©Silberschatz, Korth and Sudarshan
Equivalence Rules (Cont.)
7. The selection operation distributes over the theta join operation
under the following two conditions:
(a) When all the attributes in 0 involve only the attributes of one
of the expressions (E1) being joined.
Database System Concepts - 6th Edition 1.11 ©Silberschatz, Korth and Sudarshan
Equivalence Rules (Cont.)
8. The projection operation distributes over the theta join operation
as follows:
(a) if involves only attributes from L1 L2:
L1 L2 ( E1 E2 ) ( L1 ( E1 )) ( L2 ( E2 ))
Database System Concepts - 6th Edition 1.12 ©Silberschatz, Korth and Sudarshan
Equivalence Rules (Cont.)
Database System Concepts - 6th Edition 1.14 ©Silberschatz, Korth and Sudarshan
Transformation Example: Pushing Selections
Database System Concepts - 6th Edition 1.15 ©Silberschatz, Korth and Sudarshan
Example with Multiple Transformations
Query: Find the names of all instructors in the Music department
who have taught a course in 2009, along with the titles of the
courses that they taught
name, title(dept_name= “Music”year = 2009
(instructor (teaches course_id, title (course))))
Transformation using join associatively (Rule 6a):
name, title(dept_name= “Music”gear = 2009
((instructor teaches) course_id, title (course)))
Second form provides an opportunity to apply the “perform
selections early” rule, resulting in the subexpression
dept_name = “Music” (instructor) year = 2009 (teaches)
Database System Concepts - 6th Edition 1.16 ©Silberschatz, Korth and Sudarshan
Multiple Transformations (Cont.)
Database System Concepts - 6th Edition 1.17 ©Silberschatz, Korth and Sudarshan
Transformation Example: Pushing Projections
Database System Concepts - 6th Edition 1.18 ©Silberschatz, Korth and Sudarshan
Join Ordering Example
For all relations r1, r2, and r3,
(r1 r2) r3 = r1 (r2 r3 )
(Join Associativity)
If r2 r3 is quite large and r1 r2 is small, we choose
(r1 r2) r3
so that we compute and store a smaller temporary relation.
Database System Concepts - 6th Edition 1.19 ©Silberschatz, Korth and Sudarshan
Join Ordering Example (Cont.)
Consider the expression
name, title(dept_name= “Music” (instructor) teaches)
course_id, title (course))))
Could compute teaches course_id, title (course) first, and
join result with
dept_name= “Music” (instructor)
but the result of the first join is likely to be a large relation.
Only a small fraction of the university’s instructors are likely to
be from the Music department
it is better to compute
dept_name= “Music” (instructor) teaches
first.
Database System Concepts - 6th Edition 1.20 ©Silberschatz, Korth and Sudarshan
Enumeration of Equivalent Expressions
Database System Concepts - 6th Edition 1.21 ©Silberschatz, Korth and Sudarshan
Implementing Transformation Based
Optimization
Space requirements reduced by sharing common sub-expressions:
when E1 is generated from E2 by an equivalence rule, usually only the top
level of the two are different, subtrees below are the same and can be
shared using pointers
E.g. when applying join commutativity
E1 E2
Database System Concepts - 6th Edition 1.22 ©Silberschatz, Korth and Sudarshan
Cost Estimation
Cost of each operator computed as described in Chapter 12
Need statistics of input relations
E.g. number of tuples, sizes of tuples
Inputs can be results of sub-expressions
Need to estimate statistics of expression results
To do so, we require additional statistics
E.g. number of distinct values for an attribute
More on cost estimation later
Database System Concepts - 6th Edition 1.23 ©Silberschatz, Korth and Sudarshan
Choice of Evaluation Plans
Must consider the interaction of evaluation techniques when choosing
evaluation plans
choosing the cheapest algorithm for each operation independently
may not yield best overall algorithm. E.g.
merge-join may be costlier than hash-join, but may provide a
sorted output which reduces the cost for an outer level
aggregation.
nested-loop join may provide opportunity for pipelining
Practical query optimizers incorporate elements of the following two
broad approaches:
1. Search all the plans and choose the best plan in a
cost-based fashion.
2. Uses heuristics to choose a plan.
Database System Concepts - 6th Edition 1.24 ©Silberschatz, Korth and Sudarshan
Cost-Based Optimization
Consider finding the best join-order for r1 r2 . . . rn .
There are (2(n – 1))!/(n – 1)! different join orders for above expression.
With n = 7, the number is 665280, with n = 10, the number is greater
than 176 billion!
No need to generate all the join orders. Using dynamic programming,
the least-cost join order for any subset of
{r1, r2, . . . rn} is computed only once and stored for future use.
Database System Concepts - 6th Edition 1.25 ©Silberschatz, Korth and Sudarshan
Dynamic Programming in Optimization
Database System Concepts - 6th Edition 1.26 ©Silberschatz, Korth and Sudarshan
Join Order Optimization Algorithm
procedure findbestplan(S)
if (bestplan[S].cost )
return bestplan[S]
// else bestplan[S] has not been computed earlier, compute it now
if (S contains only 1 relation)
set bestplan[S].plan and bestplan[S].cost based on the best way
of accessing S /* Using selections on S and indices on S */
else for each non-empty subset S1 of S such that S1 S
P1= findbestplan(S1)
P2= findbestplan(S - S1)
A = best algorithm for joining results of P1 and P2
cost = P1.cost + P2.cost + cost of A
if cost < bestplan[S].cost
bestplan[S].cost = cost
bestplan[S].plan = “execute P1.plan; execute P2.plan;
join results of P1 and P2 using A”
return bestplan[S]
* Some modifications to allow indexed nested loops joins on relations that have
selections (see book)
Database System Concepts - 6th Edition 1.27 ©Silberschatz, Korth and Sudarshan
Left Deep Join Trees
In left-deep join trees, the right-hand-side input for each join is
a relation, not the result of an intermediate join.
Database System Concepts - 6th Edition 1.28 ©Silberschatz, Korth and Sudarshan
Cost of Optimization
With dynamic programming time complexity of optimization with bushy
trees is O(3n).
With n = 10, this number is 59000 instead of 176 billion!
Space complexity is O(2n)
To find best left-deep join tree for a set of n relations:
Consider n alternatives with one relation as right-hand side input
and the other relations as left-hand side input.
Modify optimization algorithm:
Replace “for each non-empty subset S1 of S such that S1 S”
By: for each relation r in S
let S1 = S – r .
If only left-deep trees are considered, time complexity of finding best join
order is O(n 2n)
Space complexity remains at O(2n)
Cost-based optimization is expensive, but worthwhile for queries on
large datasets (typical queries have small n, generally < 10)
Database System Concepts - 6th Edition 1.29 ©Silberschatz, Korth and Sudarshan
Interesting Sort Orders
Database System Concepts - 6th Edition 1.30 ©Silberschatz, Korth and Sudarshan
Cost Based Optimization with Equivalence
Rules
Physical equivalence rules allow logical query plan to be converted
to physical query plan specifying what algorithms are used for each
operation.
Efficient optimizer based on equivalent rules depends on
A space efficient representation of expressions which avoids
making multiple copies of subexpressions
Efficient techniques for detecting duplicate derivations of
expressions
A form of dynamic programming based on memoization, which
stores the best plan for a subexpression the first time it is
optimized, and reuses in on repeated optimization calls on same
subexpression
Cost-based pruning techniques that avoid generating all plans
Pioneered by the Volcano project and implemented in the SQL Server
optimizer
Database System Concepts - 6th Edition 1.31 ©Silberschatz, Korth and Sudarshan
Heuristic Optimization
Cost-based optimization is expensive, even with dynamic programming.
Systems may use heuristics to reduce the number of choices that must
be made in a cost-based fashion.
Heuristic optimization transforms the query-tree by using a set of rules
that typically (but not in all cases) improve execution performance:
Perform selection early (reduces the number of tuples)
Perform projection early (reduces the number of attributes)
Perform most restrictive selection and join operations (i.e. with
smallest result size) before other similar operations.
Some systems use only heuristics, others combine heuristics with
partial cost-based optimization.
Database System Concepts - 6th Edition 1.32 ©Silberschatz, Korth and Sudarshan
Structure of Query Optimizers
Many optimizers considers only left-deep join orders.
Plus heuristics to push selections and projections down the query
tree
Reduces optimization complexity and generates plans amenable to
pipelined evaluation.
Heuristic optimization used in some versions of Oracle:
Repeatedly pick “best” relation to join next
Starting from each of n starting points. Pick best among these
Intricacies of SQL complicate query optimization
E.g. nested subqueries
Database System Concepts - 6th Edition 1.33 ©Silberschatz, Korth and Sudarshan
Structure of Query Optimizers (Cont.)
Some query optimizers integrate heuristic selection and the generation of
alternative access plans.
Frequently used approach
heuristic rewriting of nested block structure and aggregation
followed by cost-based join-order optimization for each block
Some optimizers (e.g. SQL Server) apply transformations to entire query
and do not depend on block structure
Optimization cost budget to stop optimization early (if cost of plan is
less than cost of optimization)
Plan caching to reuse previously computed plan if query is resubmitted
Even with different constants in query
Even with the use of heuristics, cost-based query optimization imposes a
substantial overhead.
But is worth it for expensive queries
Optimizers often use simple heuristics for very cheap queries, and
perform exhaustive enumeration for more expensive queries
Database System Concepts - 6th Edition 1.34 ©Silberschatz, Korth and Sudarshan
Statistics for Cost Estimation
nr ùú
é
br = ê
f r úú
ê
ê
Database System Concepts - 6th Edition 1.36 ©Silberschatz, Korth and Sudarshan
Histograms
Histogram on attribute age of relation person
50
40
frequency
30
20
10
Equi-width histograms
Equi-depth histograms
Database System Concepts - 6th Edition 1.37 ©Silberschatz, Korth and Sudarshan
Selection Size Estimation
A=v(r)
nr / V(A,r) : number of records that will satisfy the selection
Equality condition on a key attribute: size estimate = 1
AV(r) (case of A V(r) is symmetric)
Let c denote the estimated number of tuples satisfying the condition.
If min(A,r) and max(A,r) are available in catalog
c = 0 if v < min(A,r)
v - min( A, r )
c= nr .
max( A, r ) - min( A, r )
Database System Concepts - 6th Edition 1.38 ©Silberschatz, Korth and Sudarshan
Size Estimation of Complex Selections
Database System Concepts - 6th Edition 1.39 ©Silberschatz, Korth and Sudarshan
Join Operation: Running Example
Running example:
student takes
Catalog information for join examples:
nstudent = 5,000.
fstudent = 50, which implies that
bstudent =5000/50 = 100.
ntakes = 10000.
ftakes = 25, which implies that
btakes = 10000/25 = 400.
V(ID, takes) = 2500, which implies that on average, each student
who has taken a course has taken 4 courses.
Attribute ID in takes is a foreign key referencing student.
V(ID, student) = 5000 (primary key!)
Database System Concepts - 6th Edition 1.40 ©Silberschatz, Korth and Sudarshan
Estimation of the Size of Joins
The Cartesian product r x s contains nr .ns tuples; each tuple occupies
sr + ss bytes.
If R S = , then r s is the same as r x s.
If R S is a key for R, then a tuple of s will join with at most one tuple
from r
therefore, the number of tuples in r s is no greater than the
number of tuples in s.
If R S in S is a foreign key in S referencing R, then the number of
tuples in r s is exactly the same as the number of tuples in s.
The case for R S being a foreign key referencing S is
symmetric.
In the example query student takes, ID in takes is a foreign key
referencing student
hence, the result has exactly ntakes tuples, which is 10000
Database System Concepts - 6th Edition 1.41 ©Silberschatz, Korth and Sudarshan
Estimation of the Size of Joins (Cont.)
If R S = {A} is not a key for R or S.
If we assume that every tuple t in R produces tuples in R S, the
number of tuples in R S is estimated to be:
nr * ns
V ( A, s )
If the reverse is true, the estimate obtained will be:
nr * ns
V ( A, r )
The lower of these two estimates is probably the more accurate one.
Can improve on above if histograms are available
Use formula similar to above, for each cell of histograms on the
two relations
Database System Concepts - 6th Edition 1.42 ©Silberschatz, Korth and Sudarshan
Estimation of the Size of Joins (Cont.)
Database System Concepts - 6th Edition 1.43 ©Silberschatz, Korth and Sudarshan
Size Estimation for Other Operations
Projection: estimated size of A(r) = V(A,r)
Aggregation : estimated size of AgF(r) = V(A,r)
Set operations
For unions/intersections of selections on the same relation:
rewrite and use size estimate for selections
E.g. 1 (r) 2 (r) can be rewritten as 1 ˅ 2 (r)
For operations on different relations:
estimated size of r s = size of r + size of s.
estimated size of r s = minimum size of r and size of s.
estimated size of r – s = r.
All the three estimates may be quite inaccurate, but provide
upper bounds on the sizes.
Database System Concepts - 6th Edition 1.44 ©Silberschatz, Korth and Sudarshan
Size Estimation (Cont.)
Outer join:
Estimated size of r s = size of r s + size of r
Case of right outer join is symmetric
Estimated size of r s = size of r s + size of r + size of s
Database System Concepts - 6th Edition 1.45 ©Silberschatz, Korth and Sudarshan
Estimation of Number of Distinct Values
Selections: (r)
If forces A to take a specified value: V(A, (r)) = 1.
e.g., A = 3
If forces A to take on one of a specified set of values:
V(A, (r)) = number of specified values.
(e.g., (A = 1 V A = 3 V A = 4 )),
If the selection condition is of the form A op r
estimated V(A, (r)) = V(A.r) * s
where s is the selectivity of the selection.
In all the other cases: use approximate estimate of
min(V(A,r), n (r) )
More accurate estimate can be got using probability theory, but
this one works fine generally
Database System Concepts - 6th Edition 1.46 ©Silberschatz, Korth and Sudarshan
Estimation of Distinct Values (Cont.)
Joins: r s
If all attributes in A are from r
estimated V(A, r s) = min (V(A,r), n r s)
If A contains attributes A1 from r and A2 from s, then estimated
V(A,r s) =
min(V(A1,r)*V(A2 – A1,s), V(A1 – A2,r)*V(A2,s), nr s)
More accurate estimate can be got using probability theory, but
this one works fine generally
Database System Concepts - 6th Edition 1.47 ©Silberschatz, Korth and Sudarshan
Estimation of Distinct Values (Cont.)
Estimation of distinct values are straightforward for projections.
They are the same in A (r) as in r.
The same holds for grouping attributes of aggregation.
For aggregated values
For min(A) and max(A), the number of distinct values can be
estimated as min(V(A,r), V(G,r)) where G denotes grouping attributes
For other aggregates, assume all values are distinct, and use V(G,r)
Database System Concepts - 6th Edition 1.48 ©Silberschatz, Korth and Sudarshan
Additional Optimization Techniques
Nested Subqueries
Materialized Views
Database System Concepts - 6th Edition 1.50 ©Silberschatz, Korth and Sudarshan
Optimizing Nested Subqueries (Cont.)
Correlated evaluation may be quite inefficient since
a large number of calls may be made to the nested query
there may be unnecessary random I/O as a result
SQL optimizers attempt to transform nested subqueries to joins where
possible, enabling use of efficient join techniques
E.g.: earlier nested query can be rewritten as
select name
from instructor, teaches
where instructor.ID = teaches.ID and teaches.year = 2007
Note: the two queries generate different numbers of duplicates (why?)
teaches can have duplicate IDs
Can be modified to handle duplicates correctly as we will see
In general, it is not possible/straightforward to move the entire nested
subquery from clause into the outer level query from clause
A temporary relation is created instead, and used in body of outer
level query
Database System Concepts - 6th Edition 1.51 ©Silberschatz, Korth and Sudarshan
Optimizing Nested Subqueries (Cont.)
In general, SQL queries of the form below can be rewritten as shown
Rewrite: select …
from L1
where P1 and exists (select *
from L2
where P2)
To: create table t1 as
select distinct V
from L2
where P21
select …
from L1, t1
where P1 and P22
P21 contains predicates in P2 that do not involve any correlation
variables
P22 reintroduces predicates involving correlation variables, with
relations renamed appropriately
V contains all attributes used in predicates with correlation
variables
Database System Concepts - 6th Edition 1.52 ©Silberschatz, Korth and Sudarshan
Optimizing Nested Subqueries (Cont.)
In our example, the original nested query would be transformed to
create table t1 as
select distinct ID
from teaches
where year = 2007
select name
from instructor, t1
where t1.ID = instructor.ID
The process of replacing a nested query by a query with a join (possibly
with a temporary relation) is called decorrelation.
Decorrelation is more complicated when
the nested subquery uses aggregation, or
when the result of the nested subquery is used to test for equality, or
when the condition linking the nested subquery to the other
query is not exists,
and so on.
Database System Concepts - 6th Edition 1.53 ©Silberschatz, Korth and Sudarshan
Materialized Views**
A materialized view is a view whose contents are computed and
stored.
Consider the view
create view department_total_salary(dept_name, total_salary) as
select dept_name, sum(salary)
from instructor
group by dept_name
Materializing the above view would be very useful if the total salary by
department is required frequently
Saves the effort of finding multiple tuples and adding up their
amounts
Database System Concepts - 6th Edition 1.54 ©Silberschatz, Korth and Sudarshan
Materialized View Maintenance
The task of keeping a materialized view up-to-date with the underlying
data is known as materialized view maintenance
Materialized views can be maintained by recomputation on every
update
A better option is to use incremental view maintenance
Changes to database relations are used to compute changes
to the materialized view, which is then updated
View maintenance can be done by
Manually defining triggers on insert, delete, and update of each
relation in the view definition
Manually written code to update the view whenever database
relations are updated
Periodic recomputation (e.g. nightly)
Database System Concepts - 6th Edition 1.55 ©Silberschatz, Korth and Sudarshan
Incremental View Maintenance
The changes (inserts and deletes) to a relation or expressions are
referred to as its differential
Set of tuples inserted to and deleted from r are denoted ir and dr
To simplify our description, we only consider inserts and deletes
We replace updates to a tuple by deletion of the tuple followed by
insertion of the update tuple
We describe how to compute the change to the result of each
relational operation, given changes to its inputs
We then outline how to handle relational algebra expressions
Database System Concepts - 6th Edition 1.56 ©Silberschatz, Korth and Sudarshan
Join Operation
Consider the materialized view v = r s and an update to r
Let rold and rnew denote the old and new states of relation r
Consider the case of an insert to r:
We can write rnew s as (rold ir) s
And rewrite the above to (rold s) (ir s)
But (rold s) is simply the old value of the materialized view, so
the incremental change to the view is just ir s
Thus, for inserts vnew = vold (ir s)
Similarly for deletes vnew = vold – (dr s)
A, 1 1, p A, 1, p
B, 2 2, r B, 2, r
2, s B, 2, s
C,2
C, 2, r
C, 2, s
Database System Concepts - 6th Edition 1.57 ©Silberschatz, Korth and Sudarshan
Selection and Projection Operations
Selection: Consider a view v = (r).
vnew = vold (ir)
vnew = vold - (dr)
Projection is a more difficult operation
R = (A,B), and r(R) = { (a,2), (a,3)}
A(r) has a single tuple (a).
If we delete the tuple (a,2) from r, we should not delete the tuple (a)
from A(r), but if we then delete (a,3) as well, we should delete the
tuple
For each tuple in a projection A(r) , we will keep a count of how many
times it was derived
On insert of a tuple to r, if the resultant tuple is already in A(r) we
increment its count, else we add a new tuple with count = 1
On delete of a tuple from r, we decrement the count of the
corresponding tuple in A(r)
if the count becomes 0, we delete the tuple from A(r)
Database System Concepts - 6th Edition 1.58 ©Silberschatz, Korth and Sudarshan
Aggregation Operations
count : v = Agcount(B)(r).
When a set of tuples ir is inserted
For each tuple r in ir, if the corresponding group is already present in v,
we increment its count, else we add a new tuple with count = 1
When a set of tuples dr is deleted
for each tuple t in ir.we look for the group t.A in v, and subtract 1 from
the count for the group.
– If the count becomes 0, we delete from v the tuple for the group t.A
sum: v = Agsum (B)(r)
Database System Concepts - 6th Edition 1.59 ©Silberschatz, Korth and Sudarshan
Aggregate Operations (Cont.)
min, max: v = gmin (B) (r).
A
Database System Concepts - 6th Edition 1.60 ©Silberschatz, Korth and Sudarshan
Other Operations
Set intersection: v = r s
when a tuple is inserted in r we check if it is present in s, and if so
we add it to v.
If the tuple is deleted from r, we delete it from the intersection if it
is present.
Updates to s are symmetric
The other set operations, union and set difference are handled in
a similar fashion.
Outer joins are handled in much the same way as joins but with some
extra work
we leave details to you.
Database System Concepts - 6th Edition 1.61 ©Silberschatz, Korth and Sudarshan
Handling Expressions
To handle an entire expression, we derive expressions for computing
the incremental change to the result of each sub-expressions, starting
from the smallest sub-expressions.
E.g. consider E1 E2 where each of E1 and E2 may be a complex
expression
Suppose the set of tuples to be inserted into E1 is given by D1
Computed earlier, since smaller sub-expressions are handled
first
Then the set of tuples to be inserted into E1 E2 is given by
D1 E2
This is just the usual way of maintaining joins
Database System Concepts - 6th Edition 1.62 ©Silberschatz, Korth and Sudarshan
Query Optimization and Materialized Views
Database System Concepts - 6th Edition 1.63 ©Silberschatz, Korth and Sudarshan
Materialized View Selection
Materialized view selection: “What is the best set of views to
materialize?”.
Index selection: “what is the best set of indices to create”
closely related, to materialized view selection
but simpler
Database System Concepts - 6th Edition 1.64 ©Silberschatz, Korth and Sudarshan
Additional Optimization Techniques
Database System Concepts - 6th Edition 1.66 ©Silberschatz, Korth and Sudarshan
Optimization of Updates
Halloween problem
update R set A = 5 * A
where A > 10
If index on A is used to find tuples satisfying A > 10, and tuples
updated immediately, same tuple may be found (and updated)
multiple times
Solution 1: Always defer updates
collect the updates (old and new values of tuples) and update
relation and indices in second pass
Drawback: extra overhead even if e.g. update is only on R.B,
not on attributes in selection condition
Solution 2: Defer only if required
Perform immediate update if update does not affect attributes
in where clause, and deferred updates otherwise.
Database System Concepts - 6th Edition 1.67 ©Silberschatz, Korth and Sudarshan
Join Minimization
Join minimization
select r.A, r.B
from r, s
where r.B = s.B
Check if join with s is redundant, drop it
E.g. join condition is on foreign key from r to s, r.B is declared
as not null, and no selection on s
Other sufficient conditions possible
select r.A, s2.B
from r, s as s1, s as s2
where r.B=s1.B and r.B = s2.B and s1.A < 20 and s2.A < 10
join with s1 is redundant and can be dropped (along with
selection on s1)
Lots of research in this area since 70s/80s!
Database System Concepts - 6th Edition 1.68 ©Silberschatz, Korth and Sudarshan
Multiquery Optimization
Example
Q1: select * from (r natural join t) natural join s
Q2: select * from (r natural join u) natural join s
Both queries share common subexpression (r natural join s)
May be useful to compute (r natural join s) once and use it
in both queries
But this may be more expensive in some situations
– e.g. (r natural join s) may be expensive, plans as
shown in queries may be cheaper
Multiquery optimization: find best overall plan for a set of
queries, expoiting sharing of common subexpressions between
queries where it is useful
Database System Concepts - 6th Edition 1.69 ©Silberschatz, Korth and Sudarshan
Multiquery Optimization (Cont.)
Simple heuristic used in some database systems:
optimize each query separately
detect and exploiting common subexpressions in the
individual optimal query plans
May not always give best plan, but is cheap to implement
Shared scans: widely used special case of multiquery
optimization
Set of materialized views may share common subexpressions
As a result, view maintenance plans may share
subexpressions
Multiquery optimization can be useful in such situations
Database System Concepts - 6th Edition 1.70 ©Silberschatz, Korth and Sudarshan
Parametric Query Optimization
Example
select *
from r natural join s
where r.a < $1
value of parameter $1 not known at compile time
known only at run time
Database System Concepts - 6th Edition 1.71 ©Silberschatz, Korth and Sudarshan
Extra Slides
(Not in 6th Edition book)
Database System Concepts - 6th Edition 1.72 ©Silberschatz, Korth and Sudarshan
Plan Stability Across Optimizer Changes
What if 95% of plans are faster on database/optimizer version N+1
than on N, but 5% are slower?
Why should plans be slower on new improved optimizer?
Answer: Two wrongs can make a right, fixing one wrong can
make things worse!
Approaches:
Allow hints for tuning queries
Not practical for migrating large systems with no access to
source code
Set optimization level, default to version N (Oracle)
And migrate one query at a time after testing both plans on
new optimizer
Save plan from version N, and give it to optimizer version N+1
Sybase, XML representation of plans (SQL Server)
Database System Concepts - 6th Edition 1.73 ©Silberschatz, Korth and Sudarshan
End of Chapter
Database System Concepts - 6th Edition 1.75 ©Silberschatz, Korth and Sudarshan
Figure 13.02
Database System Concepts - 6th Edition 1.76 ©Silberschatz, Korth and Sudarshan
Figure 13.03
Rule 5
q q
E1 E2 E2 E1
Rule 6.a
E3 E1
E1 E2 E2 E3
sq Rule 7.a
If q only has
attributes from E1 sq E2
E1 E2 E1
Database System Concepts - 6th Edition 1.77 ©Silberschatz, Korth and Sudarshan
Figure 13.04
Õ name, title Õ name, title
s dept_name = Music
Ù year = 2009
Õ course_id, title
sdept_name = Music
syear = 2009
instructor
course
(a) Initial expression tree (b) Tree after multiple transformations
Database System Concepts - 6th Edition 1.78 ©Silberschatz, Korth and Sudarshan
Figure 13.06
50
40
frequency
30
20
10
r5
r4
r3 r4 r5
r3
r1 r2
r1 r2
Database System Concepts - 6th Edition 1.80 ©Silberschatz, Korth and Sudarshan
Chapter 14: Transactions
Database System Concepts - 6th Edition 14.2 ©Silberschatz, Korth and Sudarshan
Transaction Concept
A transaction is a unit of program execution that accesses and
possibly updates various data items.
E.g., transaction to transfer $50 from account A to account B:
1. read(A)
2. A := A – 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
Two main issues to deal with:
Failures of various kinds, such as hardware failures and
system crashes
Concurrent execution of multiple transactions
Database System Concepts - 6th Edition 14.3 ©Silberschatz, Korth and Sudarshan
Required Properties of a Transaction
Consider a transaction to transfer $50 from account A to account B:
1. read(A)
2. A := A – 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
Atomicity requirement
If the transaction fails after step 3 and before step 6, money will be
“lost” leading to an inconsistent database state
Failure could be due to software or hardware
The system should ensure that updates of a partially executed
transaction are not reflected in the database
Durability requirement — once the user has been notified that the
transaction has completed (i.e., the transfer of the $50 has taken place), the
updates to the database by the transaction must persist even if there are
software or hardware failures.
Database System Concepts - 6th Edition 14.4 ©Silberschatz, Korth and Sudarshan
Required Properties of a Transaction (Cont.)
Database System Concepts - 6th Edition 14.5 ©Silberschatz, Korth and Sudarshan
Required Properties of a Transaction (Cont.)
T1 T2
1. read(A)
2. A := A – 50
3. write(A)
read(A), read(B), print(A+B)
4. read(B)
5. B := B + 50
6. write(B
Isolation can be ensured trivially by running transactions serially
That is, one after the other.
However, executing multiple transactions concurrently has significant
benefits, as we will see later.
Database System Concepts - 6th Edition 14.6 ©Silberschatz, Korth and Sudarshan
ACID Properties
A transaction is a unit of program execution that accesses and possibly
updates various data items. To preserve the integrity of data the database
system must ensure:
Atomicity. Either all operations of the transaction are properly reflected
in the database or none are.
Consistency. Execution of a transaction in isolation preserves the
consistency of the database.
Isolation. Although multiple transactions may execute concurrently,
each transaction must be unaware of other concurrently executing
transactions. Intermediate transaction results must be hidden from other
concurrently executed transactions.
That is, for every pair of transactions Ti and Tj, it appears to Ti that
either Tj, finished execution before Ti started, or Tj started execution
after Ti finished.
Durability. After a transaction completes successfully, the changes it
has made to the database persist, even if there are system failures.
Database System Concepts - 6th Edition 14.7 ©Silberschatz, Korth and Sudarshan
Transaction State
Active – the initial state; the transaction stays in this state while it is
executing
Partially committed – after the final statement has been executed.
Failed -- after the discovery that normal execution can no longer
proceed.
Aborted – after the transaction has been rolled back and the
database restored to its state prior to the start of the transaction.
Two options after it has been aborted:
Restart the transaction
can be done only if no internal logical error
Kill the transaction
Committed – after successful completion.
Database System Concepts - 6th Edition 14.8 ©Silberschatz, Korth and Sudarshan
Transaction State (Cont.)
Database System Concepts - 6th Edition 14.9 ©Silberschatz, Korth and Sudarshan
Concurrent Executions
Multiple transactions are allowed to run concurrently in the
system. Advantages are:
Increased processor and disk utilization, leading to
better transaction throughput
E.g. one transaction can be using the CPU while
another is reading from or writing to the disk
Reduced average response time for transactions: short
transactions need not wait behind long ones.
Concurrency control schemes – mechanisms to achieve
isolation
That is, to control the interaction among the concurrent
transactions in order to prevent them from destroying the
consistency of the database
Will study in Chapter 15, after studying notion of
correctness of concurrent executions.
Database System Concepts - 6th Edition 14.10 ©Silberschatz, Korth and Sudarshan
Schedules
Schedule – a sequences of instructions that specify the
chronological order in which instructions of concurrent transactions
are executed
A schedule for a set of transactions must consist of all
instructions of those transactions
Must preserve the order in which the instructions appear in
each individual transaction.
A transaction that successfully completes its execution will have a
commit instructions as the last statement
By default transaction assumed to execute commit instruction
as its last step
A transaction that fails to successfully complete its execution will
have an abort instruction as the last statement
Database System Concepts - 6th Edition 14.11 ©Silberschatz, Korth and Sudarshan
Schedule 1
Let T1 transfer $50 from A to B, and T2 transfer 10% of the balance from A to B.
An example of a serial schedule in which T1 is followed by T2 :
Database System Concepts - 6th Edition 14.12 ©Silberschatz, Korth and Sudarshan
Schedule 2
A serial schedule in which T2 is followed by T1 :
Database System Concepts - 6th Edition 14.13 ©Silberschatz, Korth and Sudarshan
Schedule 3
Let T1 and T2 be the transactions defined previously. The following
schedule is not a serial schedule, but it is equivalent to Schedule 1.
Database System Concepts - 6th Edition 14.14 ©Silberschatz, Korth and Sudarshan
Schedule 4
The following concurrent schedule does not preserve the sum
of “A + B”
Database System Concepts - 6th Edition 14.15 ©Silberschatz, Korth and Sudarshan
Serializability
Database System Concepts - 6th Edition 14.16 ©Silberschatz, Korth and Sudarshan
Simplified view of transactions
Database System Concepts - 6th Edition 14.17 ©Silberschatz, Korth and Sudarshan
Conflicting Instructions
Let li and lj be two Instructions of transactions Ti and Tj
respectively. Instructions li and lj conflict if and only if there
exists some item Q accessed by both li and lj, and at least one of
these instructions wrote Q.
1. li = read(Q), lj = read(Q). li and lj don’t conflict.
2. li = read(Q), lj = write(Q). They conflict.
3. li = write(Q), lj = read(Q). They conflict
4. li = write(Q), lj = write(Q). They conflict
Intuitively, a conflict between li and lj forces a (logical) temporal
order between them.
If li and lj are consecutive in a schedule and they do not
conflict, their results would remain the same even if they had
been interchanged in the schedule.
Database System Concepts - 6th Edition 14.18 ©Silberschatz, Korth and Sudarshan
Conflict Serializability
Database System Concepts - 6th Edition 14.19 ©Silberschatz, Korth and Sudarshan
Conflict Serializability (Cont.)
Schedule 3 can be transformed into Schedule 6 -- a serial schedule where
T2 follows T1, by a series of swaps of non-conflicting instructions.
Therefore, Schedule 3 is conflict serializable.
Schedule 3 Schedule 6
Database System Concepts - 6th Edition 14.20 ©Silberschatz, Korth and Sudarshan
Conflict Serializability (Cont.)
Example of a schedule that is not conflict serializable:
Database System Concepts - 6th Edition 14.21 ©Silberschatz, Korth and Sudarshan
Precedence Graph
Consider some schedule of a set of transactions T1, T2, ..., Tn
Precedence graph — a direct graph where the vertices are
the transactions (names).
We draw an arc from Ti to Tj if the two transaction conflict,
and Ti accessed the data item on which the conflict arose
earlier.
We may label the arc by the item that was accessed.
Example
Database System Concepts - 6th Edition 14.22 ©Silberschatz, Korth and Sudarshan
Testing for Conflict Serializability
A schedule is conflict serializable if and only if its
precedence graph is acyclic.
Cycle-detection algorithms exist which take order
n2 time, where n is the number of vertices in the
graph.
(Better algorithms take order n + e where e is
the number of edges.)
If precedence graph is acyclic, the serializability
order can be obtained by a topological sorting of
the graph.
That is, a linear order consistent with the
partial order of the graph.
For example, a serializability order for the
schedule (a) would be one of either (b) or (c)
Database System Concepts - 6th Edition 14.23 ©Silberschatz, Korth and Sudarshan
Recoverable Schedules
If T8 should abort, T9 would have read (and possibly shown to the user)
an inconsistent database state. Hence, database must ensure that
schedules are recoverable.
Database System Concepts - 6th Edition 14.24 ©Silberschatz, Korth and Sudarshan
Cascading Rollbacks
Cascading rollback – a single transaction failure leads to a
series of transaction rollbacks. Consider the following schedule
where none of the transactions has yet committed (so the
schedule is recoverable)
Database System Concepts - 6th Edition 14.25 ©Silberschatz, Korth and Sudarshan
Cascadeless Schedules
Database System Concepts - 6th Edition 14.26 ©Silberschatz, Korth and Sudarshan
Concurrency Control
A database must provide a mechanism that will ensure that all
possible schedules are both:
Conflict serializable.
Recoverable and preferably cascadeless
A policy in which only one transaction can execute at a time
generates serial schedules, but provides a poor degree of
concurrency
Concurrency-control schemes tradeoff between the amount of
concurrency they allow and the amount of overhead that they incur
Testing a schedule for serializability after it has executed is a little
too late!
Tests for serializability help us understand why a concurrency
control protocol is correct
Goal – to develop concurrency control protocols that will assure
serializability.
Database System Concepts - 6th Edition 14.27 ©Silberschatz, Korth and Sudarshan
Weak Levels of Consistency
Database System Concepts - 6th Edition 14.28 ©Silberschatz, Korth and Sudarshan
Levels of Consistency in SQL-92
Serializable — default
Repeatable read — only committed records to be read, repeated reads of
same record must return same value. However, a transaction may not be
serializable – it may find some records inserted by a transaction but not
find others.
Read committed — only committed records can be read, but successive
reads of record may return different (but committed) values.
Read uncommitted — even uncommitted records may be read.
Database System Concepts - 6th Edition 14.29 ©Silberschatz, Korth and Sudarshan
Transaction Definition in SQL
Data manipulation language must include a construct for
specifying the set of actions that comprise a transaction.
In SQL, a transaction begins implicitly.
A transaction in SQL ends by:
Commit work commits current transaction and begins a
new one.
Rollback work causes current transaction to abort.
In almost all database systems, by default, every SQL
statement also commits implicitly if it executes successfully
Implicit commit can be turned off by a database directive
E.g. in JDBC, connection.setAutoCommit(false);
Database System Concepts - 6th Edition 14.30 ©Silberschatz, Korth and Sudarshan
Other Notions of Serializability
Database System Concepts - 6th Edition 14.31 ©Silberschatz, Korth and Sudarshan
View Serializability
Database System Concepts - 6th Edition 14.32 ©Silberschatz, Korth and Sudarshan
View Serializability (Cont.)
A schedule S is view serializable if it is view equivalent to a serial
schedule.
Every conflict serializable schedule is also view serializable.
Below is a schedule which is view-serializable but not conflict
serializable.
Database System Concepts - 6th Edition 14.33 ©Silberschatz, Korth and Sudarshan
Test for View Serializability
The precedence graph test for conflict serializability cannot be used
directly to test for view serializability.
Extension to test for view serializability has cost exponential in the
size of the precedence graph.
The problem of checking if a schedule is view serializable falls in the
class of NP-complete problems.
Thus, existence of an efficient algorithm is extremely unlikely.
However ,practical algorithms that just check some sufficient
conditions for view serializability can still be used.
Database System Concepts - 6th Edition 14.34 ©Silberschatz, Korth and Sudarshan
More Complex Notions of Serializability
The schedule below produces the same outcome as the serial schedule
< T1, T5 >, yet is not conflict equivalent or view equivalent to it.
If we start with A = 1000 and B = 2000, the final result is 960 and 2040
Determining such equivalence requires analysis of operations other
than read and write.
Database System Concepts - 6th Edition 14.35 ©Silberschatz, Korth and Sudarshan
End of Chapter 14
Database System Concepts - 6th Edition 22.2 ©Silberschatz, Korth and Sudarshan
Object-Relational Data Models
Extend the relational data model by including object orientation and
constructs to deal with added data types.
Allow attributes of tuples to have complex types, including non-atomic
values such as nested relations.
Preserve relational foundations, in particular the declarative access to
data, while extending modeling power.
Upward compatibility with existing relational languages.
Database System Concepts - 6th Edition 22.3 ©Silberschatz, Korth and Sudarshan
Complex Data Types
Motivation:
Permit non-atomic domains (atomic indivisible)
Example of non-atomic domain: set of integers,or set of
tuples
Allows more intuitive modeling for applications with
complex data
Intuitive definition:
allow relations whenever we allow atomic (scalar) values
— relations within relations
Retains mathematical foundation of relational model
Violates first normal form.
Database System Concepts - 6th Edition 22.4 ©Silberschatz, Korth and Sudarshan
Example of a Nested Relation
Example: library information system
Each book has
title,
a list (array) of authors,
Publisher, with subfields name and branch, and
a set of keywords
Non-1NF relation books
Database System Concepts - 6th Edition 22.5 ©Silberschatz, Korth and Sudarshan
4NF Decomposition of Nested Relation
Suppose for simplicity that
title uniquely identifies a
book
In real world ISBN is a
unique identifier
Decompose books into
4NF using the schemas:
(title, author, position )
(title, keyword )
(title, pub-name, pub-
branch )
4NF design requires users
to include joins in their
queries.
Database System Concepts - 6th Edition 22.6 ©Silberschatz, Korth and Sudarshan
Complex Types and SQL
Extensions introduced in SQL:1999 to support complex types:
Collection and large object types
Nested relations are an example of collection types
Structured types
Nested record structures like composite attributes
Inheritance
Object orientation
Including object identifiers and references
Database System Concepts - 6th Edition 22.7 ©Silberschatz, Korth and Sudarshan
Structured Types and Inheritance in SQL
Structured types (a.k.a. user-defined types) can be declared and used in SQL
create type Name as
(firstname varchar(20),
lastname varchar(20))
final
create type Address as
(street varchar(20),
city varchar(20),
zipcode varchar(20))
not final
Note: final and not final indicate whether subtypes can be created
Structured types can be used to create tables with composite attributes
create table person (
name Name,
address Address,
dateOfBirth date)
Dot notation used to reference components: name.firstname
Database System Concepts - 6th Edition 22.8 ©Silberschatz, Korth and Sudarshan
Structured Types (cont.)
User-defined row types
create type PersonType as (
name Name,
address Address,
dateOfBirth date)
not final
Can then create a table whose rows are a user-defined type
create table customer of CustomerType
Alternative using unnamed row types.
create table person_r(
name row(firstname varchar(20),
lastname varchar(20)),
address row(street varchar(20),
city varchar(20),
zipcode varchar(20)),
dateOfBirth date)
Database System Concepts - 6th Edition 22.9 ©Silberschatz, Korth and Sudarshan
Methods
Can add a method declaration with a structured type.
method ageOnDate (onDate date)
returns interval year
Method body is given separately.
create instance method ageOnDate (onDate date)
returns interval year
for CustomerType
begin
return onDate - self.dateOfBirth;
end
We can now find the age of each customer:
select name.lastname, ageOnDate (current_date)
from customer
Database System Concepts - 6th Edition 22.10 ©Silberschatz, Korth and Sudarshan
Constructor Functions
Constructor functions are used to create values of structured types
E.g.
create function Name(firstname varchar(20), lastname varchar(20))
returns Name
begin
set self.firstname = firstname;
set self.lastname = lastname;
end
To create a value of type Name, we use
new Name(‘John’, ‘Smith’)
Normally used in insert statements
insert into Person values
(new Name(‘John’, ‘Smith),
new Address(’20 Main St’, ‘New York’, ‘11001’),
date ‘1960-8-22’);
Database System Concepts - 6th Edition 22.11 ©Silberschatz, Korth and Sudarshan
Type Inheritance
Suppose that we have the following type definition for people:
create type Person
(name varchar(20),
address varchar(20))
Using inheritance to define the student and teacher types
create type Student
under Person
(degree varchar(20),
department varchar(20))
create type Teacher
under Person
(salary integer,
department varchar(20))
Subtypes can redefine methods by using overriding method in place of
method in the method declaration
Database System Concepts - 6th Edition 22.12 ©Silberschatz, Korth and Sudarshan
Multiple Type Inheritance
SQL:1999 and SQL:2003 do not support multiple inheritance
If our type system supports multiple inheritance, we can define a type for
teaching assistant as follows:
create type Teaching Assistant
under Student, Teacher
To avoid a conflict between the two occurrences of department we can
rename them
create type Teaching Assistant
under
Student with (department as student_dept ),
Teacher with (department as teacher_dept )
Each value must have a most-specific type
Database System Concepts - 6th Edition 22.13 ©Silberschatz, Korth and Sudarshan
Table Inheritance
Tables created from subtypes can further be specified as subtables
E.g. create table people of Person;
create table students of Student under people;
create table teachers of Teacher under people;
Tuples added to a subtable are automatically visible to queries on the
supertable
E.g. query on people also sees students and teachers.
Similarly updates/deletes on people also result in updates/deletes
on subtables
To override this behaviour, use “only people” in query
Conceptually, multiple inheritance is possible with tables
e.g. teaching_assistants under students and teachers
But is not supported in SQL currently
So we cannot create a person (tuple in people) who is both a
student and a teacher
Database System Concepts - 6th Edition 22.14 ©Silberschatz, Korth and Sudarshan
Consistency Requirements for Subtables
Database System Concepts - 6th Edition 22.15 ©Silberschatz, Korth and Sudarshan
Array and Multiset Types in SQL
Example of array and multiset declaration:
create type Publisher as
(name varchar(20),
branch varchar(20));
create type Book as
(title varchar(20),
author_array varchar(20) array [10],
pub_date date,
publisher Publisher,
keyword-set varchar(20) multiset);
create table books of Book;
Database System Concepts - 6th Edition 22.16 ©Silberschatz, Korth and Sudarshan
Creation of Collection Values
Array construction
array [‘Silberschatz’,`Korth’,`Sudarshan’]
Multisets
multiset [‘computer’, ‘database’, ‘SQL’]
Database System Concepts - 6th Edition 22.17 ©Silberschatz, Korth and Sudarshan
Querying Collection-Valued Attributes
To find all books that have the word “database” as a keyword,
select title
from books
where ‘database’ in (unnest(keyword-set ))
We can access individual elements of an array by using indices
E.g.: If we know that a particular book has three authors, we could write:
select author_array[1], author_array[2], author_array[3]
from books
where title = `Database System Concepts’
To get a relation containing pairs of the form “title, author_name” for each
book and each author of the book
select B.title, A.author
from books as B, unnest (B.author_array) as A (author )
To retain ordering information we add a with ordinality clause
select B.title, A.author, A.position
from books as B, unnest (B.author_array) with ordinality as
A (author, position )
Database System Concepts - 6th Edition 22.18 ©Silberschatz, Korth and Sudarshan
Unnesting
The transformation of a nested relation into a form with fewer (or no)
relation-valued attributes us called unnesting.
E.g.
select title, A as author, publisher.name as pub_name,
publisher.branch as pub_branch, K.keyword
from books as B, unnest(B.author_array ) as A (author ),
unnest (B.keyword_set ) as K (keyword )
Result relation flat_books
Database System Concepts - 6th Edition 22.19 ©Silberschatz, Korth and Sudarshan
Nesting
Nesting is the opposite of unnesting, creating a collection-valued attribute
Nesting can be done in a manner similar to aggregation, but using the
function colect() in place of an aggregation operation, to create a multiset
To nest the flat_books relation on the attribute keyword:
select title, author, Publisher (pub_name, pub_branch ) as publisher,
collect (keyword) as keyword_set
from flat_books
groupby title, author, publisher
To nest on both authors and keywords:
select title, collect (author ) as author_set,
Publisher (pub_name, pub_branch) as publisher,
collect (keyword ) as keyword_set
from flat_books
group by title, publisher
Database System Concepts - 6th Edition 22.20 ©Silberschatz, Korth and Sudarshan
Nesting (Cont.)
Another approach to creating nested relations is to use subqueries in
the select clause, starting from the 4NF relation books4
select title,
array (select author
from authors as A
where A.title = B.title
order by A.position) as author_array,
Publisher (pub-name, pub-branch) as publisher,
multiset (select keyword
from keywords as K
where K.title = B.title) as keyword_set
from books4 as B
Database System Concepts - 6th Edition 22.21 ©Silberschatz, Korth and Sudarshan
Object-Identity and Reference Types
Define a type Department with a field name and a field head which is a
reference to the type Person, with table people as scope:
create type Department (
name varchar (20),
head ref (Person) scope people)
We can then create a table departments as follows
create table departments of Department
We can omit the declaration scope people from the type declaration
and instead make an addition to the create table statement:
create table departments of Department
(head with options scope people)
Referenced table must have an attribute that stores the identifier, called
the self-referential attribute
create table people of Person
ref is person_id system generated;
Database System Concepts - 6th Edition 22.22 ©Silberschatz, Korth and Sudarshan
Initializing Reference-Typed Values
To create a tuple with a reference value, we can first create the tuple
with a null reference and then set the reference separately:
insert into departments
values (`CS’, null)
update departments
set head = (select p.person_id
from people as p
where name = `John’)
where name = `CS’
Database System Concepts - 6th Edition 22.23 ©Silberschatz, Korth and Sudarshan
User Generated Identifiers
The type of the object-identifier must be specified as part of the type
definition of the referenced table, and
The table definition must specify that the reference is user generated
create type Person
(name varchar(20)
address varchar(20))
ref using varchar(20)
create table people of Person
ref is person_id user generated
When creating a tuple, we must provide a unique value for the identifier:
insert into people (person_id, name, address ) values
(‘01284567’, ‘John’, `23 Coyote Run’)
We can then use the identifier value when inserting a tuple into
departments
Avoids need for a separate query to retrieve the identifier:
insert into departments
values(`CS’, `02184567’)
Database System Concepts - 6th Edition 22.24 ©Silberschatz, Korth and Sudarshan
User Generated Identifiers (Cont.)
Database System Concepts - 6th Edition 22.25 ©Silberschatz, Korth and Sudarshan
Path Expressions
Find the names and addresses of the heads of all departments:
select head –>name, head –>address
from departments
An expression such as “head–>name” is called a path expression
Path expressions help avoid explicit joins
If department head were not a reference, a join of departments
with people would be required to get at the address
Makes expressing the query much easier for the user
Database System Concepts - 6th Edition 22.26 ©Silberschatz, Korth and Sudarshan
Implementing O-R Features
Similar to how E-R features are mapped onto relation schemas
Subtable implementation
Each table stores primary key and those attributes defined in that
table
or,
Each table stores both locally defined and inherited attributes
Database System Concepts - 6th Edition 22.27 ©Silberschatz, Korth and Sudarshan
Persistent Programming Languages
Languages extended with constructs to handle persistent data
Programmer can manipulate persistent data directly
no need to fetch it into memory and store it back to disk (unlike
embedded SQL)
Persistent objects:
Persistence by class - explicit declaration of persistence
Persistence by creation - special syntax to create persistent
objects
Persistence by marking - make objects persistent after creation
Persistence by reachability - object is persistent if it is declared
explicitly to be so or is reachable from a persistent object
Database System Concepts - 6th Edition 22.28 ©Silberschatz, Korth and Sudarshan
Object Identity and Pointers
Degrees of permanence of object identity
Intraprocedure: only during execution of a single procedure
Intraprogram: only during execution of a single program or query
Interprogram: across program executions, but not if data-storage
format on disk changes
Persistent: interprogram, plus persistent across data
reorganizations
Persistent versions of C++ and Java have been implemented
C++
ODMG C++
ObjectStore
Java
Java Database Objects (JDO)
Database System Concepts - 6th Edition 22.29 ©Silberschatz, Korth and Sudarshan
Persistent C++ Systems
Extensions of C++ language to support persistent storage of objects
Several proposals, ODMG standard proposed, but not much action of
late
persistent pointers: e.g. d_Ref<T>
creation of persistent objects: e.g. new (db) T()
Class extents: access to all persistent objects of a particular class
Relationships: Represented by pointers stored in related objects
Issue: consistency of pointers
Solution: extension to type system to automatically maintain
back-references
Iterator interface
Transactions
Updates: mark_modified() function to tell system that a persistent
object that was fetched into memory has been updated
Query language
Database System Concepts - 6th Edition 22.30 ©Silberschatz, Korth and Sudarshan
Persistent Java Systems
Standard for adding persistence to Java : Java Database Objects (JDO)
Persistence by reachability
Byte code enhancement
Classes separately declared as persistent
Class extents
Single reference type
no difference between in-memory pointer and persistent pointer
Database System Concepts - 6th Edition 22.31 ©Silberschatz, Korth and Sudarshan
Object-Relational Mapping
Object-Relational Mapping (ORM) systems built on top of traditional
relational databases
Implementor provides a mapping from objects to relations
Objects are purely transient, no permanent object identity
Objects can be retried from database
System uses mapping to fetch relevant data from relations and
construct objects
Updated objects are stored back in database by generating
corresponding update/insert/delete statements
The Hibernate ORM system is widely used
described in Section 9.4.2
Provides API to start/end transactions, fetch objects, etc
Provides query language operating direcly on object model
queries translated to SQL
Limitations: overheads, especially for bulk updates
Database System Concepts - 6th Edition 22.32 ©Silberschatz, Korth and Sudarshan
Comparison of O-O and O-R Databases
Relational systems
simple data types, powerful query languages, high protection.
Persistent-programming-language-based OODBs
complex data types, integration with programming language, high
performance.
Object-relational systems
complex data types, powerful query languages, high protection.
Object-relational mapping systems
complex data types integrated with programming language, but built
as a layer on top of a relational database system
Note: Many real systems blur these boundaries
E.g. persistent programming language built as a wrapper on a
relational database offers first two benefits, but may have poor
performance.
Database System Concepts - 6th Edition 22.33 ©Silberschatz, Korth and Sudarshan
End of Chapter 22
Database System Concepts - 6th Edition 22.35 ©Silberschatz, Korth and Sudarshan
Figure 22.07
Database System Concepts - 6th Edition 22.36 ©Silberschatz, Korth and Sudarshan