



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















Class 1.
Last term covered Chapters 2-6; this term cover Chapters 7-11.
I will assume everything in text through Chapter 6. Ad-hoc SQL (Chap. 3), O-R SQL (Chap. 4), Embedded SQL (Chap. 5), Database Design (Chap. 6). Note solved and unsolved Exercises at end of chapters.
Starting on Chapter 7. Homework assignment online. Dotted problem solutions are at end of Chapter.
OUTLINE. Review what is to be covered this term. Chapter 7-11.
Start 7.1, Integrity Constraints, generally as part of Create Table command. (Rules for an Enterprise from Logical Design: review Chapter 6.)
7.2, Views: virtual tables to make it easier for DBA to set up data for access by users. Base tables vs. View tables.
7.3, Security. Who gets to access data, how access is controlled by DBA.
7.4, System catalogs. If you come to a new system, new database, how figure out what tables are, what columns mean, etc. All info carried in catalogs, relational data that tells about data (sometimes called Metadata).
Chapter 8. Introduction to physical structure and Indexing. Records on disk are like books in a library, and indexes are like old style card catalogues.
Generally, what we are trying to save with indexing is disk accesses (very slow -- analogous to walking to library shelves to get books).
Lots of different types of indexing for different products, not covered at all by any standards. Somewhat complex but fun topic.
Chapter 9. Query Optimization. How the Query Optimizer actually creates a Query plan to access the data. Basic access steps it can use.
Just as an example, sequential prefetch. Join algorithms. How to read Query Execution Plans. Important skill when we cover thisis learning to add quickly and make numeric estimates of how many I/Os are required.
Query Optimization is a VERY complex topic. All examples in text from MVS DB2. Will try to add examples from other vendor products.
End of Chapter 9, see Query performance measured in Set Query benchmark.
Chapter 10. Update Transactions. Transactional histories. Performance value of concurrent execution. Concurrency theory.
ACID properties, transactions make guarantees to programmer. Atomic, Consistent, Isolated, and Durable. TPC-A benchmark.
Transactional Recovery. Idea is that if the system crashes, want to save results. Transactions are all-or-nothing, like transfer of money. Don't want to be chopped off in the middle.
Chapter 11, Parallel and Distributed databases. We’ll see how much we can do on this.
Chapter 7.1 Integrity Constraints
Section 7.1: Integrity constraints. Review Chapter 6, Database Design, from last term, since concepts are applied here in Chapter 7.
Idea in Chapter 6 was that DBA performs Logical database design, analyzing an enterprise to be computerized:
o listing the data items to be kept track of o the rules of interrelatedness of these data items (FDs) o apportioning the data items to different tables (E-R or Normalization)
After do this, Chapter 7 tells you how to actually construct the tables and load them. Build rules into table so SQL update statements can't break the rules of interrelatedness (and other rules we'll come up with). Call this a
faithful representation
Class 2.
Covering idea of Create Table constraint clauses & faithful representation.
Again: Relationships in Figure 7.2, pg. 415. Each of C, A, P is related to O.
Customers requests Orders, max-card(Customers, requests) = N; can have Many links out of each Customer instance, but max-card(Orders, requests) = 1, only one into each order: single-valued participation. Many-One, N-1.
Which side is Many? Orders! Side that has single-valued participation! One customer places MANY orders, reverse not true. The MANY side is the side that can contain a foreign key in a relational table!
Representation in relational model? N-1 relationship represented by foreign key in orders referencing primary key in customers.
create table orders ( ordno integer not null, month char(3), cid char(4) not null, aid char(3) not null, pid char(3) not null, qty integer not null check(qty >= 0), dollars float default 0.0 check(dollars >= 0.0), primary key ( ordno ), foreign key (cid) references customers, foreign key (aid) references agents, foreign key (pid) references products);
In Create Table for orders, see ordno is a primary key, and at end says cid is foreign key references customers. The implication is that cid in orders must match a primary key value in customers (name needn't be cid).
If we wanted to match with a candidate key, colname2, could instead say:
foreign key (colname1) references tablename (colname2)
Can also have larger tuples matching:
Create table employees (... foreign key (cityst, staddr, zip) references ziptst(cityst, staddr, zip);
Now with this FOREIGN KEY... REFERENCES clause for cid in orders table, if try to insert an orders row with cid value that isn't in customers, insert will fail and give an error condition.
For general form of Create Table command, see pg 411, Figure 7.1. This is our Basic SQL standard. (See first page of Chapter 3 for definition of Basic SQL)
Put on board one section at a time. Here is Create Table specification block:
Figure 7.1. Clauses of the Create Table command.
CREATE TABLE tablename ((colname datatype [DEFAULT {default_constant | NULL}] [col_constr {col_constr.. .}] | table_constr {, {colname datatype [DEFAULT {default_constant | NULL}] [col_constr {col_constr.. .}] | table_constr}
.. .});
Recall CAPS means literal. Cover typographical conventions, Chap 3, pg 85
Start by naming the table. Then list of column names OR table constraints.
The column names are given with datatype and possibly a DEFAULT clause: specifies a value the system will supply if an SQL Insert statement does n o t furnish a value. (Load command is not constrained to provide this value.)
Column Constraints can be thought of as shorthand for Table Constraints. Table Constraints are a bit more flexible (except for NOT NULL).
Note that both constraints have "constraintname" for later reference. Can DROP both using later Alter Table, can only ADD Table Constraint (not CC).
Can tell difference between the two: Column Constraints stay with colname datatype definition, no separating comma. See Create Table orders on board.
Table Constraint has separating comma (like comma definition). CC keeps constraint near object constrained, TC might be far away in large table.
Def 7.1.2. Column Constraints.
The col_constr form that constrains a single column value follows:
Create table orders (... , cid char(4) not null check (cid in (select cid from customers), INVALID
The REFERENCES clause means values in this column must appear as one o f the values in the tablename referenced, a column declared unique in that table. A non-unique column won’t work, but nulls are OK.
Multi-column equivalent, use table_constr: FOREIGN KEY... REFERENCES. Column constraint "references" is just a shorthand for single column name.
The optional ON DELETE CASCADE clause says that when a row in t h e referenced table is deleted that is being referenced by rows in the referencing table, then those rows in the referencing table are deleted!
If missing, default “RESTRICTs” delete of a referenced row (disallows it).
Def 7.1.3. Table Constraints.
The table_constr form that constrains multiple columns at once follows: [CONSTRAINT constraintname] {UNIQUE (colname {, colname.. .}) | PRIMARY KEY (colname {, colname.. .}) | CHECK (search_condition) | FOREIGN KEY (colname {, colname.. .}) REFERENCES tablename [(colname {, colname.. .})] [ON DELETE CASCADE]}
The UNIQUE clause is the same as UNIQUE for column, but can name a set o f columns in combination. It is possible to have null values in some of the columns, but sets of rows with no nulls must be unique in combination.
UNIQUE multi-column, all columns declared NOT NULL is what we mean by a Candidate Key. If have multiple column candidate key, (c1, c2,.. ., cK), m u s t define each column NOT NULL & table constr. unique (c1, c2,.. ., cK)
The PRIMARY KEY clause specifies a non-empty set of columns to be a primary key. Literally, this means that a FOREIGN KEY... REFERENCES clause will refer to this set of columns by default if no columns named.
Every column that participates in a primary key is implicitly defined NOT NULL. We can specify per column too. There can be at most one PRIMARY KEY clause in Create Table. UNIQUE cannot also be written for this combination.
The CHECK clause can only be a restriction condition : can only reference other column values ON THE SAME ROW in search_condition.
A FOREIGN KEY... REFERENCES clause. The foreign key CAN contain nulls in some of its columns, but if all non-null than must match referenced table column values in some row.
Optional ON DELETE CASCADE means same here as in Column Constraint.
ORACLE has ENABLE and DISABLE clauses for constraints. Can define table with named constraint Disabled, later Enable it (after load table).
Sometimes can’t load tables if have all constraints working: e.g. tables for boys and girls at a dance, each must have partner from other table. A referential integrity constraint that must fail when insert first row.
Therefore Create Table with constraint DISABLE’d. Later use Alter Table to ENABLE it. Nice idea, since Constraint stays in Catalog Table.
Problem that this is not portable, so we depend on ADDing such a constraint later with Alter Table (can’t define it early if load by Insert).
INFORMIX and DB2 Create Table
INFORMIX has minor variation as of this version: all column definitions must precede table constraint definitions (old way), can’t be mixed in any order.
DB2, see Figure 7.6, pg 421. Adds to FOREIGN KEY... REFERENCES.
FOREIGN KEY (colname {, colname}) REFERENCES tablename [ON DELETE [NO ACTION | CASCADE | SET NULL | RESTRICT]]
What if delete a row in customers that cid values in orders reference?
NO ACTION, RESTRICT means delete of customers row won't take while foreign keys reference it. (Subtle difference between the two.) SET NULL means delete of customers row will work and referencing cid values in orders will be set to null. CASCADE means delete of customers will work and will also delete referencing orders rows. Note that this might imply cascading (recursive) deletes, if other foreign key references column in orders.
Default is NO ACTION. Presumed by products that don't offer the choice. See Fig. 7.6 for standard options in X/Open, Full SQL-99 is same as DB2.
Referential Integrity
See pg. 419, Definition 7.1.4. We define an ordered set of columns F to make up a Foreign key in table T1 to match with an ordered set of columns P in table T2. (foreign key... references.. .)
A referential integrity constraint is in force if the columns of a foreign key F in any row of T1 must either (1) have a null value in at least one column that permits null values, or (2) have no null values and equate to the values of a primary key P on some row of T2.
Thus if we want "optional participation" in a relationship from the referencing table, must allow at least one column of F to be nullable.
Example 7.1.4. Use Referential Integrity to define an enumerated Domain.
create table cities(city varchar(20) not null, primary key (city) ); create table customers (cid char(4) not null, cname varchar(13), city varchar(20), discnt real check(discnt <= 15.0), primary key (cid), foreign key city references cities);
Idea here is that cityname can't appear as city column of customers unless it also appears in cities. List all valid cities in first table, impose an enumerated domain. (Note it takes more effort to check.)
I will skip explaining rest of Referential Integrity subsection in class; read it on your own (nothing startling).
The Alter Table Statement
With Alter Table statement, can change the structure of an existing table. Must be owner of table or have Alter privileges (other privileges as well).
The earlier standards didn’t cover the Alter Table statement, and the Alter table statement in SQL-92 is too general.
So there are lots of differences between products in Alter Table. We cover the differences, and don’t attempt a Basic SQL form to bring them together.
ORACLE Alter Table, Fig. 7.7, pg. 423.
ALTER TABLE tblname [ADD ({colname datatype [DEFAULT {default_const|NULL}] [col_constr {col_constr...}] | table_constr} -- choice of colname-def. or table_constr
For more flexibility, procedural constraints: Triggers. Provided by Sybase first (T-SQL), DB2 UDB, Oracle, Informix and Microsoft SQL Server.
Triggers weren’t in SQL-92, but now in SQL-99. Fig. 7.10, pg 425.
CREATE TRIGGER trigger_name BEFORE | AFTER {INSERT | DELETE | UPDATE [OF colname {, colname...}]} ON tablename [REFERENCING corr_name_def {, corr_name_def...}] [FOR EACH ROW | FOR EACH STATEMENT] [WHEN (search_condition)] {statement - - action (single statement) | BEGIN ATOMIC statement; { statement;...} END} -- action (mult. stmts.)
The corr_name_def in the REFERENCING clause looks like: {OLD [ROW] [AS] old_row_corr_name | NEW [ROW] [AS] new_row_corr_name | OLD TABLE [AS] old_table_corr_name | NEW TABLE [AS] new_table_corr_name}
Can also use command: DROP TRIGGER trigger_name;
The trigger is fired (executed) either BEFORE or AFTER one of the events {INSERT | DELETE | UPDATE [of colname {, colname.. .}]} to the NAMED TABLE.
When trigger is fired, optional WHEN search_cond (if present) is executed to determine if the current subset of rows affected should execute the multi- statement block starting with BEGIN ATOMIC. (Example below for ORACLE.)
Jump to [FOR EACH ROW | FOR EACH STATEMENT]; Update statement might update multiple rows, so default fires only once for multiple row Update.
REFERENCING gives names to old row/new row (or old table/new table), so know which is referenced in program block.
Row_corr_name also used as table aliases in WHERE clause (in customers, old_row_corr_name x, then x.discnt is old value of discnt on row.)
CANNOT use row_corr_name on FOR EACH STATEMENT. (So FOR EACH ROW is more commonly used in practice.) Table_corr_name is used in FROM clause.
Note: you shouldn't try to put any other table into this WHEN search_cond – not even with a Subquery.
(E.g., if Update customers in Texas, fire trigger and search for customers with discnt > 5, only act on rows with AND of those two; if update adds 1 to discnt, clearly important if we are talking aboout old-corr-names or new.)
Either have single statement of a block of statements between BEGIN ATOMIC and END that executes when triggered; can refer to corr_names.
Can write procedural program using SQL-99 procedural language SQL/PSM (stands for "Persistent Stored Module").
ORACLE uses PL/SQL (BEGIN and END as in usual PL/SQL progrms, Chapter 4). DB2 has no full procedural language but invents simple one for triggers.
Now ORACLE Create Trigger statement. There will be homework on this.
CREATE [OR REPLACE] TRIGGER trigger_name {BEFORE | AFTER | INSTEAD OF} {INSERT | DELETE | UPDATE [OF columnname {, columnname...}]} ON tablename [REFERENCING corr_name_def {, corr_name_def...}] {FOR EACH ROW | FOR EACH STATEMENT} [WHEN (search_condition)] BEGIN statement {statement;.. .} END;
The corr_name_def that provides row correlation names follows: {OLD old_row_corr_name | NEW new_row_corr_name}
Figure 7.12 ORACLE Create Trigger Syntax
Note differences from SQL-99. Can REPLACE old named trigger with a new one. Trigger action can occur INSTEAD OF the INSERT, DELETE, or UPDATE.
Don't have TABLE corr_name options, only ROW. Therefore don't have access to old table in AFTER trigger.
ORACLE Examples using PL/SQL for procedures – see Chapter 4.
Example 7.1. Use an ORACLE trigger to CHECK the discnt value of a new customers row does not exceed 15.0.
create trigger discnt_max after insert on customers
OR we could create LAYER of calls, everyone has to access data through call layer, and it makes necessary tests to guarantee RULES. E.g., logic checks referential integrity holds. Will have an exercise to do that. Talk about.
One problem is that this must be a CUSTOM LAYER for a shop; some shop’s programming staff won’t know enough to do this right.
There is another issue of control. Managers often afraid of what programmers might do, and want to see the reins of control in a separate place.
This “Fear, Uncertainty, and Doubt” argument about programmers was a main way in which constraints were originally sold to business managers.
But there is real value to having a limited number of non-procedural constraints, with a few parameters: UNIQUE, FOREIGN KEY... REFERENCES.
Acts like DATA! Can place all constraints in tables (System Catalogs), check that they're all there. Not just LOGIC (hard to grasp) but DATA.
But problem we've met before. Lots of RULEs we'd like that can't be put in terms of the non-procedural constraints we've listed: not enough POWER!
I point out two weaknesses of non-procedural constraints: (1) Specifying constraint failure alternative, (2) Guaranteeing transactional Consistency.
Constraint failure alternative. E.g., try to add new order and constraint fails - misspelling of cid. Now what do we do with the order, assuming we have the wrong zip code for a new prospect address? Throw it away?
Programmer has to handle this, and it’s a pain to handle it on every return from application using SQL. (But we can handle this using Triggers now.)
Transactional Consistency. There is an accounting need to have Assets = Liabilities + Proprietorship (Owners Equity). Lots of RULES are implied.
RULE: in money transfer, money is neither created nor destroyed. In midst of transaction, may create or destroy money within single accounts, but must balance out at commit time.
But there is no constraint (even with triggers) to guarantee consistency of a transaction at commit time. And this can be a VERY hard problem.
So we have to trust programmers for these hard-to-constrain problems: Why try to second-guess them for the easy problems.
The answer probably is that non-procedural constraints can solve some easy things and thus simplify things for programmers. Most programmers would support the idea of “data-like constraints”.
Example 7.1.7. Here we assume each CAP with orders having a corresponding set of line-items in a table lineitems with foreign key ordnum. The order is originally inserted with n_items set to zero, and we keep the number current as new lineitem rows are inserted for a given order.
create trigger incordernitems after insert on lineitems referencing old oldli for each row begin --for ORACLE, leave out for DB2 UDB update orders set n_items = n_items + 1 where ordno = :oldli.ordno; end; --for ORACLE create trigger decordernitems after delete on lineitems referencing old oldli for each row begin update orders set n_items = n_items - 1 where ordno = :oldli.ordno; end;
Can leave out list of colnames if target list of subquery has colnames that require no qualifiers. Need to give names for expressions or for ambiguous colnames (c.city vs a.city). Can rename column in any way at all.
Aliases for expressions will give names to expressions in DB2 UDB and ORACLE (not INFORMIX at present), so don't need colnames in Create View.
Recall that Subquery, defined at the beginning of Section 3.9, DOES allow UNION (check this is in your notes, class), does not allow ORDER BY.
The WITH CHECK OPTION will not permit an update or insert (through a legally updatable view) that would be invisible in the resulting view.
Example 7.2.4. Assume we had no CHECK clause on discnt when we created customers. Can create updatable view custs that has this effect.
create view custs as select * from customers where discnt <= 15.0 with check option;
Now cannot insert/update a row into custs with discnt > 15.0 that will take on table customers. This will fail for customers cid = 'c002' (discnt = 12):
update custs set discnt = discnt + 4.0;
Can nest views, create a view depending on other views. Example 7.2.5 :
create view acorders (ordno, month, cid, aid, pid, qty, dollars, aname, cname) as select ordno, month, ao.cid as cid, aid, pid, qty, charge, aname, cname from agentorders ao, customers c where ao.cid = c.cid;
Listing views. All objects of a database are listed in the catalog tables (section 7.4). Can list all base tables, all views, all named columns, often all constraints (or named constraints, anyway).
In X/Open standard, would list views as follows:
select tablename from info_schem.tables where table_type = 'VIEW';
In ORACLE: select view_name from user_views;
Once you have a view_name (or table_name from user_tables) can write in ORACLE:
DESCRIBE {view_name | table_name};
To find out exactly how the view/table was defined in ORACLE:
select text from user_views where view_name = 'AGENTORDERS'; (NOTE: name must be in Upper Case)
If you find you don't get the whole view definition (it's chopped off), use the ORACLE statement: set long 1000, then try again.
In DB2 UDB, way to list views for user eoneil is:
select viewname from syscat.views where definer = 'EONEIL';
To delete a view, or table (or later, index), standard statement is:
DROP {TABLE tablename | VIEW viewname};
When table or view is dropped, what happens to other objects (views, constraints, triggers) whose definitions depend on the table or view?
By default in ORACLE, if drop table or view, other views and triggers that depend on it are marked invalid.
Later, if the table or view is recreated with the same relevant column names, those views and triggers can become usable again.
With [CASCADE CONSTRAINTS] in ORACLE, constraints referring to this table (e.g., foreign key references) are dropped. (See Figure 7.14.)
If there is no such clause, then the Drop fails (RESTRICT, effect named in INFORMIX and SQL-99).
In INFORMIX have [CASCADE | RESTRICT]; refers to constraints.and also nesting views.
In DB2, no options. Invalidates views & triggers depending on what dropped; retain definition in system catalog, but must be recreated later.