Professional Documents
Culture Documents
VARRAY
Limited collection of elements with a same type (with order)
Allow multiple nested recursions no index, can not be used in SQL (needs PL/SQL) In a type
CREATE TYPE vaddresses AS VARRAY(2) OF address
Nested table
Num. Nam. Addrese Drivers
Drivers Age
Accidents
Accident Report Photo
24
Paul
Paris
Paul Robert
45 17
Object Insurance
3
22/02/12
Nested table
General Syntax: CREATE [OR REPLACE] TYPE <object table schema> AS TABLE OF (object schema); / CREATE TABLE <table schema> (attribute attribute type, ...., attribute attribute type, nested item object table schema); NESTED TABLE nested item STORE AS storage table schema; CREATE TABLE <table schema> (attribute attribute type, ...., outer nested item object table schema); NESTED TABLE <outer nested item> STORE AS <outer storage table schema> (NESTED TABLE <inner nested item> STORE AS <inner storage table schema>);
22/02/12
Solution
Solution 1: use reference, place Team in Player(Player reference from Team) Create type Team Create type Player (ref from Team) Create TABLE OF Team Create TABLE OF Player
Statements solution
CREATE OR REPLACE TYPE Team AS OBJECT(Name varchar(20), City varchar(20)); CREATE OR REPLACE TYPE Player AS OBJECT (LastName varchar(20), FirstName varchar(20), Salary number, Birthdate DATE, TeamRef REF Team); CREATE TABLE Teams OF Team; CREATE TABLE Players OF Player;
8
INSERT INTO Teams VALUES('Bordeaux', 'France'); INSERT INTO Teams VALUES('PUF', 'HCM'); INSERT INTO Teams VALUES('HUI', 'HCM'); INSERT INTO Teams VALUES('BAK', 'HANOI'); INSERT INTO Players SELECT 'Huong','Nguyen',1000,'3-Apr-1990',ref(t) FROM Teams t WHERE Name='Bordeaux'; INSERT INTO Players SELECT 'Tuan','Nguyen',2000,'10-Apr-1980',ref(t) FROM Teams t WHERE Name='Bordeaux'; INSERT INTO Players SELECT 'Loc','Nguyen',1500,'10-Apr-1985',ref(t) FROM Teams t WHERE Name='Bordeaux';
INSERT INTO Players SELECT 'An','Nguyen',1300,'10-Apr-1989',ref(t) FROM Teams t WHERE Name='PUF'; INSERT INTO Players SELECT 'Lam','Le',1300,'10-Apr-1995',ref(t) FROM Teams t WHERE Name='PUF';
22/02/12
Solution
Solution 2: create nested table Create type Player Table of Player Create type Team (has field Player ref type Player) Create table Team (nested Player table)
10
Statements solution
CREATE TYPE Player2 AS OBJECT (LastName varchar(20), FirstName varchar(20), Salary number, Bithday DATE); CREATE TYPE Players2 AS TABLE OF Player2; CREATE TYPE Team2 AS OBJECT (Name varchar(20), City varchar(20), Player Players2); CREATE TABLE Teams2 OF Team2 NESTED TABLE Player STORE AS TabPlayer;
11
INSERT INTO Teams2 VALUES ('Bordeaux','France',Players2(Player2('Huong','Nguyen', 1000,'1-Jan-1990'),Player2('Tuan','Anh',2000,'2-Feb1975'),Player2('Ha','Le',1500,'5-Dec-1995'))); INSERT INTO Teams2 VALUES ('PUF','HCM',Players2(Player2('Ha','Nguyen',1000,'1Jan-1990'),Player2('Phong','Le',2000,'2-Feb1975'),Player2('Ha','Le',1500,'5-Dec-1995')));
22/02/12
Discuss question
How to implement many to many association relationship using object references?
13
Solution
CREATE OR REPLACE TYPE Person_T AS OBJECT (person_id VARCHAR2(10), person_name VARCHAR2(30)) / CREATE OR REPLACE TYPE Course_T AS OBJECT (course_id VARCHAR2(10), course_name VARCHAR2(30)) / CREATE TABLE Student OF Person_T (person_id NOT NULL, PRIMARY KEY (person_id));
CREATE TABLE Course OF Course_T (course_id NOT NULL, PRIMARY KEY (course_id)); CREATE TABLE Enrolls_in (student REF Person_T, course REF Course_T);
Inheritance implementation
15
22/02/12
Inheritance implementation
General Syntax: CREATE [OR REPLACE] TYPE <super-type object schema> AS OBJECT (key attribute attribute type, attribute attribute type,..., attribute attribute type) [FINAL|NOT FINAL] / CREATE [OR REPLACE] TYPE <sub-type object schema> UNDER <super-type object schema> (additional attribute attribute type, ...., additional attribute attribute type) [FINAL|NOT FINAL] / CREATE TABLE <super-type table schema> OF <super-type object schema> (key attribute NOT NULL, PRIMARY KEY (key attribute));
16
Example
CREATE OR REPLACE TYPE Person_T AS OBJECT (id VARCHAR2(10), name VARCHAR2(20), address VARCHAR2(35)) NOT FINAL / CREATE TABLE Person OF Person_T (id NOT NULL, PRIMARY KEY (id));/ CREATE OR REPLACE TYPE Student_T UNDER Person_T (course VARCHAR2(10), year VARCHAR2(4)) / CREATE TABLE Student OF Student_T (id NOT NULL, PRIMARY KEY (id)); CREATE OR REPLACE TYPE Staff_T UNDER Person_T (department VARCHAR2(10), room_no VARCHAR2(4)) / CREATE TABLE Staff OF Staff_T (id NOT NULL, PRIMARY KEY (id));
17
ORACLE PL/SQL
Data type Declare variables Control statements Exception Procedure Function Cursor Trigger
22/02/12
Define a new record type. type course_record_type is record (course_id Courses.cid%type; title Courses.title%type; credit_hours number); course_record course_record_type; %rowtype: use an existing tuple type. one_student Students%rowtype; Use . to reference record fields course_record.title = Database I;
Declare variables
<name_variable> <type> [[NOT NULL] := <value>]; (example : SOMENUMBER := 0;) Simple type : NUMBER CHAR VARCHAR BOOLEAN EXCEPTION TABLE.COL%TYPE
22/02/12
Control statements
IF condition THEN -- statements END IF; IF condition THEN --statements ELSE --statements END IF; IF condition THEN -- statements ELSIF condition THEN -- statements ELSIF condition THEN -- statements ELSE -- statements END IF;
LOOP
LOOP -- statements EXIT; -- statements END LOOP; LOOP -- statements EXIT WHEN condition; -- statements END LOOP;
WHILE condition LOOP --statements END LOOP; FOR i IN min .. Max LOOP --statements END LOOP; FOR i IN REVERSE max..Min LOOP --statements END LOOP;
22/02/12
end loop;
end; /
Declare cursor
To access query result one tuple at a time. CURSOR <cursor_name> IS <SQL request>; example : CURSOR c1 IS SELECT name FROM FRIENDS WHERE birthdate > 16/02/1976;
Working with a cursor:
open cursor_name; fetch cursor_name into record or variables; close cursor_name;
22/02/12
An Example of Cursors
declare cursor c1 is select cid, cname, city from customers; c1_rec c1%rowtype; begin open c1; fetch c1 into c1_rec; dbms_output.put_line(c1_rec.cid || , || c1_rec.cname || , || c1_rec.city); close c1; end; /
Cursor Attributes
begin if (not c1%isopen) then /* attribute */ open c1; end if; fetch c1 into c1_rec; while c1%found loop /* attribute */ dbms_output.put_line(c1_rec.cid || , || c1_rec.cname || , || c1_rec.city); fetch c1 into c1_rec; end loop; close c1; end; /
10
22/02/12
Parameterized Cursor
Declaration: cursor c1(d_name in Students.dept_name%type) is select age, avg(GPA) from Students where dept_name = d_name group by age; Usage: open c1(Computer Science);
11
22/02/12
Exceptions
An exception is any error that occurs during program execution. EXCEPTION /* exception section */ WHEN dup_val_on_index THEN dbms_output.put_line(sqlcode ||-- || sqlerrm); end; Output message if the exception occurs: -1--ORA-00001: unique constraint violated
Exceptions
Syntax: EXCEPTION WHEN exception_name THEN error-handling-code; WHEN others THEN error-handling-code; Pre-defined exceptions: invalid_cursor, too_many_rows, dup_val_on_index, no_data_found, etc.
User-Defined Exceptions
declare invalid_gpa exception; begin /* execution section */ if (gpa < 0 or gpa > 4.0) then /* Must be raised by user */ raise invalid_gpa; end if;
12
22/02/12
Errors managements
Raise exception : RAISE <exception>; Manage exception (in a block): WHEN <exception> THEN <statement>;
Errors managements
BEGIN FOR R1 IN C1 LOOP BEGIN IF THEN RAISE nb_neg; END IF; EXCEPTION WHEN nb_neg THEN <statement>; END; END LOOP; END;
13
22/02/12
Encapsulation- Example
CREATE OR REPLACE PROCEDURE Delete_Student( delete_id Student.id%TYPE) AS BEGIN DELETE FROM Student WHERE id = delete_id; END Delete_Student; / GRANT EXECUTE ON Delete_Student TO Principal;
41
Procedure
Assume a Customer relation. Write a PL/SQL program to retrieve a given customer. Report an exception if not found. set serveroutput on declare v_cid customers.cid%type; v_cname customers.cname%type; v_city customers.city%type; status boolean;
14
22/02/12
Procedure (cont.)
procedure get_customer( cust_id in customers.cid%type, cust_name out customers.cname%type, cust_city out customers.city%type, status out boolean) is begin select cname, city into cust_name, cust_city from customers where cid = cust_id; status := true; exception /* optional */ when no_data_found then status := false; end; /* procedure */
begin /* main block */ v_cid := c001; get_customer(v_cid, v_cname, v_city, status); if (status) then dbms_output.put_line(v_cid || || v_cname || || v_city); else dbms_output.put_line(Customer || v_cid || not found); end if; end; /* main block */ /
Procedure (cont.)
Functions
CREATE [OR REPLACE] FUNCTION <function name> [parameter [{IN}] parameter type, ...., parameter [{IN}] parameter type)] RETURN datatype IS [local variables] BEGIN <function body>; RETURN value; END <function name>;
45
15
22/02/12
Functions example 1
CREATE OR REPLACE FUNCTION Student_Course( s_id Student.id%TYPE) RETURN VARCHAR2 IS v_course VARCHAR(10); BEGIN SELECT course INTO v_course FROM Student WHERE id = s_id; RETURN v_course; END; /
46
Function example 2
Use a function to find the number of customers in a given city. set serveroutput on declare v_city customers.city%type; customer_no number;
16
22/02/12
Trigger
A trigger is an event-condition-action rule coded in PL/SQL and is useful for enforcing various integrity constraints and business rules. An event is an update operation: insertion, deletion or update. The action can be a set of additional update operations or other PL/SQL statements. A trigger fires (executes the action) at a time before or after an event occurs and additional condition are satisfied.
17
22/02/12
A Sample Trigger
create or replace trigger raise_sal before update of salary on employees for each row when (new.salary > old.salary * 1.2) begin dbms_output.put_line(Old salary is || :old.salary || , || New salary is || :new.salary); dbms_output.put_line(The raise is too high!); end;
Row Trigger
Row Trigger
Fire once for each row that is affected by the event and satisfies the additional condition in the when clause. Must specify for each row.
Trigger Applications
Add a log entry each time the price of a product is changed.
The log table:
18
22/02/12
create or replace trigger update_p_price after update of price on products for each row begin insert into products_log values (:old.pid, user, sysdate, :old.price, :new.price); end;
Practice
- Create a trigger that ensures that a student can not skip class or switch to a lower class. - Create a trigger that keeps the old students. For this we will get to create a new table containing also the year of release for each student.
19
22/02/12
User-Defined Queries
User-Defined Queries in Inheritance Hierarchies
Subclass Query Superclass Query
MEMBER PROCEDURE/FUNCTION
Member procedures and member functions are physically implemented as PL or SQL procedures or functions, and they are defined together within the specification of the object type.
20
22/02/12
CREATE [OR REPLACE] TYPE BODY (object schema) AS MEMBER PROCEDURE <member procedure name> [parameter [{IN | OUT | IN OUT}] parameter type, ...., parameter [{IN | OUT | IN OUT}] parameter type)] IS [local variables] BEGIN <procedure body>; END <member procedure name>;
MEMBER FUNCTION <function name> [parameter [{IN}] parameter type, ...., parameter [{IN}] parameter type)] RETURN datatype IS [local variables] BEGIN <procedure body>; END <member function name>;
Example
Write FullName function of type Student, returning full name Write Description function of type Internship, returning text about company and subject of this internship
CREATE OR REPLACE TYPE StudentType AS OBJECT (Stu_code number,FirstName varchar(20), LastName varchar(20), MEMBER FUNCTION FullName RETURN varchar2); CREATE OR REPLACE TYPE IntershipType AS OBJECT (Company varchar(20), Subject varchar(20), StudentField REF StudentType, MEMBER FUNCTION Description RETURN varchar2 ); /
21
22/02/12
Practice
Write Age function of type Player, return his age. Write SumSalaries function of type Team, return sum of salaries of this team.
66
22
22/02/12
23
22/02/12
70
72
24
22/02/12
Superclass-query example
Find the details of the author(s) whose institution name is Monash University. SELECT a.name, a.address FROM Author a, Academic b WHERE a.ao_id = b.ao_id AND b.i_name = Monash University;
74
75
25
22/02/12
76
77
78
26
22/02/12
Referencing-query representation
SELECT <referencing class attributes> FROM <referencing table> WHERE <referencing table path expression> [AND <class table.attr = &input_class_selection_predicates>] where: The referencing table or class is the one that holds the many side in an association relationship.
79
Dereferencing Query
80
Dereferencing-query representation
SELECT <class table attributes> FROM <referring table>, <referred table> WHERE <referencing join> [AND <class table.attr = &input_class_selection_predicates>] where: The referencing join takes the form of <referring class attribute = REF(referred class)>
81
27
22/02/12
Dereferencing-query representation
In the relationship between Course Manual and Author, display all course manuals written by John Smith. SELECT a.title FROM Course_Manual a, Author b, Publish c WHERE c.course_manual = REF(a) AND c.author = REF(b) AND b.name = John Smith;
82
Part Query
A part query is an aggregation-hierarchy query used to retrieve information of part classes, where the selection predicates are originated at the whole class.
83
28
22/02/12
Whole Query
A whole query is the aggregation-hierarchy query to retrieve information from the whole class, where the selection predicates are originated at the part class.
29
22/02/12
30
22/02/12
Practice
- Create a trigger that ensures that a student can not skip class or switch to a lower class. - Create a trigger that keeps the old students. For this we will get to create a new table containing also the year of release for each student.
31