8-1 Copyright  Oracle Corporation, 1998. All rights reserved. Objects in Oracle8 1.Object Types - object-relational database 2.Large Objects (LOB) types.

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

Data Definition Language (DDL)
Oracle PL/SQL IV Exceptions Packages.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Oracle Objects Object Oriented Database Approach.
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
Objectives Why PL-SQL ? Language features
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most.
KarczewskiDatenbanken 21 Object-Relational SQL Introduction: Object-Relational SQL (ORSQL) capabilities Objects and Tables OR-Create, -Insert, -Update,
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
1 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Introduction to Object-Relational features of Oracle 9i.
6 Copyright © 2009, Oracle. All rights reserved. Working with Composite Data Types.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
Oracle9i Database Administrator: Implementation and Administration 1 Chapter 7 Basic Table Management.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Oracle 8i PL/SQL Collections. Collections A Collection is a group of elements of the same kind There are three types of Collections that you can use in.
PL/SQL Declaring Variables PL/SQL Block Structure DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) - SQL statements -
Database Application Development using PL/SQL Programming.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Week 4 Lecture 2 Advanced Table Management. Learning Objectives  Create tables with large object (LOB) columns and tables that are index-organized 
Chapter 2 Object-Relational DBMSs Chapter 28 in Textbook.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
9 Copyright © 2004, Oracle. All rights reserved. Manipulating Large Objects.
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
Declaring PL/SQL Variables
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 Object-Relational Database.
Copyright  Oracle Corporation, All rights reserved. 20 Working with Composite Datatypes.
1 Part IV: Object-Relational Databases  Ch. 30: Types, Object Views, Methods  Ch. 31: Nested Tables and Varying Arrays  Ch. 32: Large Objects  Ch.
6 Copyright © 2004, Oracle. All rights reserved. Working with Composite Data Types.
8 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
8 Copyright © 2007, Oracle. All rights reserved. Implementing SecureFile LOBs.
11 Copyright © 2004, Oracle. All rights reserved. Managing XML Data in an Oracle 10g Database.
Database An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
Preface IIntroduction Course Objectives I-2 Oracle Complete Solution I-3 Course Agenda I-4 Tables Used in This Course I-5 The Order Entry Schema I-6 The.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
Introduction to Oracle. Before Computerized Database Organization use a set of data files to store each individual data. – The file contains individual.
Introduction To Oracle
Object-Relational DBMSs
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
PL/SQL Package Week 8.
DATABASE MANAGEMENT SYSTEM
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Object-Relational Features
PL/SQL Declaring Variables.
Large Object Datatypes
Prof. Arfaoui. COM390 Chapter 7
Database Programming Using Oracle 11g
Presentation transcript:

8-1 Copyright  Oracle Corporation, All rights reserved. Objects in Oracle8 1.Object Types - object-relational database 2.Large Objects (LOB) types 1.Object Types - object-relational database 2.Large Objects (LOB) types Wyk ł ad 8

8-2 Copyright  Oracle Corporation, All rights reserved. An Object Type Is a user-defined composite datatype Encapsulates a data structure along with the methods needed to manipulate it Is a user-defined composite datatype Encapsulates a data structure along with the methods needed to manipulate it Attribute Method order_nocust_infoline_itemsamount Hold CheckStatus Cancel Ship

8-3 Copyright  Oracle Corporation, All rights reserved. The Structure of an Object Type Method bodies Publicinterface Private implementation Attribute declarations Method specifications Specification Body

8-4 Copyright  Oracle Corporation, All rights reserved. CREATE TYPE type_name AS OBJECT [(attribute1 datatype, attribute2 datatype,...] [MEMBER procedure1 | function1 spec, procedure2 | function2 spec,...)] CREATE TYPE type_name AS OBJECT [(attribute1 datatype, attribute2 datatype,...] [MEMBER procedure1 | function1 spec, procedure2 | function2 spec,...)] SyntaxSyntax Creating an Object Type Specification

8-5 Copyright  Oracle Corporation, All rights reserved. SyntaxSyntax Creating an Object Type Body CREATE TYPE BODY type_name AS [MEMBER procedure1 | function1 body, procedure2 | function2 body,...] CREATE TYPE BODY type_name AS [MEMBER procedure1 | function1 body, procedure2 | function2 body,...]

8-6 Copyright  Oracle Corporation, All rights reserved. Example: Creating an Object Type Specification SQL> CREATE TYPE name_typ AS OBJECT( 2 f_name VARCHAR2(25), 3 l_name VARCHAR2(25), 4 initials varchar2(7), 5 MEMBER FUNCTION full_name return varchar2 6 PRAGMA RESTRICT_REFERENCES( full_name, 7 WNDS, RNDS, WNPS, RNPS )); 1 2

8-7 Copyright  Oracle Corporation, All rights reserved. Example: Creating an Object Type Body SQL> CREATE TYPE BODY name_typ AS 2 MEMBER FUNCTION full_name RETURN VARCHAR2 3 IS 4 BEGIN 5 RETURN (l_name || ' ' || f_name ); 6 END full_name; 7 END; 1

8-8 Copyright  Oracle Corporation, All rights reserved. Example: Creating a Complex Object Type Specification SQL> CREATE TYPE emp_typ AS OBJECT( 2 emp_id NUMBER(7), 3 name name_typ, --datatype is object type 4 street VARCHAR2(25), 5 city VARCHAR2(15), 6state CHAR(2), 7 zip INTEGER, 8 MEMBER FUNCTION get_name RETURN VARCHAR2 9 PRAGMA RESTRICT_REFERENCES(get_name, 10 WNDS, RNDS, WNPS, RNPS), 11 MEMBER PROCEDURE set_l_name (v_name VARCHAR2));

8-9 Copyright  Oracle Corporation, All rights reserved. Example: Creating a Complex Object Type Body SQL> CREATE TYPE BODY emp_typ AS 2 MEMBER FUNCTION get_name RETURN VARCHAR2 3 IS 4 BEGIN 5 RETURN (name.l_name ||' '|| name.f_name ); 6 END; 7 MEMBER PROCEDURE set_l_name (v_name VARCHAR2) 8 IS 9 BEGIN 10 name.l_name := v_name; 11 END; 12 END;

8-10 Copyright  Oracle Corporation, All rights reserved. Obtaining Descriptions of Object Type Methods from the Data Dictionary Information about objects can be found in: USER_OBJECTS Information about object type methods can be found in: USER_METHOD_PARAMS USER_METHOD_RESULTS USER_TYPE_METHODS Information about objects can be found in: USER_OBJECTS Information about object type methods can be found in: USER_METHOD_PARAMS USER_METHOD_RESULTS USER_TYPE_METHODS ALL _OBJECTS ALL _METHOD_PARAMS ALL_ METHOD_RESULTS ALL_ TYPE_METHODS

8-11 Copyright  Oracle Corporation, All rights reserved. Example: Creating an object table and calling Object Methods SQL>CREATE TABLE name_table OF name_typ; SQL>INSERT INTO name_table 2 VALUES('Marilyn','Monroe','MM'); SQL>SELECT nt.f_name,nt.name_typ.full_name() 2 FROM name_table nt;

8-12 Copyright  Oracle Corporation, All rights reserved. Using the Constructor Method SQL> CREATE TYPE tv_type AS OBJECT ( 2 tv_category VARCHAR2(20), 3 screen_size NUMBER(4)); DECLARE v_new_tv tv_type := tv_type('WEB tv', 32); v_alt_tv tv_type; BEGIN v_alt_tv := tv_type('Big Screen', 72); END; SQL> CREATE TABLE tv OF tv_type; SQL> INSERT INTO tv VALUES(tv_type('Color tv', '28')); SQL> CREATE TABLE tv OF tv_type; SQL> INSERT INTO tv VALUES(tv_type('Color tv', '28'));

8-13 Copyright  Oracle Corporation, All rights reserved. Manipulating Objects Object type SQL>CREATE TABLE person_tab OF emp_typ; Object table SQL> CREATE TYPE emp_typ AS OBJECT( 2 emp_id NUMBER(7), 3 name name_typ, --datatype is object type 4 street VARCHAR2(25), 5 city VARCHAR2(15), 6 state CHAR(2), 7 zip INTEGER, 8 MEMBER FUNCTION get_name RETURN VARCHAR2 9 PRAGMA RESTRICT_REFERENCES(get_name, 10 WNDS, RNDS, WNPS, RNPS), 11 MEMBER PROCEDURE set_l_name (v_name VARCHAR2));

8-14 Copyright  Oracle Corporation, All rights reserved. SELECT VALUE(p) FROM person_tab p WHERE p.l_name LIKE '%BLAKE'; Manipulating Objects: Selecting SELECT * FROM person_tab p WHERE p.l_name LIKE '%BLAKE'; To return a result set of rows: To return a result set of objects:

8-15 Copyright  Oracle Corporation, All rights reserved. BEGIN INSERT INTO person_tab VALUES(emp_typ(4567,'Christian','Bauwens',...)); END; Manipulating Objects: Inserting BEGIN INSERT INTO person_tab VALUES(1234,'Ellen','Gravina',...); END; Adding objects to an object table: Using the constructor for the object type:

8-16 Copyright  Oracle Corporation, All rights reserved. BEGIN UPDATE person_tab p SET p = emp_typ(5678,'Charlie','Brown',...) WHERE p.l_name = 'Bauwens'; END; Manipulating Objects: Updating BEGIN UPDATE person_tab p SET p.street = '25 Elk Street' WHERE p.l_name = 'Gravina'; END; Updating attributes of an object table: Using the constructor for the object type:

8-17 Copyright  Oracle Corporation, All rights reserved. Manipulating Objects: Deleting Removing objects from an object table: BEGIN DELETE FROM person_tab p WHERE p.street = '25 Elk Street'; END;

8-18 Copyright  Oracle Corporation, All rights reserved. Reference type of inclusion of objects CREATE TYPE Dept_Type AS OBJECT( Name VARCHAR2(10), Loc VARCHAR2(50)); CREATE TABLE Obj_Dept OF Dept_Type; CREATE TYPE Emp_Type AS OBJECT( Name VARCHAR2(20), Sal Number, Dept_ref REF Dept_Type ); CREATE TABLE Obj_Emp OF Emp_Type (Dept_ref SCOPE IS Obj_Dept); CREATE TYPE Dept_Type AS OBJECT( Name VARCHAR2(10), Loc VARCHAR2(50)); CREATE TABLE Obj_Dept OF Dept_Type; CREATE TYPE Emp_Type AS OBJECT( Name VARCHAR2(20), Sal Number, Dept_ref REF Dept_Type ); CREATE TABLE Obj_Emp OF Emp_Type (Dept_ref SCOPE IS Obj_Dept); CREATE TABLE Rel_emp( id NUMBER PRIMARY KEY, Name VARCHAR2(20), Sal Number, Dept_ref REF Dept_Type SCOPE IS Obj_Dept); CREATE TABLE Rel_emp( id NUMBER PRIMARY KEY, Name VARCHAR2(20), Sal Number, Dept_ref REF Dept_Type SCOPE IS Obj_Dept);

8-19 Copyright  Oracle Corporation, All rights reserved. Collections An attribute or a column may be of a collection type: 1. VARRAY - vector type; 2. TABLE - table type. An attribute or a column may be of a collection type: 1. VARRAY - vector type; 2. TABLE - table type.

8-20 Copyright  Oracle Corporation, All rights reserved. Summary of object types Object types give developers the ability to construct datatypes that were not previously defined. An object type has three components: – Attribute specification – Method specification – Method implementation Object types can be simple or complex. Object types give developers the ability to construct datatypes that were not previously defined. An object type has three components: – Attribute specification – Method specification – Method implementation Object types can be simple or complex.

8-21 Copyright  Oracle Corporation, All rights reserved. Overview - Large Objects Recipe (CLOB) Photo (BLOB) Movie(BFILE)

8-22 Copyright  Oracle Corporation, All rights reserved. Contrasting LONG and LOB Datatypes LONG, LONG RAW Single column per table Up to 2 Gb SELECT returns data Data stored in-line No object type support Sequential access to data LOB Multiple columns per table Up to 4 Gb SELECT returns locator Data stored in-line or out-of-line Supports object types Random access to data

8-23 Copyright  Oracle Corporation, All rights reserved. Anatomy of a LOB DECLARE lobloc BLOB; BEGIN SELECT col1 INTO lobloc FROM LOB_Table WHERE col2=123; END; Program with LOB locator handle LOB Data Table with LOB locator

8-24 Copyright  Oracle Corporation, All rights reserved. Internal LOBs Recipe (CLOB) Photo (BLOB)

8-25 Copyright  Oracle Corporation, All rights reserved. Creating a Table with LOBs: Example SQL> CREATE TABLE employee 2 (emp_id NUMBER, 3 emp_name VARCHAR2(35), 4 resume CBLOB, 5 picture BLOB);

8-26 Copyright  Oracle Corporation, All rights reserved. External LOBs Oracle8 provides a BFILE datatype to support an external or file-based large object as one of the following: Movie (BFILE) Attributes in an object type Column values in a table

8-27 Copyright  Oracle Corporation, All rights reserved. The Directory Alias Movie (BFILE) User DIRECTORY lob_path = '/oracle/lob/'

8-28 Copyright  Oracle Corporation, All rights reserved. Managing LOBs To fully interact with LOBs, file-like interfaces are provided in: – PL/SQL package DBMS_LOB – Oracle Call Interface The Oracle Server provides some support for LOB management through SQL. To fully interact with LOBs, file-like interfaces are provided in: – PL/SQL package DBMS_LOB – Oracle Call Interface The Oracle Server provides some support for LOB management through SQL.

8-29 Copyright  Oracle Corporation, All rights reserved. Managing BFILEs Create OS directory and supply files Create Oracle table Interact with BFILE using – PL/SQL package DBMS_LOB – Oracle Call Interface Create OS directory and supply files Create Oracle table Interact with BFILE using – PL/SQL package DBMS_LOB – Oracle Call Interface

8-30 Copyright  Oracle Corporation, All rights reserved. Example: Populating LOBs Using SQL To populate a LOB column: INSERT row into table, providing either a value, a NULL, or the empty function for the LOB. UPDATE row, modifying the column value to a blank RESUME. To populate a LOB column: INSERT row into table, providing either a value, a NULL, or the empty function for the LOB. UPDATE row, modifying the column value to a blank RESUME. SQL> UPDATE employee SET resume = 2> (SELECT resume FROM employee 3> WHERE emp_name='Default') 4> AND emp_id = 4508; SQL> INSERT INTO employee VALUES 2> (7898,'Marilyn Monroe', EMPTY_CLOB(),NULL);

8-31 Copyright  Oracle Corporation, All rights reserved. Populating LOBs with PL/SQL DECLARE -- populate through the PL/SQL package lobloc CLOB; --will serve as the LOB locator text VARCHAR2(2000); amount NUMBER ; offset INTEGER; BEGIN text := 'the text to be written into the LOB'; SELECT resume INTO lobloc -- get LOB handle FROM employee WHERE emp_id = 5887 FOR UPDATE; offset := DBMS_LOB.GETLENGTH(lobloc) + 1; amount := length(text); DBMS_LOB.WRITE ( lobloc, amount, offset, text ); COMMIT; DBMS_OUTPUT.PUT_LINE('You have just written ' || to_char(amount) ||' characters into the LOB'); END;

8-32 Copyright  Oracle Corporation, All rights reserved. Delete rows containing LOBs. Disassociate a LOB value from a row. Delete rows containing LOBs. Disassociate a LOB value from a row. Example: Removing LOBs SQL>DELETE FROM person_tab 2 WHERE pname = 'Opie Griffith'; SQL>UPDATE person_tab SET resume = EMPTY_CLOB() 2 WHERE pname = 'Laura Roshto';

8-33 Copyright  Oracle Corporation, All rights reserved. Working with the DBMS_LOB Package Much of the work done with LOBs will require use of the provided PL/SQL package, DBMS_LOB. DBMS_LOB defines the constants – DBMS_LOB.LOBMAXSIZE = – file_readonly constant binary_integer:=0 Much of the work done with LOBs will require use of the provided PL/SQL package, DBMS_LOB. DBMS_LOB defines the constants – DBMS_LOB.LOBMAXSIZE = – file_readonly constant binary_integer:=0

8-34 Copyright  Oracle Corporation, All rights reserved. COMPARE FILEGETNAME INSTR GETLENGTH READ SUBSTR FILEEXISTS FILEISOPEN The DBMS_LOB Package APPEND COPY ERASE TRIM WRITE FILECLOSE FILECLOSEALL FILEOPEN MutatorsObservers

8-35 Copyright  Oracle Corporation, All rights reserved. The DBMS_LOB Package NULL arguments get NULL returns Offsets – BLOB, BFILE: Measured in bytes – CLOB, NCLOB: Measured in characters No negative values for parameters NULL arguments get NULL returns Offsets – BLOB, BFILE: Measured in bytes – CLOB, NCLOB: Measured in characters No negative values for parameters

8-36 Copyright  Oracle Corporation, All rights reserved. DBMS_LOB READ and WRITE PROCEDURE READ ( lobsrc IN BFILE|BLOB|CLOB, amount IN OUT BINARY_INTEGER, offset IN INTEGER, buffer OUT RAW|VARCHAR2 ) PROCEDURE WRITE ( lobdst IN OUT BLOB|CLOB, amount IN OUT BINARY_INTEGER, offset IN INTEGER := 1, buffer IN RAW|VARCHAR2 ) -- RAW for BLOB

8-37 Copyright  Oracle Corporation, All rights reserved. Summary Four built-in types for large objects: BLOB, CLOB, NCLOB, BFILE LOBs replace LONG and LONG RAW: New and old types are not compatible Two storage options for LOBs: – Stored in the Oracle Server (internal LOBs) – Stored in external host files (external LOBs) DBMS_LOB PL/SQL package provides routines for LOB management. Four built-in types for large objects: BLOB, CLOB, NCLOB, BFILE LOBs replace LONG and LONG RAW: New and old types are not compatible Two storage options for LOBs: – Stored in the Oracle Server (internal LOBs) – Stored in external host files (external LOBs) DBMS_LOB PL/SQL package provides routines for LOB management.