1 Object-Relational Databases User-Defined Types Nested Tables
Click here to load reader
Embed Size (px)
Transcript of 1 Object-Relational Databases User-Defined Types Nested Tables
*Object-Relational DatabasesUser-Defined TypesNested Tables
*Merging Relational and Object ModelsObject-oriented models support interesting data types --- not just flat files.Structs, collectionsThe relational model supports very-high-level queries.Object-relational databases are an attempt to get the best of both.
*Evolution of DBMSsObject-oriented DBMSs failed because they did not offer the efficiencies of well-entrenched relational DBMSs.Object-relational extensions to relational DBMSs capture much of the advantages of OO, yet retain the relation as the fundamental abstraction.
*SQL-99 and Oracle FeaturesSQL-99 includes many of the object-relational features to be described.However, being so new, different DBMSs use different approaches.Well sometimes use features and syntax from Oracle.
*User-Defined Data TypesA user-defined data type, or UDT, is essentially a class definition, with a structure and methods.Two uses:As a rowtype, that is, the type of a relation.As the type of an attribute of a relation.
*UDT DefinitionCREATE TYPE AS ();Oracle syntax:Add OBJECT in CREATE AS OBJECT.Example:create or replace type Point as object ( x number, y number);
*Example: UDT DefinitionCREATE TYPE StudentType AS (name CHAR(20),addrCHAR(20), class_level CHAR);CREATE TYPE CourseType AS (numberCHAR(20),locationCHAR(20));
*ReferencesIf 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.Unlike object IDs, a REF is visible, although it is usually gibberish.
*Example: REFCREATE TYPE EnrollmentType AS (studentREF StudentType,courseREF CourseType,grade FLOAT);EnrollmentType objects look like:93.00To a StudentTypeobjectTo a CoureTypeobject
*UDTs as RowtypesA table may be defined to have a schema that is a rowtype, rather than by listing its elements.Syntax:CREATE TABLE OF;
*Example: Creating a RelationCREATE TABLE Student OF StudentType;
CREATE TABLE Course OF CourseType;
CREATE TABLE Enrollment OF EnrollmentType;
*Values of Relations with a RowtypeTechnically, 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.
*Example: Type ConstructorThe querySELECT * FROM Student;Produces tuples such as:StudentType(Mike, Maple St, U)
*Accessing Values From a RowtypeIn Oracle, the dot works as expected.
Example:SELECT s.name, s.addrFROM Student s;
*Accessing Values: SQL-99 ApproachIn 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 mutator for A takes a new value as argument, as A(v).
*Example: SQL-99 Value AccessThe same query in SQL-99 is
SELECT s.name(), s.addr()FROM Student s;
*Inserting Rowtype ValuesIn Oracle, we can use a standard INSERT statement, remembering that a relation with a rowtype is really unary and needs that type constructor.Example:INSERT INTO Student VALUES(StudentType(Mike, Maple St.,U));
*Inserting Values: SQL-99 StyleCreate a variable X of the suitable type, using the constructor method for that type.Use the mutator methods for the attributes to set the values of the fields of X.Insert X into the relation.
*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);INSERT INTO Student VALUES(newStudent);Mutator methodschange newStudentsName, addr, and classcomponents.Example: SQL-99 Insert
*UDTs as Column TypesA 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.
*Example: Column TypeCREATE TYPE AddrType AS (streetCHAR(30),cityCHAR(20),zipINT);CREATE TABLE Student (nameCHAR(30),addrAddrType,favCoureCourseType);
*Oracle Problem With Field AccessYou 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 .
*Example: Field Access in OracleWrong:SELECT favCourse.numberFROM Student;Wrong:SELECT Student.favCourse.numberFROM Student;Right:SELECT s.favCourse.numberFROM Student s;
*Following REFsA -> B makes sense if:A is of type REF T.B is an attribute (component) of objects of type T.Denotes the value of the B component of the object pointed to by A.
*Remember: Enrollment is a relation with rowtype EnrollmentType(student, course, price), where student and course are REFs to objects of types StudentType and CourseType.Find the courses took by Joe:SELECT e.course()->numberFROM Enrollment eWHERE e.student()->name = Joe;Then use thearrow to get thenumbers of the coursesand student referencedFirst, use generator methods toaccess the course and student componentsExample: Following REFs
*Following REFs: Oracle StyleREF-following is implicit in the dot.Just follow a REF by a dot and a field of the object referred to.Example:SELECT e.course.numberFROM Enrollment eWHERE e.student.name = Joe;
*Oracles DEREF Operator -- MotivationIf we want the set of courses objects took by Joe, we might try:SELECT e.courseFROM Enrollment eWHERE e.student.name = Joe;Legal, but e.course is a set of REF, hence gibberish.
*Using DEREFTo see the CourseType objects, use:SELECT DEREF(e.course)FROM Enrollment eWHERE e.student.name = Joe;Produces values like:CourseType(00101,Database Design,GAB205)
*Methods --- Oracle SyntaxClasses are more than structures; they may have methods.Well study the Oracle syntax. Declare in CREATE TYPE, and define methods in a CREATE TYPE BODY statement.Use PL/SQL syntax for methods.Variable SELF refers to the object to which the method is applied.
*Lets 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));/What Oracle callsmethods.Example: Method Declaration
*Method Definition Oracle StyleForm of create-body statement:CREATE TYPE BODY ASEND;/
*CREATE TYPE BODY EnrollmentType ASMEMBER FUNCTION gradeInScale(scale FLOAT) RETURN FLOAT ISBEGINRETURN scale* SELF.grade/100.0;END;END;/Use parentheses onlywhen there is atleast one argumentNo mode (IN)in body, justin declarationExample: Method Definition
*Method UseFollow a name for an object by a dot and the name of the method, with arguments if any.Example:SELECT e.course.number, e.course.gradeInScale(4)FROM Enrollment eWHERE e.student.name = Joe;
- *Order Methods: SQL-99Each 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.Returns TRUE if and only if the target object is = (resp.
*Order Methods: OracleWe 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.
*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));/Example: Order Method Declaration
*Example: Order Method DefinitionCREATE TYPE BODY StudentType ASORDER MEMBER FUNCTIONbefore(s2 StudentType) RETURN INT ISBEGINIF SELF.name < s2.name THEN RETURN 1;ELSIF SELF.name = s2.name THEN RETURN 0;ELSE RETURN 1;END IF;END;END;/
*Oracle Nested TablesAllows 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:CREATE TYPE S AS TABLE OF T ;
*Example: Nested Table TypeCREATE TYPE StudentType AS OBJECT (nameCHAR(20),addressCHAR(10),class_levelCHAR(10));/CREATE TYPE StudentTableType AS TABLE OF StudentType;/
*Example --- ContinuedUse 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);
*Storing Nested RelationsOracle doesnt 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.Declare in CREATE TABLE by:NESTED TABLE A STORE AS R
*Example: Storing Nested TablesCREATE TABLE Manfs (nameCHAR(30),addrCHAR(50),studentsStudentTableType)NESTED TABLE students STORE AS StudentTable;
*Querying a Nested TableWe can print the value of a nested table like any other value.But these values have two type constructors:For the table.For the type of tuples in the table.
*Example: Query a Nested TableFind the students of computer science:SELECT students FROM DepartmentWHERE name = Computer Science;Produces one value like:StudentTableType(StudentType(Sally, Maple St, U),Studenttype(Mike, Ave C, G),)
*Querying Within a Nested TableA nested table can be converted to an ordinary relation by applying THE().This relation can be used in FROM clauses like any other relation.