Programming and SQL Edel Sherratt
Motivation 1: Integrity Checking Sometimes primary keys and foreign keys are not enough For example, they do not enforce mandatory participation on the many side of a one-to- many relationship So additional facilities are needed
Integrity Checking (from before) Primary Keys Foreign Keys Referential Integrity Unique/NOT NULL Simple Efficient Preferred (Available in all DBMS) SQL stored modules: Stored Procedures Triggers Functions Whenever necessary and possible Application CodeIf all else fails
Motivation 2: Impedance mismatch Mismatches between application programming language and SQL SQL handles rows of data; a language like C or Java handles only one row at a time SQL is declarative; C is procedural; Java is object oriented and imperative There are usually mismatches between SQL and programming language data types
SQL/PSM (Persistent Stored Modules) Extensions to SQL part of the SQL standard: SQL/PSM (ANSI/ISO/IEC :1999) But not fully implemented Partly because proprietary implementations were developed before the standard
SQL Programming Languages DBMS-specific SQL procedural languages: – Oracle: PL/SQL – Sybase and Microsoft: T-SQL – Informix: SPL – PostgreSQL: PL/pgSQL
Oracle Manual, v10 Using Oracle PL/SQL to improve performance
SQL Programming Language Constructs Functions and procedures IF, WHILE Assignment: both the usual kind and also assignment that results from SQL queries Cursors: allow the rows of a query to be handled one at a time Exception handling Triggers
To find out more The PostgreSQL reference manual: dex.html See section V, Server Programming dex.html Oracle PL/SQL: e/features/plsql/index.html e/features/plsql/index.html Connolly and Begg, Database Systems, fifth ed., Chapter 8: Advanced SQL