1 Moving a Java Program into a Database. 2 Motivation We would like to be able to run a Java program from within Oracle. This will allow us to: –call.

Slides:



Advertisements
Similar presentations
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 Query-by-Example (QBE). 2 v A “GUI” for expressing queries. –Based on the Domain Relational Calulus (DRC)! –Actually invented before GUIs. –Very convenient.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
SQL Constraints and Triggers
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
1 Views and Null values. 2 What does this return? SELECT B.bid, COUNT(*) FROM Boats B, Reserves R WHERE R.bid=B.bid and B.color=‘red’ GROUP BY B.bid For.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
1 Views. 2 Views A view is a "virtual table" defined using a query You can use a view as if it were a table, even though it doesn't contain data The view.
1 Triggers. 2 What is a Trigger? A trigger is a PL/SQL block that is automatically called when certain events occur in the database. Triggers can be made.
1 Table Alteration. 2 Altering Tables Table definition can be altered after its creation –Adding columns –Changing columns’ definition –Dropping columns.
1 Views. 2 What are views good for?(1) Simplifying complex queries: we saw one example. Here is another example that allows the user to "pretend" that.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
View Sen Zhang. Views are very common in business systems users view of data is simplified a form of security - user sees only the data he/she needs to.
1 Views. 2 What are views good for? (1) Simplifying complex queries: We saw one example. Here is another that allows the user to "pretend" that there.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5 (cont.)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
The Relational Model These slides are based on the slides of your text book.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Advanced SQL: Triggers & Assertions
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
The Oracle Database System. Connecting to the Database At the command line prompt, write: sqlplus In the beginning your password.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: Structured Query Language Chapter 5 (cont.)  Constraints  Triggers.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
1 Triggers. 2 PL/SQL reminder We presented PL/SQL- a Procedural extension to the SQL language. We reviewed the structure of an anonymous PL/SQL block:
Spring 2011 ITCS3160: Database Design and Implementation Hands-on Learning.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Programming in postgreSQL with PL/pgSQL ProceduralLanguageextension topostgreSQL 1.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
7.5 Using Stored-Procedure and Triggers NAME MATRIC NUM GROUP Muhammad Azwan Bin Khairul Anwar CS2305A Muhammad Faiz Bin Badrol Shah CS2305B.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
CS122A: Introduction to Data Management Lecture #11: SQL (4) -- Triggers, Views, Access Control Instructor: Chen Li.
Web Technologies IT230 Dr Mohamed Habib.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
PL/SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Relational Algebra Chapter 4, Part A
ISC440: Web Programming 2 Server-side Scripting PHP 3
Advanced SQL: Views & Triggers
Instructor: Mohamed Eltabakh
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
CENG 351 File Structures and Data Managemnet
Presentation transcript:

1 Moving a Java Program into a Database

2 Motivation We would like to be able to run a Java program from within Oracle. This will allow us to: –call a Java function inside a query –call a Java function inside a trigger (triggers will be explained later on…)

3 Basic Procedure 1.Create a Java program. Make the function that we want to use static. 2.Load the Java program into the database. 3.Create a PL/SQL procedure that will wrap the Java function 4.Call the PL/SQL procedure. We learn each step in detail…

4 Hello World - Java CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Hello" AS public class Hello { public static void sayHello(String name) { System.out.println("Hello World, " + name); } public static String returnHello(String name) { return "Hello World, " + name; } }; /

5 Hello World – PL/SQL (1) CREATE OR REPLACE PROCEDURE say_hello(name VARCHAR2) as LANGUAGE JAVA NAME 'Hello.sayHello(java.lang.String)'; / Note: Errors in creating a procedure can be seen with the command: show errors

6 Calling say_hello SET SERVEROUTPUT ON SIZE CALL DBMS_JAVA.SET_OUTPUT ( ); CALL say_hello( ‘ Sara ’ ); We will see: “Hello World, Sara” on the screen.

7 Hello World – PL/SQL CREATE OR REPLACE FUNCTION return_hello(name VARCHAR2) return VARCHAR2 as LANGUAGE JAVA NAME 'Hello.returnHello(java.lang.String) return java.lang.String'; /

8 Calling return_hello SELECT return_hello( ‘ Sara ’ ) FROM DUAL; We will see: RETURNHELLO('SARA') Hello World, Sara

9 Notes Java programs can also be loaded using the utility loadjava. Details will be given when necessary. It may be necessary sometimes to recompile a procedure or a java source. This can be done by: –alter procedure say_hello compile; –alter java source “Hello” compile; Note the need to use quotes around Java source names, since Java is case sensitive.

10 Moving Program Logic into the Database: Triggers

11 What is a Trigger? A trigger is a procedure that is automatically called when certain events occur in the database. Triggers can be made to run when rows are inserted, deleted or updated. Triggers can be run before or after the action. Triggers can be run once per statement or once per row affected.

12 General Form of a Trigger CREATE [or REPLACE] TRIGGER trig_name {BEFORE | AFTER | INSTEAD OF} {DELETE | INSERT | UPDATE [of column [, column]...] } [or {DELETE | INSERT | UPDATE [of column [, column]...] }...] on {table_name | view_name} FOR EACH {ROW | STATEMENT} [WHEN (condition)] PL/SQL block

13 Backing Up Data CREATE TRIGGER backup_sailors_trig AFTER UPDATE of Rating on Sailors FOR EACH ROW WHEN (old.rating < new.rating) BEGIN INSERT INTO Sailors_Backup VALUES (:old.sid, :old.sname, :old:age, :old:rating); END / Why AFTER?

14 Ensuring Upper Case CREATE TRIGGER upper_case_program_type_trig BEFORE INSERT or UPDATE on Program FOR EACH ROW BEGIN :new.type := UPPER(:new.type); END / Why BEFORE?

15 Calling a Procedure CREATE TRIGGER notify_ta_trig AFTER INSERT or UPDATE or DELETE on Db_Grades FOR EACH STATEMENT WHEN(User <> ‘ db ’ ) BEGIN call send (User || ‘ trying to access table Db_Grades ’ ); END /

16 Views and Triggers

17 Changing a Table through a View (1) If a view is based on a single table you can insert, update and delete rows from the table through the view under the following conditions: –You can’t insert if the underlying table has non null columns not appearing in the view –You can’t insert or update if any of the view columns referenced in the command contains functions or calculations

18 Changing a Table through a View (2) –You can’t insert, update or delete if the view contains group by or distinct.

19 Inserting Allowed CREATE VIEW OldSailors as SELECT * FROM Sailors WHERE age > 50; INSERT INTO OldSailors(sid,sname,age,rating) VALUES(12, ‘ Joe ’,51,10); INSERT INTO OldSailors(sid,sname,age,rating) VALUES(12, ‘ Mary ’,49,10);

20 Inserting Not Allowed CREATE VIEW SailorsInfo as SELECT sname, rating FROM Sailors WHERE age>50; INSERT INTO SailorsInfo VALUES( ‘ Joe ’,10); Illegal!

21 Updating Allowed CREATE VIEW SailorsInfo as SELECT sname, rating FROM Sailors WHERE age>50; UPDATE SailorsInfo SET rating = 6 WHERE sname = ‘ Joe ’ ; UPDATE Sailors SET rating = 6 WHERE sname = ‘ Joe ’ and age>50;

22 Updating Not Allowed CREATE VIEW SailorsInfo2 as SELECT sname, rating + age as ra FROM Sailors WHERE age>50; UPDATE SailorsInfo2 SET ra = 7 WHERE sname = ‘ Joe ’ ; Illegal!

23 Deleting Allowed CREATE VIEW SailorsInfo2 as SELECT sname, rating + age as ra FROM Sailors WHERE age>50; DELETE FROM SailorsInfo2 WHERE sname = ‘ Joe ’ and ra = 56; DELETE FROM Sailors WHERE sname = ‘ Joe ’ and (rating + age) >50 and age > 50;

24 Some More Examples: What will these commands do? UPDATE OldSailors SET rating = 10; DELETE FROM OldSailors; UPDATE OldSailors SET age = age +1 WHERE age <= 50;

25 Using a Views with Triggers Consider the relation: All_Favorites(url, login) This table stores the favorite web sites of CS students. Two views have been created over this table: User_Favorites: The favorites of current user Anon_Favorites: All the favorites, but without stud_id, to preserve anonymity.

26 The Views CREATE View User_Favorites as SELECT url FROM All_Favorites WHERE login = user; CREATE View Anon_Favorites as SELECT url FROM All_Favorites;

27 Inserting New URLs The table All_Favorites can not be inserted into directly by a student because the privileges are lacking The views can’t be inserted through, since login is a non null column CREATE TRIGGER insert_url INSTEAD of INSERT on User_Favorites for each row begin insert into All_Favorites(url, login) values(User,:new.url); end;

28 Using Triggers to Manipulate Complex Views Oracle can sometimes insert through a view based on several tables. Oracle can’t insert into multiple tables through a view. CREATE VIEW SailorsReserves as SELECT S.sid, sname, rating, age, bid, date FROM Sailors S, Reserves R WHERE S.sid = R.sid;

29 Inserting into a Complex View: Won’t Work This won’t work, since we are attempting to insert into 2 tables. In addition, we are not specifying a value for the sid field of Reserves, which is part of the key! INSERT INTO SailorsReserves(sid,sname,rating,age,bid,date) VALUES (1, ’ Joe ’, 10, 50, 103, ’ ’ );

30 Using a Trigger to Insert CREATE TRIGGER insert_sailors_reserves_trig INSTEAD of INSERT on SailorsReserves for each row begin insert into Sailors(sid, sname, rating, age) values(:new.sid, :new.sname, :new.rating, :new.age); insert into Reserves(sid, bid, date) values(:new.sid, :new.bid, :new.date); end;

31 Materialized Views

32 What and Why? What: A materialized view is a view that actually exists as a table Why: This can be more efficient than re- computing the view’s query each time it is accessed Problem: How is the materialized view kept up to date when the underlying tables are changed? Note: We will just see the ideas here and not the syntax

33 Simple vs. Complex A Simple Materialized View: 1.Selects rows from only 1 table 2.Does not perform set operations, joins or group bys Otherwise, it is a Complex Materialized View.

34 Options for Materialized Views Refresh mode, one of –Fast (only possible for Simple Materialized Views): add minimal changes –Complete: Recompute the query –Force: Fast, if possible; otherwise complete When is refresh performed? –On demand: When refresh is specifically asked for –On commit: When underlying table has changed

35 Fast Refresh Consider a materialized view defined by the query: SELECT sname, age FROM Sailors WHERE rating 50;

36 Fast Refresh INSERT INTO Sailors(sid,sname,rating,age) VALUES (12, ’ Joe ’,8,52); How would the view be updated when the following commands are performed? DELETE FROM Sailors WHERE age < 54; UPDATE Sailors SET age = age - 1 WHERE rating > 10;

37 More Options Query Rewrite: Can a materialized view be used instead of a table in a query? Suppose we had the view defined before, and we wanted to compute the query: SELECT age FROM Sailors WHERE age > 51 and age < 85;