Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Object-Relational SQL CIS 4301 Lecture Notes 4/20/2006."— Presentation transcript:

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

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

3 © CIS 4301 - Spring 20063 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);

4 © CIS 4301 - Spring 20064 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

5 © CIS 4301 - Spring 20065 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”

6 © CIS 4301 - Spring 20066 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

7 © CIS 4301 - Spring 20067 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

8 © CIS 4301 - Spring 20068 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

9 © CIS 4301 - Spring 20069 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 ); /

10 © CIS 4301 - Spring 200610 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 ”

11 © CIS 4301 - Spring 200611 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

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

13 © CIS 4301 - Spring 200613 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 ;

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

15 © CIS 4301 - Spring 200615 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

16 © CIS 4301 - Spring 200616 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)

17 © CIS 4301 - Spring 200617 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()

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

19 © CIS 4301 - Spring 200619 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

20 © CIS 4301 - Spring 200620 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

21 © CIS 4301 - Spring 200621 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 );

22 © CIS 4301 - Spring 200622 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;

23 © CIS 4301 - Spring 200623 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.

24 © CIS 4301 - Spring 200624 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”;

25 © CIS 4301 - Spring 200625 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”;

26 © CIS 4301 - Spring 200626 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”;

27 © CIS 4301 - Spring 200627 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

28 © CIS 4301 - Spring 200628 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;

29 © CIS 4301 - Spring 200629 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

30 © CIS 4301 - Spring 200630 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” ;

31 © CIS 4301 - Spring 200631 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

32 © CIS 4301 - Spring 200632 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) ); /

33 © CIS 4301 - Spring 200633 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”

34 © CIS 4301 - Spring 200634 Not Covered Nested relations Collections Other type constructors Inheritance etc.


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

Similar presentations


Ads by Google