Chapter 10 Oracle9i Developer: PL/SQL Programming

Slides:



Advertisements
Similar presentations
Oracle Object-Relational Model. - Structures : tables, views, indexes, etc. - Operations : actions that manipulate data stored in structures - Integrity.
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.
Chapter 8 Embedded SQL.
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 11 Introduction to Dynamic SQL and Object Technology.
1 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Introduction to Object-Relational features of Oracle 9i.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
Advanced SQL: Cursors & Stored Procedures
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Database Technology Jing Shen.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Dynamic SQL Oracle Database PL/SQL 10g Programming Chapter 13.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 8 Program Unit Dependencies.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 10 Oracle-Supplied Packages, Dynamic SQL, and Hiding Source Code.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
Fundamentals of DBMS Notes-1.
A Guide to SQL, Seventh Edition
Chapter 8 Dependencies, Privileges and Compilation Oracle11g:
Interacting with the Oracle Server
Chapter 10 Oracle11g: PL/SQL Programming Oracle-Supplied Packages,
Interacting with the Oracle Server
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing.
All Powder Board and Ski
Introduction to Oracle9i: SQL
Chapter 7 Program Unit Dependencies Oracle9i Developer:
Database Management Systems 2
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Oracle Data Definition Language (DDL)
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Prof. Arfaoui. COM390 Chapter 9
Prof. Arfaoui. COM390 Chapter 6
Handling Data in PL/SQL Blocks
The University of Akron College of Applied Science & Technology Dept
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

Chapter 10 Oracle9i Developer: PL/SQL Programming Introduction to Dynamic SQL and Object Technology

Objectives Create dynamic SQL Use object technology

Brewbean’s Challenge Add flexibility to queries such as allowing shoppers to determine if they want to search product names or descriptions Explore potential capabilities of object technology features for providing greater data consistency and control

Dynamic SQL Allows identifiers such as column and table names to be provided at runtime Two mechanisms available: DBMS_SQL package (Oracle7) Native dynamic SQL (Oracle8)

DBMS_SQL – DML Example CREATE OR REPLACE PROCEDURE dyn_dml_sp (p_col VARCHAR2, p_price NUMBER, p_id NUMBER) IS lv_cursor INTEGER; lv_update VARCHAR2(150); lv_rows NUMBER(1); BEGIN --Open Cursor lv_cursor := DBMS_SQL.OPEN_CURSOR;

DBMS_SQL (continued) --Create DML statement lv_update := 'UPDATE bb_product SET ' || p_col || ' = :ph_price WHERE idProduct = :ph_id'; --Parse the statement DBMS_SQL.PARSE(lv_cursor, lv_update, DBMS_SQL.NATIVE); --Associate parameters with placeholders in the statement DBMS_SQL.BIND_VARIABLE(lv_cursor, ':ph_price', p_price); DBMS_SQL.BIND_VARIABLE(lv_cursor, ':ph_id', p_id);

DBMS_SQL (continued) --Run the DML statement lv_rows := DBMS_SQL.EXECUTE(lv_cursor); --Close the cursor DBMS_SQL.CLOSE_CURSOR(lv_cursor); --Save changes COMMIT; --Check how many rows affected DBMS_OUTPUT.PUT_LINE(lv_rows); END;

DBMS_SQL

DBMS_SQL – Query Example CREATE OR REPLACE PROCEDURE dyn_query1_sp (p_col IN VARCHAR2, p_value IN VARCHAR2) IS lv_query LONG; lv_status INTEGER; lv_cursor INTEGER; lv_col1 NUMBER(2); lv_col2 VARCHAR2(25); lv_col3 NUMBER(6,2); lv_col4 NUMBER(5,1);

DBMS_SQL (continued) BEGIN --Open the cursor lv_cursor := DBMS_SQL.OPEN_CURSOR; --Build the query lv_query := 'SELECT idProduct, productname, price, stock FROM bb_product WHERE '|| UPPER(p_col) ||' = ' || 'UPPER(:ph_value)'; --Parse the statement DBMS_SQL.PARSE(lv_cursor, lv_query, DBMS_SQL.NATIVE); --Identify data types for each item selected DBMS_SQL.DEFINE_COLUMN(lv_cursor, 1, lv_col1); DBMS_SQL.DEFINE_COLUMN(lv_cursor, 2, lv_col2, 25); DBMS_SQL.DEFINE_COLUMN(lv_cursor, 3, lv_col3); DBMS_SQL.DEFINE_COLUMN(lv_cursor, 4, lv_col4);

DBMS_SQL (continued) --Associate placeholder with a parameter DBMS_SQL.BIND_VARIABLE(lv_cursor, ':ph_value', p_value); --Execute the query lv_status := DBMS_SQL.EXECUTE(lv_cursor); --Fetch row returned and place into PL/SQL variables IF (DBMS_SQL.FETCH_ROWS(lv_cursor) > 0) THEN DBMS_SQL.COLUMN_VALUE(lv_cursor, 1, lv_col1); DBMS_SQL.COLUMN_VALUE(lv_cursor, 2, lv_col2); DBMS_SQL.COLUMN_VALUE(lv_cursor, 3, lv_col3); DBMS_SQL.COLUMN_VALUE(lv_cursor, 4, lv_col4); DBMS_OUTPUT.PUT_LINE(lv_col1||' '||lv_col2||' '||lv_col3||' '||lv_col4); END IF; --Close cursor DBMS_SQL.CLOSE_CURSOR(lv_cursor); END;

DBMS_SQL

Native Dynamic SQL Simpler coding More efficient processing Limited capabilities compared to DBMS_SQL package Two methods: EXECUTE IMMEDIATE OPEN FOR

Native Dynamic SQL Example CREATE OR REPLACE PROCEDURE dyn_query3_sp (p_col IN VARCHAR2, p_value IN VARCHAR2) IS lv_query VARCHAR2(200); lv_id bb_product.idProduct%TYPE; lv_name bb_product.productname%TYPE; lv_price bb_product.price%TYPE; lv_stock bb_product.stock%TYPE; BEGIN --use a variable to hold the query construction to -- make it more readable

Native Dynamic SQL (continued) lv_query := 'SELECT idProduct, productname, price, stock FROM bb_product WHERE UPPER(' || p_col || ') = UPPER(:ph_value)'; --Run the dynamic query supplying variables to hold the -- return values in the INTO clause and associate the -- parameter to the placeholder with the USING clause EXECUTE IMMEDIATE lv_query INTO lv_id, lv_name, lv_price, lv_stock USING p_value; DBMS_OUTPUT.PUT_LINE(lv_id||' '||lv_name||' ' ||lv_price||' '||lv_stock); END;

Native Dynamic SQL

DBMS_SQL vs. Native Dynamic SQL Use native dynamic SQL when: The number and types of columns to be used is known The number and type of bind variables is known To perform DDL Executing the statement only once or twice User-defined types such as object and collections are used (not supported by DBMS_SQL) Fetching rows of data into PL/SQL records (not supported by DBMS_SQL) SQL statement is less than 32KB in size

Object Technology Object Types Object Methods Object Relations Object Views

Object types Represent an entity such as an address or a person Defined with attributes and methods Can be used as a data type for a table column Can contain multiple data elements or attributes

Create an Object Type CREATE OR REPLACE TYPE addr_ot AS OBJECT (street1 VARCHAR2(25), street2 VARCHAR2(25), city VARCHAR2(25), state CHAR(2), zip NUMBER(9) );

Use Object Type CREATE TABLE bb_order (ord_id NUMBER(4), cust_id NUMBER(4), ord_date DATE, total NUMBER(6,2), bill_addr addr_ot, ship_addr addr_ot );

Object Type Constructor DECLARE lv_bill addr_ot; lv_ship addr_ot; BEGIN lv_bill := addr_ot('11 Bush Dr' ,NULL, 'Savannah', 'GA',346668229); lv_ship := addr_ot('812 Scott Lane','Apt #52','Savannah','GA',346668227); INSERT INTO bb_order VALUES (102,31,'11-NOV-03’ ,34.50,lv_bill,lv_ship); END;

DESCRIBE command

Methods Add program units to object types Referred to as members Similar to package creation Add function: ALTER TYPE addr_ot ADD MEMBER FUNCTION lbl_print RETURN VARCHAR2;

Object Type Body

Object Relations Create customer object type: CREATE TYPE cust_ot AS OBJECT (cust_id NUMBER(4), first VARCHAR2(15), last VARCHAR2(20), email VARCHAR2(25), phone NUMBER(10) ); Create customer table: CREATE TABLE bb_cust OF cust_ot (PRIMARY KEY (cust_id));

Object Relations (continued) Create orders object type: CREATE TYPE ord_ot AS OBJECT (ord_id NUMBER(4), cust_ref REF cust_ot, ord_date DATE, total NUMBER(6,2)); CREATE TABLE bb_ord OF ord_ot (PRIMARY KEY (ord_id));

Object Relations (continued) Use REF variable to establish relation: INSERT INTO bb_cust VALUES (cust_ot(12,'Joe','Cool','jcool@yahoo.com', 7773335555)); INSERT INTO bb_ord SELECT ord_ot(1,REF(c),SYSDATE,24.50) FROM bb_cust c WHERE cust_id = 12; COMMIT;

REF pointers Does not prevent broken relations like a foreign key constraint

Object Views Provide a layer on top of traditional database designs to enable object features OBJECT OID identifies unique view row identifier CAST and MULTISET handle subquery mapping to view elements

Object View Example CREATE TYPE bask_ot AS OBJECT (idBasket NUMBER(5), total NUMBER(7,2)) / CREATE TYPE bask_tab AS TABLE OF bask_ot CREATE TYPE shop_ot AS OBJECT (idShopper NUMBER(4), last_name VARCHAR2(20), city VARCHAR2(20), idBasket bask_tab)

Object View Example CREATE VIEW shop_vu OF shop_ot WITH OBJECT OID(idShopper) AS SELECT s.idShopper, s.lastname, s.city, CAST(MULTISET(SELECT idBasket, total FROM bb_basket b WHERE b.idShopper = s.idShopper) AS bask_tab) FROM bb_shopper s;

Summary Dynamic SQL allows identifiers and DDL statements to process within PL/SQL DBMS_SQL package and native dynamic SQL are two mechanisms providing dynamic SQL capabilities Native dynamic SQL is simpler to code and executes more efficiently Object types represent an entity which can contain multiple data attributes and program units

Summary Program units in object types are called methods Relationships between object rows are established using REF variables Object views provide a mechanism to use object technology features with a traditional database design