90 likes | 238 Views
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.
E N D
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
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 9075-4: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: http://www.postgresql.org/docs/9.1/static/index.htmlSee section V, Server Programming • Oracle PL/SQL: http://www.oracle.com/technetwork/database/features/plsql/index.html • Connolly and Begg, Database Systems, fifth ed., Chapter 8: Advanced SQL