Ch6_DBApp - Database Management Systems 3ed, R....

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Database Application Development Chapter 6 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2 Overview Concepts covered in this lecture: ¡ SQL in application code ¡ Embedded SQL ¡ Cursors ¡ Dynamic SQL ¡ JDBC ¡ SQLJ ¡ Stored procedures Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 SQL in Application Code ¡ SQL commands can be called from within a host language (e.g., C++ or Java) program. ¢ SQL statements can refer to host variables (including special variables used to return status). ¢ Must include a statement to connect to the right database. ¡ Two main integration approaches: ¢ Embed SQL in the host language (Embedded SQL, SQLJ) ¢ Create special API to call SQL commands (JDBC) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4 SQL in Application Code (Contd.) Impedance mismatch: ¡ SQL relations are (multi-) sets of records, with no a priori bound on the number of records. No such data structure exist traditionally in procedural programming languages such as C++. (Though now: STL) ¢ SQL supports a mechanism called a cursor to handle this. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Embedded SQL ¡ Approach: Embed SQL in the host language. ¢ A preprocessor converts the SQL statements into special API calls. ¢ Then a regular compiler is used to compile the code. ¡ Language constructs: ¢ Connecting to a database: EXEC SQL CONNECT ¢ Declaring variables: EXEC SQL BEGIN (END) DECLARE SECTION ¢ Statements: EXEC SQL Statement; Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6 Embedded SQL: Variables EXEC SQL BEGIN DECLARE SECTION char c_sname[20]; long c_sid; short c_rating; float c_age; EXEC SQL END DECLARE SECTION ¡ Two special “error” variables: ¢ SQLCODE (long, is negative if an error has occurred) ¢ SQLSTATE (char[6], predefined codes for common errors) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Cursors ¡ Can declare a cursor on a relation or query statement (which generates a relation). ¡ Can open a cursor, and repeatedly fetch a tuple then move the cursor, until all tuples have been retrieved. ¢ Can use a special clause, called ORDER BY , in queries that are accessed through a cursor, to control the order in which tuples are returned. • Fields in ORDER BY clause must also appear in SELECT clause. ¢ The ORDER BY clause, which orders answer tuples, is only allowed in the context of a cursor. ¡ Can also modify/delete tuple pointed to by a cursor. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8 Cursor that gets names of sailors who ’ ve reserved a red boat, in alphabetical order ¡ Note that it is illegal to replace S.sname by, say, S.sid in the ORDER BY clause! (Why?) ¡ Can we add S.sid to the SELECT clause and replace S.sname by S.sid in the ORDER BY clause?...
View Full Document

This note was uploaded on 02/06/2010 for the course CSE 302 taught by Professor Joel during the Summer '05 term at Punjab Engineering College.

Page1 / 14

Ch6_DBApp - Database Management Systems 3ed, R....

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online