SQL Views 8. More SQL features: May be defined on base tables (ordinary tables) Views, PL/SQL, Functions,Triggers or on views (or both) CREATE VIEW LargeCities (name,population,country, code,fraction) 8.1 Views and updates AS (SELECT ci.name, ci.population, co.name, co.code, 8.2 Application architectures ci.population/co.population 8.3 PL/SQL – PL/pgSQL FROM City ci JOIN Country co ON ci.country = co.code 8.4 Functions and Procedures WHERE ci.population > 1000000) 8.5 Triggers implicite 8.6 Abstract Data Types CREATE VIEW VeryLargeCities AS (SELECT name, population, country FROM LargeCities l names see Kemper/Eickler chap. 14, Elmasri chap. 6.8, O'Neill: Chap. 4, WHERE l.population >= 3000000) Melton: SQL99, Postgres and Oracle Manuals (PL/PGSQL,PL/SQL)

© HS-2010 08-PLSQLetc-4

8.1 Views Views and privacy

Def.: A view is a named SQL-query, which Very large American cities: becomes part of the schema as a virtual JOIN with encompasses(continent,country...)

Intention CREATE OR REPLACE VIEW VLAmeriCities AS • Casting the database schema for different (SELECT c.name, c.population, c.country FROM LargeCities c JOIN Encompasses e applications ON c.code =e.country • Access protection WHERE e.continent = 'America' •Privacy AND c.population >= 3000000) • Structuring of SQL programs Views may be used like ordinary table in queries. ⇒ The RDM concept for external schemas ("3-schema-architecture") Privacy: column access may be granted even if access to base table is not allowed !

© HS-2010 08-PLSQLetc-2 © HS-2010 08-PLSQLetc-5

Materialized Views Views and code readability

.. simplify SQL queries Def.: A materialized view is a temporary Table , which contains the result set of an SQL query Countries more inhabitants than all american big cities • Not in all DBMS • Often used in replication scenarios SELECT c.name, c.population FROM country c • No way to / data WHERE c.population < ALL(SELECT population • But refreshing of the view makes sense FROM VLAmeriCities) • Sometimes called snapshot

• Different temporary tables CREATE TEMPORARY TABLE Temp AS () • Insertion / Deletion allowed Operator tree of query more complicated... • Dropped at the end of a session

© HS-2010 08-PLSQLetc-3 © HS-2010 08-PLSQLetc-6

1 8.2 Updatable views

View updates Many views are not updatable. Obviously:

CREATE OR REPLACE VIEW PopulInCities (country, cityPop) AS (SELECT co.name, sum(ci.population) FROM City ci JOIN Country co ON Joint optimization of ci.country=co.code views and query GROUP BY co.name)

View not updatable if defined using: • Aggregation • Arithmetic in Projection • DISTINCT

© HS-2010 08-PLSQLetc-7 © HS-2010 08-PLSQLetc-10

Evaluation of views Semantic characterization of updatable views

Steps: Def: A view V is updatable if for every u (*)

[1. Transform query on view using its definition] there exist one or more updates cu which applied to 2. Construct operator tree including view definitions the base relations and the subsequent application of the and query view definition result in the same result: 3. Optimize plan u (V(D)) = V (cu (D) ) 4. Execute query on base tables

• Semantic characterization, • Wanted: syntactic criteria for updatability

(*) as if it were materialized © HS-2010 08-PLSQLetc-8 © HS-2010 08-PLSQLetc-11

Views in Postgres Syntactic criteria

More general substitution concept in Postgres Read only views may be arbitrarily defined, Rules are "first class objects": CREATE RULE... Update is rejected, if view not updatable.

CREATE VIEW myview AS SELECT * FROM mytab; Syntactic criteria equivalent to Not updatable (SQL 92) • if grouped (GROUP BY), HAVING or aggregated CREATE TABLE myview (); • DISTINCT in SELECT clause • set operators (INTERSECT, EXCEPT, UNION) CREATE RULE "_RETURN" AS ON SELECT TO myview DO • more than one table in FROM clause INSTEAD SELECT * FROM mytab; • No updates on views (restrictive!)

Kind of dynamic view evaluation compared to static rewrite of query or query tree

© HS-2010 08-PLSQLetc-9 © HS-2010 08-PLSQLetc-12

2 Views and joins Find updatable columns

Find updatable columns by querying the CREATE VIEW CCP AS catalogue (SELECT c.name, c.capital, ci.population FROM Country c JOIN City ci ON c.capital=ci.name and c.code=ci.country SELECT column_name, updatable WHERE ci.population > 1000000 FROM user_updatable_columns This is a (system) view c.name) WHERE table_name ='LARGECITIES' -- Oracle must be upper case

Base tables: Country, City, COLUMN_NAME UPDATABLE Join on key: insertion in one table (Country) may ------generate one new row in in the other (City), if not NAME YES already present. POPULATION YES COUNTRY NO CODE NO FRACTION NO

© HS-2010 08-PLSQLetc-13 © HS-2010 08-PLSQLetc-16

Syntactic criteria (2) Views WITH CHECK OPTION

SQL 1999 Issue: side effects on base table rows, no effect on view Columns (of views) are potentially updatable if ... CREATE VIEW CCLarge(ctryName, capital, population) AS no DISTINCT operator (SELECT c.name as ctryName, c.capital, ci.population FROM Country c JOIN City ci no GROUP BY, HAVING clause ON c.capital=ci.name and c.code=ci.country no derived columns (e.g. arithmetic expressions) and c.province = ci.province WHERE ci.population > 1000000) WITH CHECK OPTION (1) Column is updatable if potentially updatable and one table in FROM clause (!) UPDATE TABLE CC_Large SET population = population - 20000 WHERE capital = 'Amsterdam' --has 1011000 inhabitants

What happens?

© HS-2010 08-PLSQLetc-14 © HS-2010 08-PLSQLetc-17

Key preserved tables CHECK OPTION

… SQL 1999: more than one table in FROM clause (2) Column c is updatable if potentially updatable and Update may result in insertion and deletion (!) of rows - c belongs to exactly one table - the key of the table is preserved, i.e. the update of c CHECK OPTION: update and insert must result in rows the may be traced back to exactly one row. view can , otherwise exception raised

Table is key preserved if every key of the table can also Example above: update has to be performed on base table be a key of the join result table. A key-preserved table has its keys preserved through a join.

© HS-2010 08-PLSQLetc-15 © HS-2010 08-PLSQLetc-18

3 View update by triggers Business logic

Triggers: Event – Condition – Action rules Big question: sits the "business logic" ? Event: Update, insert, delete (basically) Condition: WHEN < some conditon on table> • Business logic: the steps which have to be Action: some operation ( expressed as DML, DB- made in order to process a user query. Script language expression, even Java) e.g. "go to check out" in an Internet shop is implemented by several steps, most of them access the DB: User logged in? if not..., perform stock keeping operations, prepare INSTEAD OF Triggers (Postgres: rules) invoice, charge client, ..... - defined on views • Two tier or Three tier: ~ business logic separated - specify what to do in case of an update from user interaction as well as data access? of the view

details on triggers: see below

© HS-2010 08-PLSQLetc-19 © HS-2010 08-PLSQLetc-22

Summary views Architectures

• Views: important mechanism for Client server model access protection / privacy – Business logic sits in application program simplyfy SQL application programming – Runs on a machine different from database server – Interaction by means of SQL queries, inserts, updates • The mechanism for defining external schemas in the RDM • Useful for modeling generalization hierarchies • Disadvantage: updates (inserts, deletes) not always Application code possible DB- • Criteria for updatable views complex ... Server • INSTEAD OF triggers are a convenient work around Application code SQL, Result sets User interaction: web browser or integrated (e.g. Swing)

© HS-2010 08-PLSQLetc-20 © HS-2010 08-PLSQLetc-23

8.2 Application Architectures Client server example

class JdbcTest { • SQL is an interactive language, but... public static void main (String args []) throws SQLException { • Main usage: access database from application program // Load driver DriverManager.registerDriver (new oracle.jdbc.OracleDriver()); Means basically: SQL-statements statically // Connect to the local database Connection conn = known, but parameterized: DriverManager.getConnection ("jdbc:oracle:thin:@myhost:1521:orcl", SELECT name INTO :ctryName "hr", "hr"); FROM Country JOIN Economy ON... // Query the employee names WHERE gdp < :threshold Statement stmt = conn.createStatement (); ResultSet rset = stmt.executeQuery ("SELECT last_name FROM "Impedance mismatch": tuple sets vs records or objects employees"); // Print the name out • Typical database usage: while (rset.next ()) System.out.println (rset.getString (1)); independent applications concurrently access DB // Close the result set, statement, and the connection • Web based user interface is standard today rset.close(); stmt.close(); ⇒ Big differences of (application) system conn.close(); } architectures }

© HS-2010 08-PLSQLetc-21 © HS-2010 08-PLSQLetc-24

4 Persistence abstraction mechanisms Server side architectures

Object oriented programming model with persistence abstraction hides SQL database access

user interaction request handling in web server "Business Logic" Object Basically Generated stored by OR- DB access interface procedures mapping tool Table interface (JDBC)

request handling in DB server © HS-2010 08-PLSQLetc-25 © HS-2010 08-PLSQLetc-28

Server side application logic Pros and Cons

• Business logic in stored procedures Server based code: + performance Call procedure, + communication efficiency "Thin" Result sets + Database servers provide (most of) the functionality app. code DB-Server ... Application code "Thin" (Stored Procedures) Multi tier architecture app. code + scalability Thin clients + interoperability of autonomous systems + secure and reliable transport of request / reply messages • Stored procedures written in DB specific host language + Better workflow support e.g. PL/SQL, PL/pgSQL based on SQL/PSM standard • Programming language like C, C++, Java, But base technologies are basically the same in both architectures...

© HS-2010 08-PLSQLetc-26 © HS-2010 08-PLSQLetc-29

Multi tier architecture Base technologies

... to come: GUI client Web / Web DB client browser browser • Database script languages (like PL/pgSQL) also used for trigger programming Middleware layer Middle tier • Stored procedures using Java, C or alike Web Server Application • Embedding SQL into programming languages Server call level interface e.g. JDBC DB Application DB Application integration in PL e.g. Embedded SQL ESQL/C, java integration: SQLJ • Object relational mapping: hiding data access and persistence from application code. DB-Server DB-Server File System DB-Server

© HS-2010 08-PLSQLetc-27 © HS-2010 08-PLSQLetc-30

5 8.3 Stored procedures Usage

Server extension by user defined functions • Blocks used for direct excecution (e.g. SQL +) (only for testing and some administrative tasks)

SQL based: PL/SQL (Oracle), PL/pgSQL • Used within programs. e.g. C • adds control structures to SQL EXEC SQL EXECUTE • easy way to define complex functions on the DB < Block > Programming language based C, Java, ...,Perl, Python, Tcl for Postgres • Definition of independent functions / functions Any Programming language suitable in principle CREATE PROCDURE … (…) IS • For definition of triggers

• Inside object / type declarations Type definitions: see CREATE TYPE BODY below

© HS-2010 08-PLSQLetc-31 © HS-2010 08-PLSQLetc-34

SQL standards Declarations

DB-Script languages Standard declarations Based on SQL/PSM ("persistent stored modules") standard All variables have DECLARE to be declared, Only propriatary implementations: PL/SQL (Oracle), price NUMBER; all SQL types PL/pgSQL (Postgres), Transact-SQL (Micorsoft), SQL procedure prodName VARCHAR(20); allowed. language (IBM) But conceptually similar Use table types table Programming language based DECLARE SQL/OLB (object language binding) prodName Product.name%TYPE; SQL/JRT (SQL routines and types using the Java language) Use row type column SQL/CLI (SQL call level interface): How to call SQL from Programming language. DECLARE productTuple Product%ROWTYPE; This is a record type

© HS-2010 08-PLSQLetc-32 © HS-2010 08-PLSQLetc-35

DB script languages basics: Blocks Record types

Syntax Example PL/SQL syntax

[DECLARE DECLARE countryRec Country%ROWTYPE; /* Declarative section: variables, types, and local subprograms. */ ] BEGIN BEGIN SELECT * INTO countryRec FROM Country WHERE CODE='D'; /* Executable section: procedural and SQL statements go here. */ dbms_output.PUT_LINE('Name: ' || countryRec.name); /* This is the only section of the block that is required. */ END; [EXCEPTION Library function (Oracle) /* Exception handling section: error handling statements go here. */ ] END; • May be executed from the command line Block: Scope as in programming languages, • Works only with exactly one result row nesting allowed. • How to iterate over result sets?

© HS-2010 08-PLSQLetc-33 © HS-2010 08-PLSQLetc-36

6 PL/SQL Control flow Result set: example

CREATE TABLE TNumb DECLARE , internal object, not a variable (x NUMBER, y NUMBER); CURSOR ctry IS SELECT * FROM Country WHERE CODE LIKE 'D%'; DECLARE Only SQL/DML countryRec Country%ROWTYPE; i NUMBER := 1; has few operations: within block BEGIN BEGIN OPEN, CLOSE, FETCH OPEN ctry; LOOP LOOP INSERT INTO T1 VALUES(i,i+1); and attributes: i := i+1; FETCH ctry INTO countryRec; %NOTFOUND, % , EXIT WHEN i>100; EXIT WHEN ctry%NOTFOUND; OPEN % et al END LOOP; dbms_output.PUT_LINE ROWCOUNT END; ('Name: ' || countryRec.name || ', Popul: '|| countryRec.population); Similar : WHILE () LOOP ... END LOOP END LOOP; FOR IN .. LOOP...END LOOP CLOSE ctry; see Manual END;

© HS-2010 08-PLSQLetc-37 © HS-2010 08-PLSQLetc-40

PL/SQL Insertion in FOR loop Cursor (*)

CREATE TABLE TestNormal (empno number(10), ename varchar2(30), sal number(10)); Def: A cursor is an abstraction of a result set for a particular SQL statement with operations: OPEN, FETCH, CLOSE BEGIN and attributes %ROWCOUNT, %FOUND, %NOTFOUND FOR i in 1..1000000 Library function LOOP INSERT INTO Test_normal • Explicit cursors have to be defined for SQL statements VALUES (i, dbms_random.string('U',80), with more than one result record dbms_random.value(1000,7000)); • Implicit cursors are defined for every SQL statement IF mod(i, 10000) = 0 THEN COMMIT; END IF; Transaction commit: inserted BEGIN END LOOP; data stored in DB now. DELETE FROM TNUMB WHERE x > 50; END; All or nothing semantics. DBMS_OUTPUT.PUT_LINE('Deleted rows: ' || SQL%ROWCOUNT); END;

(*) Important concept for embedding SQL in host (programming) languages, typically more operations, see JDBC below © HS-2010 08-PLSQLetc-38 © HS-2010 08-PLSQLetc-41

Result sets Cursors and FOR loops DECLARE Problem: how to process result set of unkown cardinality? CURSOR ctry IS SELECT * FROM Country WHERE CODE LIKE 'C%'; DECLARE countryRec Country%ROWTYPE; row# int; BEGIN BEGIN LOOP is part of SELECT * INTO countryRec FROM Country WHERE CODE='D%'; FOR resRecord IN ctry LOOP FOR loop on result set of implicit dbms_output.PUT_LINE('Name: ' || countryRec.name); row# :=ctry%ROWCOUNT; END; dbms_output.PUT_LINE cursor. ('Name: ' || resRecord.name || ...does not work – more than one result record expected. ', Popul: '|| resRecord.population); END LOOP; dbms_output.PUT_LINE('Number of countries: ' || row#); Needed: a kind of pointer to result set records, which allows END; to iterate through the result set. • Implicit: open, close, record variable of result record. • Cursor closed at END LOOP, no attributes defined after that point.

© HS-2010 08-PLSQLetc-39 © HS-2010 08-PLSQLetc-42

7 Collection variables Functions in PL/SQL CREATE FUNCTION CountryCity(cname IN VARCHAR) DECLARE TABLE variables allow RETURNS int TYPE largeCtry IS RECORD ( for manipulation IS name country.name%TYPE, of sets within a block capital country.capital%TYPE); CURSOR ctry IS TYPE largeCtryTab IS TABLE OF largeCtry; SELECT * FROM Country WHERE CODE LIKE cname||'%'; row# int; lTab largeCtryTab; Bulk load from DB BEGIN i int; or individual FOR resRecord IN ctry LOOP BEGIN assignement SELECT name, capital BULK COLLECT INTO lTab row# :=ctry%ROWCOUNT; FROM country WHERE population >= 100000000; dbms_output.PUT_LINE ('Name: ' || resRecord.name || FOR i IN 1..lTab.LAST LOOP Set operations in DB ', Capital: '|| resRecord.capital); dbms_output.PUT_LINE usually preferrable END LOOP; ('Name: ' || lTab(i).name || ', capital: '|| RETURN (row#); lTab(i).capital); END; END LOOP; END;

© HS-2010 08-PLSQLetc-43 © HS-2010 08-PLSQLetc-46

8.4 Functions and procedures Calling functions / procedures

Recall... • Embedded in host language like C, Java similar to execution of plain SQL → below Browser • Big difference: no result set, but usage of INOUT, OUT Webserver: parameters and function values Database - interpret request • Inside PL/SQL block with business - call stored procedure logic as - return html stored procedures BEGIN dbms_output.Put_Line('Number of countries: ' || Needed: procedures and functions , not just TO_CHAR(CountryCity('G'))); anonymous blocks END; • Postgres: Server Programming interface (SPI) • Major syntactic (and some semantic) differences between PL/SQL and PL/pgSQL • e.g. no procedure in PL/pgSQL but FUNCTION RETURNS VOID

© HS-2010 08-PLSQLetc-44 © HS-2010 08-PLSQLetc-47

PL/SQL procedures Packages PL/SQL packages: CREATE PROCEDURE addtuple2 ( x IN T2.a%TYPE, define API and its implementation for related y IN T2.b%TYPE) AS No DECLARE (!) functions and procedures i NUMBER = dbms_random.value(1000,7000) CREATE PACKAGE MyMondial AS The API for -- here go declarations TYPE myCity City%ROWTYPE; this package BEGIN Cursor myC RETURNS myCity; INSERT INTO T2(k NUMBER,a, b) FUNCTION BigCites(countryName VARCHAR) RETURN NUMBER; VALUES(i, x, y); PROCEDURE NewCityInsert(newC myCity); END addtuple2; END MyMondial;

Parameter passing like in ADA: CREATE PACKAGE BODY MyMondial AS Implementation • call by value (IN), myVar NUMBER; -- local to package! • call by result (OUT), CURSOR myC AS SELECT * FROM City WHERE.. –-full def. • call by value-result ( ) FUNCTION BigCities(...)AS ... –- full definition INOUT PROCEDURE NewCityInsert(newC myCity) AS...; --full def. Why no call by reference?? BEGIN ... -- initializations END MyMondial © HS-2010 08-PLSQLetc-45 © HS-2010 08-PLSQLetc-48

8 PL/SQL: etc SQL based functions

Table result types Exception handling CREATE FUNCTION getfoo(integer) RETURNS SETOF movie AS $$ EXCEPTION SELECT * FROM movie WHEN [OR…] WHERE m_id = $1; THEN ; $$ LANGUAGE SQL; WHEN OTHERS placeholder for parameters THEN SELECT title, director FROM getfoo(93) AS m1; • Flexible concept comparable with Java exceptions. • Different semantics for special situations. (see manual) Alias for returned table value

© HS-2010 08-PLSQLetc-49 © HS-2010 08-PLSQLetc-52

Realistic PL/SQL (Oracle) example PL/pgSQL in a nutshell

-- very simple purchase transaction CREATE PROCEDURE Purchase() AS Example qty_on_hand NUMBER(5); CREATE OR REPLACE FUNCTION rand (hi integer,low int4) BEGIN SELECT quantity INTO qty_on_hand FROM inventory RETURNS integer AS WHERE product = 'TENNIS RACKET' -- $BODY$ FOR UPDATE OF quantity; -- no DECLARE Here go the variable IF qty_on_hand > 0 THEN -- check quantity BEGIN declarations UPDATE inventory SET quantity = quantity - 1 RETURN low + ceil((hi-low) * random()); WHERE product = 'TENNIS RACKET'; END; INSERT INTO purchase_record VALUES ('Tennis racket purchased', SYSDATE); $BODY$ ELSE LANGUAGE 'plpgsql' VOLATILE; Standard functions: INSERT INTO purchase_record returns VALUES ('Out of tennis rackets', SYSDATE); random() uniformly distributed END IF; $-quote, useful for COMMIT; values 0<= v <= 1.0 END; string literals Function may not return the same / value for same argument: hint for optimization

© HS-2010 08-PLSQLetc-50 © HS-2010 08-PLSQLetc-53

PL/pgSQL in a nutshell PL/pgSQL in a nutshell

CREATE OR REPLACE FUNCTION video.randtab(count integer, Example low integer, hi integer) CREATE FUNCTION foo (acc integer, amount numeric) RETURNS RETURNS integer AS numeric AS $BODY$ $B$ UPDATE bank SET balance = balance - amount DECLARE c INTEGER :=0; variable declarations WHERE accountno = acc; r INTEGER; SELECT balance FROM bank WHERE accountno = acc; BEGIN CREATE TABLE randomTable (numb integer, randVal $B$ LANGUAGE SQL; integer); FOR i IN 1..count LOOP side effects! $ quoting of PG INSERT INTO randomTable VALUES(i, rand(low,hi)); END LOOP; RETURN (SELECT MAX(numb) FROM randomTable); - Many SQL-statements in one call: performance gain END; - value returned: first row of last query result $BODY$ - Compound result type and table valued functions allowed LANGUAGE 'plpgsql' VOLATILE; ⇒ Table valued function in FROM clause

© HS-2010 08-PLSQLetc-51 © HS-2010 08-PLSQLetc-54

9 PL/pgSQL in a nutshell Anatomy of a trigger (Oracle)

Evaluation of functions CREATE OR REPLACE TRIGGER movie_DVD_Trigger INSTEAD OF INSERT ON T_M Within a select statement: FOR EACH ROW Semantics: trigger for each row affected SELECT randtab(100,0,9) DECLARE m_row NUMBER; (not only once per Without result value -- local variable BEGIN excecuted statement) PERFORM my_function(args) SELECT COUNT(*) INTO m_row EXECUTE query plan FROM Movie Action WHERE m_id = :NEW.mid; EXECUTE PROCEDURE emp_stamp(); (here: IF m_row = 0 PL/SQL) Note: Functions may have side effects! THEN RAISE_APPLICATION_ERROR(-20300, 'Movie does not exist'); ELSE INSERT INTO DVD (DVD_id, m_id) VALUES (:NEW.DVD_id, No (pretty) PRINT facilities :NEW.mid); workarounds: SELECT 'This is my heading' END IF; End; - put PLSQL-call into shell script - use Programming language for I/O CREATE view T_M AS SELECT m.m_Id AS mid, DVD_id, title ... © HS-2010 08-PLSQLetc-56 © HS-2010 08-PLSQLetc-59

8.5 Triggers Using an INSTEAD OF TRIGGER

Without the trigger: Insert into T_M (mid, DVD_id) VALUES(93,14); Triggers: Event – Condition – Action rules * Event: Update, insert, delete (basically) FEHLER in Zeile 1: Condition: < some conditon on table> ORA-01779: Kann keine Spalte, die einer Basistabelle zugeordnet WHEN wird, verändern Action: some operation ( expressed as DML, DB- Script language expression, C, Java,…) Using the INSTEAD OF TRIGGER

Insert into T_M (mid, DVD_id) VALUES(93,14) Triggers make data base systems pro-active 1 Zeile eingefügt compared to re-active (and interactive) Insert into T_M (mid, DVD_id) VALUES(99,14) * FEHLER in Zeile 1: ORA-20300: Movie does not exist ORA-06512: in "VIDEODB.MOVIE_DVD_TRIGGER", Zeile 8 ORA-04088: Fehler bei der Ausführung von Trigger 'VIDEODB.MOVIE_DVD_TRIGGER'

© HS-2010 08-PLSQLetc-57 © HS-2010 08-PLSQLetc-60

Triggers: simple example Triggers...

Basic Functionality ... are a powerful DB programming concept Allow complex integrity constraints CREATE TRIGGER myTrigger BEFORE [AFTER] event Used in most real-life database applications ON TABLE myTable FOR EACH ROW { | STATEMENT} Sometimes dangerous: EXECUTE PROCEDURE myFunction(myArgs); CREATE TRIGGER myTrigger1 BEFORE INSERT event: UPDATE, INSERT, DELETE ON TABLE myTable1 EXCECUTE myfct (...) Semantics -- inserts some record into myTable2

Execute the function after each event CREATE TRIGGER myTrigger2 once for each row changed or once per statement BEFORE INSERT e.g. per statement: write log-record ON TABLE myTable2 EXCECUTE myfct (...) per row: write new time-stamp -- inserts some record into myTable1 Cycle!

© HS-2010 08-PLSQLetc-58 © HS-2010 08-PLSQLetc-61

10 8.6 SQL3: Abstract data types Summary

"ADT is a data type defined by the operations allowed on its • Extensions of relational model popular values" • SQL 3 keeps extensions under control – somehow • Object-relational extensions more important than CREATE TYPE ( object oriented database systems • Extensions basically are: structured types and set types < declaration of more methods> ) functions, written in a db script language or supported only by a few DBS some programming language ADT equivalent to 'object type' (Oracle) active elements: triggers (SQL 3) , rules (only PGres) ... or functions may be defined stand-alone (PG)

© HS-2010 08-PLSQLetc-62 © HS-2010 08-PLSQLetc-70

Functions, methods, procedures

Method interface in an object type definition (Oracle flavor)

CREATE TYPE LineType AS OBJECT ( end1 PointType, end2 PointType, MEMBER FUNCTION length(scale IN NUMBER) RETURN NUMBER, PRAGMA RESTRICT_REFERENCES(length, WNDS)); CREATE TABLE Lines ( lineID INT, line LineType );

Predicates defined over functions SELECT lineID, k.length (1.0) FROM Lines k WHERE k.length(1.0) > 8.0

© HS-2010 08-PLSQLetc-63

Defining methods (Oracle) Implementation of a method signature* CREATE TYPE BODY LineType AS MEMBER FUNCTION length(scale NUMBER) RETURN NUMBER IS BEGIN RETURN scale * SQRT((SELF.end1.x- SELF.end2.x)*(SELF.end1.x-SELF.end2.x) + (SELF.end1.y-SELF.end2.y)*(SELF.end1.y- SELF.end2.y) ); END; END; Methods may be defined in Java or PL/SQL (Oracle) Functions: independent of types, no SELF attribute

*compare: java interface vs. class see: Ullman, J.: Object-Relational Features of Oracle http://www-db.stanford.edu/~ullman/fcdb/oracle/or-objects.html

© HS-2010 08-PLSQLetc-64

11