Download presentation
1
Database Management Systems I Alex Coman, Winter 2006
Midterm Review Database Management Systems I Alex Coman, Winter 2006
2
Summary 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.8)
3
Purpose 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
4
Overall Structure of a DBMS
5
Database 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
6
Entity-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
7
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
8
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?)
9
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, ) Alternative representation(is it?): fastlane (cust-id, name,phone, cc) newsletter (cust-id, name, phone, )
10
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?)
11
ER 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
12
E-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)
13
Relational 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
14
Cartesian Product vs. Natural Join
Relations r, s: A C D A B 10 20 a b 1 2 r s r x s: r s: A B A C D A B C D 1 2 10 20 a b 1 2 10 20 a b
15
Rename 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 .
16
Aggregate 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))
17
Division Operation Suited to queries that include the phrase “for all” (or “every”). Relations r, s: A B C D E D E a a b 1 3 a b 1 s r r s: A B C a
18
Example (1) Find the accounts held by more than two customers
using an aggregate function without using any aggregate functions
19
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
20
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 1
21
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 1
22
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 1
23
Cartesian Product vs. Natural Join
Relations r, s: r s A B A C D 1 2 10 20 a b select * from r, s A B A C D select A,B,C,D from r, s where r.a=s.a 1 2 10 20 a b A B C D select * from r natural inner join s 1 2 10 20 a b
24
Example (1) Find the names of employees who have borrowed all books published by McGraw-Hill.
25
Example (2) For each publisher, find the names of employees who have borrowed more than five books of that publisher
26
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
27
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*
28
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
29
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
30
End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.