Bazi Na Podatoci (Predavanja) PDF
Bazi Na Podatoci (Predavanja) PDF
1
Purpose of Database System
2
Is the WWW a DBMS?
Thought Experiment 1:
You and your project partner are editing the same file.
You both save it at the same time.
Whose changes survive?
A) Yours B) Partner’s C) Both D) Neither E) ???
Thought Experiment 2:
You’re updating a file.
The power goes out.
Which of your changes survive?
A) All B) None C) All Since last save D) ???
3
Levels of Abstraction
View of Data
An architecture for a database system
4
Instances and Schemas
Similar to types and variables in programming languages
Schema – the logical structure of the database
e.g., the database consists of information about a set of customers and
accounts and the relationship between them)
Analogous to type information of a variable in a program
Physical schema: database design at the physical level
Logical schema: database design at the logical level
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.
Data Models
5
Entity-Relationship Model
6
Relational Model
Attributes
Example of tabular data in the relational model
192-83-7465 Johnson
Alma Palo Alto A-101
019-28-3746 Smith
North Rye A-215
192-83-7465 Johnson
Alma Palo Alto A-201
321-12-3123 Jones
Main Harrison A-217
019-28-3746 Smith
North Rye A-201
7
Data Definition Language (DDL)
8
SQL
SQL: widely used non-procedural language
E.g. find the name of the customer with customer-id 192-83-7465
select [Link]-name
from customer
where [Link]-id = ‘192-83-7465’
E.g. find the balances of all accounts held by the customer with
customer-id 192-83-7465
select [Link]
from depositor, account
where [Link]-id = ‘192-83-7465’ and
[Link]-number = [Link]-number
Application programs generally access databases through
Language extensions to allow embedded SQL
Application program interface (e.g. ODBC/JDBC) which allow SQL
queries to be sent to a database
Database Users
Users are differentiated by the way they expect to interact
with the system
Application programmers – interact with system through
DML calls
Sophisticated users – form requests in a database query
language
Specialized users – write specialized database
applications that do not fit into the traditional data
processing framework
Naïve users – invoke one of the permanent application
programs that have been written previously
E.g. people accessing database over the web, bank tellers, clerical
staff
9
Database Administrator
Transaction Management
10
Storage Management
11
Application Architectures
Advantages of a DBMS
Data independence
Efficient data access
Data integrity & security
Data administration
Concurrent access, crash recovery
Reduced application development time
12
DBMS vs. IRS
Distribution of selected information
13
13.10.2011
Entity Sets
Relationship Sets
Design Issues
Mapping Constraints
Keys
E-R Diagram
Extended E-R Features
Design of an E-R Database Schema
Reduction of an E-R Schema to Tables
Entity Sets
1
13.10.2011
Attributes
An entity is represented by a set of attributes, that is
descriptive properties possessed by all members of an
entity set.
Example:
customer = (customer-id, customer-name,
customer-street, customer-city)
loan = (loan-number, amount)
Domain – the set of permitted values for each attribute
Attribute types:
Simple and composite attributes.
Single-valued and multi-valued attributes
E.g. multivalued attribute: phone-numbers
Derived attributes
Can be computed from other attributes
E.g. age, given date of birth
2
13.10.2011
Composite Attributes
Relationship Sets
3
13.10.2011
4
13.10.2011
Mapping Cardinalities
5
13.10.2011
Mapping Cardinalities
Mapping Cardinalities
6
13.10.2011
E-R Diagrams
7
13.10.2011
8
13.10.2011
Roles
Entity sets of a relationship need not be distinct
The labels “manager” and “worker” are called roles; they specify how
employee entities interact via the works-for relationship set.
Roles are indicated in E-R diagrams by labeling the lines that connect
diamonds to rectangles.
Role labels are optional, and are used to clarify semantics of the
relationship
Cardinality Constraints
We express cardinality constraints by drawing either a directed
line (), signifying “one,” or an undirected line (—), signifying
“many,” between the relationship set and the entity set.
E.g.: One-to-one relationship:
A customer is associated with at most one loan via the relationship
borrower
A loan is associated with at most one customer via borrower
9
13.10.2011
One-To-Many Relationship
Many-To-One Relationships
10
13.10.2011
Many-To-Many Relationship
11
13.10.2011
Keys
12
13.10.2011
13
13.10.2011
14
13.10.2011
15
13.10.2011
16
13.10.2011
Logins @ Hosts
17
13.10.2011
In this special case, where bar and beer determine a price, we can
omit price from the key, and remove the double diamond from
ThePrice.
Better: price is attribute of BBP.
Name Qty
Qty ID
Name
Shipment
This is wrong. Why?
Database System Concepts 2.36 ©Silberschatz, Korth and Sudarshan
18
13.10.2011
Name Qty
Name Qty
Ordered
Part-of is ID
many-many Part of Shipment
and not a weak Qty
relationship!
Shipped
Database System Concepts 2.37 ©Silberschatz, Korth and Sudarshan
Design Issues
19
13.10.2011
name name
Wrong:
Beers ManfBy Manfs
Example
20
13.10.2011
Avoid redundancy
Setting: client has (possibly vague) idea of what he/she wants. You must
design a database that represents these thoughts and only these thoughts.
Good:
Beers ManfBy Manfs
name manf
Bad: Repeats manufacturer
address for each beer
Beers Manf they manufacture.
addr
21
13.10.2011
Specialization
22
13.10.2011
Specialization Example
Generalization
23
13.10.2011
Design Constraints on a
Specialization/Generalization
Constraint on which entities can be members of a given
lower-level entity set.
condition-defined
E.g. all customers over 65 years are members of senior-
citizen entity set; senior-citizen ISA person.
user-defined
Constraint on whether or not entities may belong to more than
one lower-level entity set within a single generalization.
Disjoint
an entity can belong to only one lower-level entity set
Noted in E-R diagram by writing disjoint next to the ISA
triangle
Overlapping
an entity can belong to more than one lower-level entity set
24
13.10.2011
Design Constraints on a
Specialization/Generalization (Contd.)
Completeness constraint -- specifies whether or not an
entity in the higher-level entity set must belong to at least
one of the lower-level entity sets within a generalization.
total : an entity must belong to one of the lower-level entity sets
partial: an entity need not belong to one of the lower-level entity
sets
Aggregation
Consider the ternary relationship works-on, which we saw earlier
25
13.10.2011
Aggregation (Cont.)
Relationship sets works-on and manages represent overlapping
information
Every manages relationship corresponds to a works-on relationship
However, some works-on relationships may not correspond to any
manages relationships
So we can’t discard the works-on relationship
26
13.10.2011
Beers-Bars-Drinkers Example
27
13.10.2011
28
13.10.2011
29
13.10.2011
UML
30
13.10.2011
Entity sets are shown as boxes, and attributes are shown within the
box, rather than as separate ellipses in E-R diagrams.
Binary relationship sets are represented in UML by just drawing a
line connecting the entity sets. The relationship set name is written
adjacent to the line.
The role played by an entity set in a relationship set may also be
specified by writing the role name on the line, adjacent to the entity
set.
The relationship set name may alternatively be written in a box,
along with attributes of the relationship set, and the box is
connected, using a dotted line, to the line depicting the relationship
set.
Non-binary relationships drawn using diamonds, just as in ER
diagrams
31
13.10.2011
overlapping
disjoint
32
13.10.2011
33
13.10.2011
34
13.10.2011
Redundancy of Tables
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
E.g.: Instead of creating a table for relationship account-
branch, add an attribute branch to the entity set account
35
13.10.2011
36
13.10.2011
Relations Corresponding to
Aggregation
37
13.10.2011
Relations Corresponding to
Aggregation (Cont.)
E.g. to represent aggregation manages between relationship
works-on and entity set manager, create a table
manages(employee-id, branch-name, title, manager-name)
Table works-on is redundant provided we are willing to store
null values for attribute manager-name in table manages
End of Chapter 2
38
13.10.2011
39
13.10.2011
40
13.10.2011
Existence Dependencies
41
Chapter 3: Relational Model
Example of a Relation
1
Basic Structure
Formally, given sets D1, D2, …. Dn a relation r is a subset of
D1 x D2 x … x Dn
Thus a relation is a set of n-tuples (a1, a2, …, an) where
each ai Di
Example: if
customer-name = {Jones, Smith, Curry, Lindsay}
customer-street = {Main, North, Park}
customer-city = {Harrison, Rye, Pittsfield}
Then r = { (Jones, Main, Harrison),
(Smith, North, Rye),
(Curry, North, Rye),
(Lindsay, Park, Pittsfield)}
is a relation over customer-name x customer-street x customer-city
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. multivalued attribute values are not atomic
E.g. composite attribute values are not 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
2
Relation Schema
A1, A2, …, An are attributes
R = (A1, A2, …, An ) is a relation schema
E.g. Customer-schema =
(customer-name, customer-street, customer-city)
r(R) is a relation on the relation schema R
E.g. 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
customer
3
Relations are Unordered
Order of tuples is irrelevant (tuples may be stored in
an arbitrary order)
E.g. account relation with unordered tuples
Why Relations?
Very simple model.
Often a good match for the way we think about our data.
Abstract model that underlies SQL, the most important
language in DBMS’s today.
But SQL uses “bags” while the abstract relational model is set-
oriented.
4
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
E.g.: 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. two customers own an account)
the need for null values (e.g. represent a customer without an
account)
Normalization theory (Chapter 7) deals with how to design
relational schemas
5
The depositor Relation
6
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)
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.
K is a candidate key if K is minimal
Example: {customer-name} is a candidate key for Customer,
since it is a superkey (assuming no two customers can possibly
have the same name), and no subset of it is a superkey.
Example 1
Drinkers(name, addr, beersLiked, manf, favoriteBeer)
{name, beersLiked} FD’s all attributes, as seen.
Shows {name, beersLiked} is a superkey.
name beersLiked is false, so name is not a superkey.
beersLiked name also false, so beersLiked is not a superkey.
Thus, {name, beersLiked} is a key.
No other keys in this example.
Neither name nor beersLiked is on the right of any observed FD, so they
must be part of any superkey.
Important point: “key” in a relation refers to tuples, not the entities they
represent. If an entity is represented by several tuples, then entity-key will
not be the same as relation-key.
7
Example 2
Key Key
(2 attributes)
Superkey
8
Schema Diagram for the Banking Enterprise
Query Languages
Language in which user requests information
from the database.
Categories of languages
procedural
non-procedural
“Pure” languages:
Relational Algebra
Tuple Relational Calculus
Domain Relational Calculus
Pure languages form underlying basis of
query languages that people use.
9
Relational Algebra
Procedural language
Six basic operators
select
project
union
set difference
Cartesian product
rename
The operators take one or more relations as inputs and
give a new relation as a result.
• Relation r A B C D
1 7
5 7
12 3
23 10
1 7
23 10
10
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:
branch-name=“Perryridge”(account)
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
11
Project Operation
Notation:
Relations r, s: A B A B
1 2
2 3
1 s
r
r s: A B
1
2
1
3
12
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 (e.g., 2nd
column
of r deals with the same type of values as does the 2nd
column of s)
E.g. to find all customers with either an account or a loan
customer-name (depositor) customer-name (borrower)
Relations r, s: A B A B
1 2
2 3
1 s
r
r – s: A B
1
1
13
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
Cartesian-Product Operation-Example
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
14
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 = ).
If attributes of r(R) and s(S) are not disjoint, then renaming
must be used.
Composition of Operations
Can build expressions using multiple operations
Example: A=C(r x s)
rxs 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
A=C(r x s) A B C D E
1 10 a
2 20 a
2 20 b
Database System Concepts 3.30 ©Silberschatz, Korth and Sudarshan
15
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.
Banking Example
16
Example Queries
Find the loan number for each loan of an amount greater than
$1200
Example Queries
17
Example Queries
Find the names of all customers who have a loan at the Perryridge
branch.
customer-name (branch-name=“Perryridge”
([Link]-number = [Link]-number(borrower x loan)))
Example Queries
Find the names of all customers who have a loan at the Perryridge
branch.
Query 1
customer-name(branch-name = “Perryridge” (
[Link]-number = [Link]-number(borrower x loan)))
Query 2
customer-name([Link]-number = [Link]-number(
(branch-name = “Perryridge”(loan)) x borrower))
18
Example Queries
Find the largest account balance
Rename account relation as d
The query is:
balance(account) - [Link]
([Link] < [Link] (account x d (account)))
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
19
Additional Operations
Set intersection
Natural join
Division
Assignment
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)
20
Set-Intersection Operation - Example
Relation r, s: A B A B
1 2
2 3
1
r s
rs A B
2
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 t on r
r
t has the same value as t
s on s
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))
Database System Concepts 3.42 ©Silberschatz, Korth and Sudarshan
21
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
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 ) }
22
Division Operation – Example
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
23
Division Operation (Cont.)
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
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.
24
Example Queries
Find all customers who have an account from at least the
“Downtown” and the Uptown” branches.
Query 1
CN(BN=“Downtown”(depositor account))
CN(BN=“Uptown”(depositor account))
Query 2
customer-name, branch-name (depositor account)
temp(branch-name) ({(“Downtown”), (“Uptown”)})
Example Queries
Find all customers who have an account at all branches
located in Brooklyn city.
25
Extended Relational-Algebra-Operations
Generalized Projection
Outer Join
Aggregate Functions
Generalized Projection
Extends the projection operation by allowing arithmetic functions
to be used in the projection list.
26
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
sum-C
g sum(c) (r)
27
27
Aggregate Operation – Example
28
Outer Join
An extension of the join operation that avoids loss of information.
Computes the join and then adds tuples form one relation that do
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.
Will study precise meaning of comparisons with nulls later
Relation loan
Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
29
Outer Join – Example
Inner Join
loan Borrower
loan-number branch-name amount customer-name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
30
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
Is an arbitrary decision. Could have returned null as result instead.
We follow the semantics of SQL in its handling of null values
For duplicate elimination and grouping, null is treated like any
other value, and two nulls are assumed to be the same
Alternative: assume each null is different from each other
Both are arbitrary decisions, so we simply follow SQL
Null Values
Comparisons with null values return the special truth 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
In SQL “P is unknown” evaluates to true if predicate P evaluates
to unknown
Result of select predicate is treated as false if it evaluates to
unknown
31
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.
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:
rr–E
where r is a relation and E is a relational algebra query.
32
Deletion Examples
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.
33
Insertion Examples
Insert information in the database specifying that Smith has
$1200 in account A-973 at the Perryridge branch.
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 F1, F2, …, FI, (r)
Each Fi is either
the ith attribute of r, if the ith 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
34
Update Examples
Make interest payments by increasing all balances by 5 percent.
Views
In some cases, it is not desirable for all users to see the entire
logical model (i.e., all the actual relations stored in the database.)
Consider a person who needs to know a customer’s loan number
but has no need to see the loan amount. This person should see
a relation described, in the relational algebra, by
customer-name, loan-number (borrower loan)
Any relation that is not of the conceptual model but is made
visible to a user as a “virtual relation” is called a view.
35
View Definition
A view is defined using the create view statement which has the
form
create view v as <query expression
View Examples
Consider the view (named all-customer) consisting of branches
and their customers.
customer-name
(branch-name = “Perryridge” (all-customer))
36
Updates Through View
Database modifications expressed as views must be translated
to modifications of the actual relations in the database.
Consider the person who needs to see all loan data in the loan
relation except amount. The view given to the person, branch-
loan, is defined as:
create view branch-loan as
branch-name, loan-number (loan)
Since we allow a view name to appear wherever a relation name
is allowed, the person may write:
37
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.
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
38
Tuple Relational Calculus
A nonprocedural query language, where each query is of the form
{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
39
Banking Example
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 the loan-number, branch-name, and amount for loans of
over $1200
{t | t loan t [amount] 1200}
Find the loan number for each loan of an amount greater than $1200
40
Example Queries
Find the names of all customers having a loan, an account, or
both at the bank
Find the names of all customers who have a loan and an account
at the bank
Example Queries
Find the names of all customers having a loan at the Perryridge
branch
{t | s borrower(t[customer-name] = s[customer-name]
u loan(u[branch-name] = “Perryridge”
u[loan-number] = s[loan-number]))}
41
Example Queries
Find the names of all customers having a loan from the
Perryridge branch, and the cities they live in
{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])))}
Example Queries
Find the names of all customers who have an account at all
branches located in Brooklyn:
42
Safety of Expressions
It is possible to write tuple calculus expressions that generate
infinite relations.
For example, {t | t r} results in an infinite relation if the
domain of any attribute of relation r is infinite
To guard against the problem, we restrict the set of allowable
expressions to safe expressions.
An expression {t | P(t)} in the tuple relational calculus is safe if
every component of t appears in one of the relations, tuples, or
constants that appear in P
NOTE: this is more than just a syntax condition.
E.g. { t | t[A]=5 true } is not safe --- it defines an infinite set with
attribute values that do not appear in any relation or tuples or
constants in P.
43
Example Queries
Find the loan-number, branch-name, and amount for loans of over
$1200
{ l, b, a | l, b, a loan a > 1200}
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:
Example Queries
Find the names of all customers having a loan, an account, or
both at the Perryridge branch:
{ 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)}
44
Safety of Expressions
{ x1, x2, …, xn | P(x1, x2, …, xn)}
End of Chapter 3
45
Result of branch-name = “Perryridge” (loan)
46
Names of All Customers Who Have
Either a Loan or an Account
47
Result of borrower loan
48
Result of customer-name
49
Largest Account Balance in the Bank
50
Customers With Both an Account and a Loan
at the Bank
51
Result of branch-name(customer-city =
“Harrison”(customer account depositor))
Result of branch-name(branch-city =
“Brooklyn”(branch))
52
Result of customer-name, branch-name(depositor account)
53
Result of customer-name, (limit – credit-balance) as
credit-available(credit-info).
54
The pt-works Relation After Grouping
55
Result of branch-name sum salary, max(salary) as
max-salary (pt-works)
56
The Result of employee ft-works
57
Result of employee ft-works
58
Tuples Inserted Into loan and borrower
59
E-R Diagram
60
The loan Relation
61
Chapter 7: SQL
Basic Structure
Simple Queries
Nested Subqueries
Aggregate Functions
Set Operations
With Clause
Views
Modification of the Database
Joined Relations
Data Definition Language
Embedded SQL, ODBC and JDBC
Basic 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 predicate
Ais represent attributes
ris represent relations (tables)
predicate is any predicate.
This query is equivalent to the relational algebra expression.
A1, A2, ..., An(P (r1 x r2 x ... x rm))
The result of an SQL query is a relation.
NOTE: SQL does not permit the ‘-’ character in names. SQL names are
case insensitive, i.e. you can use capital or small letters.
SubQueries
S# Sname Status City Get suppliers names for suppliers who supplies part P2.
S1 Smith 20 London
select [Link]
S2 Jones 10 Paris
from S
S3 Blake 30 Paris
where S.S# in ( select SP.S#
S4 Clark 20 London
from SP
S5 Adams 30 Athens
where SP.P# = ‘P2’ ) ;
P# Pname Color Weight City
P1 Nut Red 12 London Result: The nested subqueries are evaluated first.
P2 Bolt Green 17 Paris Sname So, our query is equivalent to:
P3 Screw Blue 17 Rome Smith select [Link]
Jones from S
P4 Screw Red 14 London Blake
P5 Cam Blue 12 Paris Clark where S.S# in ( ‘S1’, ‘S2’, ‘S3’, ‘S4’ ) ;
P6 Cog Red 19 London
The same using join.
S# P# QTY
S1 P1 300 S2 P1 300 select [Link]
S1 P2 200 S2 P2 400 from S, SP
S1 P3 400 S3 P2 200 where S.S# = SP.S# and SP.P# = ‘P2’ ;
S1 P4 200 S4 P2 200 The join of S and SP over supplier numbers
S1 P5 100 S4 P4 300 is a table of 12 rows from which we select
S1 P6 100 S4 P5 400 those 4 rows that have the part number P2.
With Clause
Get all supplier names with maximum status.
S# Sname Status City
with maxst(value) as
S1 Smith 20 London
select max(Status) Result:
S2 Jones 10 Paris
from S
S3 Blake 30 Paris
Sname
select Sname
Blake
S4 Clark 20 from S
London
Adams
S5 Adams 30 where Status = [Link];
Athens
P# Pname Color Weight City With clause allows views to be defined locally to a query, rather
P1 Nut Red 12 London than globally. Analogous to procedures in a programming language.
P2 Bolt Green 17 Paris Get all part numbers where the total their shipments is greater
P3 Screw Blue 17 Rome than the average of the total supplier shipments at all
P4 Screw Red 14 London suppliers.
P5 Cam Blue 12 Paris with ptotal(P#, value) as
P6 Cog Red 19 London select P#, sum(QTY) Result:
S# P# QTY from SP P#
S1 P1 300 S2 P1 300 group by P# P1
S1 P2 200 S2 P2 400
with pavg(S#, value) as P2
select S#, avg(QTY)
S1 P3 400 S3 P2 200
from SP
S1 P4 200 S4 P2 200
group by P#
S1 P5 100 S4 P4 300 select P#
S1 P6 100 S4 P5 400 from ptotal, pavg
where [Link] > [Link];
Views
S# Sname Status City Create view from good suppliers (with status greater
S1 Smith 20 London than 15).
S2 Jones 10 Paris
S# Status City
S3 Blake 30 Paris create view GoodSup S1 20 London
S4 Clark 20 London as select S#, Status,City
S5 Adams 30 Athens S3 30 Paris
from S S4 20 London
P# Pname Color Weight City where Status > 15 ; S5 30 Athens
P1 Nut Red 12 London
P2 Bolt Green 17 Paris
GoodSup is in effect a “window” into real table S. The window
P3 Screw Blue 17 Rome
is dynamic because changes of S is automatically visible
P4 Screw Red 14 London
through the window GoodSup. Some users may genuinely
P5 Cam Blue 12 Paris believe that GoodSup is a “real” table.
P6 Cog Red 19 London
S# P# QTY Provide a mechanism to hide certain data from the view of
S1 P1 300 S2 P1 300 certain users. To create a view we use the command:
S1 P2 200 S2 P2 400
create view v as <query expression>
S1 P3 400 S3 P2 200
S1 P4 200 S4 P2 200 where:
S1 P5 100 S4 P4 300 • <query expression> is any legal expression
S1 P6 100 S4 P5 400 • the view name is represented by v
Motivating example
Transfer of money from one account to another involves two steps:
deduct from one account and credit to another
If one steps succeeds and the other fails, database is in an
inconsistent state
Therefore, either both steps should succeed or neither should
If any step of a transaction fails, all work done by the transaction
can be undone by rollback work.
Rollback of incomplete transactions is done automatically, in case
of system failures
Transactions (Cont.)
In most database systems, each SQL statement that executes
successfully is automatically committed.
Each transaction would then consist of only a single statement
Automatic commit can usually be turned off, allowing multi-
statement transactions, but how to do so depends on the database
system
Another option in SQL:1999: enclose statements within
begin atomic
…
end
Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
Note: borrower information missing for L-260 and loan
information missing for L-155
select customer-name
from (depositor natural full outer join borrower)
where account-number is null or loan-number is null
1
13.10.2011
2
13.10.2011
Example
Consider the relation schema:
Lending-schema = (branch-name, branch-city, assets,
customer-name, loan-number, amount)
Redundancy:
Data for branch-name, branch-city, assets are repeated for each loan
that a branch makes
Wastes space
Complicates updating, introducing possibility of inconsistency of
assets value
Null values
Cannot store information about a branch if no loans exist
Can use null values, but they are difficult to handle.
Database System Concepts 7.6 ©Silberschatz, Korth and Sudarshan
3
13.10.2011
Decomposition
Decomposition of R = (A, B)
R1 = (A) R2 = (B)
A B A B
1 1
2 2
1 B(r)
A(r)
r
A B
A (r) B (r)
1
2
1
2
4
13.10.2011
Functional Dependencies
5
13.10.2011
6
13.10.2011
Example
Drinkers(name, addr, beersLiked, manf, favoriteBeer)
Functional Dependencies
7
13.10.2011
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
from CG H and CG I : “union rule” can be inferred from
– definition of functional dependencies, or
– Augmentation of CG I to infer CG CGI, augmentation of
CG H to infer CGI HI, and then transitivity
8
13.10.2011
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
9
13.10.2011
10
13.10.2011
Goals of Normalization
Decide whether a particular relation R is in “good” form.
In the case that a relation R is not in “good” form, decompose it
into a set of relations {R1, R2, ..., Rn} such that
each relation is in good form
the decomposition is a lossless-join decomposition
Our theory is based on:
functional dependencies
multivalued dependencies
11
13.10.2011
Decomposition
12
13.10.2011
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)
13
13.10.2011
Example
Drinkers (name, addr, beersLiked, manf, favoriteBeer)
is trivial (i.e., )
is a superkey for R
14
13.10.2011
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
Example of Problems
Drinkers(name, addr, beersLiked, manf, favoriteBeer)
name addr beersLiked manf favoriteBeer
Janeway Voyager Bud A.B. WickedAle
Janeway ??? WickedAle Pete's ???
Spock Enterprise Bud ??? Bud
FD’s:
1. name addr
2. name favoriteBeer
3. beersLiked manf
???’s are redundant, since we can figure them out from the FD’s.
Update anomalies: If Janeway gets transferred to the Intrepid,
will we change addr in each of her tuples?
Deletion anomalies: If nobody likes Bud, we lose track of Bud’s
manufacturer.
15
13.10.2011
Another Example
Beers(name, manf, manfAddr). (Note: 2NF is satisfied)
FD’s = name manf, manf manfAddr.
Only key is name.
manf manfAddr violates BCNF with a left side unrelated to
any key.
16
13.10.2011
17
13.10.2011
R X X+
Example
R = Drinkers(name, addr, beersLiked, manf, favoriteBeer)
F=
1. name addr
2. name favoriteBeer
3. beersLiked manf
18
13.10.2011
(Repeating)
Decomposed relations:
Drinkers1(name, addr, favoriteBeer)
Drinkers2(name, beersLiked, manf)
Projected FD’s:
For Drinkers1: name addr and name favoriteBeer.
For Drinkers2: beersLiked manf.
BCNF violations?
For Drinkers1, name is key and all left sides of FD’s are
superkeys.
For Drinkers2, {name, beersLiked} is the key, and beersLiked
manf violates BCNF.
Decompose Drinkers2
First set of decomposed relations:
Drinkers1(name, addr, favoriteBeer)
Drinkers2(name, beersLiked, manf)
19
13.10.2011
R = (J, K, L)
F = {JK L
L K}
Two candidate keys = JK and JL
R is not in BCNF
Any decomposition of R will fail to preserve
JK L
20
13.10.2011
Example
One FD structure causes problems:
If you decompose, you can’t check all the FD’s only in the
decomposed relations.
If you don’t decompose, you violate BCNF.
Abstractly: R = (A, B, C), F = {AB C, C B.}
Example: street city zip, zip city.
Keys: {A, B} and {A, C}, but C B has a left side that is not a superkey.
21
13.10.2011
22
13.10.2011
23
13.10.2011
Example
Relation schema:
Banker-info-schema = (branch-name, customer-name,
banker-name, office-number)
The functional dependencies for this relation schema are:
banker-name branch-name office-number
customer-name branch-name banker-name
The key is:
{customer-name, branch-name}
24
13.10.2011
null b2 c2
25
13.10.2011
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
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.
Multivalued Dependencies
There are database schemas in BCNF that do not seem to be
sufficiently normalized
Consider a database
classes(course, teacher, book)
26
13.10.2011
course teacher
database Avi
database Hank
database Sudarshan
operating systems Avi
operating systems Jim
teaches
course book
database DB Concepts
database Ullman
operating systems OS Concepts
operating systems Shaw
text
We shall see that these two relations are in Fourth Normal
Form (4NF)
Database System Concepts 7.54 ©Silberschatz, Korth and Sudarshan
27
13.10.2011
X Y others
Example (Cont.)
In our example:
course teacher
course book
The above formal definition is supposed to formalize the
notion that given a particular value of Y (course) it has
associated with it a set of values of Z (teacher) and a set
of values of W (book), 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.
28
13.10.2011
Example
Drinkers(name, addr, phones, beersLiked)
with MVD Name phones. If Drinkers has the two tuples:
it must also have the same tuples with phones components swapped:
MVD Rules
1. Every FD is an MVD.
Because if X Y, then swapping Y’s between tuples that agree on X
doesn’t create new tuples.
Example, in Drinkers: name addr.
2. Complementation: if X Y, then X Z, where Z is all
attributes not in X or Y.
Example: since name phones
holds in Drinkers, so does
name addr beersLiked.
29
13.10.2011
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
30
13.10.2011
Example
Drinkers(name, addr, phones, beersLiked)
FD: name addr
Nontrivial MVD’s: name phones and
name beersLiked.
Only key: {name, phones, beersLiked}
All three dependencies above violate 4NF.
Successive decomposition yields 4NF relations:
D1(name, addr)
D2(name, phones)
D3(name, beersLiked)
31
13.10.2011
Normalization
No transitive
dependency
between
nonkey
Functional
attributes
dependency
of nonkey
attributes on
Boyce- the primary
No key - Atomic
multivalued values only
4NF
dependency
Full
Codd Functional
dependency
All of nonkey
determinants attributes on
are candidate the primary
keys - Single key
multivalued
dependency
32
13.10.2011
33
13.10.2011
34
13.10.2011
End of Chapter
35
13.10.2011
Sample Relation r
36
13.10.2011
37
13.10.2011
38
13.10.2011
An Instance of Banker-schema
39
13.10.2011
40
13.10.2011
An Illegal bc Relation
Decomposition of loan-info
41
13.10.2011
42
A Form with Visual Basic
Cisti_Click()
sdob najdi Prikazi_Click()
sdel najdi Nov_Click()
kol najdi Otvori_Click()
Brisi_Click()
data najdi
Nazad_Click()
1
Cisti_Click()
sdob najdi Prikazi_Click()
sdel najdi Nov_Click()
kol najdi Otvori_Click()
Brisi_Click()
data najdi
Nazad_Click()
2
Procedure for a Query Building
Option Compare Database
Option Explicit
Private Sub AddToWhere(FieldValue As Variant, FieldName As String,
MyCriteria As String, ArgCount As Integer)
' Create criteria for WHERE clause.
If FieldValue <> "" Then
' Add "and" if other criterion exists. Chr(34) = “
If ArgCount > 0 Then
Chr(39) = ‘
MyCriteria = MyCriteria & " and "
End If Chr(42) = *
‘ Err_Cisti_Click:
' MsgBox "Greska-->" & [Link], vbInformation, "Greska"
' Resume Exit_Cisti_Click
End Sub
Database System Concepts 1.6 ©Silberschatz, Korth and Sudarshan
3
Procedure for Searching in Database
Private Sub Prikazi_Click()
On Error GoTo Err_Prikazi_Click
Dim MySQL As String, MyCriteria As String, MyRecordSource As String
Dim ArgCount As Integer
Dim Tmp As Variant
' Initialize argument count.
ArgCount = 0
' Initialize SELECT statement.
MySQL = "SELECT * FROM NajdiNaracka WHERE "
MyCriteria = ""
' Use values entered in text boxes in form header to create criteria for WHERE clause.
AddToWhere [sdob najdi], "[[Link]]", MyCriteria, ArgCount
AddToWhere [sdel najdi], "[[Link]]", MyCriteria, ArgCount
AddToWhere [kol najdi], "[[Link]]", MyCriteria, ArgCount
AddToWhere [data najdi], "[[Link]]", MyCriteria, ArgCount
Continues
' Set RecordSource property of Find Customers Subform.
Me![Naracka subform].[Link] = MyRecordSource
Err_Prikazi_Click:
MsgBox "Greska-->" & [Link], vbInformation, "Greska"
Resume Exit_Prikazi_Click
End Sub
4
Do almost Nothing
Private Sub Form_Activate()
' Used by Solutions to show toolbar that includes Show Me button.
' Hide built-in Form View toolbar.
' Show Custom Form View toolbar.
' [Link] "Form View", A_TOOLBAR_NO
' [Link] "Custom Form View", A_TOOLBAR_YES
End Sub
Levels of Abstraction
Private Sub Nov_Click()
On Error GoTo Err_Nov_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Naracka"
[Link] stDocName, , , stLinkCriteria, acFormAdd
Exit_Nov_Click: Exit Sub
Err_Nov_Click:
MsgBox [Link] Private Sub Otvori_Click()
Resume Exit_Nov_Click On Error GoTo Err_Otvori_Click
End Sub Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Naracka"
If IsNull(Me![Naracka subform].Form![sdob]) Then
MsgBox "-->Nemate sifra za ovoj zapis!", vbInformation, "Greska"
Else
stLinkCriteria = "[sdob]='" & Me![Naracka subform].Form![sdob] & "'"
[Link] stDocName, , , stLinkCriteria, acFormEdit
End If
Exit_Otvori_Click: Exit Sub
Err_Otvori_Click:
MsgBox "Greska-->" & [Link], vbInformation, "Greska"
Resume Exit_Otvori_Click
End Sub
5
Instances and Schemas
Private Sub Brisi_Click()
On Error GoTo Err_Brisi_Click
Err_Brisi_Click:
MsgBox "Greska-->" & [Link], vbInformation, "Greska"
Resume Exit_Brisi_Click
End Sub
Err_Nazad_Click:
MsgBox [Link]
Resume Exit_Nazad_Click
End Sub