Presentation is loading. Please wait.

Presentation is loading. Please wait.

KarczewskiDatenbanken 21 Object-Relational SQL Introduction: Object-Relational SQL (ORSQL) capabilities Objects and Tables OR-Create, -Insert, -Update,

Similar presentations


Presentation on theme: "KarczewskiDatenbanken 21 Object-Relational SQL Introduction: Object-Relational SQL (ORSQL) capabilities Objects and Tables OR-Create, -Insert, -Update,"— Presentation transcript:

1 KarczewskiDatenbanken 21 Object-Relational SQL Introduction: Object-Relational SQL (ORSQL) capabilities Objects and Tables OR-Create, -Insert, -Update, -Select User Defined Functions

2 KarczewskiDatenbanken 22 ORSQL capabilities Definition of table will enlarged with user-defined types: Columns that look like multi-valued fields or have any internal structure (like a record) are not permitted (non 1NF) Example: eidenamepositiondependents dep_namedep_age e01Smith, JohnAgentMichael J.9 Susan R.7 e02Andrews, DavidSuperintendentDavid M. Jr.10 e03Jones, FranklinAgentAndrew K.11 Mark W.9 Louisa M.4

3 KarczewskiDatenbanken 23 ORSQL capabilities In OO-modeling there are methods defined to operate on the objects. The only possibility to operate on objects is to use this class-specific methods. There is no other posibiltiy to work on the data (encapsulation). In ORDBMS there exists the language ORSQL (like SQL in relational databases). This language allows to work on all defined multi-valued and structured tables. In addition you can define functions belonging to the defined OR tables (encapsulation).

4 KarczewskiDatenbanken 24 Objects and Tables An object type has attributes of various types, analogous to columns of a table. Example (Oracle): create type name_t as object-- „Create Object Type“ ( lnamevarchar(10),-- last name fnamevarchar(10),-- first name michar(1)-- middle initial ); /-- SQL*Plus

5 KarczewskiDatenbanken 25 Objects and Tables After making the SQL statement „create type“ you can use this new defined type like an attribute type. Example (Oracle): create table teachers-- „Create Table Scheme“ ( tidint,-- identifier tnamename_t,-- object type defined above roomint,-- room number primary key (tid) ); There is still no object exisiting from object type name_typ.

6 KarczewskiDatenbanken 26 Objects and Tables To insert a row into the table defined above you can use the insert statement like in relational SQL, extended with the possibility of using object types Example (Oracle): insert into teachers values ( 1234, name_t( ' Einstein ', ' Albert ', ' E ' ), 120 ); Object constructor

7 KarczewskiDatenbanken 27 Objects and Tables After the insert-statement used above the table content can be shown as follows: Example (Oracle): tid 1234 tname lnamefnamemi EinsteinAlbertE room 120

8 KarczewskiDatenbanken 28 Objects and Tables To select values of the object type we use a „dot“-notation: Example (Oracle): selectt.tid from teachers t wheret.room = 120; -- normal SQL-select selectt.tid, t.tname.fname, t.tname.lname fromteachers t wheret.room = 120; -- extended SQL selecttid, tname.fname, tname.lname fromteachers whereroom = 120; -- doesn´t work We must use the alias t for table teachers in the extended SQL.

9 KarczewskiDatenbanken 29 Objects and Tables It is possible to use the object type-definition within another object type-definition. Example: drop type person_t; -- drop a possibly existing type create type person_t as object ( ssnonumber(5), pnamename_t,-- must be defined first agenumber(5) ); person_typ is dependent on name_typ, that means you cannot drop name_typ before person_typ is dropped.

10 KarczewskiDatenbanken 210 Objects and Tables A table is called object table if its rows are of object type, that is, each of its rows consists of an object of that type. Example: create table people of person_t ( primary key(ssno) );

11 KarczewskiDatenbanken 211 Objects and Tables An example of a state of the table people: nameless top-level column holding the row object named columns (also known as top-level attributes) attributes within pname row 1 row 3 row 2 ssno 123550123 023894455 245882134 pname pname.lname pname.fname pname.mi Sanchez Jose F Delaney Patrick X March Jacquelin E age 23 30 59 people row objects column objects select * select value(p)

12 KarczewskiDatenbanken 212 select p.age from people p where p.ssno = 245882134 (like relational statements) select p.pname from people p where p.age > 35 The columns will delivered in the form name_t(´Sanchez´, ´Jose´, ´F´) select * from people p where p.age > 35 delivers the top-level attributes (row objects) ssnopname(lname,fname,mi) age --------- ------------------------------- ---- 245882134name_typ(´Delaney´,´Patrick´,´X´)59 023894455name_typ(´Sanchez´,´Jose´,´F´) 30 Objects and Tables ( Select-statements )

13 KarczewskiDatenbanken 213 Objects and Tables ( Select-statements ) select value(p) from people p where age > 35 The nameless top-level column will be shown. Value(p) doesn´t mean VALUES within the insert-statement. Value(p)(ssno,pname(lname,fname,mi), age) ------------------------------------ ---- person_t(245882134,name_t(´Delaney´,´Patrick´,´X´), 59) person_t(023894455,name_t(´Sanchez´,´Jose´,´F´), 30) Person_t is the object-constructor with which the table was defined, it is another result than in „select *“-queries. New functionality is given with this object constructor functionality.

14 KarczewskiDatenbanken 214 select value(p) from people p where p.pname = name_t(´Sanchez´,´Jose´,´F´) The object constructor „name_t(´Sanchez´,´Jose´,´F´)“ is allowed within the where-condition. Value(p)(ssno,pname(lname,fname,mi), age) ------------------------------------ ---- person_t(023894455,name_t(´Sanchez´,´Jose´,´F´), 30) Objects and Tables ( Select-statements )

15 KarczewskiDatenbanken 215 select p.pname, p.age from people p where p.pname.fname like ‘Pat%‘= and p.age > 50; Nested dot notation is allowed. The first name starts with Pat and the age is over 50. pname(lname,fname,mi)age ------------------------ name_t(´Delaney´,´Patrick´,´X´)59 Nested dot notation can be used in any ORACLE select-statement to access an attribute of an object that is an attribute of an object... Objects and Tables ( Select-statements )

16 KarczewskiDatenbanken 216 Remark: You have to use the fully qualified name with an alias if you use an attribute accessing expression. select pname.fname from people This is not a top-level attribute -> This statement doesn´t work. select people.pname.fname from people Alias must be used because of attributes below the top level. -> This statement doesn´t work Objects and Tables ( Select-statements )

17 KarczewskiDatenbanken 217 create table scientists of name_t (primary key (lname)); Create a table with name scientists of object type name_t (defined above). insert into scientists select p.pname from people p; Insert with selection of all pnames of people. All rows of people table will be inserted. insert into scientists values (‘Einstein’, ‘Albert’, ‘E’); Insertion of one row into the table scientists with direct values. Objects and Tables ( create and insert )

18 KarczewskiDatenbanken 218 update scientists s set s = name_t(‘Eisenstein’, ‘Andrew’, ‘F’) where values(s) = name_t(‘Einstein’, ‘Albert’, ‘E’); Update of rows using object constructor name_t. insert into people values (123441998, name_t(’Einstein’, ’Albert’, ’E’), 100); Insertion of one row into the table scientist with direct values. insert into people values (321341223, null, null); insert into people values (ssno)(321341223); Equivalent statements to insert a row with null-values. Pname and age have to be optional attributes. A qualifier in the second example is not allowed. Objects and Tables ( insert )

19 KarczewskiDatenbanken 219 update people p set p.pname = name_t(‘Gould’, ‘Ben’, null) where ssno = 321341223; Update of the row with null values. The middle initial is still null. update people p set p.pname.mi = ‘C’ where ssno = 321341223; Update the middle initial of the row above. update people p set p = Person_t(332341223, name_t(‘Gould’, ‘Glen’, ‘A’), 55) where ssno = 321341223; Update of one person with row object constructor. The primary key ssno can also be updated. Objects and Tables ( update )

20 KarczewskiDatenbanken 220 User Defined Functions (UDF) can be used to bind functions to objects. The definition of UDF follows in two steps: 1.Definition of the function header 2.Definition of the function body The function header will be defined together with the attributes of the object type. The function body will be defined in a special command with a reference to the header. User Defined Functions

21 KarczewskiDatenbanken 221 Example (Persons and dependent Persons): create type name_t as object-- „Create Object Type“ ( lnamevarchar(30),-- last name michar(1),-- middle initial fnamevarchar(30)-- first name ); / create type person_t as object ( ssnoint, pnamename_t,-- must be defined first ageint ); / create type depPerson_t as table of person_t; / User Defined Functions

22 KarczewskiDatenbanken 222 Example (Function Header): create type Employee_t as object (ENRint, Personperson_t, depPersondepPerson_t, member function NumberOfDep return integer, member function BigBoss return varchar ); / The function headers will be defined after all attribute definitions. They include the key words “member function” and “return” with a data type. User Defined Functions

23 KarczewskiDatenbanken 223 Example (Function Body): create type body Employee_t as member function NumberOfDep return integer is begin return self.depPerson.count; end NumberOfDep; member function BigBoss return varchar is begin if self.depPerson.count > 2 then return 'Big Boss'; else return 'Boss'; end if; end BigBoss; end; / User Defined Functions The body of the functions get the same name as the above defined object type. The name and the return type of the function has to be written again. Within “begin” and “end” the function is defined. One return value is needed.

24 KarczewskiDatenbanken 224 Example (Table Definition): create table Employee of Employee_t ( primary key (ENR) ) nested table depPerson store as dep_tab; User Defined Functions The table definition makes the objects real! The primary key must be defined in the table definition. The “nested table” clause allows the use of tables within the table.

25 KarczewskiDatenbanken 225 Example (Insertion): insert into Employee values (1, person_t(11, name_t('Josef', 'R', 'Ewing'), 59), depPerson_T(person_t(33, name_t('Franz', 'X', 'Nonsense'), 33), person_t(44, name_t('Uschi', 'K', 'Glas'), 48), person_t(55, name_t('Mika', 'L', 'Most'), 52)) ); insert into Employee values (2, person_t(22, name_t('Karla', 'M', 'Hut'), 34), depPerson_T(person_t(66, name_t('Hans', 'L', 'Moser'), 72), person_t(77, name_t('Paul', 'A', 'Popp'), 41)) ); User Defined Functions Insertions will be made like in the examples before!

26 KarczewskiDatenbanken 226 Example (Selection): select ENR, E.NumberOfDep() from Employee E; ENR E.NUMBEROFDEP() ---------- --------------- 1 3 2 2 The value of the result of the function (method) will be shown as an own column. The alias is mandatory! User Defined Functions Example (Selection): select ENR, E.BigBoss() from Employee E; ENR E.BIGBOSS() ---------- ------------ 1 Big Boss 2 Boss The result can go over more than one line in PL/SQL.


Download ppt "KarczewskiDatenbanken 21 Object-Relational SQL Introduction: Object-Relational SQL (ORSQL) capabilities Objects and Tables OR-Create, -Insert, -Update,"

Similar presentations


Ads by Google