Review of Database Management System
Review of Database Management System
α 1 α 2
α 2 β 3
β 1
s
r
A B
● r ∪ s: α 1
α 2
β 1
β 3
Set Difference Operation – Example
• Relations r, s:
A B A B
α 1 α 2
α 2 β 3
β 1
s
r
● r – s:
A B
α 1
β 1
Cartesian-Product Operation – Example
● Relations r, s:
A B C D E
α 1 α 10 a
β 2 β 10 a
β 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
Additional Operations
– Set intersection
– Natural join
– Aggregation
– Outer Join
– Division
Set-Intersection Operation – Example
• Relation r, s: A B A B
α 1 α 2
α 2 β 3
β 1
r s
A B
α 2
• r∩s
Natural Join Operation – Example
• Relations r, 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 δ
Division Operation – Example
● Relations r, s:
A B
B
α 1 1
α 2
2
α 3
β 1 s
γ 1
δ 1
δ 3
δ 4
∈ 6
∈ 1
β 2
● r ÷ s: A r
α
β
Aggregate Functions and Operations
• Aggregation function takes a collection of values and returns a
single value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
• Aggregate operation in relational algebra
α α 7
α β 7
β β 3
β β 10
27
Aggregate Operation – Example
• Relation account grouped by branch-name:
branch_name account_number balance
Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700
branch_name
g sum(balance) (account)
branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
Reduction to Relation Schemas
• A database which conforms to an E-R
diagram can be represented by a collection
of schemas.
• For each entity set and relationship set
there is a unique schema that is assigned
the name of the corresponding entity set
or relationship set.
• Each schema has a number of columns
(generally corresponding to attributes),
which have unique names.
Representing Entity Sets as Schemas
• A strong entity set reduces to a schema with
the same attributes.
• A weak entity set becomes a table that
includes a column for the primary key of the
identifying strong entity set
payment =
( loan_number, payment_number,
payment_date, payment_amount )
Relationship Sets as Schemas
• A many-to-many relationship set is
represented as a schema with attributes for
the primary keys of the two participating
entity sets, and any descriptive attributes
of the relationship set.
• Example: schema for relationship set
borrower
borrower = (customer_id, loan_number )
Redundancy of Schemas
● Many-to-one and one-to-many relationship sets that are
total on the many-side can be represented by adding an
extra attribute to the “many” side, containing the primary
key of the “one” side
● Example: Instead of creating a schema for relationship
set account_branch, add an attribute branch_name to the
schema arising from entity set account
Redundancy of Schemas
• For one-to-one relationship sets, either side
can be chosen to act as the “many” side
schema attributes
person name, street, city
customer name, credit_rating
employee name, salary
schema attributes
person name, street, city
customer name, street, city, credit_rating
employee name, street, city, salary
Schemas Corresponding to Aggregation
• Example:
create table branch
(branch_name char(15),
branch_city char(30),
assets integer)
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 n digits to the
right of decimal point.
• 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.
Integrity Constraints on Tables
• not null
• primary key (A1, ..., An )
Example: Declare branch_name as the primary key
for branch
.
create table branch
(branch_name char(15),
branch_city char(30) not null,
assets integer,
primary key (branch_name))
– Ai represents an attribute
– Ri represents a relation
– P is a predicate.(condition)
• This query is equivalent to the relational algebra expression.
● Find the name, loan number and loan amount of all customers
having a loan at the Perryridge branch.
update account
set balance = balance * 1.05
where balance ≤ 10000
Set Operations
• The set operations union, intersect, and except
operate on relations and correspond to the
relational algebra operations ∪, ∩, −.
Note: predicates in the having clause are applied after the formation of groups
whereas predicates in the where clause are applied before forming groups
“In” Construct
• Find all customers who have both an account and a loan at
the bank.
select distinct customer_name
from borrower
where customer_name in (select customer_name
from depositor )
● Find all customers who have a loan at the bank but do not
have an account at the bank
select customer_name
from all_customer
where branch_name = 'Perryridge'
With Clause
• The with clause provides a way of defining a
temporary view whose definition is available only
to the query in which the with clause occurs.
• Find all accounts with the maximum balance