Docsity

Database Management System-Database Management using Database Principles, Programming and Performance-Lecture 01 Notes-Computer Science, Study notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
-1-
Lecture Notes for
Database Management, using
Database Principles, Programming and Performance
Notes by Patrick E. O'Neil
Chapters 1 and 2
Class 1.
Hand out: Syllabus. Go over Syllabus, prerequisites. Grader, DBA.
Homework: Read Chapter 1 (quickly), and Chapter 2 through Section 2.4.
Homework in two weeks: All undotted exercises at end of Chapter 2.
2.1(b, d), 2.2b, 2.3, 2.4(b,d,f), 2.5(b, d, etc.), 2.6a, 2.8b, 2.9b, 2.10,
2.15a,c, the following hard ones to try: 2.12, 2.14. NOTE SOLVED
PROBLEM IN TEXT CAN HELP YOU.
Want you to enter solution online, print it out, submit hardcopy.
Apply for a course account. Using Oracle Database.
GET BUDDY PHONE #, IF MISS CLASS — CATCH UP.
Now Quickly through ideas of Chapter 1, what we will learn. Just try to get
the idea for now — helps later when will cover again.
DEF. A database management system, or DBMS, is a program product for
keeping computerized records (on disk)
What we will be learning in this course is underlying concepts you need to
make a database work: not how to build one but how to set one up.
Compiler or Operating System is also a program; not a trivial thing.
To begin with we will be dealing with the Relational Model, so all our data will
look like tables. E.g., see database, Figures 2.1 & 2.2, pgs. 27, 28.
We will be using these tables throughout the course. Draw tables & two rows
on board.
Nomenclature: We have: tables (or relations), columns (define attributes of
the data), rows (or tuples) to represent individual students or courses
(entities) or enrollments (relationships).
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Database Management System-Database Management using Database Principles, Programming and Performance-Lecture 01 Notes-Computer Science and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

Lecture Notes for

Database Management, using

Database Principles, Programming and Performance

Notes by Patrick E. O'Neil

Chapters 1 and 2

Class 1.

Hand out : Syllabus. Go over Syllabus, prerequisites. Grader, DBA. Homework : Read Chapter 1 (quickly), and Chapter 2 through Section 2.4. Homework in two weeks: All undotted exercises at end of Chapter 2. 2.1(b, d), 2.2b, 2.3, 2.4(b,d,f), 2.5(b, d, etc.), 2.6a, 2.8b, 2.9b, 2.10, 2.15a,c, the following hard ones to try: 2.12, 2.14. NOTE SOLVED PROBLEM IN TEXT CAN HELP YOU. Want you to enter solution online, print it out, submit hardcopy. Apply for a course account. Using Oracle Database.

GET BUDDY PHONE #, IF MISS CLASS — CATCH UP.

Now Quickly through ideas of Chapter 1, what we will learn. Just try to get the idea for now — helps later when will cover again.

DEF. A database management system, or DBMS, is a program product for keeping computerized records (on disk)

What we will be learning in this course is underlying concepts you need to make a database work: not how to build one but how to set one up. Compiler or Operating System is also a program; not a trivial thing.

To begin with we will be dealing with the Relational Model, so all our data will look like tables. E.g., see database, Figures 2.1 & 2.2, pgs. 27, 28.

We will be using these tables throughout the course. Draw tables & two rows on board.

Nomenclature: We have: tables (or relations), columns (define attributes of the data), rows (or tuples) to represent individual students or courses (entities) or enrollments (relationships).

Go over column names and meaning. Note aid unique, aname might duplicate, month values in ORDERS. This is not realistic, e.g., anames are too short, would want more columns, time more accurate.

Can answer questions about the data with queries. SQL Example: List agent ID, agent name, and percent commission of all agents in New York.

select aid, aname, percent

from agents where city = 'New York';

Now list the agent ID and agent name of all agents who placed an order in January. How would you do this? Need to connect TWO TABLES. In SQL:

select a.aid, a.aname from agents a, orders o

where a.aid = o.aid and o.month = 'jan';

Chapter 2. RULES of Relational Model. Tables have followed rules so all commercial products are the same, like typewriter keyboards.

For years these rules were as in Sect. 2.3. E.g., no column of a table should have a complex column value (a record), only a simple type (an integer). Similarly, can't have multiple values for a column of a row.

But there is coming to be a new model, known as the Object-Relational model, that does allow complex column values (and collection types as well: Can have a Set, Multiset, or List of values in a single row column).

Something to bear in mind: When mathematics as a field was the age that database is now, it had just come up with Plane Geometry, and was still doing arithmetic in Roman Numerals.

There are going to be a lot of changes in database systems before every- thing settles down.

Relational Algebra. A non-machine query language that gives a good idea of power of query languages: make new tables out of old tables by simple operations. E.g., List cno, cname for courses meeting MW2.

Rel. Alg. (AGENTS where city = 'New York) [aid, aname, percent]

Gives us simplest possible approach understanding what SQL is aiming at. Relational Algebra in Chapter 3, SQL in Chapter 3.

fering, then the class offering and student are related in a student schedule and a distinct class schedule.

Chapter 5. C programs with embedded SQL statements: Embedded SQL. Idea is to present a Menu to naive users so they never have to understand SQL. (SQL of Chapter 3 is called ad-hoc, or interactive SQL).

Reason: complex job to write SQL, bank tellers and airline reservation clerks don't want to have to do complex stuff like this. DANGEROUS to let people in a hurry try to update data using SQL.

Brings up problems of avoiding problems of concurrency and making data durable: Update transactions needed.

Term "User Friendly" is too vague; several different types of DBMS users.

-End Users: -Naive Users (Use Menu interface) -Casual Users (Compose SQL) -Application Programmers (Write Menu applications) -Database Administrators (DBAs)

Chapter 7. Commands for creating databases, creating and loading tables, (performance related issues, such as indexes, come later).

Integrity. Set up rules for how data can change. Rules keep errors from occurring because of inexperienced application writers. Rules can be treated as data, so high level designers know what is happening, not buried in millions of lines of applications.

Creating Views. Views are virtual tables, pretend tables created out of real base tables. DBA creates these so specific application programmers have an easier time (fewer fields to learn), easy to change DB without breaking old applications.

Security. What users can look at/change salary values for employees. Tax accountants can look at, can't change. Programmers have very low data security.

Chapter 8 and following. Skip for now. Physical design: how to place tables on disk so minimize access time; hard problem, because trade-offs. Index creation. Design for shared data.

All performance related stuff is put off to the second half of the book. But note idea of data sharing during update, something we run into in pro- gramming Embedded SQL. Problem where one person looks at data while another person is changing it. Sum up two account balances (rows of dif- ferent tables), might get sum that is false if other application has just subtracted from one to transfer money. Need something clever to handle this, call it a "Transaction".

Next time start on Chapter 2 in detail.

  • Students once implemented DB in Software Engineering course, called back later because wasn't working well in job where they ported it. Didn't have Program-Data Independence!

Heading (Schema) of table. Head(CUSTOMERS) = {cid, cname, city, discnt}. Typically name a table T, S, R with subscripted attributes. Head( T) = A 1 A 2 A 3 A 4 Note notation: set of attributes is just a LIST.

VERY IMPORTANT CONCEPT. The number of rows changes frequently and rows are not remembered by users; the columns usually DON'T change in number, many names are remembered, and USED TO POSE QUERIES.

Column type ( Column Domain ) A table is DECLARED in SQL. Columns have certain TYPES as in SQL: float4, integer, char(13).

Idea of Domain in Relational Algebra, is like an enumerated type. Domain of City: all the city names in the U.S. Domain of DIscnt: all float #s between 0.00 and 20.00.

This concept is not implemented in commercial systems today. Only have types such as char(13) and float4. But assume it in Relational Algebra.

Say CID = Domain(cid), CNAME = Domain(cname), CITY and DISCNT are the domains for CUSTOMERS table columns, then consider:

CID x CNAME x CITY x DISCNT (Cartesian Product)

consisting of all tuples: (w, x, y, z), w in CID, x in CNAME,...

ALL POSSIBLE tuples: (c999, Beowulf, Saskatoon, 0.01)

A mathematical relation between these four domains is a subset of the Cartesian product. E.g., if have 4 attributes, A 1 , A 2 , A 3 , and A 4 , and T is a relation such that Head(T) = A 1 A 2 A 3 A 4 , then:

T Domain(A 1 ) x Domain(A 2 ) x Domain(A 3 ) x Domain(A 4 )

T is said to relate a data item in Domain(A 1 ) with one in Domain(A 2 ),...

Section 2.3. Relational Rules. Idea is to make all products have same characteristics. But a bit too mathematical, overlooking important im- plementation aspects, so some rules are broken by most products.

Rule 1. First Normal Form rule. Can't have multi-valued fields. (Repeating fields). See pg. 37. All pure relational products obey this rule.

  • But new object-relational products break this rule
  • Thus can't have employees table with column "dependents" which contains multiple dependent's names (Figure 2.3)
  • Could create one table with duplicates on different rows (e.g., employees- dependents table join of employees and dependents), but this is bad for other reasons. (Figure 2.4)
  • Ends up meaning we have to create two tables and join them in later queries. (Figure 2.5) — in OR model, Figure 2.3 is OK, but won't handle this for awhile so as not to confuse you; assume relational -- no multi-valued fields.

Rule 2. Access rows by content only. Can't say: the third row down from the top. No order to the rows. (Also: No order to the columns.)

  • Disallows "pointers" to rows, e.g. Row IDs (RIDs) or "refs".
  • Most relational products break this rule by allowing users to get at rows by RIDs; new object-relational products have refs as part of syntax.

Rule 3. Unique rows. Two tuples cannot be identical in all column values at once. A relation is an unordered SET of tuples (2 rows can't be same in all attributes). But many products allow this for efficiency of load.

  • There are even some tables where it is a good thing (temperature readings in table, might repeat).

But in the current Chapter, Chapter 2, we will assume that all these rules hold perfectly.

Section 2.4. Keys and Superkeys.

Idea is that by intention of DBA, some set of columns in a table distinguishes rows. E.g., cid, ordno.

In commercial product, DBA declares which set of columns has this property and it becomes impossible for two columns to have the same values in all these columns.

It is USEFUL to have such a key for a table, so other tables can refer to a row, e.g.: employee number, bank account ID.

In CAP database, keys are columns: cid, aid, pid, and ordno.

a 1 b 2 c 2 d 1

a 2 b 2 c 1 d 1

a 2 b 1 c 2 d 1

Assume intent of designer is that this table will remain with the same contents. Then can determine keys from content alone (this is a VERY UN- USUAL situation).

(1) No single column can be a key. (2) No set of columns can be a key if it contains D. (3) Pairs: AB, AC, BC. Each of these pairs distinguishes all rows. Therefore all are keys. (4) All other sets (must have 3 or more el- ements) contain one of these or else D as a proper subset. (Show subset lattice). Therefore no more keys.

Various keys specified by intent of DBA are called Candidate Keys. A Pri- mary Key is defined to be the candidate key chosen by the designer to identifies rows of T used in references by other tables.

As ORDERS references CUSTOMERS by cid. Probably keep that way of ref- erencing even if cname city was another candidate key.

Class 3.

Assignment Read to end of Chapter 2. Homework: All undotted problems through end of Chapter 2, due at Class 6.

Theorem 2.4.2. Every table T has at least one key.

Idea of proof based on fact that a key is a minimal set of columns in T that distinguishes all rows: if u and v are different rows and K is a key, then designer intention: u[K] (u restricted to K) is not identical to v[K].

Since the set of all columns has that property (is a superkey for T — note always relative to table T), seems that there MUST be a MINIMAL set of columns that does. (NOTE PARTICULARLY: dealing with SETS of columns.)

But many with no mathematical training can't show this. Like you wanted to write a program to find such a set, and argue why the program terminates.

Proof. NAME ITEMS. Given a table T with Head(T) = A 1... A (^) n. SET UP TO LOOP: Let attribute set S 1 be this entire set. (In what follows, we assume we know the intentions of the table designer to decide which sets of columns will always distinguish all rows, but can't answer the questions until the set of columns is named.)

LOOP: Now S 1 is a superkey for T; either S 1 is a Key for T or it has a proper subset S 2 , S 2 S 1 , such that S 2 is also a superkey for T. Now, either S 2 is a Key or it has a proper subset S 3 , S 3 S 2 , such that S 3 is also a superkey for T. Can this go on forever? (OK if you say can't go on forever. Mathematician: why not?)

But each successive element in the sequence S 1 , S 2 , S 3 ,... has a smaller number of columns (it is a PROPER subset), and we can never go to 0 ( columns don't have unique values) or below. Thus this must be a finite se- quence with a smallest set at the end S (^) n. That must be the key. QED. You should see why it wasn't enough to stop without giving the loop and explaining why it terminates: proof explains as much as possible.

Def. Primary Key. A Primary Key of a table T is the candidate key chosen by the DBA to uniquely identify rows in T (usually used in references by other tables in "Foreign Key"). Thus on pg. 28, aid is the Primary key for agents and used as a foreign key in orders.

SET THEORETIC OPERATIONS

KEYBOARD

NAME SYMBOL FORM EXAMPLE

UNION UNION R UNION S INTERSECTION INTERSECT R INTERSECT S DIFFERENCE - or MINUS R - S, or R MINUS S PRODUCT x orTIMES R x S, or R TIMES S

SPECIAL OPERATIONS

KEYBOARD

NAME SYMBOL FORM EXAMPLE

PROJECT R [ ] R [ ] R [Ai 1.. .Ai k ]

SELECT R where C R where C R where A 1 = 5

JOIN JOIN R S, or R JOIN S

DIVISION DIVIDEBY R DIVIDEBY S

Idea of Keyboard form: name to use for an operation on a typewriter that doesn't have special operation symbol.

Set Theoretic Operations

Def. 2.6.1. Two tables are said to be compatible iff they have the same schema. (iff means "if and only if", or "means exactly the same thing".)

R S

A B C A B C

a 1 b 1 c 1 a 1 b 1 c 1

a 1 b 2 c 3 a 1 b 1 c 2

a 2 b 1 c 2 a 1 b 2 c 3

a 3 b 2 c 3

Illustrate R UNION S. Union of two tables considering table as set of rows. 5 rows in result. Clearly must be compatible tables, because rows of different schemas wouldn't fit in same table.

Illustrate R INTERSECT S. two rows.

Illustrate R - S. only one row.

illustrate S - R. two rows.

Before we go on to product (Cartesian product), consider idea of assignment or alias.

Def 2.6.3 Let R be a table with Head(R) = A 1... , A (^) n , and assume we wish to create a table S with Head(S) = B 1 ,.. ., B (^) n , B (^) i attributes such that Dom(B (^) i ) = Dom(A (^) i ) for all i, 1 = i = n, which has the SAME CONTENT as A. We can define the table S by writing the assignment

S(B 1 ,.. .,B (^) n ) := R(A 1 ,.. .,A (^) n ).

The content of the new table S is exactly the same as the content of the old table R, that is, a row u is in S if and only if a row t exists in R such that u[B (^) i ] = t[A (^) i ] for i, 1 = i = n. The symbol := used in this assignment is called the assignment operator.

If don't need to rename columns, just want a different table names with same attributes, we call this a table alias, and write:

S := R.

Use alias operator to save intermediate results of operations. Can write:

T := (R INTERSECT S) - (R × S),

or could write instead:

T1 := (R INTERSECT S) T2 := (R × S) T := T1 - T2 (Draw picture of this result)

NOTE THAT we often do not absolutely NEED to get intermediate results. Can usually use subexpression in place of named alias. One example below where absolutely need it.

If C, C', also get C and C', C or C', and finally: not C.

Example: Give the cid and cname of all customers living in Dallas with discount greater than 8.

(CUSTOMERS where city = 'Dallas' and discnt > 8) [cid, cname]

Class 4.

Assignment Homework (Hardcopy, Due in one week.): all non-dotted problems in Ch 2.

Display on board R TIMESS (Cartesian Product) (remember qualified names) for R & S below.

R S

A B B C

a 1 b 1 b 1 c 1

a 1 b 2 b 2 c 2

a 2 b 1 b 3 c 3

But now show what it is for R JOIN S.

JOIN****. Start by taking R TIMES S. Cross out rows where equivalently named attributes with qualifiers R or S in R TIMES S do not have equal values (do it above, rewrite smaller table). Do away with one column with duplicate name; drop qualification names (don't need, since now all columns have different names)

Draw lines between matching rows in R & S. Note b3 in table S has no matching row in table R, does not appear in join. Write out R JOIN S.

Example: Look at: ORDERS JOIN CUSTOMERS (pg. 28,). For each row in orders, exactly one in customers with same cid value (only common name). Draw one row. extends orders row with more information about customer.

Most Relops defined now. Things can get kind of complex in forming rela- tional algebra expressions to answer queries; take things one step at a time.

Example: Give the aid values of agents who take orders in dollar volume greater than 500 for customers living in Kyoto.

In following, assume have: C := CUSTOMERS, A := AGENTS, P := PRODUCTS, O := ORDERS.

Note will need info from ORDERS and from CUSTOMERS, since don't know where customers live in ORDERS, don't know anything about orders in CUS- TOMERS. Join ORDERS and CUSTOMERS, and have necessary info.

Look at pg. 28. What do we want? (c001, c004) and (c002, c003). Will the following do this? (Assume K := CUSTOMERS)

(C JOIN K) [C.cid, K.cid]

No. No qualified names in join result, no duplicated attribute names. In fact C JOIN K (same attribute names) looks like what? (Look to def of Join.) OK, don't always use Join, try:

((C JOIN K) where C.city = K.city)[C.cid, K.cid]

Look at pg. 28. Draw multiplication table, 5 rows (C cid values), 5 columns (K cid values), put X in when equal city. Problem, will get (c002, c002). Will also get both (c001, c004) and (c004, c001). To solve this, write:

((C x K) where C.city = K.city and C.cid < K.cid)[C.cid, K.cid]

Like choosing lower triangle below the diagonal.

Now, Relational Division. Define it in terms of Cartesian product. The idea is, as with integers, given two integers X and Y, define Z = X/Y, so that Z*Y = X. Can't always quite do this, even with integers, so instead define Z to be the largest whole number smaller than X/Y: e.g., if X = 17 and Y = 5, then 3.4 = 17/5 and Z = 3. Same with table division.

Definition 2.7.5. Division. Consider two tables R and S, where the schema of S is a subset of the schema of R. Tables R and S:

Head(R) = A 1.. .A (^) n B 1.. .B (^) m , and Head(S) = B 1.. .B (^) m.

The table T is the result of the division R ÷ S (which is read as "R DIVIDEBY S") if Head(T) = A 1.. .A (^) n and T consists of those rows t such that for every row s in S, the row resulting from concatenating t and s can be found in table R, and there is no larger set of rows for which this is true.ϒ

Note there are no columns in common between S (Head(S) = B 1.. .B (^) m ) and T = R ÷ S (Head(T) = A 1.. .A (^) n ), but since Head(R) = A 1.. .A (^) n B 1.. .B (^) m , we see that S and T have the proper schemas so that we might be able to say: T x S = R.) (This is the division operation we want to define, the inverse of product.) If R = T JOIN S, then we will be able to say that T = R ÷ S.

Might start with R and S so R? T JOIN S, for any possible choice of T.

Still, when T = R ÷ S, the table T contains the largest possible set of rows such that T S is contained in R. (Exercise 3.) Make Analogy to Integer Division.

Example 2.7.9. (In book) Start with the table R given by: R A B C a1 b1 c 1 a2 b1 c 1 a1 b2 c 1 a1 b2 c 2 a2 b1 c 2 a1 b2 c 3 a1 b2 c 4 a1 b1 c 5

We list a number of possible tables S and the resulting table T := R ÷ S.

S T C A B c 1 a1 b a2 b a1 b

Note that all of the rows in S JOIN T are in R, and there couldn't be any larger a set of rows in T for which that is true because there are only three rows in R with a c1 value in column C.

S T C A B c 1 a1 b c 2 a2 b

All of the rows in S JOIN T are in R, and there couldn't be any larger a set of rows in T with this true: look at rows in R with C values c2.

S T C A B