Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views60 pages

Relational Algebra

The document discusses the relational data model and SQL. It describes the structure of relational databases and key concepts like relations, attributes, tuples, domains, relation schemas, and keys. It also explains how a database can consist of multiple relations and covers some fundamental SQL statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views60 pages

Relational Algebra

The document discusses the relational data model and SQL. It describes the structure of relational databases and key concepts like relations, attributes, tuples, domains, relation schemas, and keys. It also explains how a database can consist of multiple relations and covers some fundamental SQL statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Lesson 10: Relational Data Model

&
SQL
Contents
 Structure of Relational Databases
 Relational Algebra
 Basic Relational-Algebra Operations
 Additional Relational-Algebra Operations
 Extended Relational-Algebra Operations
 Null Values and Three-valued Logics
 Database Modification by Relational-Algebra Operations

 Brief Introduction to SQL


 SQL and Relations
 Fundamental SQL statements
 null values in SQL
 Database modifications in SQL

AE3B33OSD Lesson 9 / Page 2 Silberschatz, Korth, Sudarshan S. ©2007


Why Relations?
 We have seen tables account_number branch_name balance
 Why do we need another A-101 Downtown 500
A-102 Bridges 400
view of data? A-201 Brighton 900
 There is a number of A-215 Berkeley 700
reasons: A-217 Brighton 750
 Need to create a rigorous A-222 Redwood 700
mathematical model A-305 Palo Alto 350
 This model enables for formalizing database operations
 The exact model is needed to formulate declarative queries and
optimize their processing
 The central idea is to describe a database as a collection of
predicates over a finite set of predicate variables, defining
constraints on the possible values and combinations of
values.

AE3B33OSD Lesson 9 / Page 3 Silberschatz, Korth, Sudarshan S. ©2007


What is a Relation?
 Mathematically, given sets D1, D2, …, Dn a relation R is a
subset of the Cartesian product D1 x D2 x … x Dn
Thus, a relation is a set of n-tuples (a1, a2, …, an) where each
ai Di
 Example:
 customer_name = {Jones, Smith, Curry, Lindsay, …}
/* Set of all customer names */
 customer_street = {Main, North, Park, …} /* Set of all street names*/
 customer_city = {Harrison, Rye, Pittsfield, …} /* Set of all city names */
Then r = { (Jones, Main, Harrison),
(Smith, North, Rye),
(Curry, North, Rye),
(Lindsay, Park, Pittsfield) }
is a relation, i.e. subset of
customer_name x customer_street x customer_city
 As we are concerned with finite sets, such sets can be
expressed by enumeration, i.e. tables

AE3B33OSD Lesson 9 / Page 4 Silberschatz, Korth, Sudarshan S. ©2007


Relation is a Subset of a Cartesian Product
 No duplicates in sets
 Very important for Bush
database applications
Carter
 Component set
members can be in Clinton

Last names
any order Jefferson
 Sorted or unsorted Kenedy
Lincoln
Obama
Roosevelt
Washington

Theodore
Abraham

Thomas
Franklin

George

Jimmy
Barac

John
Bill
First names
Selected U.S. Presidents

AE3B33OSD Lesson 9 / Page 5 Silberschatz, Korth, Sudarshan S. ©2007


Attribute Types
 Each attribute of a relation has a name
 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
 E.g. the value of an attribute can be an account number,
but cannot be a set of account numbers
 Domain is said to be atomic if all its members are atomic
 The special value null is a member of every domain
 The null value causes complications in the definition of
many operations
 We shall ignore the effect of null values in our main presentation
and consider their effect later

AE3B33OSD Lesson 9 / Page 6 Silberschatz, Korth, Sudarshan S. ©2007


Relation Schema & Relation Instance
 Relation Schema
 A1, A2, …, An are attributes
 R = (A1, A2, …, An ) is a relation schema
Example:
Customer_schema = (customer_name, customer_street, customer_city)
 r(R) denotes a relation r on the relation schema R
Example:
customer (Customer_schema)
 Relation Instance
 The current values (relation instance) of a relation are specified by a
table
 An element t of r is a tuple, represented by a row in a table
attributes
(or columns)
customer_name customer_street customer_city

Jones Main Harrison


Smith North Rye tuples
Curry North Rye (or rows)
Lindsay Park Pittsfield

customer
AE3B33OSD Lesson 9 / Page 7 Silberschatz, Korth, Sudarshan S. ©2007
Database
 A database consists of multiple relations
 Information about an enterprise is broken up into parts,
with each relation storing one part of the information
account : stores information about accounts
depositor : stores information about which customer
owns which account
customer : stores information about customers
 Storing all information as a single relation such as
bank(account_number, balance, customer_name, ..)
results in
 repetition of information
 e.g., if two customers own an account (What gets repeated?)
 the need for null values
 e.g., to represent a customer without an account

 Normalization theory deals with how to design relational


schemas

AE3B33OSD Lesson 9 / Page 8 Silberschatz, Korth, Sudarshan S. ©2007


Keys (revisited)
 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)
 by “possible r ” we mean a relation r that could exist in the
enterprise we are modeling.
 Example: {customer_name, customer_street} and
{customer_name}
are both superkeys of Customer, if no two customers can possibly
have the same name
 In real life, an attribute such as customer_id would be used instead of
customer_name to uniquely identify customers, but we omit it to keep
our examples small, and instead assume customer names are unique.
 K is a candidate key if K is minimal
 Example: {customer_name} is a candidate key for Customer, since
it is a superkey and no subset of it is a superkey.
 Primary key: a candidate key chosen as the principal
means of identifying tuples within a relation
 Should choose an attribute whose value never, or very rarely,
changes.
 E.g. email address is unique, but may change

AE3B33OSD Lesson 9 / Page 9 Silberschatz, Korth, Sudarshan S. ©2007


Foreign Keys
 A relation schema may have an attribute that corresponds
to the primary key of another relation. The attribute is
called a foreign key.
 E.g. customer_name and account_number attributes of depositor
are foreign keys to customer and account respectively.
 Only values occurring in the primary key attribute of the
referenced relation may occur in the foreign key attribute of the
referencing relation.

branch account depositor customer


branch_name account_number customer_name customer_name
branch_city branch_name account_number customer_street
assets balance customer_city

loan borrower
loan_number customer_name
branch_name loan_number
amount

AE3B33OSD Lesson 9 / Page 10 Silberschatz, Korth, Sudarshan S. ©2007


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.

AE3B33OSD Lesson 9 / Page 11 Silberschatz, Korth, Sudarshan S. ©2007


Select Operation
 Notation:  p(r)
 p is called the selection predicate
 Defined as:
p(r) = {t | t  r and p(t)}
Where p is a formula in propositional calculus consisting of terms
connected by :  (and),  (or),  (not)
Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, , >, , <, 
 Example of selection:  (account)
branch_name=“Redwood”

A B C D
A B C D
  1 7
r   5 7 A=B ^ D > 5 (r)   1 7
  12 3   23 10
  23 10

AE3B33OSD Lesson 9 / Page 12 Silberschatz, Korth, Sudarshan S. ©2007


Project Operation
 Notation: ∏A , A , …, A
1 2 k
(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 are removed from result, since relations are sets
 Example: To eliminate the branch_name attribute of
account
account_number, balance (account)

A B C A C
A C
 10 1  1
r  20 1 A,C (r)  1
=  1
 1
 30 1  1
 2
 40 2  2

AE3B33OSD Lesson 9 / Page 13 Silberschatz, Korth, Sudarshan S. ©2007


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 2nd column of s)
 Example: to find all customers with either an account or a
loan
customer_name (depositor)  customer_name (borrower)

A B A B A B

Relations r, s:  1  1
 2
r  s:  2
 2  3
 1 s  1
r  3
AE3B33OSD Lesson 9 / Page 14 Silberschatz, Korth, Sudarshan S. ©2007
Set Difference Operation
 Notation r – s
 Defined as:
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

A B
A B
A B
 1
Relations r, s:  2 r – s:
 2  1
 3
 1  1
s
r

AE3B33OSD Lesson 9 / Page 15 Silberschatz, Korth, Sudarshan S. ©2007


Cartesian-Product Operation
 Notation r x s
 Defined as:
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 = .
 Can build expressions using multiple operations
 If attributes of r(R) and s(S) are not disjoint, then renaming
must be used.
A B C D E

C D E  1  10 a
A B  1  10 a
 10 a  1  20 b
r x s:
Relations r, s:  1  10 a  1  10 b
 2  20 b  2  10 a
 10 b  2  10 a
r  2  20 b
s  2  10 b

Caution: May generate HUGE tables


AE3B33OSD Lesson 9 / Page 16 Silberschatz, Korth, Sudarshan S. ©2007
Composition of Operations
 Building operations by composing several together

A B C D E

 1  10 a
 1  10 a
r x s:  1  20 b
 1  10 b
 2  10 a
 2  10 a
 2  20 b
 2  10 b

A B C D E

A=C(r x s):  1  10 a
 2  10 a
 2  20 b

AE3B33OSD Lesson 9 / Page 17 Silberschatz, Korth, Sudarshan S. ©2007


Rename Operation
 Not a “true relational algebra” 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( A , A , . . . , A ) (E)
1 2 n

returns the result of expression E under the name X, and


with the attributes renamed to A1 , A2 , …., An .

AE3B33OSD Lesson 9 / Page 18 Silberschatz, Korth, Sudarshan S. ©2007


Banking Example
 Relations
branch(branch_name, branch_city, assets)

customer(customer_name, customer_street, customer_city)

account(account_number, branch_name, balance)

loan(loan_number, branch_name, amount)

depositor(customer_name, account_number)

borrower(customer_name, loan_number)

 Example Queries
 Find all loans of over $1200
σ amount>1200 (loan)
 Find the loan number for each loan amounting over $1200
Π loan number
( σ amount >1200 (loan))
 Find the names of all customers who have an account at the
Redwood branch
Π customer (σ branch
name name = Redwood
(σ depositor. account_number = account . account_number ( depositor×loan)))

AE3B33OSD Lesson 9 / Page 19 Silberschatz, Korth, Sudarshan S. ©2007


Banking Example (cont.)
 Example Queries (cont.)
 Find the names of all customers who have a loan at the Redwood
branch but do not have an account at any branch of the bank
Π customer_name ( σ branch_name= Redwood
( σ borrower. loan_number = loan . loan_number ( borrower × loan)) )
−Π customer_name ( depositor )

 Find the names of all customers who have a loan at the Redwood
branch
 Possibility No. 1
Π customer (σ branch
name name = Redwood
(σ borrower. loan_number = loan . loan_number (borrower×loan)))
 Possibility No. 2
Π customer (σ borrower. loan_number = loan . loan_number (
name

(σ branch name = Redwood (borrower ))×loan))

AE3B33OSD Lesson 9 / Page 20 Silberschatz, Korth, Sudarshan S. ©2007


Banking Example (cont.)
 Example Queries (use of rename)
 Find the largest account balance
 Strategy:
 Find those balances that are not the largest
 Rename account relation as temp so that we can compare each
account balance with all others
 Use set difference to find those account balances that were not found in
the earlier step.
 The query is:
Π balance ( account )−Π account . balance
( σ account . balance < temp. balance ( account ×ρtemp ( account ) ))

AE3B33OSD Lesson 9 / Page 21 Silberschatz, Korth, Sudarshan S. ©2007


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
 P (E1), P is a predicate on attributes in E1
 s(E1), S is a list consisting of some of the attributes in E1
  x (E1), x is the new name for the result of E1

AE3B33OSD Lesson 9 / Page 22 Silberschatz, Korth, Sudarshan S. ©2007


Additional Operations
 We define additional operations that do not add any
power to the relational algebra, but that simplify common
queries.
 Set intersection
 Natural join
 Division
 Assignment

AE3B33OSD Lesson 9 / Page 23 Silberschatz, Korth, Sudarshan S. ©2007


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)

A B A B
 1 A B
 2
Relations r, s:  2 r  s:
 3  2
 1
r s

AE3B33OSD Lesson 9 / Page 24 Silberschatz, Korth, Sudarshan S. ©2007


Natural-Join Operation
 Notation: r ⋈ s
 Let r and s be relations on schemas R and S respectively.
Then, r ⋈ s is a relation on schema R  S obtained as
follows:
 Consider each pair of tuples tr from r and ts from s.
 If tr and ts have the same value on each of the attributes in R  S,
add a tuple t to the result, where
 t has the same value as tr on r
 t has the same value as ts on s
 The result of the natural join is the set of all combinations of
tuples in R and S that are equal on their common attribute
names
 Example:
R = (A, B, C, D)
S = (E, B, D)
Π
 Result schema = (A, B, C, D, E)
 r ⋈ s ris. Adefined
, r. B , r .C (σ
as:, r. D, s. E r. B=s .B∧r .D= s. D ( r×s ) )
AE3B33OSD Lesson 9 / Page 25 Silberschatz, Korth, Sudarshan S. ©2007
Natural Join Operation – Example
 Relations r, s: A B C D B D E r ⋈s: A B C D E

 1  a 1 a   1  a 
 2  a 3 a   1  a 
 4  b 1 a   1  a 
 1  a 2 b   1  a 
 2  b 3 b   2  b 

r s
 Practical example

AE3B33OSD Lesson 9 / Page 26 Silberschatz, Korth, Sudarshan S. ©2007


Division Operation
 Notation: r  s
 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 ) and 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) }
where tu means the concatenation of tuples t and u to produce a
single tuple
 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
r  s = R-S (r ) – R-S ( ( R-S (r ) x s ) – R-S,S(r ))
 To see why
 R-S,S (r) simply reorders attributes of r
 R-S (R-S (r) x s ) – R-S,S(r)) gives those tuples t in
AE3B33OSD Lesson 9 / Page 27 Silberschatz, Korth, Sudarshan S. ©2007
Division Operation – Example
 Relations r, s: A B r s: A
 1
 2 
B
 3 
 1
 1 1
 1
 3 2
 4 s
 6
 1
 2
r
 Practical example

AE3B33OSD Lesson 9 / Page 28 Silberschatz, Korth, Sudarshan S. ©2007


Assignment Operation
 The assignment operation () provides a convenient way
to express complex queries.
 Write query as a sequential program consisting of
 a series of assignments
 followed by an expression whose value is displayed as a result of the
query.
 Assignment must always be made to a temporary relation variable.
 Example: Write r  s as
temp1  R-S (r )
temp2  R-S ((temp1 x s ) – R-S,S (r ))
result = temp1 – temp2
 The result to the right of the  is assigned to the relation variable
on the left of the .
 May use variable in subsequent expressions

AE3B33OSD Lesson 9 / Page 29 Silberschatz, Korth, Sudarshan S. ©2007


Bank Example Queries
 Find the names of all customers who simultaneously have
a loan and an account at bank
customer_name (borrower)  customer_name (depositor)
 Find the name of all customers who have a loan at the
bank and the loan amount
customer_name, loan_number, amount (borrower ⋈ loan)
 Find all customers who have an account from at least the
“Downtown” and the "Uptown” branches
 Possibility 1
customer_name (branch_name = “Downtown” (depositor ⋈ account)) 
customer_name (branch_name = “Uptown” (depositor ⋈ account))
 Possibility 2
customer_name, branch_name (depositor ⋈ account)
 temp(branch_name) ({(“Downtown”), (“Uptown”)})
 Note, that this version uses a "constant relation"

AE3B33OSD Lesson 9 / Page 30 Silberschatz, Korth, Sudarshan S. ©2007


Extended Relational-Algebra-Operations

 Generalized Projection
 Aggregate Functions
 Outer Join

AE3B33OSD Lesson 9 / Page 31 Silberschatz, Korth, Sudarshan S. ©2007


Generalized Projection
 Extends the projection operation by allowing arithmetic
functions to be used in the projection list
∏F , F , ⋯, F ( E )
1 2 n
 E is any relational-algebra expression
 Each of F1, F2, …, Fn are are arithmetic expressions
involving constants and attributes in the schema of E.
 Is used to compute ‘derived’ (calculated) attributes
 Given relation
credit_info(customer_name, limit, credit_balance),
find how much more each person can spend:
customer_name, limit – credit_balance (credit_info)

AE3B33OSD Lesson 9 / Page 32 Silberschatz, Korth, Sudarshan S. ©2007


Aggregate Functions and Operations
 Aggregate 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
G1 , G2 ,…, Gn ϑ F 1 ( A1 ), F 2 ( A2 ,…, Fn ( A n ) ( E )

E is any relational-algebra expression


 G1, G2, …, Gn is a list of attributes on which to group (can be empty)
 Each Fi is an aggregate function
 Each Ai is an attribute name

AE3B33OSD Lesson 9 / Page 33 Silberschatz, Korth, Sudarshan S. ©2007


Aggregate Operation – Example
 Relation r: A B C sum(C)(r): sum(c )
  7
27
  7
  3
  10

 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 sum(balance)
 branch_name  sum(balance)(account):
Perryridge 1300
Brighton 1500
Redwood 700

AE3B33OSD Lesson 9 / Page 34 Silberschatz, Korth, Sudarshan S. ©2007


Outer Join
 An extension of the join operation that avoids loss of
information.
 Computes the join and then adds tuples form one relation
that does not match tuples in the other relation to the result
of the join.
 Uses null values:
 null signifies that the value is unknown or does not exist
 All comparisons involving null are (roughly speaking) false by
definition.
 We shall study precise meaning of comparisons with nulls later

AE3B33OSD Lesson 9 / Page 35 Silberschatz, Korth, Sudarshan S. ©2007


Outer Join – Example

Natural join

Left outer join

Right outer join

Full outer join

AE3B33OSD Lesson 9 / Page 36 Silberschatz, Korth, Sudarshan S. ©2007


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.
 Aggregate functions simply ignore null values
 For duplicate elimination and grouping, null is treated
like any other value, and two nulls are assumed to be
the same

AE3B33OSD Lesson 9 / Page 37 Silberschatz, Korth, Sudarshan S. ©2007


Null Values (cont.)
 Comparisons with null values return the special logical
value: unknown
 If false was used instead of unknown, then not (A < 5)
would not be equivalent to A >= 5
 Three-valued logic using the truth 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
 Result of select predicate is treated as false if it evaluates
to unknown

AE3B33OSD Lesson 9 / Page 38 Silberschatz, Korth, Sudarshan S. ©2007


Modification of the Database
 The content of the database may be modified using the
following operations:
 Deletion
 Insertion
 Updating
 All these operations are expressed using the assignment
operator.

AE3B33OSD Lesson 9 / Page 39 Silberschatz, Korth, Sudarshan S. ©2007


Deletion
 A delete request is expressed similarly to a query, except
instead of displaying tuples to the user, the selected tuples
are removed from the database.
 Can delete only whole tuples; cannot delete values on only
particular attributes
 A deletion is expressed in relational algebra by:
rr–E
where r is a relation and E is a relational algebra query
 Examples
 Delete all account records in the Perryridge branch
account  account – branch_name = “Perryridge” (account )

 Delete all loan records with amount in the range of 0 to 50


loan  loan – amount 0and amount  50(loan)

AE3B33OSD Lesson 9 / Page 40 Silberschatz, Korth, Sudarshan S. ©2007


Insertion
 To insert data into a relation, we either:
 specify a tuple to be inserted
 write a query whose result is a set of tuples to be inserted
 In relational algebra, an insertion is expressed by:
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
 Example:
 Insert information in the database specifying that Smith has $1200
in account A-973 at the Perryridge branch
account  account  {(“A­973”, “Perryridge”, 1200)}
depositor  depositor  {(“Smith”, “A­973”)}
 Provide as a gift for all loan customers in the Perryridge branch, a
$200 savings account. Let the loan number serve as the account
number for the new savings account.
r1  (branch_name = “Perryridge” (borrower ⋈ loan))
account  account  loan_number, branch_name, 200 (r1)
depositor  depositor  customer_name, loan_number (r1)
AE3B33OSD Lesson 9 / Page 41 Silberschatz, Korth, Sudarshan S. ©2007
Updating
 A mechanism to change a value in a tuple without
charging all values in the tuple
 Use the generalized projection operator to do this task
r ← ∏F , F 2 ,…, F l ,
(r )
1

 Each Fi is either
 the I th attribute of r, if the I th attribute is not updated, or,
 if the attribute is to be updated Fi is an expression, involving only
constants and the attributes of r, which gives the new value for the
attribute
 Examples
 Make interest payments by increasing all balances by 5 %
account   account_number, branch_name, balance * 1.05 (account)

 Pay all accounts with balances over $10,000 6 % interest and pay
all others 5 %
account   account_number, branch_name, balance * 1.06 ( BAL  10000 (account ))
  account_number, branch_name, balance * 1.05 (BAL  10000 (account))

AE3B33OSD Lesson 9 / Page 42 Silberschatz, Korth, Sudarshan S. ©2007


Lesson 9, part 2
Structured Query Language (SQL)
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
 Integrity constraints in create table
 not null
 primary key(A1, ..., AL )
 Example:
create table branch
( branch_name char(15) not null,
branch_city char(30),
assets integer,
primary key(branch_name)
)
 Note:
 SQL names are case insensitive (i.e., you may use upper- or
lower-case letters); e.g.: Branch_Name ≡ BRANCH_NAME ≡
branch_name
AE3B33OSD Lesson 9 / Page 44 Silberschatz, Korth, Sudarshan S. ©2007
Basic Query Structure
 SQL is based on set and relational operations with certain
modifications and enhancements
 A typical SQL query has the form:
select A1, A2, ..., An
from R1, R2, ..., Rm
where P
 Ai represents an attribute
 Ri represents a relation
 P is a predicate.
 This query is equivalent to the relational algebra expression
∏A , A , …, A
1 2 n
(σ P ( R1 ¿ R 2 ¿…×R m ))

 The result of an SQL query is a relation


 Important remark:
 SQL is a declarative (query) language while relational algebra is
procedural
 Mapping SQL queries to relational expressions converts declarative
queries to procedures
 Query execution will use procedures implementing relation algebra
operations
AE3B33OSD Lesson 9 / Page 45 Silberschatz, Korth, Sudarshan S. ©2007
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 branches in the loan relation:
select branch_name from loan
 In the relational algebra, the query would be:
branch_name (loan)
 SQL allows duplicates in relations as well as in query
results
 This violates relational model assumptions but may speed-up
processing
 To force the elimination of duplicates, insert the keyword
distinct after select.
 Find the names of all branches in the loan relations, and remove
duplicates
select distinct branch_name from loan
 The keyword all specifies that duplicates not be removed
select all branch_name from loan

AE3B33OSD Lesson 9 / Page 46 Silberschatz, Korth, Sudarshan S. ©2007


The select clause (cont.)
 An asterisk in the select clause denotes “all attributes”
select * from loan
 The select clause can contain arithmetic expressions
involving the operation, +, –, , and /, and operating on
constants or attributes of tuples
 The query
select loan_number, branch_name, amount  100
from loan
would return a relation that is the same as the loan relation, except
that the value of the attribute amount is multiplied by 100
 This is, in fact, the generalized projection
loan_number, branch_name, amount  100(loan)

AE3B33OSD Lesson 9 / Page 47 Silberschatz, Korth, Sudarshan S. ©2007


The where clause
 The where clause specifies conditions that the result
must satisfy
 Corresponds to the selection predicate of the relational algebra.
 Example
 Find all loan numbers for loans made at the Perryridge branch
with loan amounts greater than $1200.
select loan_number
from loan
where branch_name = 'Perryridge' and amount > 1200
 Comparison
 results can be combined using the logical connectives and, or,
and not.
 Comparisons may be applied to results of arithmetic expressions.
 SQL includes a between comparison operator
 Example: Find the loan number of those loans with loan amounts
between $90,000 and $100,000 (that is, $90,000 and $100,000)
select loan_number from loan
where amount between 90000 and 100000
which maps to
loan_number((amount ≥ 90000)(amount ≤ 100000)(loan))

AE3B33OSD Lesson 9 / Page 48 Silberschatz, Korth, Sudarshan S. ©2007


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 borrower x loan
select  from borrower, loan
 Find the name, loan number and loan amount of all customers
having a loan at the Brighton branch
select customer_name, borrower.loan_number, amount
from borrower, loan
where borrower.loan_number = loan.loan_number and
branch_name = 'Brighton'
corresponds to
 customer_name, borrower.loan_number, amount (
 borrower.loan_number = loan.loan_number  branch_name = 'Brighton‘
(borrower x loan))

AE3B33OSD Lesson 9 / Page 49 Silberschatz, Korth, Sudarshan S. ©2007


The Rename Operation
 The SQL allows renaming relations and attributes using the
as clause:
old-name as new-name
 Find the name, loan number and loan amount of all customers;
rename the column name loan_number as loan_id
select customer_name, borrower.loan_number as loan_id, amount
from borrower, loan
where borrower.loan_number = loan.loan_number
 Home work:
 Rewrite this query to relational expression

AE3B33OSD Lesson 9 / Page 50 Silberschatz, Korth, Sudarshan S. ©2007


Tuple Variables
 Tuple variables are defined in the from clause via the use
of the as clause
 Example
 Find the customer names and their loan numbers for all customers
having a loan at some branch
select customer_name, T.loan_number, S.amount
from borrower as T, loan as S
where T.loan_number = S.loan_number
 Find the names of all branches that have greater assets than some
branch located in Brooklyn
select distinct T.branch_name
from branch as T, branch as S
where T.assets > S.assets and S.branch_city = 'Brooklyn'

AE3B33OSD Lesson 9 / Page 51 Silberschatz, Korth, Sudarshan S. ©2007


SQL allows duplicates
 Multiset versions of some of the relational algebra
operators – given multiset relations r1 and r2:
   (r1): If there are c1 copies of tuple t1 in r1, and t1 satisfies selections
,, then there are c1 copies of t1 in  (r1).
  A (r ): For each copy of tuple t1 in r1, there is a copy of tuple A (t1)
in A (r1) where A (t1) denotes the projection of the single tuple t1.
 r1 x r2: If there are c1 copies of tuple t1 in r1 and c2 copies of tuple t2 in
r2, there are c1  c2 copies of the tuple t1t2 in r1 x r2
 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:
∏A 1, A 2 , …, A n
( σ P ( r 1 ¿ r 2 ¿…×r m ) )

AE3B33OSD Lesson 9 / Page 52 Silberschatz, Korth, Sudarshan S. ©2007


Set Operations
 The set operations union, intersect, and except
operate on relations and correspond to the relational
algebra operations 
 Find all customers who have a loan, an account, or both:
(select customer_name from depositor)
union
(select customer_name from borrower)
 Find all customers who have both a loan and an account:
(select customer_name from depositor)
intersect
(select customer_name from borrower)
 Find all customers who have an account but no loan
(select customer_name from depositor)
except
(select customer_name from borrower)

AE3B33OSD Lesson 9 / Page 53 Silberschatz, Korth, Sudarshan S. ©2007


Aggregate Functions in SQL
 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
 Find the average account balance at the Perryridge branch
select avg (balance)
from account
where branch_name = 'Perryridge'
 Find the number of depositors in the bank
select count (distinct customer_name)
from depositor

AE3B33OSD Lesson 9 / Page 54 Silberschatz, Korth, Sudarshan S. ©2007


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 predicate is null can be used to check for null
values.
 Example: Find all loan number which appear in the loan relation
with null values for amount.
select loan_number from loan
where amount is null
 The result of any arithmetic expression involving null is null
 Example: 5 + null returns null
 However, aggregate functions simply ignore nulls
 Any comparison with null returns unknown
 Example: 5 < null or null <> null or null = null
 Three-valued logic using the truth value unknown is the
same as above for relations
 “P is unknown” evaluates to true if predicate P evaluates
to unknown

AE3B33OSD Lesson 9 / Page 55 Silberschatz, Korth, Sudarshan S. ©2007


Nested Subqueries
 SQL provides a mechanism for the nesting of queries
 A subquery is a select-from-where expression that is
nested within another query
 A common use of subqueries is to perform tests for set
membership, set comparisons, and set cardinality
 Example:
 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 )

AE3B33OSD Lesson 9 / Page 56 Silberschatz, Korth, Sudarshan S. ©2007


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 a customer’s name,
loan number and branch name, but has no need to see the
loan amount. This person should see a relation described,
in SQL, by
(select customer_name, borrower.loan_number, branch_name
from borrower, loan
where borrower.loan_number = loan.loan_number )
 A view provides a mechanism to hide certain data from the
view of certain users.
 Any relation that is not of the conceptual model but is made visible
to a user as a “virtual relation” is called a view.
 A view is defined using the create view statement which
has the form
create view v as <query expression>
The view name is represented by v.
 Once a view is defined, the view name can be used to refer to the
virtual relation that the view generates
AE3B33OSD Lesson 9 / Page 57 Silberschatz, Korth, Sudarshan S. ©2007
Modification of the Database
 Deletion
 Statement is delete-from-where with the arguments similar to the
select-from-where construct
 Delete all account tuples at the Brighton branch
delete from account where branch_name = ‘Brighton‘
 Insertion
 Statement is: insert into relation values
<compatible_relation>
 Add a new tuple to account
insert into account (branch_name, balance, account_number)
values ('Perryridge', 1200, 'A-9732')
 Updates
 Statement is: update relation set attribute = expression where
condition
 Add 6% to accounts over $1000
update account set balance = balance1.06 where balance>1000

AE3B33OSD Lesson 9 / Page 58 Silberschatz, Korth, Sudarshan S. ©2007


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.
 Completely based on relational-algebra joins. SQL syntax
described in the SQL standards
 Example
 Find all customers who have either an account or a loan (but not
both) at the bank
select customer_name
from (depositor full outer join borrower )
where account_number is null or loan_number is null
AE3B33OSD Lesson 9 / Page 59 Silberschatz, Korth, Sudarshan S. ©2007
End of Lesson 10

Questions?

You might also like