1 Object-Relational Databases User-Defined Types Nested Tables.

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.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.
CSC 570 Notes for Chapter 10 Advanced Topics in Relational Databases.
1 Object-Relational Databases User-Defined Types Object ID’s Nested Tables.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Winter 2002Arthur Keller – CS 18012–1 Schedule Today: Feb. 19 (T) u Object-Relational Systems. u Read Sections 4.5, Assignment 5 due. Feb. 21.
2/1/04Database Systems: Salman Azhar1 Object-Relational Databases Salman Azhar User-Defined Types Object IDs Nested Tables These slides use some figures,
Object-Orientation in Query Languages By: Toan Nguyen Class: CS 157A.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
Oracle Objects Object Oriented Database Approach.
Fall 2001Arthur Keller – CS 18014–1 Schedule Nov. 13 (T) Object-Relational, O-R Queries. u Read Sections 4.5, Assignment 6 due. (No office hours.)
Winter 2002Judy Cushing12–1 Schedule Today (week 8): u Object-Relational Systems. Sections 4.5, u Assignment 5 debriefed. u Transactions, Authorization.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object and Object- Relational Databases.
Object and object-relational databases 1. Object databases vs. Object-relational databases Object databases Stores complex objects – Data + functions.
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.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
1 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Introduction to Object-Relational features of Oracle 9i.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
ADVANCED DATABASE SYSTEMS DR. FATEMEH AHMADI- ABKENARI SEPTEMBER Object Databases.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
Winter 2006Keller, Ullman, Cushing12–1 Object-Relational Systems Object-oriented ideas enter the relational world. u Keep relation as the fundamental abstraction.
Chapter 18 Object Database Management Systems. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Motivation for object.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Object-Relational SQL CIS 4301 Lecture Notes 4/20/2006.
1 Object-Relational Databases User-Defined Types Object ID’s Nested Tables.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Databases 1 9th lecture. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
Introduction to PL/SQL. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Object Relational Features
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 Object-Relational Database.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Chapter 12 Outline Overview of Object Database Concepts Object-Relational Features Object Database.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: Stored Procedure & Embedded SQL Ch. 9.3, 9.4.
1 Introduction to Database Systems, CS420 SQL Persistent Stored Modules (PSM) – Stored Procedure.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
CS422 Principles of Database Systems Object-Oriented Features in DBMS Chengyu Sun California State University, Los Angeles.
CPSC-310 Database Systems
Slides are reused by the approval of Jeffrey Ullman’s
Object-Relational Databases
CMSC-461 Database Management Systems
Introduction to Database Systems, CS420
UNIT - V STORED PROCEDURE.
Object-Relational DBMSs
Chapter 12 Outline Overview of Object Database Concepts
CPSC-310 Database Systems
Object-Relational Systems
Object-Relational Databases
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
CMSC-461 Database Management Systems
C Programming Lecture-13 Structures
Java Programming Language
Modification to Views Via Triggers
CSC Database Management Systems
CMSC-461 Database Management Systems
Presentation transcript:

1 Object-Relational Databases User-Defined Types Nested Tables

2 Merging Relational and Object Models Object-oriented models support interesting data types --- not just flat files. Object-oriented models support interesting data types --- not just flat files. Structs, collections Structs, collections The relational model supports very-high-level queries. The relational model supports very-high-level queries. Object-relational databases are an attempt to get the best of both. Object-relational databases are an attempt to get the best of both.

3 Evolution of DBMS’s Object-oriented DBMS’s failed because they did not offer the efficiencies of well-entrenched relational DBMS’s. Object-oriented DBMS’s failed because they did not offer the efficiencies of well-entrenched relational DBMS’s. Object-relational extensions to relational DBMS’s capture much of the advantages of OO, yet retain the relation as the fundamental abstraction. Object-relational extensions to relational DBMS’s capture much of the advantages of OO, yet retain the relation as the fundamental abstraction.

4 SQL-99 and Oracle Features SQL-99 includes many of the object-relational features to be described. SQL-99 includes many of the object-relational features to be described. However, being so new, different DBMS’s use different approaches. However, being so new, different DBMS’s use different approaches. We’ll sometimes use features and syntax from Oracle. We’ll sometimes use features and syntax from Oracle.

5 User-Defined Data Types A user-defined data type, or UDT, is essentially a class definition, with a structure and methods. A user-defined data type, or UDT, is essentially a class definition, with a structure and methods. Two uses: Two uses: 1. As a rowtype, that is, the type of a relation. 2. As the type of an attribute of a relation.

6 UDT Definition CREATE TYPE AS ( ); Oracle syntax: Oracle syntax: 1. Add “OBJECT” in CREATE … AS OBJECT. 2. Example: create or replace type Point as object ( x number, y number);

7 Example: UDT Definition CREATE TYPE StudentType AS ( name CHAR(20), addrCHAR(20), class_level CHAR class_level CHAR); CREATE TYPE CourseType AS ( numberCHAR(20), locationCHAR(20) );

8 References If T is a type, then REF T is the type of a reference to T, that is, a pointer to an object of type T. If T is a type, then REF T is the type of a reference to T, that is, a pointer to an object of type T. Often called an “object ID” in OO systems. Often called an “object ID” in OO systems. Unlike object ID’s, a REF is visible, although it is usually gibberish. Unlike object ID’s, a REF is visible, although it is usually gibberish.

9 Example: REF CREATE TYPE EnrollmentType AS ( studentREF StudentType, courseREF CourseType, grade FLOAT ); EnrollmentType objects look like: EnrollmentType objects look like: To a StudentType object To a CoureType object

10 UDT’s as Rowtypes A table may be defined to have a schema that is a rowtype, rather than by listing its elements. A table may be defined to have a schema that is a rowtype, rather than by listing its elements. Syntax: Syntax: CREATE TABLE OF ; ;

11 Example: Creating a Relation CREATE TABLE Student OF StudentType; CREATE TABLE Course OF CourseType; CREATE TABLE Enrollment OF EnrollmentType;

12 Values of Relations with a Rowtype Technically, a relation like Student, declared to have a rowtype StudentType, is not a set of tuples --- it is a unary relation, whose tuples are objects with components: name, addr... Technically, a relation like Student, declared to have a rowtype StudentType, is not a set of tuples --- it is a unary relation, whose tuples are objects with components: name, addr... Each UDT has a type constructor of the same name that wraps objects of that type. Each UDT has a type constructor of the same name that wraps objects of that type.

13 Example: Type Constructor The query The query SELECT * FROM Student; Produces “tuples” such as: Produces “tuples” such as: StudentType(’Mike’, ’Maple St’, ‘U’)

14 Accessing Values From a Rowtype In Oracle, the dot works as expected. In Oracle, the dot works as expected. Example: Example: SELECT s.name, s.addr FROM Student s;

15 Accessing Values: SQL-99 Approach In SQL-99, each attribute of a UDT has generator (get the value) and mutator (change the value) methods of the same name as the attribute. In SQL-99, each attribute of a UDT has generator (get the value) and mutator (change the value) methods of the same name as the attribute. The generator for A takes no argument, as A(). The generator for A takes no argument, as A(). The mutator for A takes a new value as argument, as A(v). The mutator for A takes a new value as argument, as A(v).

16 Example: SQL-99 Value Access The same query in SQL-99 is The same query in SQL-99 is SELECT s.name(), s.addr() FROM Student s;

17 Inserting Rowtype Values In Oracle, we can use a standard INSERT statement, remembering that a relation with a rowtype is really unary and needs that type constructor. In Oracle, we can use a standard INSERT statement, remembering that a relation with a rowtype is really unary and needs that type constructor. Example: Example: INSERT INTO Student VALUES( StudentType(’Mike’, ’Maple St.’,’U’) );

18 Inserting Values: SQL-99 Style 1. Create a variable X of the suitable type, using the constructor method for that type. 2. Use the mutator methods for the attributes to set the values of the fields of X. 3. Insert X into the relation.

19 The following must be part of a procedure, e.g., PSM, so we have a variable newStudent. The following must be part of a procedure, e.g., PSM, so we have a variable newStudent. SET newStudent = StudentType(); newStudent.name(’Mike’); newStudent.addr(’Maple St.’); newStudent.class(’U’); newStudent.class(’U’); INSERT INTO Student VALUES(newStudent); Mutator methods change newStudent’s Name, addr, and class components. Example: SQL-99 Insert

20 UDT’s as Column Types A UDT can be the type of an attribute. A UDT can be the type of an attribute. In either another UDT definition, or in a CREATE TABLE statement, use the name of the UDT as the type of the attribute. In either another UDT definition, or in a CREATE TABLE statement, use the name of the UDT as the type of the attribute.

21 Values of addr and favCourse components are objects with their own fields. Example: Column Type CREATE TYPE AddrType AS ( streetCHAR(30), cityCHAR(20), zipINT ); CREATE TABLE Student ( nameCHAR(30), addrAddrType, favCoureCourseType );

22 Oracle Problem With Field Access You can access a field F of an object that is the value of an attribute A by A.F. You can access a field F of an object that is the value of an attribute A by A.F. However, you must use an alias, say rr, for the relation R with attribute A, as rr.A.F. However, you must use an alias, say rr, for the relation R with attribute A, as rr.A.F.

23 Example: Field Access in Oracle Wrong: Wrong: SELECT favCourse.number FROM Student; Wrong: Wrong: SELECT Student.favCourse.number FROM Student; Right: Right: SELECT s.favCourse.number FROM Student s;

24 Following REF’s A -> B makes sense if: A -> B makes sense if: 1. A is of type REF T. 2. B is an attribute (component) of objects of type T. Denotes the value of the B component of the object pointed to by A. Denotes the value of the B component of the object pointed to by A.

25 Remember: Enrollment is a relation with rowtype EnrollmentType(student, course, price), where student and course are REF’s to objects of types StudentType and CourseType. Remember: Enrollment is a relation with rowtype EnrollmentType(student, course, price), where student and course are REF’s to objects of types StudentType and CourseType. Find the courses took by Joe: Find the courses took by Joe: SELECT e.course()->number FROM Enrollment e WHERE e.student()->name = ’Joe’; Then use the arrow to get the numbers of the courses and student referenced First, use generator methods to access the course and student components Example: Following REF’s

26 Following REF’s: Oracle Style REF-following is implicit in the dot. REF-following is implicit in the dot. Just follow a REF by a dot and a field of the object referred to. Just follow a REF by a dot and a field of the object referred to. Example: Example: SELECT e.course.number FROM Enrollment e WHERE e.student.name = ’Joe’;

27 Oracle’s DEREF Operator -- Motivation If we want the set of courses objects took by Joe, we might try: If we want the set of courses objects took by Joe, we might try: SELECT e.course FROM Enrollment e WHERE e.student.name = ’Joe’; Legal, but e.course is a set of REF, hence gibberish. Legal, but e.course is a set of REF, hence gibberish.

28 Using DEREF To see the CourseType objects, use: To see the CourseType objects, use: SELECT DEREF(e.course) FROM Enrollment e WHERE e.student.name = ’Joe’; Produces values like: Produces values like: CourseType(00101,’Database Design’,’GAB205’)

29 Methods --- Oracle Syntax Classes are more than structures; they may have methods. Classes are more than structures; they may have methods. We’ll study the Oracle syntax. Declare in CREATE TYPE, and define methods in a CREATE TYPE BODY statement. We’ll study the Oracle syntax. Declare in CREATE TYPE, and define methods in a CREATE TYPE BODY statement. Use PL/SQL syntax for methods. Use PL/SQL syntax for methods. Variable SELF refers to the object to which the method is applied. Variable SELF refers to the object to which the method is applied.

30 Let’s add method gradeInScale to Enrollment. Let’s add method gradeInScale to Enrollment. CREATE TYPE EnrollmentType AS OBJECT ( student REF StudentType, courseREF CourseType, gradeFLOAT, MEMBER FUNCTION gradeInScale(scale IN FLOAT) RETURN FLOAT, PRAGMA RESTRICT_REFERENCES(gradeInSacle, WNDS) );/ “Write no database state.” That is, whatever gradeInScale does it won’t modify the database. What Oracle calls methods. Example: Method Declaration

31 Method Definition – Oracle Style Form of create-body statement: Form of create-body statement: CREATE TYPE BODY AS <method definitions = PL/SQL procedure definitions, using “MEMBER FUNCTION” in place of “PROCEDURE”>END;/

32 CREATE TYPE BODY EnrollmentType AS MEMBER FUNCTION gradeInScale(scale FLOAT) RETURN FLOAT IS gradeInScale(scale FLOAT) RETURN FLOAT ISBEGIN RETURN scale* SELF.grade/100.0; END;END;/ Use parentheses only when there is at least one argument No mode (IN) in body, just in declaration Example: Method Definition

33 Method Use Follow a name for an object by a dot and the name of the method, with arguments if any. Follow a name for an object by a dot and the name of the method, with arguments if any. Example: Example: SELECT e.course.number, e.course.gradeInScale(4) FROM Enrollment e WHERE e.student.name = ’Joe’;

34 Order Methods: SQL-99 Each UDT T may define two methods called EQUAL and LESSTHAN. Each UDT T may define two methods called EQUAL and LESSTHAN. Each takes an argument of type T and is applied to another object of type T. Each takes an argument of type T and is applied to another object of type T. Returns TRUE if and only if the target object is = (resp. <) the argument object. Returns TRUE if and only if the target object is = (resp. <) the argument object. Allows objects of type T to be compared by =, <, etc. in WHERE clauses and for sorting (ORDER BY). Allows objects of type T to be compared by =, <, etc. in WHERE clauses and for sorting (ORDER BY).

35 Order Methods: Oracle We may declare any one method for any UDT to be an order method. We may declare any one method for any UDT to be an order method. The order method returns a value 0, as the value of object SELF is the argument object. The order method returns a value 0, as the value of object SELF is the argument object.

36 Order StudentType objects by name: Order StudentType objects by name: CREATE TYPE StudentType AS OBJECT ( nameCHAR(20), addrCHAR(20), ORDER MEMBER FUNCTION before( s2 IN StudentType) RETURN INT, PRAGMA RESTRICT_REFERENCES(before, WNDS, RNDS, WNPS, RNPS) );/ Read/write no database state/package state. A “package” is a collection of procedures and variables that can communicate values among them. Example: Order Method Declaration

37 Example: Order Method Definition CREATE TYPE BODY StudentType AS ORDER MEMBER FUNCTION before(s2 StudentType) RETURN INT IS BEGIN IF SELF.name < s2.name THEN RETURN –1; ELSIF SELF.name = s2.name THEN RETURN 0; ELSE RETURN 1; END IF; END;END;/

38 Oracle Nested Tables Allows values of tuple components to be whole relations. Allows values of tuple components to be whole relations. If T is a UDT, we can create a type S whose values are relations with rowtype T, by: If T is a UDT, we can create a type S whose values are relations with rowtype T, by: CREATE TYPE S AS TABLE OF T ;

39 Example: Nested Table Type CREATE TYPE StudentType AS OBJECT ( nameCHAR(20), addressCHAR(10), class_levelCHAR(10) );/ CREATE TYPE StudentTableType AS TABLE OF StudentType; /

40 Example --- Continued Use StudentTableType in a Department relation that stores the set of students by each department in one tuple for that department. Use StudentTableType in a Department relation that stores the set of students by each department in one tuple for that department. CREATE TABLE Department ( nameCHAR(30), addrCHAR(50), studentsStudentTableType );

41 Storing Nested Relations Oracle doesn’t really store each nested table as a separate relation --- it just makes it look that way. Oracle doesn’t really store each nested table as a separate relation --- it just makes it look that way. Rather, there is one relation R in which all the tuples of all the nested tables for one attribute A are stored. Rather, there is one relation R in which all the tuples of all the nested tables for one attribute A are stored. Declare in CREATE TABLE by: Declare in CREATE TABLE by: NESTED TABLE A STORE AS R

42 Example: Storing Nested Tables CREATE TABLE Manfs ( nameCHAR(30), addrCHAR(50), studentsStudentTableType ) NESTED TABLE students STORE AS StudentTable;

43 Querying a Nested Table We can print the value of a nested table like any other value. We can print the value of a nested table like any other value. But these values have two type constructors: But these values have two type constructors: 1. For the table. 2. For the type of tuples in the table.

44 Example: Query a Nested Table Find the students of computer science: Find the students of computer science: SELECT students FROM Department WHERE name = ’Computer Science’; Produces one value like: Produces one value like:StudentTableType( StudentType(’Sally’, ’Maple St’, ’U’), Studenttype(’Mike’, ’Ave C’, ’G’),… )

45 Querying Within a Nested Table A nested table can be converted to an ordinary relation by applying THE(…). A nested table can be converted to an ordinary relation by applying THE(…). This relation can be used in FROM clauses like any other relation. This relation can be used in FROM clauses like any other relation.

46 Find the graduate students of computer science: Find the graduate students of computer science: SELECT s.name FROM THE( SELECT students FROM Department WHERE name = ’Computer Science’ ) s WHERE s.class_level = ’G’; An alias for the nested table, which has no name The one nested table for the Students of computer science Example: Use of THE

47 Turning Relations Into Nested Tables Any relation with the proper number and types of attributes can become the value of a nested table. Any relation with the proper number and types of attributes can become the value of a nested table. Use CAST(MULTISET(…) AS ) on the relation to turn it into the value with the proper type for a nested table. Use CAST(MULTISET(…) AS ) on the relation to turn it into the value with the proper type for a nested table.

48 Example: CAST Suppose we have a relation Stuent(student, dept, building), where student is a StudentType object, dept a string --- the department of the student, building a string – building name. Suppose we have a relation Stuent(student, dept, building), where student is a StudentType object, dept a string --- the department of the student, building a string – building name. We want to insert into Department a new tuple, with computer science as the name, a set of students of that department, and ‘RP F201’ as the building name. We want to insert into Department a new tuple, with computer science as the name, a set of students of that department, and ‘RP F201’ as the building name.

49 INSERT INTO Department VALUES ( ’Computer Science’, CAST(MULTISET( SELECT s.student FROM Student s WHERE s.department = ’Computer Science’ ) AS StudentTableType ), ‘RP F201’ ); Turn the set of objects into a nested relation The set of StudentType objects for computer science Example: CAST --- 2