Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/1/04Database Systems: Salman Azhar1 Object-Relational Databases Salman Azhar User-Defined Types Object IDs Nested Tables These slides use some figures,

Similar presentations


Presentation on theme: "2/1/04Database Systems: Salman Azhar1 Object-Relational Databases Salman Azhar User-Defined Types Object IDs Nested Tables These slides use some figures,"— Presentation transcript:

1 2/1/04Database Systems: Salman Azhar1 Object-Relational Databases Salman Azhar User-Defined Types Object IDs Nested Tables These slides use some figures, definitions, and explanations from Elmasri- Navathe’s Fundamentals of Database Systems and Molina-Ullman-Widom’s Database Systems

2 2/1/04Database Systems: Salman Azhar2 Merging Relational and Object Models uObject-oriented models support winteresting data types --- not just flat files. E.g., Maps, multimedia, etc. uThe relational model supports wvery-high-level queries uObject-relational databases are an attempt to get the best of both.

3 2/1/04Database Systems: Salman Azhar3 Evolution of DBMSs uObject-oriented DBMSs fell short wbecause they did not offer the efficiencies of a relational DBMS. uObject-relational extensions to relational DBMSs wcapture much of the advantages of OO wyet retain the relation as the fundamental abstraction.

4 2/1/04Database Systems: Salman Azhar4 SQL-99 and DBMS Features uSQL-99 includes many of the object- relational features to be described. uHowever, being so new, different DBMSs use different approaches. wWe’ll sometimes use features and syntax from Oracle and SQL Server.

5 2/1/04Database Systems: Salman Azhar5 User Defined Types uA user-defined type, or UDT, is essentially a class definition, with a structure and methods. uTwo uses: 1.As a row-type, that is, the type of a relation. 2.As an column-type, that is, the type if attribute in a relation.

6 2/1/04Database Systems: Salman Azhar6 UDT Definition CREATE TYPE AS ( ); uOracle syntax: 1.Add “OBJECT” as in CREATE TYPE AS OBJECT. 2.Follow with / to have the type stored.

7 2/1/04Database Systems: Salman Azhar7 Example: UDT Definition CREATE TYPE DealerType AS ( name CHAR(20), addrCHAR(20) ); CREATE TYPE CarType AS ( nameCHAR(20), manfCHAR(20) );

8 2/1/04Database Systems: Salman Azhar8 References uIf T is a type, then REF T is the type of a reference to T wthis means a pointer to an object of type T. woften called an “object ID” in OO systems. uUnlike object ID’s, a REF is visible, walthough it is usually gibberish.

9 2/1/04Database Systems: Salman Azhar9 Example: REF CREATE TYPE SellsType AS ( dealerREF DealerType, carREF CarType, priceFLOAT ); uSellsType objects look like: REF DealerType REF CarType FLOAT 30000 Reference to a DealerType object Reference to a CarType object

10 2/1/04Database Systems: Salman Azhar10 UDTs as Row Types uA table may be defined to have a schema that is a row type, rather than by listing its elements. uSyntax: wCREATE TABLE OF ; uCreates a table where each row is of

11 2/1/04Database Systems: Salman Azhar11 Example: Creating Tables CREATE TABLE Dealer OF DealerType; CREATE TABLE Car OF CarType; CREATE TABLE Sells OF SellsType;

12 2/1/04Database Systems: Salman Azhar12 Values of Relations with a Row-type uTechnically, a relation declared to have a rowtype DealerType (such as Dealer), wis not a set of pairs wit is a unary relation whose tuples are objects with two components: –name and addr.

13 2/1/04Database Systems: Salman Azhar13 Type Constructors uEach UDT has a type constructor of the same name that wraps objects of that type.

14 2/1/04Database Systems: Salman Azhar14 Example: Type Constructor uThe query SELECT * FROM Dealer; uProduces “tuples” such as: DealerType(‘AutoNation’, ‘Maple St.’)

15 2/1/04Database Systems: Salman Azhar15 Aliasing: Accessing Values From a Rowtype uIn Oracle and MS SQL Server, the dot works as expected wbut it may not work in all DBMS wso it is a good idea, to use an alias for every relation, when O-R features are used. uExample: SELECT dd.name, dd.addr FROM Dealer dd;

16 2/1/04Database Systems: Salman Azhar16 Accessing Values: SQL-99 Approach uIn SQL-99, each attribute of a UDT has: wGet method Called Generator methods (get the value) and wSet method Called Mutator methods (change the value) uThese methods have the same name as the attribute. wThe generator for A takes no argument, as A( ). wThe mutator for A takes a new value as argument, as A(v).

17 2/1/04Database Systems: Salman Azhar17 Example: SQL-99 Value Access uConsider SELECT dd.name, dd.addr FROM Dealer dd; uThe same query in SQL-99 is SELECT dd.name( ), dd.addr( ) FROM Dealer dd; Explicitly access generators

18 2/1/04Database Systems: Salman Azhar18 Inserting Row Type Values (Oracle Style) uWe can use a standard INSERT statement, wremembering that a relation with a row type is really unary and needs that type constructor. uExample: INSERT INTO Dealer VALUES( DealerType(‘AutoNation’, ‘Maple St.’) ); uNote: DealerType is a constructor

19 2/1/04Database Systems: Salman Azhar19 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.

20 2/1/04Database Systems: Salman Azhar20 Example: SQL-99 Insert uThe following must be part of a procedure, so we have a variable newDealer. SET newDealer = DealerType( ); newDealer.name(‘AutoNation’); newDealer.addr(‘Maple St.’); INSERT INTO Dealer VALUES(newDealer); Mutator methods change newDealer’s name and addr components. Construct a new dealer Insert new values

21 2/1/04Database Systems: Salman Azhar21 UDTs as Column Types uA UDT can be the type of a column (an attribute) win either another UDT definition wor in a CREATE TABLE statement uUse the name of the UDT as the type of the attribute

22 2/1/04Database Systems: Salman Azhar22 Example: Column Type CREATE TYPE AddrType AS ( streetCHAR(30), cityCHAR(20), zipINT ); CREATE TABLE Buyers ( nameCHAR(30), addrAddrType, favCarCarType ); Values of addr and favCar components are objects with 3 and 2 fields, respectively.

23 2/1/04Database Systems: Salman Azhar23 Oracle Problem With Field Access uYou can access a field F of an object that is the value of an attribute A by A.F. uHowever, you must use an alias, say tt, for the table T with attribute A, as tt.A.F.

24 2/1/04Database Systems: Salman Azhar24 Example: Field Access in Oracle uWrong (can’t have nothing): SELECT favCar.name FROM Buyers; uWrong (can’t have the table name): SELECT Buyers.favCar.name FROM Buyers; uRight (must have alias name): SELECT b.favCar.name FROM Buyers b;

25 2/1/04Database Systems: Salman Azhar25 Following References (REFs) uA  B denotes the value of the B component of the object pointed to by A uA  B makes sense if: 1.A is of type REF T 2.B is an attribute (component) of objects of type T

26 2/1/04Database Systems: Salman Azhar26 Following REF’s: Oracle Style uREF-following is implicit in the dot uJust follow a REF by a dot and a field of the object referred to uExample: SELECT ss.car.name FROM Sells ss WHERE ss.dealer.name = ‘AutoNation’; Field name in object car in table aliases as ss

27 2/1/04Database Systems: Salman Azhar27 Oracle’s DEREF Operator -- Motivation uIf we want the set of car objects for the cars sold by AutoNAtion, we might try: SELECT ss.car FROM Sells ss WHERE ss.dealer.name = ‘AutoNation’; uLegal, but ss.car is a REF, hence gibberish! wNeed to add DEREF object car (ref) in table aliases as ss

28 2/1/04Database Systems: Salman Azhar28 Using DEREF uTo see the CarType objects, use: SELECT DEREF(ss.car) FROM Sells ss WHERE ss.dealer.name = ‘AutoNation’; uProduces values like: CarType(‘MiniCooper’, ‘BMW’)

29 2/1/04Database Systems: Salman Azhar29 Methods uClasses are more than structures; they may have methods. wDeclare in CREATE TYPE wDefine methods in a CREATE TYPE BODY statement wUse T-SQL syntax for methods. (T-SQL: MS SQL Server = PL/SQL: Oracle) wVariable SELF refers to the object to which the method is applied (this)

30 2/1/04Database Systems: Salman Azhar30 Method Definition uForm of create-body statement: CREATE TYPE BODY AS PROCEDURE methodName(arg ) RETURN BEGIN … END;

31 2/1/04Database Systems: Salman Azhar31 Method Definition – Oracle Style Method definitions are called PL/SQL procedure definitions in Oracle Oracle uses “MEMBER FUNCTION” in place of “PROCEDURE”

32 2/1/04Database Systems: Salman Azhar32 Example: Method Definition CREATE TYPE BODY SellsType AS MEMBER FUNCTION priceConvert(rate FLOAT) RETURN FLOAT IS BEGIN RETURN rate * SELF.price; END; Argument & argument type Sorta like “this” keyword in C++/Java (parenthesis only if an argument is passed)

33 2/1/04Database Systems: Salman Azhar33 Method Use uobjectName.methodName(arguments if any) uExample: SELECT ss.car.name, ss.priceConvert(1.33) FROM Sells ss WHERE ss.dealer.name = ‘AutoNation’;

34 2/1/04Database Systems: Salman Azhar34 Example: Nested Table Type CREATE TYPE CarType AS OBJECT ( nameCHAR(20), editionCHAR(20) ); GO CREATE TYPE CarTableType AS TABLE OF CarType; GO

35 2/1/04Database Systems: Salman Azhar35 Example --- Continued uUse CarTableType in a Manfs relation that stores the set of cars by each manufacturer in one tuple for that manufacturer. CREATE TABLE Manfs ( nameCHAR(30), addrCHAR(50), carcarTableType );

36 2/1/04Database Systems: Salman Azhar36 Nested Tables uAllows values of row components to be whole relations uIf T is a UDT, wwe can create a type S whose values are relations with rowtype T, by: CREATE TYPE S AS TABLE OF T ;

37 2/1/04Database Systems: Salman Azhar37 Querying a Nested Table uWe can print the value of a nested table like any other value uBut these values have two type constructors: 1.For the table 2.For the type of tuples in the table

38 2/1/04Database Systems: Salman Azhar38 Example: Query a Nested Table uFind the cars by Toyota: SELECT car FROM Manfs WHERE name = ‘Toyota’; uProduces one value: CarTableType( CarType(‘Camry’, ‘LE’) CarType(‘Corolla’, ‘L’) …. )

39 2/1/04Database Systems: Salman Azhar39 Querying Within a Nested Table uA nested table can be converted to an ordinary relation by applying THE(…) uThis relation can be used in FROM clauses like any other relation

40 2/1/04Database Systems: Salman Azhar40 Example: Use of THE uFind the cars made by Toyota: SELECT dd.edition FROM THE( SELECT car FROM Manfs WHERE name = ‘Toyota’; ) dd WHERE dd.name = ‘Camry’;


Download ppt "2/1/04Database Systems: Salman Azhar1 Object-Relational Databases Salman Azhar User-Defined Types Object IDs Nested Tables These slides use some figures,"

Similar presentations


Ads by Google