Lecture 8: SQL Programming and Transactions: Friday, January 24, 2003
Lecture 8: SQL Programming and Transactions: Friday, January 24, 2003
1
Impedance Mismatch
• Example: SQL in C:
– C uses int, char[..], pointers, etc
– SQL uses tables
• Impedance mismatch = incompatible types
2
The Impedance Mismatch
Problem
Why not use only one language?
EXEC
EXECSQL
SQL BEGIN
BEGINDECLARE
DECLARESECTION;
SECTION;
char
char productName[30];
productName[30];
EXEC
EXECSQL
SQL END
ENDDECLARE
DECLARESECTION;
SECTION; 4
Example
Product (pname, price, quantity, maker)
Purchase (buyer, seller, store, pname)
Company (cname, city)
Person(name, phone, city)
5
Using Shared Variables
Void
Void simpleInsert()
simpleInsert(){{
EXEC
EXECSQL
SQLBEGIN
BEGINDECLARE
DECLARESECTION;
SECTION;
char
char n[20],
n[20],c[30];
c[30]; /*/*product-name,
product-name,company-name
company-name*/*/
int
int p,p,q;q; /*/*price,
price,quantity
quantity*/*/
char
char SQLSTATE[6];
SQLSTATE[6];
EXEC
EXECSQL
SQLEND
ENDDECLARE
DECLARESECTION;
SECTION;
/*/* get
getvalues
valuesfor
forname,
name,price
priceand
andcompany
company somehow
somehow */*/
EXEC
EXECSQL
SQLINSERT
INSERT INTOINTOProduct(pname,
Product(pname,price,
price,quantity,
quantity,maker)
maker)
VALUES
VALUES (:n,
(:n,:p,
:p,:q,
:q,:c);
:c);
}}
6
Cursors
1. Declare the cursor
2. Open the cursor
3. Fetch tuples one by one
4. Close the cursor
7
Cursors
void product2XML() {
EXEC SQL BEGIN DECLARE SECTION;
char n[20], c[30];
int p, q;
char SQLSTATE[6];
EXEC SQL END DECLARE SECTION;
10
More on Cursors
• cursors can modify a relation as well as read it.
11
In JDBC
public void doIt(){ try
{ Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection c =
DriverManager.getConnection("jdbc:odbc:cse444","cse444","cse444");
java.sql.Statement s= c.createStatement();
java.sql.ResultSet rs; rs = s.executeQuery("Select * from beers");
java.sql.ResultSetMetaData md = rs.getMetaData();
while (rs.next()){
area.append("\nTUPLE: |");
for (int i = 1; i <= md.getColumnCount();i++)
{ area.append(rs.getString(i) + " | "); } }
rs.close(); }
catch (Exception e){ e.printStackTrace(); System.out.println("something
went wrong in database land"); } } 12
Transactions
Address two issues:
13
Flight Reservation
get values for :flight, :date, :seat
if (!occ) {
EXEC SQL UPDATE Flights
SET occupied = ‘true’
WHERE fltNum= :flight AND fltdt= :date AND fltSeat=:seat
/* more code missing */
}
else /* notify customer that seat is not available */
14
Problem #1
Customer 1 - finds a seat empty
serializability
15
Bank Transfers
Transfer :amount from :account1 to :account2
In SQL:
• In “embedded” SQL:
BEGIN TRANSACTION
[SQL statements]
COMMIT or ROLLBACK (=ABORT)
20
Transactions: Serializability
Serializability = the technical term for
isolation
• An execution is serial if it is completely
before or completely after any other
transaction’s execution
• An execution is serializable if it equivalent
to one that is serial
• DBMS can offer serializability guarantees
21
Serializability
• Enforced with locks, like in Operating Systems !
• But this is not enough:
User 1 LOCK
LOCKAA User 2
[write
[writeA=1]
A=1]
UNLOCK
UNLOCKAA LOCK
LOCKAA
. .. .. . [write
[writeA=3]
A=3]
. .. .. . UNLOCK
UNLOCKAA
. .. .. . LOCK
LOCKBB
. .. .. . [write
[writeB=4]
B=4] time
LOCK
LOCKBB UNLOCK
UNLOCKBB
[write
[writeB=2]
B=2]
UNLOCK
UNLOCKBB 22
What is wrong ?
Serializability
• Solution: two-phase locking
– Lock everything at the beginning
– Unlock everything at the end
25
Building an Application with a
DBMS
• Requirements modeling (conceptual, pictures)
– Decide what entities should be part of the application and
how they should be linked.
• Schema design and implementation
– Decide on a set of tables, attributes.
– Define the tables in the database system.
– Populate database (insert tuples).
• Write application programs using the DBMS
– way easier now that the data management is taken care of.
26
Database Design
• Why do we need it?
– Agree on structure of the database before
deciding on a particular implementation.
• Consider issues such as:
– What entities to model
– How entities are related
– What constraints exist in the domain
– How to achieve good designs
27
Database Design Formalisms
1. Object Definition Language (ODL):
– Closer in spirit to object-oriented models
2. Entity/Relationship model (E/R):
– More relational in nature.
28