Download presentation
Presentation is loading. Please wait.
1
Lecture 06: SQL Systems Aspects
Friday, October 10, 2003
2
Outline Two Examples Views (6.7) -- finish up Constraints (Chapter 7)
Embedded SQL (8.1) Transactions in SQL (8.6)
3
Two Examples Store(sid, sname) Product(pid, pname, price, sid)
Find all stores that sell only products with price > 100 (Equivalent formulation: find all stores s.t. all their products have price > 100)
4
SELECT Store.name FROM Store, Product WHERE Store.sid = Product.sid GROUP BY Store.sid HAVING 100 < min(Product.price) SELECT Store.name FROM Store WHERE < ALL (SELECT Product.price FROM product WHERE Store.sid = Product.sid) Your pick… SELECT Store.name FROM Store WHERE Store.sid NOT IN (SELECT Product.sid FROM Product WHERE Product.price <= 100)
5
Two Examples Store(sid, sname) Product(pid, pname, price, sid)
For each store, find the Product ID of its most expensive product
6
Two Examples This is easy but doesn’t do what we want:
SELECT Store.name, max(Product.price) FROM Store, Product WHERE Store.sid = Product.sid GROUP BY Store.sid Better: SELECT Store.name, x.pid FROM Store, Product x WHERE Store.sid = x.sid and x.price >= ALL (SELECT y.price FROM Product y WHERE Store.sid = y.sid) But may return multiple pids
7
Two Examples Finally, choose some pid arbitrarily, if there are many with highest price: SELECT Store.name, max(x.pid) FROM Store, Product x WHERE Store.sid = x.sid and x.price >= ALL (SELECT y.price FROM Product y WHERE Store.sid = y.sid) GROUP BY Store.name
8
Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department, project, salary) CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = “Development” If we make the following insertion: INSERT INTO Developers VALUES(“Joe”, “Optimizer”) INSERT INTO Employee(ssn, name, department, project, salary) VALUES(NULL, “Joe”, NULL, “Optimizer”, NULL) It becomes:
9
Non-Updatable Views Person(name, city)
Purchase(buyer, seller, product, store) CREATE VIEW City-Store AS SELECT Person.city, Purchase.store FROM Person, Purchase WHERE Person.name = Purchase.buyer How can we add the following tuple to the view? (“Seattle”, “Nine West”) We don’t know the name of the person who made the purchase; cannot set to NULL (why ?)
10
Constraints in SQL A constraint = a property that we’d like our database to hold The system will enforce the constraint by taking some actions: forbid an update or perform compensating updates
11
Constraints in SQL Constraints in SQL: Keys, foreign keys
Attribute-level constraints Tuple-level constraints Global constraints: assertions The more complex the constraint, the harder it is to check and to enforce simplest Most complex
12
Keys OR: CREATE TABLE Product ( name CHAR(30) PRIMARY KEY,
category VARCHAR(20)) OR: CREATE TABLE Product ( name CHAR(30), category VARCHAR(20) PRIMARY KEY (name))
13
Keys with Multiple Attributes
CREATE TABLE Product ( name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (name, category)) Name Category Price Gizmo Gadget 10 Camera Photo 20 30 40
14
Other Keys CREATE TABLE Product ( productID CHAR(10), name CHAR(30),
category VARCHAR(20), price INT, PRIMARY KEY (productID), UNIQUE (name, category)) There is at most one PRIMARY KEY; there can be many UNIQUE
15
Foreign Key Constraints
Referential integrity constraints CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME) prodName is a foreign key to Product(name) name must be a key in Product
16
Product Purchase Name Category Gizmo gadget Camera Photo OneClick
ProdName Store Gizmo Wiz Camera Ritz
17
Foreign Key Constraints
(name, category) must be a PRIMARY KEY CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category)
18
What happens during updates ?
Types of updates: In Purchase: insert/update In Product: delete/update Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz
19
What happens during updates ?
SQL has three policies for maintaining referential integrity: Reject violating modifications (default) Cascade: after a delete/update do a delete/update Set-null set foreign-key field to NULL READING ASSIGNEMNT: 7.1.5, 7.1.6
20
Constraints on Attributes and Tuples
Constraints on attributes: NOT NULL -- obvious meaning... CHECK condition -- any condition ! Constraints on tuples CHECK condition
21
What is the difference from Foreign-Key ?
CREATE TABLE Purchase ( prodName CHAR(30) CHECK (prodName IN SELECT Product.name FROM Product), date DATETIME NOT NULL)
22
General Assertions CREATE ASSERTION myAssert CHECK NOT EXISTS(
SELECT Product.name FROM Product, Purchase WHERE Product.name = Purchase.prodName GROUP BY Product.name HAVING count(*) > 200)
23
Final Comments on Constraints
Can give them names, and alter later Read in the book !!! We need to understand exactly when they are checked We need to understand exactly what actions are taken if they fail
24
Embedded SQL direct SQL (= ad-hoc SQL) is rarely used
in practice: SQL is embedded in some application code SQL code is identified by special syntax
25
Impedance Mismatch Example: SQL in C:
C uses int, char[..], pointers, etc SQL uses tables Impedance mismatch = incompatible types
26
The Impedance Mismatch Problem
Why not use only one language? Forgetting SQL: “we can quickly dispense with this idea” [textbook, pg. 351]. SQL cannot do everything that the host language can do. Solution: use cursors
27
Programs with Embedded SQL
Host language + Embedded SQL Preprocessor Preprocessor Call-level interface (CLI): ODBC,JDBC, ADO Host Language + function calls Host language compiler Host language compiler Host language program
28
Interface: SQL / Host Language
Values get passed through shared variables. Colons precede shared variables when they occur within the SQL statements. EXEC SQL: precedes every SQL statement in the host language. The variable SQLSTATE provides error messages and status reports (e.g., “00000” says that the operation completed with no problem). EXEC SQL BEGIN DECLARE SECTION; char productName[30]; EXEC SQL END DECLARE SECTION;
29
Example Product (pname, price, quantity, maker)
Purchase (buyer, seller, store, pname) Company (cname, city) Person(name, phone, city)
30
Using Shared Variables
Void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char n[20], c[30]; /* product-name, company-name */ int p, q; /* price, quantity */ char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* get values for name, price and company somehow */ EXEC SQL INSERT INTO Product(pname, price, quantity, maker) VALUES (:n, :p, :q, :c); }
31
Single-Row Select Statements
int getPrice(char *name) { EXEC SQL BEGIN DECLARE SECTION; char n[20]; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; strcpy(n, name); /* copy name to local variable */ EXEC SQL SELECT price INTO :p FROM Product WHERE Product.name = :n; return p; }
32
Cursors Declare the cursor Open the cursor Fetch tuples one by one
Close the cursor
33
Cursors void product2XML() { EXEC SQL BEGIN DECLARE SECTION;
char n[20], c[30]; int p, q; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE crs CURSOR FOR SELECT pname, price, quantity, maker FROM Product; EXEC SQL OPEN crs;
34
Cursors printf(“<allProducts>\n”); while (1) {
EXEC SQL FETCH FROM crs INTO :n, :p, :q, :c; if (NO_MORE_TUPLES) break; printf(“ <product>\n”); printf(“ <name> %s </name>\n”, n); printf(“ <price> %d </price>\n”, p); printf(“ <quantity> %d </quantity>\n”, q); printf(“ <maker> %s </maker>\n”, c); printf(“ </product>\n”); } EXECT SQL CLOSE crs; printf(“</allProducts>\n”);
35
What is NO_MORE_TUPLES ?
#define NO_MORE_TUPLES !(strcmp(SQLSTATE,”02000”))
36
More on Cursors cursors can modify a relation as well as read it.
We can determine the order in which the cursor will get tuples by the ORDER BY keyword in the SQL query. Cursors can be protected against changes to the underlying relations. The cursor can be a scrolling one: can go forward, backward +n, -n, Abs(n), Abs(-n).
37
Dynamic SQL So far the SQL statements were visible to the compiler
In dynamic SQL we have an arbitrary string that represents a SQL command Two steps: Prepare: compiles the string Execute: executes the compiled string
38
Dynamic SQL Void someQuery() { EXEC SQL BEGIN DECLARE SECTION;
char *command=“UPDATE Product SET quantity=quantity+1 WHERE name=“gizmo” EXEC SQL END DECLARE SECTION; EXEC SQL PREPARE myquery FROM :command; EXEC SQL EXECUTE myquery; } myquery = a SQL variable, does not need to be prefixed by “:”
39
Transactions Address two issues: Access by multiple users
Remember the “client-server” architecture: one server with many clients Protection against crashes
40
Multiple users: single statements
Client 1: UPDATE Product SET Price = Price – WHERE pname = ‘Gizmo’ Client 2: UPDATE Product SET Price = Price*0.5 WHERE pname=‘Gizmo’ Two managers attempt to do a discount. Will it work ?
41
Multiple users: multiple statements
Client 1: INSERT INTO SmallProduct(name, price) SELECT pname, price FROM Product WHERE price <= 0.99 DELETE Product WHERE price <=0.99 Client 2: SELECT count(*) FROM Product SELECT count(*) FROM SmallProduct What’s wrong ?
42
Protection against crashes
Client 1: INSERT INTO SmallProduct(name, price) SELECT pname, price FROM Product WHERE price <= 0.99 DELETE Product WHERE price <=0.99 Crash ! What’s wrong ?
43
Transactions Transaction = group of statements that must be executed atomically Transaction properties: ACID ATOMICITY = all or nothing CONSISTENCY = leave database in consistent state ISOLATION = as if it were the only transaction in the system DURABILITY = store on disk !
44
Transactions in SQL In “ad-hoc” SQL: In “embedded” SQL:
Default: each statement = one transaction In “embedded” SQL: BEGIN TRANSACTION [SQL statements] COMMIT or ROLLBACK (=ABORT)
45
Transactions: Serializability
Serializability = the technical term for isolation An execution is serial if it is completely before or completely after any other function’s execution An execution is serializable if it equivalent to one that is serial DBMS can offer serializability guarantees
46
Serializability Enforced with locks, like in Operating Systems !
But this is not enough: User 1 LOCK A [write A=1] UNLOCK A LOCK B [write B=2] UNLOCK B User 2 LOCK A [write A=3] UNLOCK A LOCK B [write B=4] UNLOCK B The final values are A=3, B=2. If the two transactions were executed serially, then we would have either A=1,B=2, or A=3,B=4. This is not a serializable execution time What is wrong ?
47
Serializability Solution: two-phase locking
Lock everything at the beginning Unlock everything at the end Read locks: many simultaneous read locks allowed Write locks: only one write lock allowed Insert locks: one per table
48
Isolation Levels in SQL
“Dirty reads” SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED “Committed reads” SET TRANSACTION ISOLATION LEVEL READ COMMITTED “Repeatable reads” SET TRANSACTION ISOLATION LEVEL REPEATABLE READ Serializable transactions (default): SET TRANSACTION ISOLATION LEVEL SERIALIZABLE Reading assignment: chapter 8.6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.