lecture 05: sql systems aspects
DESCRIPTION
Lecture 05: SQL Systems Aspects. Friday, October 11, 2002. Outline. Embedded SQL (8.1) Transactions in SQL (8.6). 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. Impedance Mismatch. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/1.jpg)
1
Lecture 05: SQLSystems Aspects
Friday, October 11, 2002
![Page 2: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/2.jpg)
2
Outline
• Embedded SQL (8.1)
• Transactions in SQL (8.6)
![Page 3: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/3.jpg)
3
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
![Page 4: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/4.jpg)
4
Impedance Mismatch
• Example: SQL in C:– C uses int, char[..], pointers, etc– SQL uses tables
• Impedance mismatch = incompatible types
![Page 5: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/5.jpg)
5
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
![Page 6: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/6.jpg)
6
Programs with Embedded SQL
Host language + Embedded SQL
Preprocessor
Host Language + function calls
Host language compiler
Host language program
Preprocessor
Host language compiler
Call-levelinterface (CLI):ODBC,JDBC,
ADO
![Page 7: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/7.jpg)
7
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 noproblem).
EXEC SQL BEGIN DECLARE SECTION; char productName[30];EXEC SQL END DECLARE SECTION;
EXEC SQL BEGIN DECLARE SECTION; char productName[30];EXEC SQL END DECLARE SECTION;
![Page 8: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/8.jpg)
8
Example
Product (pname, price, quantity, maker)Purchase (buyer, seller, store, pname)Company (cname, city)Person(name, phone, city)
![Page 9: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/9.jpg)
9
Using Shared VariablesVoid 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); }
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); }
![Page 10: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/10.jpg)
10
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 :pFROM ProductWHERE Product.name = :n;
return p;}
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 :pFROM ProductWHERE Product.name = :n;
return p;}
![Page 11: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/11.jpg)
11
Cursors
1. Declare the cursor
2. Open the cursor
3. Fetch tuples one by one
4. Close the cursor
![Page 12: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/12.jpg)
12
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;
![Page 13: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/13.jpg)
13
Cursorsprintf(“<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”);
}
![Page 14: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/14.jpg)
14
• What is NO_MORE_TUPLES ?
#define NO_MORE_TUPLES !(strcmp(SQLSTATE,”02000”))
![Page 15: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/15.jpg)
15
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).
![Page 16: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/16.jpg)
16
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
![Page 17: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/17.jpg)
17
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 “:”
![Page 18: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/18.jpg)
18
Transactions
Address two issues:
• Access by multiple users– Remember the “client-server” architecture: one
server with many clients
• Protection against crashes
![Page 19: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/19.jpg)
19
Multiple users: single statements
Client 1:UPDATE ProductSET Price = Price – 1.99WHERE pname = ‘Gizmo’
Client 2:UPDATE ProductSET Price = Price*0.5WHERE pname=‘Gizmo’
Client 1:UPDATE ProductSET Price = Price – 1.99WHERE pname = ‘Gizmo’
Client 2:UPDATE ProductSET Price = Price*0.5WHERE pname=‘Gizmo’
Two managers attempt to do a discount.Will it work ?
![Page 20: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/20.jpg)
20
Multiple users: multiple statements
Client 1: INSERT INTO SmallProduct(name, price)SELECT pname, priceFROM ProductWHERE price <= 0.99
DELETE ProductWHERE price <=0.99
Client 2: SELECT count(*)FROM Product
SELECT count(*)FROM SmallProduct
Client 1: INSERT INTO SmallProduct(name, price)SELECT pname, priceFROM ProductWHERE price <= 0.99
DELETE ProductWHERE price <=0.99
Client 2: SELECT count(*)FROM Product
SELECT count(*)FROM SmallProduct
What’s wrong ?
![Page 21: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/21.jpg)
21
Protection against crashes
Client 1:INSERT INTO SmallProduct(name, price)
SELECT pname, priceFROM ProductWHERE price <= 0.99
DELETE ProductWHERE price <=0.99
Client 1:INSERT INTO SmallProduct(name, price)
SELECT pname, priceFROM ProductWHERE price <= 0.99
DELETE ProductWHERE price <=0.99
What’s wrong ?
Crash !
![Page 22: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/22.jpg)
22
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 !
![Page 23: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/23.jpg)
23
Transactions in SQL
• In “ad-hoc” SQL:– Default: each statement = one transaction
• In “embedded” SQL:BEGIN TRANSACTION
[SQL statements]
COMMIT or ROLLBACK (=ABORT)
![Page 24: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/24.jpg)
24
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
![Page 25: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/25.jpg)
25
Serializability
• Enforced with locks, like in Operating Systems !• But this is not enough:
LOCK A[write A=1]UNLOCK A. . .. . .. . .. . .LOCK B[write B=2]UNLOCK B
LOCK A[write A=1]UNLOCK A. . .. . .. . .. . .LOCK B[write B=2]UNLOCK B
LOCK A[write A=3]UNLOCK ALOCK B[write B=4]UNLOCK B
LOCK A[write A=3]UNLOCK ALOCK B[write B=4]UNLOCK B
User 1 User 2
What is wrong ?
time
![Page 26: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/26.jpg)
26
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
![Page 27: Lecture 05: SQL Systems Aspects](https://reader030.vdocuments.mx/reader030/viewer/2022032612/5681344b550346895d9b3140/html5/thumbnails/27.jpg)
27
Isolation Levels in SQL
1. “Dirty reads”SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
2. “Committed reads”SET TRANSACTION ISOLATION LEVEL READ COMMITTED
3. “Repeatable reads”SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
4. Serializable transactions (default):SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
Reading assignment: chapter 8.6