Object-Relational SQL CIS 4301 Lecture Notes 4/20/2006.

Slides:



Advertisements
Similar presentations
Oracle Object-Relational Model. - Structures : tables, views, indexes, etc. - Operations : actions that manipulate data stored in structures - Integrity.
Advertisements

Object Oriented Database Group 4 Mathieu Metz Palani Kumaresan Napa Gavinlertvatana Kristine Pei Keow Lee Prabhu Ramachandran.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
CSC 570 Notes for Chapter 10 Advanced Topics in Relational Databases.
Chapter 5 Types. 5-2 Topics in this Chapter Values vs. Variables Types vs. Representations Type Definition Operators Type Generators SQL Facilities.
1 Object-Relational Databases User-Defined Types Object ID’s Nested Tables.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
©Silberschatz, Korth and Sudarshan8.1Database System Concepts Chapter 8: Object-Oriented Databases Need for Complex Data Types The Object-Oriented Data.
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,
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.
RIZWAN REHMAN, CCS, DU. Advantages of ORDBMSs  The main advantages of extending the relational data model come from reuse and sharing.  Reuse comes.
Chapter 4 Object and Object-Relational Databases (Part ½: Object-Oriented Concepts) Lecturer: H.Ben Othmen Department of Computer Science, Umm Al-Qura.
Object and object-relational databases 1. Object databases vs. Object-relational databases Object databases Stores complex objects – Data + functions.
Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Database System Concepts and Architecture Lecture # 3 22 June 2012 National University of Computer and Emerging Sciences.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Pointer Data Type and Pointer Variables
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.
Company LOGO OODB and XML Database Management Systems – Fall 2012 Matthew Moccaro.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Introduction to Object-Relational features of Oracle 9i.
PostgreSQL and relational databases As well as assignment 4…
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
1 Object-Relational Databases User-Defined Types Nested Tables.
1 CS 430 Database Theory Winter 2005 Lecture 17: Objects, XML, and DBMSs.
ADVANCED DATABASE SYSTEMS DR. FATEMEH AHMADI- ABKENARI SEPTEMBER Object Databases.
Winter 2006Keller, Ullman, Cushing12–1 Object-Relational Systems Object-oriented ideas enter the relational world. u Keep relation as the fundamental abstraction.
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
Chapter 18 Object Database Management Systems. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Motivation for object.
Learners Support Publications Classes and Objects.
1 Object-Relational Databases User-Defined Types Object ID’s Nested Tables.
Object relational database managmement systems (ORDBMS) Adapted by Edel Sherratt from originals by Nigel Hardy.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 2 Object-Relational DBMSs Chapter 28 in Textbook.
Object Relational Features
FEN NOEA/IT - Databases/ORDB1 Object-Relational Databases The Evolutionary Approach to Closing the Gap between Relational Tables and Object Models.
© D. Wong Security and User Authorization in SQL 8.7 pp. 410  Authorization ID = user name  Special authorization ID: PUBLIC  Privileges for:
Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
Jennifer Widom Relational Databases The Relational Model.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 Object-Relational Database.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Programming Fundamentals Enumerations and Functions.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Chapter 12 Outline Overview of Object Database Concepts Object-Relational Features Object Database.
1 10 Systems Analysis and Design in a Changing World, 2 nd Edition, Satzinger, Jackson, & Burd Chapter 10 Designing Databases.
IST 210 Object Oriented Database. IST 210 Object-Oriented Concepts  Abstract Data Types  Class definition, provides extension to complex attribute types.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 Introduction to Database Systems, CS420 SQL Persistent Stored Modules (PSM) – Stored Procedure.
CS422 Principles of Database Systems Object-Oriented Features in DBMS Chengyu Sun California State University, Los Angeles.
Introduction to Database Programming with Python Gary Stewart
Object-Oriented Databases By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
Object-Relational Databases
Object-Relational DBMSs
CPSC-310 Database Systems
Object-Relational Systems
Relational Databases The Relational Model.
Relational Databases The Relational Model.
Object-Relational Databases
Classes and Objects.
Structured Types (9-12-2) Structured types allow composite attributes of E-R diagrams to be represented directly. Unnamed row types can also be used in.
Java Programming Language
Presentation transcript:

Object-Relational SQL CIS 4301 Lecture Notes 4/20/2006

© CIS Spring Outline User-Defined Data Types Operations on Object-Relational Data

© CIS Spring Sample Problem Dept of WR management in CA maintains database of images 40,000 digitized images, need to classify images electronically create table slides ( idint, datedate, captionvarchar(255), picturephoto_CD_image); Query: Find all sunset pictures taken within 20 miles of Sacramento SELECTid FROM slides P, landmarks L, landmarks S WHEREsunset (P.picture) and contains (P.caption, L.name) and L.location || S.location and S.name = ‘Sacramento’; create table landmarks ( namevarchar(50), locationpoint);

© CIS Spring Need for Complex Data Types Traditional database applications in data processing had conceptually simple data types Relatively few data types, first normal form holds Complex data types have grown more important in recent years E.g. Addresses can be viewed as a Single string, or Separate attributes for each part, or Composite attributes (which are not in first normal form) E.g. it is often convenient to store multivalued attributes as- is, without creating a separate relation to store the values in first normal form Applications computer-aided design, computer-aided software engineering multimedia and image databases, and document/hypertext databases

© CIS Spring Object-Relational Systems Need to allow DBMS’s to deal with specialized types – maps, signals, images, etc. – with their own specialized methods Need to support specialized methods even on conventional relational data Need to support structure more complex than “flat files”

© CIS Spring Object-Relational Systems Object-oriented models support interesting data types Failed because they did not offer efficiencies of well-accepted RDBMS’s Relational model supports very high-level queries Object-relational systems are an attempt to get the best of both worlds Capture many of the advantages of OO yet retain the relation as fundamental abstraction

© CIS Spring SQL-99 and Oracle Features SQL-99 includes many of the object- relational features to be described However, different DBMS’s use different approaches (read manual before using) We will use a mixture of features from Oracle and SQL-99

© CIS Spring User-Defined Types Classes of ODL are morphed into User- defined data types (UDTs) in SQL UDT’s play a dual role: 1.They can be the types of relations; i.e., the type of their tuple Sometimes called a row type. 2.They can be the type of an attribute in a relation

© CIS Spring Defining UDT’s – Example in Oracle Syntax CREATE TYPE StudentType AS OBJECT ( name CHAR(20) UNIQUE, addr CHAR(20), age INTEGER ); / CREATE TYPE CampusType AS OBJECT ( name CHAR(20) UNIQUE, location CHAR(20), enrollment INTEGER ); /

© CIS Spring Notes In Oracle, type definitions must be followed by a slash ( / ) in order to get them to compile. The SQL standard is similar, but “ OBJECT ” is not used after “ AS ”

© CIS Spring 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 a.k.a. object ID in OO speak However, unlike object IDs, REF is visible although it is usually gibberish

© CIS Spring Example CREATE TYPE ApplicationType AS OBJECT ( applicant REF(StudentType), applied_at REF(CampusType), received DATE ); / Sample ApplicationType object: (&1, &2, ) &1: (“John", “LA”, 18) &2: (“USC", “LA”, 33000)

© CIS Spring UDT’s as Row Types A table may be defined to have a schema that is a row type, rather than by listing its elements Syntax: CREATE TABLE OF ;

© CIS Spring Example: Creating a Relation CREATE TABLE Student OF StudentType; CREATE TABLE Campus OF CampusType; CREATE TABLE Application OF ApplicationType;

© CIS Spring Values of Relations with a Row Type Technically, a relation like Student, declared to have a row type StudentType, is not a set of triples Unary relation whose tuples are objects with three components: name, address, age

© CIS Spring Values of User-Defined Types – Oracle Approach Each UDT has a type constructor of the same name. Values of that type are the values of its fields wrapped in the constructor. SELECT * FROM Student; produces values such as StudentType(“John”, “LA”, 18)

© CIS Spring Accessing Fields of an Object – Oracle Approach The dot operator works as expected Thus, if we want the name and address without the constructor: SELECT S.name, S.addr FROM Student S; The alias S is not technically necessary, but there are other places where we must use an alias in order to access objects, and it is a good habit to use an alias always SQL standard: Same idea, but the attribute is treated as a generator method, with parentheses, e.g., S.name()

© CIS Spring Accessing Fields of an Object – SQL-99 Approach Same query in SQL-99 is: SELECT S.name(), S.addr() FROM Student S;

© CIS Spring Inserting Values – Oracle Approach We can use the standard INSERT in Oracle, but we must wrap the inserted object in its type-constructor Relation with row type is really unary!! INSERT INTO Student VALUES( StudentType(“Andy”, “SF”, 19) ); SQL standard involves generator and mutator methods; see text

© CIS Spring UDT as Column Type Remember, UDT can be type of an attribute In either another UDT definition or another create table statement, use the name of the UDT as the type of the attribute

© CIS Spring Example – Oracle Syntax Let’s create an address type for use with Student and Applications CREATE TYPE AddrType AS OBJECT ( street CHAR(30), city CHAR(20), zip INT ); Then we can say: CREATE TABLE Student ( name CHAR(20) UNIQUE, addr AddrType, age INTEGER );

© CIS Spring Need to Use Aliases If you access an attribute whose type is an object type, you must use an alias for the relation E.g., SELECT addr.city FROM Student; will not work in Oracle; neither will: SELECT Student.addr.city FROM Student; You have to say: SELECT S.addr.city FROM Student S;

© CIS Spring References Revisited Remember, UDT’s can have references. If T is a UDT, then REF(T) is the type of a reference to a T object. Unlike OO systems, refs are values that can be seen by queries.

© CIS Spring Dereferencing in SQL A  B = the B attribute of the object referred to by reference A Find the Universities that Joe applied to SELECT applied_at -> name, applied_at -> location FROM Application WHERE applicant -> name = “Joe”;

© CIS Spring Dereferencing in Oracle Dereferencing automatic, using dot operator Same query in Oracle syntax: SELECT App.applied_at -> name, App.applied_at -> location FROM Application App WHERE App.applicant -> name = “Joe”;

© CIS Spring Oracle’s DEREF Operator If we wanted the entire CampusType object, we might try to write SELECT App.applied_at FROM Application App WHERE ss.applicant.name = “Joe”; Legal, but App.applied_at is a reference, and we’d get a gibberish value To see the whole campus object, use: SELECT DREF(App.applied_at) FROM Application App WHERE ss.applicant.name = “Joe”;

© CIS Spring Methods Real reason object-relational isn’t just nested structures in relations We’ll follow Oracle syntax. Declared in a CREATE TYPE statement, defined in a CREATE TYPE BODY statement Methods are functions or procedures; in Oracle they are defined like any PL/SQL procedure or function But, there is a special tuple variable SELF that refers to that object to which the method is applied

© CIS Spring Example Let’s add a method rank to the ApplicationType and thus to the Application relation CREATE TYPE ApplicationType AS OBJECT ( applicant REF(StudentType), applied_at REF(CampusType), received DATE MEMBER FUNCTION rank( SAT IN INTEGER) RETURN INTEGER, PRAGMA RESTRICT_REFERENCES(rank, WNDS) ); CREATE TYPE BODY ApplicationType AS MEMBER FUNCTION rank(SAT INTEGER) RETURN INTEGER IS BEGIN // body of function goes here; END; / CREATE TABLE Application OF ApplicationType;

© CIS Spring Some Points to Remember The pragma is needed to allow rank to be used in queries WNDS = “write no database state” In the declaration, function/procedure arguments need a mode, IN, OUT, or IN OUT, just like PL/SQL procedures But the mode does not appear in the definition Many methods will take no arguments (relying on the built-in “self”) In that case, do not use parentheses after the function name The body can have any number of function declarations, separated by semicolons

© CIS Spring Example of Method Use Follow a designator for the object to which you want to apply the method by a dot, the name of the method, and argument(s) SELECT App.applicant.name, App.rank(789) FROM Application App WHERE App.applicant.name = “Joe” ;

© CIS Spring Built-In Comparison Functions (SQL) We can define for each UDT two functions EQUAL and LESSTHAN Allow values of this UDT to participate in WHERE clauses involving =, <=, etc. and in ORDER-BY sorting Order Methods in Oracle We can declare one method for a type to be an ORDER method Definition of this method must return -1, 0, 1, if “self” is less than, equal to, or greater than the argument object Also used in comparisons for WHERE and ORDER BY

© CIS Spring Example Order StudentType objects by name: CREATE TYPE StudentType AS OBJECT ( name CHAR(20) UNIQUE, addr CHAR(20), age INTEGER, ORDER MEMBER FUNCTION before( student2 IN StudentType) RETURN INT, PRAGMA RESTRICT_REFERENCES(before, WNDS,RNDS,WNPS,RNPS) ); /

© CIS Spring Body CREATE TYPE BODY StudentType AS ORDER MEMBER FUNCTION before(student2 StudentType) RETURN INT IS BEGIN IF SELF.name < student2.name THEN RETURN -1; ELSIF SELF.name = student2.name THEN RETURN 0; ELSE RETURN 1; END IF; END; The extra codes in the pragma guarantee no reading or writing of the database state or the “package state”

© CIS Spring Not Covered Nested relations Collections Other type constructors Inheritance etc.