Object-Relational SQL

26
Karczewski Datenbanken 2 1 Object-Relational SQL ntroduction: Object-Relational SQL (ORSQL) capabilities bjects and Tables R-Create, -Insert, -Update, -Select ser Defined Functions

description

Object-Relational SQL. Introduction: Object-Relational SQL (ORSQL) capabilities Objects and Tables OR-Create, -Insert, -Update, -Select User Defined Functions. ORSQL capabilities. Definition of table will enlarged with user-defined types: - PowerPoint PPT Presentation

Transcript of Object-Relational SQL

Page 1: Object-Relational SQL

Karczewski Datenbanken 2 1

Object-Relational SQL

Introduction: Object-Relational SQL (ORSQL) capabilities

Objects and Tables

OR-Create, -Insert, -Update, -Select

User Defined Functions

Page 2: Object-Relational SQL

Karczewski Datenbanken 2 2

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: eid ename position dependents

dep_name dep_age

e01 Smith, John Agent Michael J. 9Susan R. 7

e02 Andrews, David Superintendent David M. Jr. 10

e03 Jones, Franklin Agent Andrew K. 11Mark W. 9Louisa M. 4

Page 3: Object-Relational SQL

Karczewski Datenbanken 2 3

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).

Page 4: Object-Relational SQL

Karczewski Datenbanken 2 4

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“(

lname varchar(10), -- last namefname varchar(10), -- first namemi char(1) -- middle initial

);/ -- SQL*Plus

Page 5: Object-Relational SQL

Karczewski Datenbanken 2 5

Objects and Tables

After making the SQL statement „create type“ you can use this newdefined type like an attribute type.

Example (Oracle):

create table teachers -- „Create Table Scheme“(

tid int, -- identifiertname name_t, -- object type defined aboveroom int, -- room number

primary key (tid));

There is still no object exisiting from object type name_typ.

Page 6: Object-Relational SQL

Karczewski Datenbanken 2 6

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

Page 7: Object-Relational SQL

Karczewski Datenbanken 2 7

Objects and Tables

After the insert-statement used above the table content can be shown as follows:

Example (Oracle):

tid

1234

tnamelname fname miEinstein Albert E

room

120

Page 8: Object-Relational SQL

Karczewski Datenbanken 2 8

Objects and Tables

To select values of the object type we use a „dot“-notation:Example (Oracle):

select t.tid from teachers t where t.room = 120; -- normal SQL-select

select t.tid, t.tname.fname, t.tname.lnamefrom teachers twhere t.room = 120;-- extended SQL

select tid, tname.fname, tname.lnamefrom teacherswhere room = 120; -- doesn´t work

We must use the alias t for table teachers in the extended SQL.

Page 9: Object-Relational SQL

Karczewski Datenbanken 2 9

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 typecreate type person_t as object (

ssno number(5),pname name_t, -- must be defined firstage number(5)

);

person_typ is dependent on name_typ, that means you cannot drop name_typ before person_typ is dropped.

Page 10: Object-Relational SQL

Karczewski Datenbanken 2 10

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));

Page 11: Object-Relational SQL

Karczewski Datenbanken 2 11

Objects and Tables

An example of a state of the table people:

nameless top-levelcolumn holding the row object

named columns (alsoknown as top-levelattributes)

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 objectscolumn objectsselect *

select value(p)

Page 12: Object-Relational SQL

Karczewski Datenbanken 2 12

select p.age from people p where p.ssno = 245882134 (like relational statements)

select p.pname from people p where p.age > 35The columns will delivered in the form name_t(´Sanchez´, ´Jose´, ´F´)

select * from people p where p.age > 35delivers the top-level attributes (row objects)

ssno pname(lname,fname,mi) age--------- ------------------------------- ----245882134 name_typ(´Delaney´,´Patrick´,´X´)59023894455 name_typ(´Sanchez´,´Jose´,´F´) 30

Objects and Tables (Select-statements)

Page 13: Object-Relational SQL

Karczewski Datenbanken 2 13

Objects and Tables (Select-statements)

select value(p) from people p where age > 35The 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.

Page 14: Object-Relational SQL

Karczewski Datenbanken 2 14

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)

Page 15: Object-Relational SQL

Karczewski Datenbanken 2 15

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)

Page 16: Object-Relational SQL

Karczewski Datenbanken 2 16

Remark: You have to use the fully qualified name with an alias if you use an attribute accessing expression.

select pname.fname from peopleThis is not a top-level attribute -> This statement doesn´t work.

select people.pname.fname from peopleAlias must be used because of attributes below the top level. -> This statement doesn´t work

Objects and Tables (Select-statements)

Page 17: Object-Relational SQL

Karczewski Datenbanken 2 17

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)

Page 18: Object-Relational SQL

Karczewski Datenbanken 2 18

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)

Page 19: Object-Relational SQL

Karczewski Datenbanken 2 19

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)

Page 20: Object-Relational SQL

Karczewski Datenbanken 2 20

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 header2. 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

Page 21: Object-Relational SQL

Karczewski Datenbanken 2 21

Example (Persons and dependent Persons):

create type name_t as object -- „Create Object Type“(

lname varchar(30), -- last namemi char(1), -- middle initialfname varchar(30)-- first name

);/

create type person_t as object (

ssno int,pname name_t, -- must be defined firstage int

);/

create type depPerson_t as table of person_t;/

User Defined Functions

Page 22: Object-Relational SQL

Karczewski Datenbanken 2 22

Example (Function Header):

create type Employee_t as object( ENR int,

Person person_t,depPerson depPerson_t,member function NumberOfDepreturn integer,member function BigBossreturn 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

Page 23: Object-Relational SQL

Karczewski Datenbanken 2 23

Example (Function Body):

create type body Employee_t asmember function NumberOfDepreturn integer isbegin

return self.depPerson.count;end NumberOfDep;

member function BigBossreturn varchar isbegin

if self.depPerson.count > 2then 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.

Page 24: Object-Relational SQL

Karczewski Datenbanken 2 24

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.

Page 25: Object-Relational SQL

Karczewski Datenbanken 2 25

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!

Page 26: Object-Relational SQL

Karczewski Datenbanken 2 26

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 Boss2 Boss

The result can go over more than one line in PL/SQL.