Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)

  • 2,963 views
Uploaded on

This lecture is part of an Introduction to Databases course given at the Vrije Universiteit Brussel.

This lecture is part of an Introduction to Databases course given at the Vrije Universiteit Brussel.

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,963
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
0
Comments
0
Likes
8

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. 2 December 2005 Introduction to Databases Advanced SQL Prof. Beat Signer Department of Computer Science Vrije Universiteit Brussel http://www.beatsigner.com
  • 2. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 2March 21, 2014 Context of Today's Lecture Access Methods System Buffers Authorisation Control Integrity Checker Command Processor Program Object Code DDL Compiler File Manager Buffer Manager Recovery Manager Scheduler Query Optimiser Transaction Manager Query Compiler Queries Catalogue Manager DML Preprocessor Database Schema Application Programs Database Manager Data Manager DBMS Programmers Users DB Admins Based on 'Components of a DBMS', Database Systems, T. Connolly and C. Begg, Addison-Wesley 2010 Data, Indices and System Catalogue
  • 3. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 3March 21, 2014 Authorisation  Different types of authorisations (privileges) can be assigned to a user via DDL  insert, read, update and delete privileges  The creator of a new relation is automatically given all privileges on that relation grantPrivilege = "GRANT" , "ALL PRIVILEGES" , "TO" , ( "PUBLIC" | grantee ) | "GRANT" , privilege , { "," , privilege } ) , "ON" , ( table | view ) , { "," , ( table | view ) } , "TO" , ( "PUBLIC" | grantee ) , [ "WITH GRANT OPTION" ]; privilege = "INSERT" | "SELECT" | ( "UPDATE" , [ "(" , column , { "," , column } , ")" ] ) | "DELETE"; grantee = ( user | role ) , { "," , ( user | role ) };
  • 4. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 4March 21, 2014 Authorisation ...  An update privilege may be limited to certain attributes only  Roles can be used to group users GRANT ALL PRIVILEGES TO Alice, Bob; GRANT INSERT, UPDATE (amount, status) ON Order TO Eve WITH GRANT OPTION; createRole = "CREATE ROLE" , role; grantRole = "GRANT" , role , "TO" ( "PUBLIC" | grantee ); CREATE ROLE PowerUser; GRANT PowerUser TO Alice, Bob;
  • 5. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 5March 21, 2014 Authorisation ...  Privileges can be passed to other users if the WITH GRANT OPTION has been specified  Privileges and roles can be removed via the REVOKE command (similar to GRANT statement)  Examples  Note that the revocation of a privilege may have an effect on grants that have been passed to other users  authorisation graph has to be inspected REVOKE INSERT ON Order FROM Eve; REVOKE PowerUser FROM Bob;
  • 6. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 6March 21, 2014 Authorisation Graph  Privilege revocation examples  remove privilege from user1 (g1)  user4 still has privilege via g24  remove privilege from user2 (g2)  user2 still has privilege via g23  remove privilege from user3 (g3)  user3 still has privilege via g23  remove privilege from user2 (g2) and user3 (g3) - g23 still exists but is no longer part of a path starting from the admin  user2 and user3 no longer have privilege  user5 no longer has privilege user1 user2 user3 user4 user5admin g1 g2 g24 g3 g14 g25 g23
  • 7. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 7March 21, 2014 Working with SQL  There are two main modes to work with SQL  via generic query interfaces or SQL application interfaces  note that standard SQL is not Turing complete  Generic SQL interface  command-line or web-based interface  answer single questions (one query)  interactive query evaluation  SQL interface for applications  sequence of queries, inserts, updates and deletes  SQL statements that are embedded within a host language  query results are processed and reused in non-declarative actions - I/O interfaces - human-computer interaction
  • 8. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 8March 21, 2014 Impedance Mismatch Problem  Combination of SQL with a host language  mix of declarative and procedural programming paradigms  two completely different data models - relational model with sets and bags - no pointers, loops and branches in basic SQL - no NULL values in typical host languages  different set of data types - many programming languages do not offer Date or Time data types  Interfacing with SQL is not straightforward  data has to be converted between the host language and SQL due to the impedance mismatch  ~30% of the code and effort is used for this conversion!  various libraries to simplify the mapping (e.g. Hibernate)
  • 9. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 9March 21, 2014 Impedance Mismatch Problem ...  Why not simply use a single language?  both languages have their domain-specific strengths  Note that the impedance mismatch problem is discussed again later, when we have a look at the "integration" of object-oriented languages and relational databases /** * Transforms the Swiss Projection * coordinates (y, x, h') to the * ellipsoidic WGS84 coordinates (phi, * lambda, h) using and approach of * [U. Marti] based on the formulas of * [Bolliger, 1967]. * @param swissCoordinates the Swiss * coordinates to be transformed. * @return the WGS84 coordinates for * the specified Swiss coordinates. */ public static final WGS84Coordinates transformSwissCoordinatesToWGS84( SwissCoordinates swissCoordinates) { double yPrime = (swissCoordinates.getY() - 600000) / 1000000; double xPrime = (swissCoordinates.getX() - 200000) / 1000000; double xPrimePow2 = xPrime * xPrime; double xPrimePow3 = xPrimePow2 * xPrime; double yPrimePow2 = yPrime * yPrime; double yPrimePow3 = yPrimePow2 * ... Impedance Mismatch Application Code Database
  • 10. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 10March 21, 2014 SQL Programming Environments  Embedded SQL (ESQL)  integration of SQL in a host programming language  e.g. interfacing with C through ESQL/C  SQL Call-Level Interface (SQL/CLI)  e.g. interfacing with Java through JDBC  SQL Persistent Stored Modules (SQL/PSM)  stored procedures  Triggers
  • 11. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 11March 21, 2014 Database Applications  Task sharing between client and server  client does data presentation  server does data management  Where should we implement the application logic?  on the client - ESQL, SQL/CLI, ODBC, JDBC, ...  on the server - SQL/PSM, Stored Procedures, Triggers  thin client vs. thick client
  • 12. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 12March 21, 2014 Example Database customerID name street postcode city orderID customerID cdID date amount status Customer CDOrder name address Producer cdID title duration price year producer CREATE TABLE Customer ( customerID INTEGER CHECK (customerID > 0) PRIMARY KEY, name VARCHAR(30) NOT NULL, street VARCHAR(30) NOT NULL, postcode SMALLINT CHECK (postcode > 0), city VARCHAR(20) );
  • 13. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 13March 21, 2014 Example Database ... CREATE TABLE Producer ( name VARCHAR(40) PRIMARY KEY, address VARCHAR(30) ); CREATE TABLE Order ( orderID INTEGER CHECK (orderID > 0) PRIMARY KEY, customerID INTEGER, cdID INTEGER, date DATE, amount INTEGER, Status VARCHAR(20) NOT NULL DEFAULT 'open', UNIQUE (customerID, cdID, date), FOREIGN KEY (customerID) REFERENCES Customer(customerID) ON UPDATE CASCADE ON DELETE SET NULL, FOREIGN KEY (cdID) REFERENCES CD(cdID) ON UPDATE CASCADE );
  • 14. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 14March 21, 2014 Example Database ... CREATE TABLE CD ( cdID INTEGER PRIMARY KEY, title VARCHAR(30) NOT NULL, duration SMALLINT, price SMALLINT, year SMALLINT, producer VARCHAR(40), FOREIGN KEY (producer) REFERENCES Producer(name) );
  • 15. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 15March 21, 2014 Embedded SQL  The expressiveness of SQL can be enhanced by embedding SQL statements into application code  Use of a preprocessor  transforms SQL statements into function calls in the host language  type checks, syntax and semantic validation  error handling  Data interchange via shared variables  shared variables are used in SQL statements as well as in statements of the host language  A special SQLSTATE variable is used to connect the host language with the SQL execution engine
  • 16. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 16March 21, 2014 Embedded SQL Program Processing  The preprocessor transforms a program with embedded SQL statements into the host language  e.g. nsqlprep.exe for ESQL-C  The compiler produces the linked program object code Host language with Embedded SQL Host language with function calls Program object code Embedded SQL preprocessor Host language compiler SQL library
  • 17. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 17March 21, 2014 Shared Variables  Shared variables are referenced with their name prefixed with a colon in SQL (e.g. :producerName)  SQLSTATE contains a five character return code  the first two characters define the class; the remaining three characters the subclass  declared as 6 char array in C since the strcmp function requires a terminator char ('0') EXEC SQL BEGIN DECLARE SECTION; char producerName[40]; producerAddress[30]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;
  • 18. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 18March 21, 2014 SQLSTATE Return Codes SQLSTATE Description 00000 Successful completion 01001 Cursor update or delete failure due to optimistic concurrency check failure 01003 Elimination of null values by aggregate operator 01004 Select or fetch into host variable that is too short 02000 No data found 07002 Number of columns does not match number of host variables 22001 String data truncated (on the right) on insert or update 40001 Transaction rollback due to deadlock ... ...
  • 19. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 19March 21, 2014 Using Shared Variables void createProducer() { EXEC SQL BEGIN DECLARE SECTION; char producerName[40], producerAddress[30]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; printf("Producer Name: "); scanf("%s", producerName); printf("Producer Address: "); scanf("%s", producerAddress); EXEC SQL INSERT INTO Producer(name, address) VALUES (:producerName, :producerAddress); }
  • 20. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 20March 21, 2014 Retrieving Data  Embeddable SQL statements  INSERT, DELETE, UPDATE (data)  CREATE, ALTER, DROP (schema)  SELECT FROM WHERE queries are not directly embeddable due to the impedance mismatch  a set data type is not supported by most host languages  One of the following two mechanisms can be used for accessing query results  single-row select statement - if the query returns a single result tuple  cursor - enables the iteration over the tuples of a result set
  • 21. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 21March 21, 2014 Single-Row Select Statement void getMostExpensiveCDOfProducer(char *producer) { EXEC SQL BEGIN DECLARE SECTION; char cdName[30]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL SELECT TOP 1 title INTO :cdName FROM CD JOIN Producer ON producer = name WHERE name = :producer ORDER BY price DESC; if (strcmp(SQLSTATE, "00000")) { printf("Most Expensive CD: %s.n", cdName); } }
  • 22. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 22March 21, 2014 Cursors  Flexible way to establish a connection between SQL and the host language  A cursor is an iterator over a relation such as a stored table, a computed view or a query result declareCursor = "EXEC SQL DECLARE" , name , "CURSOR FOR" , query; initialiseCursor = "EXEC SQL OPEN" , name; fetchTuple = "EXEC SQL FETCH FROM" , name , "INTO" , variable , { "," , variable }; closeCursor = "EXEC SQL CLOSE" , name;
  • 23. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 23March 21, 2014 Cursor Example void getAllCDsOfProducer(char *producer) { EXEC SQL BEGIN DECLARE SECTION; char cdTitle[30]; int year; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE cdCursor CURSOR FOR SELECT title, year FROM CD JOIN Producer ON producer = name WHERE name = :producer; EXEC SQL OPEN cdCursor; do { EXEC SQL FETCH FROM cdCursor INTO :cdTitle, :year; if (strcmp(SQLSTATE, "00000")) { printf("Found CD: %s (%d).n", cdTitle, year); } else { break; } } while (1); EXEC SQL CLOSE cdCursor; }
  • 24. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 24March 21, 2014 Data Modification with Cursors  Cursors can be used to modify the current tuple  SQL UPDATE and DELETE statements  cursor references a relation that is stored in the database  a modification of views and query results is not allowed  Extended syntax for the WHERE clause update = "UPDATE" , table , "SET" , column , "=" , value , { "," , column , "=" , value } , "WHERE CURRENT OF" , name; delete = "DELETE FROM" , table , "WHERE CURRENT OF" , name;
  • 25. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 25March 21, 2014 Cursor Update/Delete Example void changeCDs(int oldest) { EXEC SQL BEGIN DECLARE SECTION; char title[30], producer[40]; int cdID, duration, price, year, pID; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE cdCursor CURSOR FOR CD; EXEC SQL OPEN cdCursor; do { EXEC SQL FETCH FROM cdCursor INTO :cdID, :title, :duration, :price, :year, :pID; if (!strcmp(SQLSTATE, "00000")) { break; } if (year >= oldest) { EXEC SQL UPDATE CD SET price = 15 WHERE CURRENT OF cdCursor; } else { EXEC SQL DELETE FROM CD WHERE CURRENT OF cdCursor; } } while (1); EXEC SQL CLOSE cdCursor; }
  • 26. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 26March 21, 2014 Scrolling Cursors  Scrolling cursors can be used to move through a relation in non-sequential order  Scrolling cursor definition declareSCursor = "EXEC SQL DECLARE" , name , "SCROLL CURSOR FOR" , query; fetchTuple = "EXEC SQL FETCH" , ( "NEXT" | "PRIOR" | "FIRST" | "LAST" | "RELATIVE" , offset | "ABSOLUTE" , index ) , "FROM" , name , "INTO" , variable , { "," , variable };
  • 27. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 27March 21, 2014 Concurrent Updates  What happens if another process updates the set of tuples referenced in a cursor?  To deal with concurrent access, cursors may be declared as  INSENSITIVE - prevents concurrent updates by locking the set of tuples  READ ONLY - declares that no updates are performed - a read only cursor can run in parallel with an insensitive cursor iCursor = "EXEC SQL DECLARE" , name , "INSENSITIVE CURSOR FOR" , query; rCursor = "EXEC SQL DECLARE" , name , "CURSOR FOR" , query, "FOR READ ONLY";
  • 28. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 28March 21, 2014 ESQL Summary  The concepts introduced in Embedded SQL are common to most database connectivity libraries  Implementations for all major languages  Ada, C, C++, Java, COBOL, Fortran, Pascal, Eiffel, PL/1, ...  Problems  debugging of preprocessed programs  vendor and platform-specific preprocessor
  • 29. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 29March 21, 2014 Call Level Interface  SQL library functions are called directly  not tied to particular DBMS (drivers stored separately)  No preprocessor required  Various implementations exist  SQL/CLI for C  Open Database Connectivity (ODBC) and its successor OLE DB  Java™ Database Connectivity (JDBC)  ActiveX® Data Objects (ADO)  Vendors plan to discontinue ESQL support and advise to use CLI interfaces instead
  • 30. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 30March 21, 2014 Java Database Connectivity (JDBC)  The following JDBC packages are part of Java SE 7  java.sql.* - basic JDBC functionality for J2SE  javax.sql.* - supplemental extensions for J2EE  The standardised Java API provides database connectivity through vendor-specific drivers  Limited code portability among SQL platforms due to vendor-specific SQL dialects
  • 31. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 31March 21, 2014 Opening a JDBC Connection  The JDBC URL varies for different drivers  e.g. jdbc:microsoft:sqlserver for Microsoft SQL Server public Connection openConnection() { try { Connection conn = DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433", "fred", "password"); return conn; } catch (SQLException e) { System.out.println("Opening JDBC Connection failed."); return null; } }
  • 32. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 32March 21, 2014 Statements  There are three different kinds of statements  Statement - general statement  PreparedStatement - precompiled statement - more efficient if the same query is executed multiple times  CallableStatement - used to call stored procedures  Methods for executing a statement  executeUpdate(String sql)  executeQuery(String sql)  No parameters are required for PreparedStatements
  • 33. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 33March 21, 2014 Statements ... void insertProducer(String name, String address) { try { Connection conn = this.openConnection(); Statement s = conn.createStatement(); s.executeUpdate("INSERT INTO Producer VALUES ('" + name + "', " + address + ")"); } catch (SQLException e) { System.out.println("Inserting "" + name + "" failed."); } } public void insertProducer(String name, String address) { try { Connection conn = this.openConnection(); Statement s = conn.prepareStatement("INSERT INTO Producer VALUES ('" + name + "', " + address + ")"); s.executeUpdate(); } catch (SQLException e) { System.out.println("Inserting "" + name + "" failed."); } }
  • 34. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 34March 21, 2014 Result Set  JDBC represents cursors as ResultSet  API offers navigation methods on a ResultSet  next(), previous()  first(), last()  absolute(int row), relative(int rows)  API offers various get methods to fetch data as well as update methods for common SQL data types
  • 35. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 35March 21, 2014 Result Set Example  Note that it would be easier to use the SQL AVG operator public float getAverageCDLength() { float result = 0.0; try { Connection conn = this.openConnection(); Statement s = conn.createStatement(); ResultSet set = s.executeQuery("SELECT length FROM CD"); int i = 0; while (set.next()) { result += set.getInt(1); i++; } return result/i; } catch (SQLException e) { System.out.println("Calculation of average length failed."); return 0; } }
  • 36. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 36March 21, 2014 Parameter Parsing  Prepared statements can be parameterised  flexibility to use different values in a given query  A question mark (?) is used as a placeholder in the query string  Various methods to insert data at runtime  setString(int index, String s)  setInt(int index, int i)  setBoolean(int index, boolean b)  setDate(int index, Date d)  ...
  • 37. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 37March 21, 2014 Parameter Parsing Example public void insertProducers(List<Producer> producers) { try { Connection conn = this.openConnection(); Statement s = conn.prepareStamement( "INSERT INTO Producer(name, address) VALUES(?, ?)"); for (Producer producer : producers) { s.setString(1, producer.getName()); s.setString(2, producer.getAddress()); s.executeUpdate(); } } catch (SQLException e) { System.out.println("Insertion of Producers failed."); } }
  • 38. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 38March 21, 2014 Special Datatypes and NULL Values  Some SQL datatypes cannot be easily mapped to Java datatypes  JDBC offers implementations for these types  Clob, Blob, Date, Time, Timestamp  Null values are handled via special methods  wasNull()  updateNull(int index)  setNull(int index, int sqlType)
  • 39. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 39March 21, 2014 JDBC Summary  Concepts of ESQL and JDBC are similar  State of the art for Java database programming  Generic JDBC-ODBC driver if no specific JDBC driver is available  Java Data Objects (JDO)  enables Java programmers to directly store their Java domain model instances in a database  hides SQL from the programmer  http://java.sun.com/javase/technologies/database/
  • 40. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 40March 21, 2014 Application Logic on the Server Side  Stored Procedures  PL/SQL - Oracle  Transact-SQL - Microsoft  standardised as SQL/PSM in SQL-99 - Persistent Stored Modules  Triggers  event driven execution of application logic  User Defined Types  standardised in SQL-99
  • 41. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 41March 21, 2014 SQL Persistent Stored Modules  SQL is extended with constructs known from high-level programming languages  assignments, branches, loops, subroutines and exceptions createProcedure = "CREATE PROCEDURE" , name , "(" , parameters , ")" , localDeclarations , procedureBody; createFunction = "CREATE FUNCTION" , name , "(" , parameters , ")" , "RETURNS" , type , localDeclarations , functionBody;
  • 42. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 42March 21, 2014 Parameters  SQL/PSM defines three types for parameters  IN - input only  OUT - output only  INOUT - both input and output  The default mode is IN  Procedure parameters can have any mode  Function parameters can have IN mode only
  • 43. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 43March 21, 2014 Procedure Example CREATE PROCEDURE RelocateProducer( IN oldAddress VARCHAR(30), IN newAddress VARCHAR(30) ) UPDATE Producer SET address = newAddress WHERE address = oldAddress;
  • 44. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 44March 21, 2014 Function Example CREATE FUNCTION AverageCDLengthOfProducer( IN name VARCHAR(30), ) RETURNS INTEGER DECLARE result INTEGER; BEGIN SELECT AVG(length) INTO result FROM CD WHERE producer = name; RETURN result; END;
  • 45. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 45March 21, 2014 SQL/PSM Statements  Procedure calls  Function evaluation  functions cannot be called  functions are directly invoked as parts of expressions  Return statement  does not terminate the function  additional RETURN statements may cause the return value to change after an initial return statement CALL name (arguments) RETURN expression
  • 46. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 46March 21, 2014 SQL/PSM Statements ...  Local variable declaration  Assignments  NULL is a permissible expression  Compound statement DECLARE name type SET variable = expression BEGIN ... END
  • 47. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 47March 21, 2014 Branch Statement  If statement  ELSEIFs and final ELSE and are optional IF condition THEN statements ELSEIF condition THEN statements ELSE statements ENDIF
  • 48. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 48March 21, 2014 Branch Example CREATE FUNCTION IsExpensive( IN cdTitle CHAR(30) ) RETURNS BOOLEAN IF NOT EXISTS(SELECT * FROM CD WHERE title = cdTitle) THEN RETURN FALSE; ELSEIF 50 < (SELECT MAX(price) FROM CD WHERE title = cdTitle) THEN RETURN TRUE; ELSE RETURN FALSE; ENDIF;
  • 49. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 49March 21, 2014 Loop Statements  Basic loop statement  The loop can be terminated with LEAVE label  the label identifies which loop has to be aborted label: LOOP statements END LOOP
  • 50. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 50March 21, 2014 Loop Statements ...  While loop statement  Repeat loop statement WHILE condition DO statements END WHILE REPEAT statements UNITIL condition END REPEAT
  • 51. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 51March 21, 2014 Loop Statements ...  For loop statement  Convenience statement to iterate over cursor  open / close cursor  check if more tuples to be fetched  Name and cursor are needed for transformation into basic loop (by PSM interpreter) FOR name AS cursor CURSOR FOR query DO statements END FOR
  • 52. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 52March 21, 2014 Loop Example CREATE FUNCTION LengthVarianceOfProducer( IN name VARCHAR(40), ) RETURNS REAL DECLARE mean REAL; DECLARE count INTEGER; BEGIN SET variance = 0.0; SET mean = 0.0; SET count = 0; FOR cdLoop AS cdCursor CURSOR FOR SELECT length FROM CD WHERE producer = name; DO SET count = count + 1; SET variance = variance + length * length; SET mean = mean + length; END FOR; SET mean = mean/count; RETURN variance/count – mean * mean; END;
  • 53. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 53March 21, 2014 SQL/PSM Summary  Perform computation on the server side  Implement functionality not contained in SQL  recursion  closure  mathematical and statistical functions  SQL in combination with SQL/PSM forms a Turing complete programming language  Functionality for administrative tasks is often implemented as PSM
  • 54. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 54March 21, 2014 Transitive Closure (Recursion)  Inconvenient to specify transitive closure using iteration  Since SQL:1999 there is a form of recursion using the WITH RECURSIVE clause  Example  "Find all destinations that can be reached (directly or indirectly) from Zurich Airport" from to ZRH BRU ZRH JFK BRU CDG JFK BRU ... ... Flight
  • 55. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 55March 21, 2014 Transitive Closure (Recursion) ...  Recursive view is a union of two subqueries  base query  recursive query making use of the recursive view - recursive query should be monotonic (e.g. no aggregation in recursive view)  Repeat until no new tuples added and fixpoint is reached WITH RECURSIVE Connection(from, to) AS ( SELECT from, to FROM Flight UNION SELECT Flight.from, Connection.to FROM Flight, Connection WHERE Flight.to = Connection.from ) SELECT DISTINCT to FROM Connection WHERE Connection.from = 'ZRH';
  • 56. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 56March 21, 2014 Triggers  A trigger is a statement that the DBMS executes as a side effect of a modification to the database  The definition of a trigger follows the so-called event-condition-action (ECA) model  event - specifies the signal that triggers the rule (e.g. insert, update or delete)  condition - the condition that has to be satisfied for the execution of the action  action - the action to be executed  Triggers are a powerful tool for checks before/after an operation on a relation (e.g. for integrity constraints)
  • 57. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 57March 21, 2014 Triggers ...  Example  When data is restored from a backup or replicated, triggers often have to be disabled  otherwise triggers might be executed multiple times  The execution of a trigger can lead to a (infinitite) cascade of other triggers that get fired  often the length of such a trigger chain is limited by the system CREATE TRIGGER setNull BEFORE UPDATE ON Producer REFERENCING NEW ROW AS newRow FOR EACH ROW WHEN newRow.address = '' SET newRow.address = NULL;
  • 58. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 58March 21, 2014 SQL Transactions  Generic SQL Interface  every statement (query) is a single transaction  Programmatic SQL  several SQL statements can be grouped in a transaction  applications can initiate transactions  transactions can be committed or aborted  there are different transaction modes and isolation levels  A transaction is initiated with the START TRANSACTION command and terminated with a COMMIT or ROLLBACK operation
  • 59. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 59March 21, 2014 Transaction Modes  A transaction can be in read-only or read-write mode  SET TRANSACTION READ ONLY  SET TRANSACTION READ WRITE  The specification of a mode enables database systems to use this information for optimisation  multiple read-only transactions can run in parallel
  • 60. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 60March 21, 2014 Isolation Levels  SQL defines four isolation levels  READ UNCOMMITTED - dirty reads possible  READ COMMITED - read only committed values  REPEATABLE READ - each read gives same value  SERIALIZABLE - equal to serial execution  An isolation level is set by using  can be set together with mode SET TRANSACTION ISOLATION LEVEL level;
  • 61. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 61March 21, 2014 Homework  Study the following chapter of the Database System Concepts book  chapter 5 - sections 5.1-5.4, 5.7 - Advanced SQL
  • 62. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 62March 21, 2014 Exercise 6  Structured Query Language (SQL) 
  • 63. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 63March 21, 2014 References  A. Silberschatz, H. Korth and S. Sudarshan, Database System Concepts (Sixth Edition), McGraw-Hill, 2010  H. Garcia-Molina, J.D. Ullman and J. Widom, Database Systems: The Complete Book, Prentice Hall, 2002
  • 64. 2 December 2005 Next Lecture DBMS Architectures and Features