Lecture 17: Systems Aspects of SQL

Slides:



Advertisements
Similar presentations
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
Advertisements

Embedded SQL John Ortiz. Lecture 15Embedded SQL2 Why Isn’t Interactive SQL Enough?  How to do this using interactive SQL?  Print a well-formatted transcript.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
1 Chapter 7 System Aspects of SQL uSQL in a Programming Environment uTransactions uAuthorization.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #15 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #16 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Murali Mani SQL with other Programming Languages.
End of SQL: Triggers, Impedance Mismatch and Transactions February 6 th, 2004.
Remaining Topics in SQL to be covered… NULL values in SQL Outer joins in SQL Constraints and Triggers in SQL Embedded SQL.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
Matthew P. Johnson, OCL2, CISDD CUNY, January OCL2 Oracle 10g: SQL & PL/SQL Session #6 Matthew P. Johnson CISDD, CUNY Fall, 2004.
Programs with SQL Host language + Embedded SQL Preprocessor Host Language + function calls Host language compiler Host language program Preprocessor Host.
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
Winter 2002Arthur Keller – CS 18011–1 Schedule Today: Feb. 7 (TH) u PL/SQL, Embedded SQL, CLI, JDBC. u Read Sections 8.1, Feb. 12 (T) Advising.
Embedded SQL Direct SQL is rarely used: usually, SQL is embedded in some application code. We need some method to reference SQL statements. But: there.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #14 M.P. Johnson Stern School of Business, NYU Spring, 2008.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept.
Remaining Topics in SQL to be covered… NULL values in SQL Outer joins in SQL Constraints and Triggers in SQL Embedded SQL.
Agenda Journalling More Embedded SQL. Journalling.
Chapter 8 Using SQL in an Application. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 CSC 4480 outline Intro (3-9) Static SQL (10-11)
Stored Procedures, Transactions, and Error-Handling
Transactions1 Unit of work on a database. Transactions2 Transactions, concept Logical unit of work on the database –Examples Transfer money from bank.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
An Introduction to SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 4.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
Phase 2, Answering queries using views. February 2 nd, 2004.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
Constraining Attribute Values Constrain invalid values –NOT NULL –gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) –MovieName CHAR(30) CHECK (MovieName IN (SELECT.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
10 1 Chapter 10 - A Transaction Management Database Systems: Design, Implementation, and Management, Rob and Coronel.
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
1 Lecture 8: SQL Programming and Transactions Friday, January 24, 2003.
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: Stored Procedure & Embedded SQL Ch. 9.3, 9.4.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Embedded SQL. u Read Sections 8.1, 8.5. Next u Transaction concepts, security u Read Sections 8.6 – 8.7.
Chapter 4 An Introduction to SQL.
Why Object-Oriented DBMS?
Introduction to SQL Programming Techniques
SQL Environment.
Introduction to Database Systems, CS420
Databases We are particularly interested in relational databases
Error Handling Summary of the next few pages: Error Handling Cursors.
Remaining Topics in SQL to be covered…
CPSC-310 Database Systems
CPSC-310 Database Systems
Database Application Development
Transactions Properties.
Lecture 06: SQL Systems Aspects
Lecture 4: Updates, Views, Constraints and Other Fun Features
DB Programming: SQL Programming Techniques
Unit I-2.
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
Lecture 13: Transactions in SQL
Lecture 14: XML Publishing & Storage
Lecture 05: SQL Systems Aspects
Embedded SQL Chapter 8.
Lecture 11: Transactions in SQL
Presentation transcript:

Lecture 17: Systems Aspects of SQL Wednesday, November 1, 2000

Outline Embedded SQL (7.1) Transactions in SQL (7.2)

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 Example: SQL in C: C uses int, char[..], pointers, etc SQL uses tables Impedance mismatch = incompatible types

The Impedance Mismatch Problem Why not use only one language? Forgetting SQL: “we can quickly dispense with this idea” [Ullman & Widom, pg. 363]. SQL cannot do everything that the host language can do. Solution: use cursors

Programs with Embedded SQL Host language + Embedded SQL Preprocessor Preprocessor Host Language + function calls Host language compiler Host language compiler Host language program

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;

Example Product (pname, price, quantity, maker) Purchase (buyer, seller, store, pname) Company (cname, city) Person(name, phone, city)

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); }

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; }

Cursors Declare the cursor Open the cursor Fetch tuples one by one Close the cursor

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;

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”);

What is NO_MORE_TUPLES ? #define NO_MORE_TUPLES !(strcmp(SQLSTATE,”02000”))

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).

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

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 “:”

Transactions in SQL serializability In real life, many updates happen simultaneously on the database Want to ensure that each has an effect equivalent to running alone

Transactions: Serializability int order(char *prod, int quant) { 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; EXEC SQL SELECT quantity INTO :q FROM Product WHERE name=:n if (quant > q) return (-1); /* error: not enough items */ q = quant; EXEC SQL UPDATE Product SET quantity =quantity-:q WHERE name=:n; }

Transactions: Serializability processes order(3) Write: -2 Read: q=4 OK order(3) Read: q=4 OK Write: 1 t1 t2 t3 t4 time quantity=4 quantity=-2 ????

Transactions: Serializability a function execution is serial if it is completely before or completely after any other function’s execution a function execution is serializable if it equivalent to one that is serial DBMS can offer serializability guarnatees

Transactions: Atomicity the danger: some database operation may leave the database in an unacceptable state, because of: concurrent execution with other operations Crashes Atomicity = an operation either terminates correctly, or does not affect the database

Transactions: Atomicity void order(int q) { …. EXEC SQL UPDATE Product SET quantity =quantity-:q WHERE …; EXEC SQL UPDATE Customer SET quantity =quantity+:q Crash !

Transactions: Atomicity Can end a database operation in two ways: EXEC SQL COMMIT; EXEC SQL ROLLBACK;