View
217
Download
2
Tags:
Embed Size (px)
Citation preview
Some slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed.
Midterm ReviewMidterm Review
Database Management Systems I
Alex Coman, Winter 2006
Alex Coman Winter 2006
2
Midterm Review Database Management Systems I
SummarySummary
Introduction (Ch. 1)
Entity-Relationship Model (Sec. 6.1-6.9)
Relational Model and Algebra (Ch. 2)
Introduction to SQL (Ch. 3)
Advanced SQL (Sec. 4.1-4.5,4.8)
Alex Coman Winter 2006
3
Midterm Review Database Management Systems I
Purpose of Database SystemsPurpose of Database Systems
In the early days, database applications were built directly on top of file systems
Drawbacks of using file systems to store data:
Data redundancy and inconsistency
Difficulty in accessing data
Data isolation — multiple files and formats
Integrity problems
Atomicity of updates
Concurrent access by multiple users
Security problems
Database systems offer solutions to all the above problems
Alex Coman Winter 2006
4
Midterm Review Database Management Systems I
Overall Structure of a DBMSOverall Structure of a DBMS
Alex Coman Winter 2006
5
Midterm Review Database Management Systems I
Database AdministratorDatabase Administrator
Coordinates all the activities of the database system; the database administrator has a good understanding of the enterprise’s information resources and needs.
Database administrator's duties include:
Schema definition
Storage structure and access method definition
Schema and physical organization modification
Granting user authority to access the database
Maintenance
Specifying integrity constraints
Acting as liaison with users
Monitoring performance and responding to changes in requirements
Alex Coman Winter 2006
6
Midterm Review Database Management Systems I
Entity-Relatioship ModelEntity-Relatioship Model Design Process
characterize the data needs
conceptual design (E-R modeling)
specification of functional requirements
abstract model to implementation (reduction to relational schemas)
E-R Modeling entity and entity set
relationship and relationship set
attributes
constraints: keys, cardinality and participation
weak entity sets
specialization/generalization
aggregation
Reduction to Relation Schemas
Alex Coman Winter 2006
7
Midterm Review Database Management Systems I
ER Modeling (1)ER Modeling (1) Mapping cardinality constraints
Express the number of entities to which another entity can be associated via a relationship set.
For a binary relationship: one-to-one, one-to-many, many-to-one, many-to-many
Participation constraints
Total vs. partial participation
Keys for relationship sets
The combination of primary keys of the participating entity sets forms a super key of a relationship set
Must consider the mapping cardinality of the relationship set when deciding what are the candidate keys
Alex Coman Winter 2006
8
Midterm Review Database Management Systems I
ER Modeling (2)ER Modeling (2) Weak entity set
An entity set that does not have a primary key and whose existence depends on the existence of an identifying entity set
The primary key of a weak entity set is formed by the primary key of the identifying entity set plus the weak entity set’s discriminator (partial key)
“Each geographical location is described by city, province and country and uniquely identified by a location-id. In this context, each 330music store is identified by its address which is unique for a location.”
Mapping to relations:
• location (loc-id,…)
• stores (loc-id, address,…)
• has is redundant (why?)
Alex Coman Winter 2006
9
Midterm Review Database Management Systems I
ER Modeling (3)ER Modeling (3) Specialization/generalization
A lower-level entity set inherits all the attributes (including the primary key) of the higher-level entity set to which it is linked
Lower-level entity sets may have attributes or participate in relationships that do not apply to the higher-level entity set
“The preferred customers are uniquely identified by a customer-id, and their names and phone numbers are also available. The preferred customers may belong in one or both of two programs: Fastlane … and Newsletter …”
Mapping to relations:
• customer (cust-id, name,phone)• fastlane (cust-id, cc)• newsletter (cust-id, email)
Alternative representation(is it?):
• fastlane (cust-id, name,phone, cc)
• newsletter (cust-id, name, phone, email)
Alex Coman Winter 2006
10
Midterm Review Database Management Systems I
ER Modeling (4)ER Modeling (4) 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
“Each track belongs to a genre (and one genre only) …”
Mapping to relations:
• tracks (track-id, artist, title, length, year, genre-id)
• genre (genre-id, main, sub, style)
• has is redundant (why?)
Alex Coman Winter 2006
11
Midterm Review Database Management Systems I
ER Design Issues and DecisionsER Design Issues and Decisions
Use of entity sets vs. attributes
Choice mainly depends on the structure of the enterprise being modeled, and on the associated semantics
Use of entity sets vs. relationship sets
Possible guideline is to designate a relationship set to describe an action that occurs between entities
Binary versus n-ary relationship sets
Use n-ary relationship set when it shows more clearly that several entities participate in a single relationship (eg: 330music history).
Mapping cardinalities affect ER design
Placement of relationship attributes
Use of a strong or weak entity set
Use of specialization/generalization
Alex Coman Winter 2006
12
Midterm Review Database Management Systems I
E-R Diagram for a Banking EnterpriseE-R Diagram for a Banking Enterprise
Mapping to relations (partial):
• payment (loan-number, payment-number, date, amount)
• Is loan_payment redundant?
• account (acc-number, balance)
• checking (acc-number, overdraft)
• savings (acc-number, interest)
• depositor (cust-id, acc-number , access-date)
Alex Coman Winter 2006
13
Midterm Review Database Management Systems I
Relational ModelRelational Model Structure of relational databases
a relation is a set of n-tuples (a1, a2, …, an) where each ai Di
R = (A1, …, An ) is a relation schema, where A1,…, An are attributes
Fundamental relational-algebra operations
select (), project (), union (), set difference (–), Cartesian product (x), rename ()
Additional relational-algebra operations
set intersection (), natural join ( ), division (), assignment ()
Extended relational-algebra operations
generalized projection, aggregate functions, outer join
Null values
a value does not exist or an unknown value
Modification of the database
deletion, insertion, update
Alex Coman Winter 2006
14
Midterm Review Database Management Systems I
Cartesian Product vs. Natural JoinCartesian Product vs. Natural Join Relations r, s:
r x s:
A B
1
2
A B
11112222
A C
1010201010102010
D
aabbaabb
A C
10102010
D
aabbr
s
r s:
A B
122
C
101020
D
aab
Alex Coman Winter 2006
15
Midterm Review Database Management Systems I
Rename OperationRename Operation
Renaming a relation (or expression)
x (E)returns the expression E under the name X
Renaming a relation (or expression) and its attributes
If a relational-algebra expression E has arity n, then
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
)(),...,,( 21E
nAAAx
Alex Coman Winter 2006
16
Midterm Review Database Management Systems I
Aggregate Functions and OperationsAggregate Functions and Operations
Aggregation functions:
avg, min, max, sum, count
take a collection of values and returns a single value as a result
Aggregate operation in relational algebra (calligraphic G)
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
use renaming operator or as part of aggregate operation (with as)
branch_name g sum(balance) as sum_balance (account)
sum_account(branch_name, sum_balance) (branch_name g sum(balance) (account))
)()(,),(),(,,, 221121E
nnn AFAFAFGGG g
Alex Coman Winter 2006
17
Midterm Review Database Management Systems I
Division OperationDivision Operation
Suited to queries that include the phrase “for all” (or “every”).
A B
aaaaaaaa
C D
aabababb
E
11113111
Relations r, s:
r s:
D
ab
E
11
A B
aa
C
r
s
Alex Coman Winter 2006
18
Midterm Review Database Management Systems I
Example (1)Example (1)
Find the accounts held by more than two customers
a. using an aggregate function
b. without using any aggregate functions
Alex Coman Winter 2006
19
Midterm Review Database Management Systems I
Example (2)Example (2)
Find the names of employees who have borrowed all books published by McGraw-Hill.
For each publisher, find the names of employees who have borrowed more than five books of that publisher
Alex Coman Winter 2006
20
Midterm Review Database Management Systems I
SQL (1)SQL (1) Data Definition Language (DDL)
constructs: create table, drop table, alter table
constraints: primary key, unique, not null
Basic Query Structure: select … from … where …
distinct vs. all
rename operation: as
tuple variables
Set Operations
union, intersect, except
Aggregate Functions
avg, min, max, sum, count
group by and having clauses
Alex Coman Winter 2006
21
Midterm Review Database Management Systems I
SQL (2)SQL (2)
Null Values
is null and is not null predicates
Nested Subqueries
set membership: in, not in
set comparison: some, any
empty relations: exist, not exist
set containment and division:
not exist (B except A) to test if B A
absence of duplicates: unique, not unique
Complex Queries
derived relations
with clause
Alex Coman Winter 2006
22
Midterm Review Database Management Systems I
SQL (3)SQL (3) Views
virtual relations
create view, drop view constructs
Joined Relations
join types: inner join, left/right/full outer join
join conditions: natural, on <predicate>, using (A1,…,Ak)
Modification of the Database
deletion
insertion
updates
case construct
Alex Coman Winter 2006
23
Midterm Review Database Management Systems I
Cartesian Product vs. Natural JoinCartesian Product vs. Natural Join Relations r, s:
A B
1
2
A B
11112222
A C
1010201010102010
D
aabbaabb
A C
10102010
D
aabb
r s
A B
122
C
101020
D
aab
select * from r, s
select A,B,C,D from r, s where r.a=s.a
select * from r natural inner join s
Alex Coman Winter 2006
24
Midterm Review Database Management Systems I
Example (1)Example (1)
Find the names of employees who have borrowed all books published by McGraw-Hill.
Alex Coman Winter 2006
25
Midterm Review Database Management Systems I
Example (2)Example (2)
For each publisher, find the names of employees who have borrowed more than five books of that publisher
Alex Coman Winter 2006
26
Midterm Review Database Management Systems I
Advanced SQL (1)Advanced SQL (1) SQL Data Types and Schemas
Basic: int, char (n), numeric (p,d), float (p), real
Advanced: date, time, timestamp, interval, blob, clob
User-defined: create type, create domain
Integrity Constraints
check (P)
referential integrity: primary/foreign/unique key
assertions
Authorization
access: read, insert, update, delete
modification: resources, drop, alteration, index
grant and revoke statements
Alex Coman Winter 2006
27
Midterm Review Database Management Systems I
Advanced SQL (2)Advanced SQL (2) Embedded SQL
pre-defined statements
EXEC SQL statement
open, fetch, close operations on cursors
Dynamic SQL
run-time statements
ODBC vs. JDBC APIs
connect to database
execute SQL statement
fetch results
prepared statements, metadata features, transactions
Advanced SQL Features*
Alex Coman Winter 2006
28
Midterm Review Database Management Systems I
More Examples (1)More Examples (1)
Find all branches where the total account deposit is less than the average total account deposit at all branches using a nested query in the from clause
Alex Coman Winter 2006
29
Midterm Review Database Management Systems I
More Examples (2)More Examples (2)
Find all branches where the total account deposit is less than the average total account deposit at all branches using a nested query in the having clause
Some slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed.
EndEnd