Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice...

96
Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice Sophia Antipolis (UCA)

Transcript of Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice...

Page 1: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

wwwmbds-frorg

MBDS course

laquo From data bases to big data raquo

(7 lectures)

Professor Serge Miranda

Dept of Computer Science

University of Nice Sophia Antipolis (member of UCA)

Director of MBDS Master degree

(wwwmbds-frorg)

wwwmbds-frorg

Introduction to SQL3

(lecture 6)

Professor Serge Miranda

Departement of Computer Science

University of Nice Sophia Antipolis

Director of MBDS Master (wwwmbds-frorg)

3

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Contents

1Major object features of SQL3

2Analysis of values and pointerswithin SQL3

4

1 Major object features withinSQL3

5

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard

ANSI (American National Standard Institute) httpwwwansiorgX3H2 database language committe(X3 ANSI division for Information processing systems

SQL3 definition from 1991 to 1999 with 28 voting members

ISO International Standard Organization encompassing ANSI AFNOR (France)hellip and NIST (wwwncslnistgov)

4 phases for SQL3 standard

Working draft (WD)

Committee draft (CD) for open revision (196)

Draft international standard (DIS) (1298)

International standard (799)

6

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard (small history)

First laquo SEQUEL raquo (Structured English as a QuEry Language)SEQUEL 1 (1974) from System R (IBM)SEQUEL 2 (1977)

then laquo SQL raquo

SQL 1 (Ansi 1986 ISO 1987) 100 pages then revisions in 1989 laquo SQL89 raquo

With tests and validation developped by NIST (National Institute of Standards and Technology) to avoid Codasyl problems (before the relationaldata model)

SQL2 (1992 laquo SQL92 raquo) by X3H2 600 pages with 3 levelsLevel 1 laquo Entry raquo (SQL1) 2 laquo Intermediate raquo and 3 laquo Full raquo

SQL3 ( Standard in 1999 laquo SQL99 raquo) 1500 pagesLevel 1 (SQL2)hellip

7

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Towards laquo SQL4 or SQL8 raquo

SQL3-99 (Hybrid Object relational)

laquo SQL4 raquo-2003 (SQLOLB Object Language Binding OLAP)

laquo SQL5 raquo-2006 (SQLXML Xquery)

laquo SQL6 raquo-2008 (SQLJRT for Java functions)

laquo SQL7 raquo-2011 ISOIEC 90752011 Dec 2011

SQL4 SQL8

(UQL Universal Query Language for BIG DATA cf Last lecture of the course)

8

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 standard

Part1 Framework

Part2 Foundation (kernel with abstract data types)

Part3 SQLCLI (Call Level Interface)

Part4 SQLPSM (Persistent Stored Modules)

Part5 SQLBindings

Part6 SQLXA (Transaction monitor)

Part7 SQLTemporal

9

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo OR raquo (Object-Relational) data model RICE extensions of the relational data model

R

I

C

E

REUSE (Multiple inheritance or polymorphism)

Identity of objects (OID amp ROWID)

Object COMPLEXITY (SET and TUPLE constructs)

ENCAPSULATION (methods and UDF)

10

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT concept in SQL3

Duality of DATA structures for OBJECT CLASS

laquo TABLES raquo (Relations) for STRUCTURED objectsCREATE TABLEhellip with IDENTITY

laquo TYPES raquo (Domains) for ENCAPSULATED objects

CREATE TYPEhellip with OID

11

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 2: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

wwwmbds-frorg

MBDS course

laquo From data bases to big data raquo

(7 lectures)

Professor Serge Miranda

Dept of Computer Science

University of Nice Sophia Antipolis (member of UCA)

Director of MBDS Master degree

(wwwmbds-frorg)

wwwmbds-frorg

Introduction to SQL3

(lecture 6)

Professor Serge Miranda

Departement of Computer Science

University of Nice Sophia Antipolis

Director of MBDS Master (wwwmbds-frorg)

3

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Contents

1Major object features of SQL3

2Analysis of values and pointerswithin SQL3

4

1 Major object features withinSQL3

5

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard

ANSI (American National Standard Institute) httpwwwansiorgX3H2 database language committe(X3 ANSI division for Information processing systems

SQL3 definition from 1991 to 1999 with 28 voting members

ISO International Standard Organization encompassing ANSI AFNOR (France)hellip and NIST (wwwncslnistgov)

4 phases for SQL3 standard

Working draft (WD)

Committee draft (CD) for open revision (196)

Draft international standard (DIS) (1298)

International standard (799)

6

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard (small history)

First laquo SEQUEL raquo (Structured English as a QuEry Language)SEQUEL 1 (1974) from System R (IBM)SEQUEL 2 (1977)

then laquo SQL raquo

SQL 1 (Ansi 1986 ISO 1987) 100 pages then revisions in 1989 laquo SQL89 raquo

With tests and validation developped by NIST (National Institute of Standards and Technology) to avoid Codasyl problems (before the relationaldata model)

SQL2 (1992 laquo SQL92 raquo) by X3H2 600 pages with 3 levelsLevel 1 laquo Entry raquo (SQL1) 2 laquo Intermediate raquo and 3 laquo Full raquo

SQL3 ( Standard in 1999 laquo SQL99 raquo) 1500 pagesLevel 1 (SQL2)hellip

7

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Towards laquo SQL4 or SQL8 raquo

SQL3-99 (Hybrid Object relational)

laquo SQL4 raquo-2003 (SQLOLB Object Language Binding OLAP)

laquo SQL5 raquo-2006 (SQLXML Xquery)

laquo SQL6 raquo-2008 (SQLJRT for Java functions)

laquo SQL7 raquo-2011 ISOIEC 90752011 Dec 2011

SQL4 SQL8

(UQL Universal Query Language for BIG DATA cf Last lecture of the course)

8

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 standard

Part1 Framework

Part2 Foundation (kernel with abstract data types)

Part3 SQLCLI (Call Level Interface)

Part4 SQLPSM (Persistent Stored Modules)

Part5 SQLBindings

Part6 SQLXA (Transaction monitor)

Part7 SQLTemporal

9

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo OR raquo (Object-Relational) data model RICE extensions of the relational data model

R

I

C

E

REUSE (Multiple inheritance or polymorphism)

Identity of objects (OID amp ROWID)

Object COMPLEXITY (SET and TUPLE constructs)

ENCAPSULATION (methods and UDF)

10

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT concept in SQL3

Duality of DATA structures for OBJECT CLASS

laquo TABLES raquo (Relations) for STRUCTURED objectsCREATE TABLEhellip with IDENTITY

laquo TYPES raquo (Domains) for ENCAPSULATED objects

CREATE TYPEhellip with OID

11

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 3: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

wwwmbds-frorg

Introduction to SQL3

(lecture 6)

Professor Serge Miranda

Departement of Computer Science

University of Nice Sophia Antipolis

Director of MBDS Master (wwwmbds-frorg)

3

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Contents

1Major object features of SQL3

2Analysis of values and pointerswithin SQL3

4

1 Major object features withinSQL3

5

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard

ANSI (American National Standard Institute) httpwwwansiorgX3H2 database language committe(X3 ANSI division for Information processing systems

SQL3 definition from 1991 to 1999 with 28 voting members

ISO International Standard Organization encompassing ANSI AFNOR (France)hellip and NIST (wwwncslnistgov)

4 phases for SQL3 standard

Working draft (WD)

Committee draft (CD) for open revision (196)

Draft international standard (DIS) (1298)

International standard (799)

6

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard (small history)

First laquo SEQUEL raquo (Structured English as a QuEry Language)SEQUEL 1 (1974) from System R (IBM)SEQUEL 2 (1977)

then laquo SQL raquo

SQL 1 (Ansi 1986 ISO 1987) 100 pages then revisions in 1989 laquo SQL89 raquo

With tests and validation developped by NIST (National Institute of Standards and Technology) to avoid Codasyl problems (before the relationaldata model)

SQL2 (1992 laquo SQL92 raquo) by X3H2 600 pages with 3 levelsLevel 1 laquo Entry raquo (SQL1) 2 laquo Intermediate raquo and 3 laquo Full raquo

SQL3 ( Standard in 1999 laquo SQL99 raquo) 1500 pagesLevel 1 (SQL2)hellip

7

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Towards laquo SQL4 or SQL8 raquo

SQL3-99 (Hybrid Object relational)

laquo SQL4 raquo-2003 (SQLOLB Object Language Binding OLAP)

laquo SQL5 raquo-2006 (SQLXML Xquery)

laquo SQL6 raquo-2008 (SQLJRT for Java functions)

laquo SQL7 raquo-2011 ISOIEC 90752011 Dec 2011

SQL4 SQL8

(UQL Universal Query Language for BIG DATA cf Last lecture of the course)

8

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 standard

Part1 Framework

Part2 Foundation (kernel with abstract data types)

Part3 SQLCLI (Call Level Interface)

Part4 SQLPSM (Persistent Stored Modules)

Part5 SQLBindings

Part6 SQLXA (Transaction monitor)

Part7 SQLTemporal

9

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo OR raquo (Object-Relational) data model RICE extensions of the relational data model

R

I

C

E

REUSE (Multiple inheritance or polymorphism)

Identity of objects (OID amp ROWID)

Object COMPLEXITY (SET and TUPLE constructs)

ENCAPSULATION (methods and UDF)

10

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT concept in SQL3

Duality of DATA structures for OBJECT CLASS

laquo TABLES raquo (Relations) for STRUCTURED objectsCREATE TABLEhellip with IDENTITY

laquo TYPES raquo (Domains) for ENCAPSULATED objects

CREATE TYPEhellip with OID

11

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 4: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Contents

1Major object features of SQL3

2Analysis of values and pointerswithin SQL3

4

1 Major object features withinSQL3

5

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard

ANSI (American National Standard Institute) httpwwwansiorgX3H2 database language committe(X3 ANSI division for Information processing systems

SQL3 definition from 1991 to 1999 with 28 voting members

ISO International Standard Organization encompassing ANSI AFNOR (France)hellip and NIST (wwwncslnistgov)

4 phases for SQL3 standard

Working draft (WD)

Committee draft (CD) for open revision (196)

Draft international standard (DIS) (1298)

International standard (799)

6

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard (small history)

First laquo SEQUEL raquo (Structured English as a QuEry Language)SEQUEL 1 (1974) from System R (IBM)SEQUEL 2 (1977)

then laquo SQL raquo

SQL 1 (Ansi 1986 ISO 1987) 100 pages then revisions in 1989 laquo SQL89 raquo

With tests and validation developped by NIST (National Institute of Standards and Technology) to avoid Codasyl problems (before the relationaldata model)

SQL2 (1992 laquo SQL92 raquo) by X3H2 600 pages with 3 levelsLevel 1 laquo Entry raquo (SQL1) 2 laquo Intermediate raquo and 3 laquo Full raquo

SQL3 ( Standard in 1999 laquo SQL99 raquo) 1500 pagesLevel 1 (SQL2)hellip

7

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Towards laquo SQL4 or SQL8 raquo

SQL3-99 (Hybrid Object relational)

laquo SQL4 raquo-2003 (SQLOLB Object Language Binding OLAP)

laquo SQL5 raquo-2006 (SQLXML Xquery)

laquo SQL6 raquo-2008 (SQLJRT for Java functions)

laquo SQL7 raquo-2011 ISOIEC 90752011 Dec 2011

SQL4 SQL8

(UQL Universal Query Language for BIG DATA cf Last lecture of the course)

8

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 standard

Part1 Framework

Part2 Foundation (kernel with abstract data types)

Part3 SQLCLI (Call Level Interface)

Part4 SQLPSM (Persistent Stored Modules)

Part5 SQLBindings

Part6 SQLXA (Transaction monitor)

Part7 SQLTemporal

9

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo OR raquo (Object-Relational) data model RICE extensions of the relational data model

R

I

C

E

REUSE (Multiple inheritance or polymorphism)

Identity of objects (OID amp ROWID)

Object COMPLEXITY (SET and TUPLE constructs)

ENCAPSULATION (methods and UDF)

10

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT concept in SQL3

Duality of DATA structures for OBJECT CLASS

laquo TABLES raquo (Relations) for STRUCTURED objectsCREATE TABLEhellip with IDENTITY

laquo TYPES raquo (Domains) for ENCAPSULATED objects

CREATE TYPEhellip with OID

11

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 5: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

1 Major object features withinSQL3

5

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard

ANSI (American National Standard Institute) httpwwwansiorgX3H2 database language committe(X3 ANSI division for Information processing systems

SQL3 definition from 1991 to 1999 with 28 voting members

ISO International Standard Organization encompassing ANSI AFNOR (France)hellip and NIST (wwwncslnistgov)

4 phases for SQL3 standard

Working draft (WD)

Committee draft (CD) for open revision (196)

Draft international standard (DIS) (1298)

International standard (799)

6

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard (small history)

First laquo SEQUEL raquo (Structured English as a QuEry Language)SEQUEL 1 (1974) from System R (IBM)SEQUEL 2 (1977)

then laquo SQL raquo

SQL 1 (Ansi 1986 ISO 1987) 100 pages then revisions in 1989 laquo SQL89 raquo

With tests and validation developped by NIST (National Institute of Standards and Technology) to avoid Codasyl problems (before the relationaldata model)

SQL2 (1992 laquo SQL92 raquo) by X3H2 600 pages with 3 levelsLevel 1 laquo Entry raquo (SQL1) 2 laquo Intermediate raquo and 3 laquo Full raquo

SQL3 ( Standard in 1999 laquo SQL99 raquo) 1500 pagesLevel 1 (SQL2)hellip

7

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Towards laquo SQL4 or SQL8 raquo

SQL3-99 (Hybrid Object relational)

laquo SQL4 raquo-2003 (SQLOLB Object Language Binding OLAP)

laquo SQL5 raquo-2006 (SQLXML Xquery)

laquo SQL6 raquo-2008 (SQLJRT for Java functions)

laquo SQL7 raquo-2011 ISOIEC 90752011 Dec 2011

SQL4 SQL8

(UQL Universal Query Language for BIG DATA cf Last lecture of the course)

8

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 standard

Part1 Framework

Part2 Foundation (kernel with abstract data types)

Part3 SQLCLI (Call Level Interface)

Part4 SQLPSM (Persistent Stored Modules)

Part5 SQLBindings

Part6 SQLXA (Transaction monitor)

Part7 SQLTemporal

9

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo OR raquo (Object-Relational) data model RICE extensions of the relational data model

R

I

C

E

REUSE (Multiple inheritance or polymorphism)

Identity of objects (OID amp ROWID)

Object COMPLEXITY (SET and TUPLE constructs)

ENCAPSULATION (methods and UDF)

10

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT concept in SQL3

Duality of DATA structures for OBJECT CLASS

laquo TABLES raquo (Relations) for STRUCTURED objectsCREATE TABLEhellip with IDENTITY

laquo TYPES raquo (Domains) for ENCAPSULATED objects

CREATE TYPEhellip with OID

11

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 6: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard

ANSI (American National Standard Institute) httpwwwansiorgX3H2 database language committe(X3 ANSI division for Information processing systems

SQL3 definition from 1991 to 1999 with 28 voting members

ISO International Standard Organization encompassing ANSI AFNOR (France)hellip and NIST (wwwncslnistgov)

4 phases for SQL3 standard

Working draft (WD)

Committee draft (CD) for open revision (196)

Draft international standard (DIS) (1298)

International standard (799)

6

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard (small history)

First laquo SEQUEL raquo (Structured English as a QuEry Language)SEQUEL 1 (1974) from System R (IBM)SEQUEL 2 (1977)

then laquo SQL raquo

SQL 1 (Ansi 1986 ISO 1987) 100 pages then revisions in 1989 laquo SQL89 raquo

With tests and validation developped by NIST (National Institute of Standards and Technology) to avoid Codasyl problems (before the relationaldata model)

SQL2 (1992 laquo SQL92 raquo) by X3H2 600 pages with 3 levelsLevel 1 laquo Entry raquo (SQL1) 2 laquo Intermediate raquo and 3 laquo Full raquo

SQL3 ( Standard in 1999 laquo SQL99 raquo) 1500 pagesLevel 1 (SQL2)hellip

7

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Towards laquo SQL4 or SQL8 raquo

SQL3-99 (Hybrid Object relational)

laquo SQL4 raquo-2003 (SQLOLB Object Language Binding OLAP)

laquo SQL5 raquo-2006 (SQLXML Xquery)

laquo SQL6 raquo-2008 (SQLJRT for Java functions)

laquo SQL7 raquo-2011 ISOIEC 90752011 Dec 2011

SQL4 SQL8

(UQL Universal Query Language for BIG DATA cf Last lecture of the course)

8

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 standard

Part1 Framework

Part2 Foundation (kernel with abstract data types)

Part3 SQLCLI (Call Level Interface)

Part4 SQLPSM (Persistent Stored Modules)

Part5 SQLBindings

Part6 SQLXA (Transaction monitor)

Part7 SQLTemporal

9

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo OR raquo (Object-Relational) data model RICE extensions of the relational data model

R

I

C

E

REUSE (Multiple inheritance or polymorphism)

Identity of objects (OID amp ROWID)

Object COMPLEXITY (SET and TUPLE constructs)

ENCAPSULATION (methods and UDF)

10

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT concept in SQL3

Duality of DATA structures for OBJECT CLASS

laquo TABLES raquo (Relations) for STRUCTURED objectsCREATE TABLEhellip with IDENTITY

laquo TYPES raquo (Domains) for ENCAPSULATED objects

CREATE TYPEhellip with OID

11

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 7: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL standard (small history)

First laquo SEQUEL raquo (Structured English as a QuEry Language)SEQUEL 1 (1974) from System R (IBM)SEQUEL 2 (1977)

then laquo SQL raquo

SQL 1 (Ansi 1986 ISO 1987) 100 pages then revisions in 1989 laquo SQL89 raquo

With tests and validation developped by NIST (National Institute of Standards and Technology) to avoid Codasyl problems (before the relationaldata model)

SQL2 (1992 laquo SQL92 raquo) by X3H2 600 pages with 3 levelsLevel 1 laquo Entry raquo (SQL1) 2 laquo Intermediate raquo and 3 laquo Full raquo

SQL3 ( Standard in 1999 laquo SQL99 raquo) 1500 pagesLevel 1 (SQL2)hellip

7

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Towards laquo SQL4 or SQL8 raquo

SQL3-99 (Hybrid Object relational)

laquo SQL4 raquo-2003 (SQLOLB Object Language Binding OLAP)

laquo SQL5 raquo-2006 (SQLXML Xquery)

laquo SQL6 raquo-2008 (SQLJRT for Java functions)

laquo SQL7 raquo-2011 ISOIEC 90752011 Dec 2011

SQL4 SQL8

(UQL Universal Query Language for BIG DATA cf Last lecture of the course)

8

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 standard

Part1 Framework

Part2 Foundation (kernel with abstract data types)

Part3 SQLCLI (Call Level Interface)

Part4 SQLPSM (Persistent Stored Modules)

Part5 SQLBindings

Part6 SQLXA (Transaction monitor)

Part7 SQLTemporal

9

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo OR raquo (Object-Relational) data model RICE extensions of the relational data model

R

I

C

E

REUSE (Multiple inheritance or polymorphism)

Identity of objects (OID amp ROWID)

Object COMPLEXITY (SET and TUPLE constructs)

ENCAPSULATION (methods and UDF)

10

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT concept in SQL3

Duality of DATA structures for OBJECT CLASS

laquo TABLES raquo (Relations) for STRUCTURED objectsCREATE TABLEhellip with IDENTITY

laquo TYPES raquo (Domains) for ENCAPSULATED objects

CREATE TYPEhellip with OID

11

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 8: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Towards laquo SQL4 or SQL8 raquo

SQL3-99 (Hybrid Object relational)

laquo SQL4 raquo-2003 (SQLOLB Object Language Binding OLAP)

laquo SQL5 raquo-2006 (SQLXML Xquery)

laquo SQL6 raquo-2008 (SQLJRT for Java functions)

laquo SQL7 raquo-2011 ISOIEC 90752011 Dec 2011

SQL4 SQL8

(UQL Universal Query Language for BIG DATA cf Last lecture of the course)

8

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 standard

Part1 Framework

Part2 Foundation (kernel with abstract data types)

Part3 SQLCLI (Call Level Interface)

Part4 SQLPSM (Persistent Stored Modules)

Part5 SQLBindings

Part6 SQLXA (Transaction monitor)

Part7 SQLTemporal

9

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo OR raquo (Object-Relational) data model RICE extensions of the relational data model

R

I

C

E

REUSE (Multiple inheritance or polymorphism)

Identity of objects (OID amp ROWID)

Object COMPLEXITY (SET and TUPLE constructs)

ENCAPSULATION (methods and UDF)

10

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT concept in SQL3

Duality of DATA structures for OBJECT CLASS

laquo TABLES raquo (Relations) for STRUCTURED objectsCREATE TABLEhellip with IDENTITY

laquo TYPES raquo (Domains) for ENCAPSULATED objects

CREATE TYPEhellip with OID

11

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 9: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 standard

Part1 Framework

Part2 Foundation (kernel with abstract data types)

Part3 SQLCLI (Call Level Interface)

Part4 SQLPSM (Persistent Stored Modules)

Part5 SQLBindings

Part6 SQLXA (Transaction monitor)

Part7 SQLTemporal

9

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo OR raquo (Object-Relational) data model RICE extensions of the relational data model

R

I

C

E

REUSE (Multiple inheritance or polymorphism)

Identity of objects (OID amp ROWID)

Object COMPLEXITY (SET and TUPLE constructs)

ENCAPSULATION (methods and UDF)

10

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT concept in SQL3

Duality of DATA structures for OBJECT CLASS

laquo TABLES raquo (Relations) for STRUCTURED objectsCREATE TABLEhellip with IDENTITY

laquo TYPES raquo (Domains) for ENCAPSULATED objects

CREATE TYPEhellip with OID

11

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 10: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo OR raquo (Object-Relational) data model RICE extensions of the relational data model

R

I

C

E

REUSE (Multiple inheritance or polymorphism)

Identity of objects (OID amp ROWID)

Object COMPLEXITY (SET and TUPLE constructs)

ENCAPSULATION (methods and UDF)

10

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT concept in SQL3

Duality of DATA structures for OBJECT CLASS

laquo TABLES raquo (Relations) for STRUCTURED objectsCREATE TABLEhellip with IDENTITY

laquo TYPES raquo (Domains) for ENCAPSULATED objects

CREATE TYPEhellip with OID

11

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 11: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT concept in SQL3

Duality of DATA structures for OBJECT CLASS

laquo TABLES raquo (Relations) for STRUCTURED objectsCREATE TABLEhellip with IDENTITY

laquo TYPES raquo (Domains) for ENCAPSULATED objects

CREATE TYPEhellip with OID

11

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 12: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT CLASS in SQL3 with TABLE

1 laquo TABLE raquo Create tablehellipwith identity

NF2 structure (Non First Normal Form)

Attributes with VALUES and POINTERS (ROWID OID)

2 types of TABLES

laquo Object raquo table laquo CREATE TABLE hellip With Identity raquo)

ROWID (line pointer) REF type possible for columns (containingROWID)

Typical relational table

12

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 13: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OBJECT class in SQL3 withTYPE

2 laquo TYPE raquo Create type (Object class)

2 types ofhellip TYPE

(laquo VALUE raquo) TYPENo OID

(laquo OBJECT raquo) TYPE (Create type with OID ltvisiblegt)

OID for each object instance

Possible REF TYPE for attributes containing OID (instead of values) hellip

13

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 14: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

multiple hybrid inheritance (UNDER clause both for TABLES and TYPES)

Polymorphism and Templates (generic data types)

R

14

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 15: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

For tables laquo ROW ID raquo(explicit-laquo with identity raquo)

for tuple encapsulation and REF type

For TYPES laquo OID raquo (Object ID)(laquo with OID visible raquo ) for object classes

I (Duality of object data structures and hellipOID)

15

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 16: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

ADT User- defined data types laquo OBJECT ADT raquo (with OID) Object CLASS

laquo VALUE ADT raquo (without OID) basic type

Complex-object support (tables or ADT) SET amp TUPLECollection construct operator laquo SET raquo laquo LIST raquo laquo MULTISET raquo (set

of sets)

Tuple Constructcartesian product(ROW)

C TYPES (ADT) and TABLES

16

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 17: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

TABLES laquo With identity raquo (ROWID) Object CLASS

without ROWID relational table with laquo stored raquo or laquo virtual raquo attributes(laquo updatable raquo laquo read-only raquo laquo fixed raquo)

C TYPES (ADT) and TABLES

17

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 18: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

laquo Routines raquo (SQL) associated with tablesADTdata base

laquo Functions raquo (laquo Actor raquo laquo Constructor raquo or laquo destructor raquo)

laquo Procedures raquo

3 Levels of ENCAPSULATION for an ADT (like C++)(laquo Private raquo laquo Protected raquo (sub type) laquo Public raquo)

E PSM (Persistent Stored Module) STORED PROCEDURESMETHODS

18

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 19: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3

Typetable definition language

Object programming language

Query language (OR)

Communication language

Versioning language

19

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 20: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Schema definition example)

Two possibilities for an object class table or type (ADT) with ROWIDOID for objects

1 CREATE TABLEhellip with IDENTITYExample

Create Table Person

(Name char (20)

Gender char (1)

Age integer

Mate REF Person

WITH IDENTITY)

20

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 21: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

2 CREATE ltOBJECTgt TYPE (With OIDhellip)

ExampleCreate TYPE person_type with OID

(name varchar not null

gender constant

age virtual

date-of-birth date

ACTOR FUNCTION age

(P person_type) return real

DESTRUCTOR FUNCTION discard_person

End function)

21

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 22: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example)

(after create person_type)

CREATE TABLE Person(data_person person_type)

Only way to store an ADT instance in persistent memory

22

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 23: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 and ORACLE

starting with Oracle 9 version OBJECT table Create table T of type t

Example Create type person_type as object (Phellip)

Create table person of type person_type

(with OID an object table)

Distinction betwween OID (16 bytes no index no update) and ROWID (10 bytes index and updatable)

PKOID (pseudo OID on views) laquo Primary key OID raquo)PKREF object vision of relational tables

23

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 24: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) inheritance

3 ltfrom-1- Create Table Persongt

CREATE TABLE Pilot UNDER Person

(NbrHours integer)

ltUNDER for structural and operational inheritancegt

24

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 25: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Example) REF type (containing ROWIDOID)

CREATE TABLE Flight(F Char(4)

PIL REFERENCE Pilot (PL) ltforeign key containing VALUE with generic relational algebra operatorsgt

REFPIL REF Pilot ltREF type containing POINTERS (ROWID) with specific pointer chasing operators(dereferencing operator)

hellip)

25

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 26: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

2 D Example seen in 3rd manifesto (previous course) and process it in SQL3

1 Schema of a rectangle characterized by 2 points (with sidesparallel to axes)

2 Query What are the rectangles which intersect the square (00) (11) raquo

26

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 27: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

CREATE (OBJECT) TYPE RECTANGLE

with OID (x1 y1 x2 y2 real)Actor function MAKE_RECTreturn RECTANGLE

ltPoint in rectangle gt

Actor function IS_IN (xy real R RECTANGLE) if (Rx1lt=x and xlt=Rx2) and (Ry1lt=y and ylt=Ry2) then return TRUE else FALSE

ltside in rectangle gt

Actor function ONE_SIDE_IS_IN (R0 R RECTANGLE) if (IS_IN (Rx1 Ry1 R0) or IS_IN(Rx2 Ry1 R0) or IS_IN (Rx2 Ry2 R0) or IS_IN (Rx1 Ry2 R0) ) then return TRUE else FALSE

27

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 28: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

ltintersection between 2 rectangles gt

Actor function OVERLAP (R1 R2 RECTANGLE) if ONE_SIDE_IS_IN (R1 R2) then return TRUE else if ONE_SIDE_IS_IN (R2R1) then return TRUE else return FALSE

28

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 29: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 2D Exercice

Create table RECTANGLES

(datarectangle RECTANGLE)

SQL3 querySELECT

FROM RECTANGLES R

WHERE OVERLAP (R

MAKE_RECT (0101)) = TRUE

Note compare with Datersquos query in 3rd manifesto

29

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 30: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Some new features levels 2 and 3)

Definition

Manipulation

Control

Development

30

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 31: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) basic data Types

basic data typesNumbers Integer Smallint Numeric Numeric (P) Numeric (PS) Decimal (P)

Decimal (PS) (Precision Scale)

Real Double Precision and Float

Character strings Character character varying national character

Bit strings Bit Bit Varying

Date date time timestamp time range(daymonthyear)

unknown (NULL value) and boolean

31

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 32: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) New types

Enumeration

boolean

Character LOB (CLOB) binary LOB (BLOB)

Tuple construct (cartesian product) row

Collection construct set multiset and list

Distinct (ud) typesCreate distinct type euro as integer (9)

Create distinct type usadollar as integer (9)

32

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 33: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ROW construct (cartesian product)

ROW construct (expression) for TYPES or TABLES EX

Create domain address ROW (number street city zipcode) Create ROW type address (number street city zipcode )Create tabletype Pilot (hellip address ROW( number varchar (3) street varchar (12))

ROW used in INSERT with specific operator (Note laquo raquo in Oracle )ROW enables to have NF2 table (Non First Normal Form)

33

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 34: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT

ADT (Abstract Data Type)

Declaration (attributes methods) laquo type specification raquo withlaquo member raquo for methods in Oracle

1 Implementation (methods) laquo type body raquo in Oracle

Object CLASS with ADT and OID (create type with OID ) which encompasses (i) attribute specification

(ii) associated methods and routines (data base type or table) laquo functions raquo (Actor Constructor Destructor) or laquo procedures raquo

34

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 35: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Trigger

Trigger definition

CREATE TRIGGER BEFOREAFTER lteventgt of ltAttgt on ltTablegt REFERENCING OLDNEW as ltxgt ltactiongtlaquo BEFOREAFTER raquo

lteventgt = INSERT UPDATE DELETE lt storage operatorgt

35

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 36: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) Schema

Example Create table plane ltor CreateType plane with OIDgt

( P Integer

PNAME varchar (12)

PICT blob (600 K)

CONTRACT clob (60K)

REFPILOT REF Pilot ltAssigned Pilot gt

PRICE Decimal (92)

CAP SET Integer ltset of capacitiesgt

LOC SET address ROW

REVISION boolean

with identity)

36

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 37: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) JOIN

Clause from with cartesian product (CROSS JOIN) and orthogonality

EX Select From (Select From (((T1 Union T2) INTERSECT T3) EXCEPT T4))

NATURAL Join on attributes with the same name

Join (T1 join T2) with clauses using foreign key using primary key using constraint and recursive union

EXISTENTIAL and UNIVERSAL quantifiers for someall table (condition)

37

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 38: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Manipulation) Recursion (treetraversal)

Recursive queries with CYCLE clause and research mode DEPTH FIRST orBREADTH FIRST

EX Select direct and indirect flights from NICE

Create view RECURSIVE INDIR-flight (DC AC) AS(Select DC ACfrom flightunionSelect INDC OUTACFrom flight IN flight OUTWhere INAC = OUTDC and INAT lt OUTDT and INConnections lt3 ) orSearch Breadth First by DC set hellip)

Select From INDIR-flight Where DC = lsquoNicersquo

38

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 39: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

2 Critics of the double paradigm(VALUE and POINTER) withinSQL3

39

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 40: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 et Datersquos manifesto

laquo It seems clear that the correct mapping of encapsulated objects into the relational data model is that classes of objects correspond to DOMAINS (not to rows) raquo

DON CHAMBERLIN

SQL3 member for IBM April 97

(InfoDB Vol 10 number 6 p25)

40

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 41: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

OR data model in the third Datersquosmanifesto

(V1) relational data model (TCodd)

Object-relational data model (CDate)

Data structures Operators Data structures Operators

RELATIONSRelationalalgebra(SQL2)

RELATIONS SQL3

DOMAINS

CLASSES(D)(R) ICE inlaquo domains raquo with laquo (primary) domainhierarchy raquo

SpecificoperatorswithSELECT or WHERE

41

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 42: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

laquo Value raquo vs laquo POINTER raquo vslaquo Variable raquo

42

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 43: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

VALUES vs VARIABLES vs POINTERS in programming

laquo VALUE raquo Scalar which could not be modified

laquo VARIABLE raquo Every variable in computing has 3 features a NAME a VALUE and an ADDRESS

VARIABLE = ( NAME VALUE ADDRESS)

POINTER Variable which encompasses the ADDRESS of another variable as value (laquo indirection raquo)

43

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 44: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model TABLES vs RELVARS

1 RelationsTABLES (laquo values raquo) vs RelationsVARIABLES (laquo RELVARS raquo)

(DATE 95 99)

1 laquo header raquo (set of pairs ltcolumn name domain namegt) lt--gt PREDICATE laquo RELVARRELATION raquo

2 laquo bodyraquo (set of tuples of values compliant with the header) lt--gtTABLE of values with important updatability laquo TABLERELATION raquo

laquo RELATION raquo double meaning

laquo variable raquo and laquo value raquo

44

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 45: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model

laquo Declare N Integer raquo N is an integer variable whose values are Integer

laquo CREATE TABLE T raquo T is a relation variable (RELVAR) whose values are tables (TABLESRELATIONS)

45

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 46: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

2 laquo DOMAINS raquo

A laquo DOMAIN raquo is a semantical data TYPE (Codd 1970)

==gt operators could be attached (de facto ENCAPSULATION )==gt STRONG TYPING (every value belongs to a domain)

46

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 47: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Back to the relational data model (domains)

NOTES

1 No restriction on the complexity of VALUES (ex graphs of videos)

2 No limitation to attach METHODS

3 No prohibition to have a hierarchy of (primary) domains

DOMAINS = naturally OBJECT CLASS (RICE properties are inherent)

SUMMARY of DOMAINS and CLASSES

- Domains for Ted CODD laquo a semantical data type raquo

- Object CLASS laquo a data type satisfying the RICE properties raquo

- Domains for Chris Date laquo a semantical data type satisfying the RICEproperties raquo

47

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 48: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

laquo RELATIONS raquo (TABLES or RELVARS) vs laquo DOMAINS raquo

Neitherlaquo TABLES raquo nor laquo RELVARS raquo cannot play the part of laquo DOMAINS raquo

RelationsTABLES are not laquo Domains raquo

RelationsRELVARS are not laquo Domains raquo

48

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 49: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

TABLES vs DOMAINS

3RELATIONSTABLES and DOMAINS are DISTINCT

Domain potential values (laquo type raquo) vs Relationtable (Attributes) real values

RELATIONSTABLES values are time-dependantvs DOMAIN values are fixed

- STATIC Domains vs dynamic relationsTables

A laquo relationtable raquo HAS a type (le RELVAR) while a domainIS a type

49

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 50: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS

A RELVAR encompasses simpleTUPLES OF VALUES while a domain consists of COMPLEX values

EX Dcity domain contains the value laquo Paris raquo while the Relvar Dcity contains the tuple ltDcity PARISgt

RELVAR does not encapsulate values

50

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 51: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN

4 RELVARs are not good candidates to be OBJECT classes

Example of object classesRELVARS

CREATE TABLE PERSON

(ID CHAR(9)

DATE-BIRTH DATE

ADDRESS CHAR (50))

51

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 52: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs Domains (cont)

CREATE TABLE EMP UNDER PERSON

(EName CHAR(20)

Salary NUMERIC

HOBBIES SET_OF (HOBBY) lt line pointer ROWIDgt

WORK-for (COMPANY) lt line pointer ROWIDgt

METHODS

RETIREMENT () NUMERIC

52

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 53: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAINS(cont)

CREATE TABLE HOBBY

(HNAMEhellip)

CREATE TABLE COMPANY

(CNAME CHAR(20)

LOC CHAR (12))

53

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 54: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Query extension needs

Path expressionEX EMPWORK-forLOC

Embedded expressionEX EMPHOBBIESHNAME

Relation comparison operatorsEX SUBSET

Graph-traversal operators

Methods within expressions

54

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 55: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

Some attributescolumns HAVE real data types (ex CHAR (20) potential values) while others do not have (Ex hobbies REAL VALUES )

Incorrect Table from a relational DM point of view(cf 3NF) and programming point of view

Object lines can contain other objects

EX EMP objects encompass HOBBIES objects (POINTERS towards these objets)

Storage-operator anomalies on these lines )

Updating contained objects cascading

55

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 56: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

RELVAR vs DOMAIN(cont)

PB a laquo relation raquo not only contains VALUES but POINTERS

Relational algebra extension for embedded objects

Method inheritance for derived relations from algebraicoperators

(EX PROJ (SALARY COMPANY) with RETIREMENT)

Where are the DOMAINS

56

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 57: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

VALUES vs VARIABLES (cf PB RelationsTABLES vs RelationsRELVAR)

VARIABLE = (NAME Value ADDRESS)

VALUES vs POINTERS (Relational data model vs CodasylOO data model)

VALUE everlasting (EX laquo LOVE raquo) vs VARIABLE updatable and time-dependent

57

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 58: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type (cont)

Variables have addresses (NOT VALUES)

laquo address raquo type with two basic operators REFERENCING value address

in C ptr = ampv (with char v and char ptr)

in PL1 DECLARE N INTEGER

DECLARE P POINTER

P= ADDR (N)

DEREFERENCING address Value

in C A in PL1 A--gt V

58

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 59: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF type

3 object extensions of relational SQL2

ROWID REF type and laquo raquo (dereferencing op)

1 ROW ID (line pointer and tupleobject identifier) The dereferencing operator is represented by laquo raquo in SQL3 (laquo deref raquo in

Oracle)

The referencing operator is named laquo ampROW raquo in SQL3

In Oracle this operator is named laquo ref raquo Example Select ref (p) from pilot p where pname = lsquosergersquo

59

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 60: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 REF TYPE

2) an attribute with laquo REF raquo type contains POINTERS (not VALUES) ie ROWID of another relation

Example In the PILOT table the laquo refplane raquo attribute isintroduced to represent the N1 link between Pilot and Plane labelled laquo preferred plane for a given pilot raquo

refplane REF (plane_type)SCOPE FOR refplane IS planeltrefplane contains the ROWID of the preferred planegt

NOTE laquo REF raquo type in Oracle REF type is introduced within TABLES or TYPES Attached operators laquo ref raquo and laquo deref raquoOID ROWID PKIDPKREF (for object views of relational tables)

60

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 61: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Ref Type

3 Dereferencing operator noted laquo raquo

Example laquo refplane p pname raquo

Abbreviated form for

SELECT p pname

FROM plane p

WHERE pampROW = refplane

In Oracle

hellipWhere ref (p) = refplane

lsquorsquo notation possible in Oracle for

61

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 62: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Examples with SQL3 REF types

pilot (pil pilname address REFPLANE )

plane(p pname cap loc)

ltREFPLANE has a REF type laquo refplane raquo encompasses line pointers (ROWID) of planes preferred by pilotsgt

Dereferencing operator

REFX ATTRIBUTES (REFY possible)

Example REFPLANE pname loc

62

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 63: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query (using dereferencingoperator)

Q1 What are the names of the planes preferred by pilots living in Nice

63

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 64: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query example using REF type

Q1 What are the names of the planes preferred by pilots living in Nice

SELECT plrefplanepname

FROM pilot pl

WHERE pladr = Nice

64

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 65: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

65

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 66: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Q2 What are the names of the pilots who prefer an Airbus A300

SELECT plpilname

FROM pilot pl

WHERE plrefplane pname=A300

66

SQL3 query example using REF type

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 67: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

67

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 68: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 Outer-join with dereferencingop

Q3 laquo for every pilot living in Nice give the name of the pilot along with the preferred-plane name (if they have one)

SELECT plPilname plrefplane pname

FROM Pilot pl

WHERE plAdr = Nice

68

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 69: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Let us assume that each plane has a unique assigned pilot we append a new REF ndashtype attribute REFPILOT in the PLANE relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT)

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

69

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 70: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q4 laquo What are the names of the pilots who prefer a plane whose assigned pilot lives in Nice raquo

(REFPIL in Plane relation for assigned pilot

REFPLANE in Pilot relation for preferred plane)

SELECT plpilname

FROM Pilot pl

WHERE plrefplanerefpilotAdr = Nice

70

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 71: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (using laquo raquo)

We append two REF-type attributes in the Flight table REFPIL and REFP which contain ROWID of Pilots and planes used on the flight

FLIGHT ( F PIL P REFPIL REFP )

Q5 What are the names of the pilots who drive an Airbus A300

71

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 72: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 query with double join (usinglaquo raquo)

Q5 What are the names of the pilots who drive an Airbus A300

SELECT fREFpilpilname

FROM flight f

WHERE fREFppname=lsquoA300rsquo

72

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 73: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice in SQL3 (and SQL2)

Let us append REF-type attribute REFpbis (for replacement plane) in the Plane relation

pilot (pil pilname address REFPLANE )

plane(p pname cap loc REFPILOT REFPBIS)

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

73

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 74: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q6 What are the names of the pilots who drive a plane whose replacement plane is localized in Nice

(REFPBIS for ROWIDs of replacement planes)

SELECT fREFpilpilname

FROM flight f

WHERE fREFpREFpbisLOC= lsquo Nice rsquo

74

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 75: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Same query in SQL2 (using foreignkeys)

using PIL P and PBIS as foreign keys

SELECT PlPILNAME

FROM Pilot Pl Plane P1 P2 Flight F

WHERE PlPIL= FPIL AND

FP= P1P AND

P1PBIS= P2P AND

P2Loc= Nice

75

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 76: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Pilot (PIL PILNAME ADDR REFPLANE)

ltREFPLANE preferred plane for a given pilot gt

Plane (PPNAME LOC REFPBIS REFPILOT)

ltREFPILOT assigned pilot for a given plane gt

REFPBIS replacement planegt

Flight (F PIL P REFPIL REFP DC AC DT AT)

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

76

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 77: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Q7 What are the names of the pilots who prefer a B747 and drive an A300 whose replacement plane is the preferred plane of JOHN

SELECT fREFpilpilnameFROM flight fWHERE fREFpPname = lsquoA300rsquo and REFpbisREFPILOT

PILNAME= lsquoJOHNrsquo andfREFpilREFPLANEPname=lsquoB747rsquo

Note last condition could have been placed in the SELECT Clause

SELECT fREFpil -gt pilname and refplane -gt pname = lsquoB747rsquo

77

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 78: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Complete Schema

Pilot (PIL PILNAME ADDR REFPLANE REFCOPIL) ltREFPLANE preferred plane for a given pilot REFCOPIL copilotgtPlane (PPNAME LOC REFPBIS REFPILOT) ltREFPILOT assigned pilot for a given plane Pilote attitreacute drsquoun avion REFPBIS replacement planegtFlight (F PIL P REFPIL REFP DC AC DT AT)

TRAINING (PIL REFPILT PNAME DATE)ltREFPILT Pilot which followed the traininggt

78

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 79: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice1

Q8 Names of the pilots who prefer a plane whosereplacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assignedpilot lives in Paris

79

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 80: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 1

Names of the pilots who prefer a plane whose replacement plane is located in Nice and whose assigned pilot has a copilot living in Nice who received a training in 2000 to drive an A300 which is the preferred plane whose assigned pilot lives in Paris

Select PlPilname

From Pilot Pl Training T

Where

PlREFPLANEREFPBISLOC=lsquoNicersquo and REFPILOTREFCOPIL ADR=lsquoNicersquo and PlPIL=TPIL and TDATE=lsquo2000rsquo and TPname=lsquoA300rsquo and REFPLANEREFPILOTADR=lsquoParisrsquo

80

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 81: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice

Exercice 2

Invent a query using 4 times the laquo raquo operator in the WHERE clause

Select refPilPILNAME

From flight

Where refprefPilotrefplaneRefPbisPname=lsquoA300rsquo

Meaning in GB

81

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 82: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 3

Write the ODMG version of the last SQL3 schema (for the PILOT relation only)

82

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 83: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Solution ODMG for the PILOT Relation

Class Pilot (

PIL Integer

PILNAME String

ADDRESS String

ltDirect linkslt

REF lt planegt prefers INVERSE is preferred

REF ltpilotegt is-copil INVERSE is-copil

ltIndirect (reverse) linksgt

SET REF lt flightgt insures INVERSE is-insured-by

REFlt planegt is-assigned-to INVERSE assignpref

SET REF lt Traininggt is-trained INVERSE training

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 84: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice 4

Given by Don Chamberlin (SQL3 Commitee IBM) in INFO DB

SQL3 schema with 2 relations EMPLOYEE and DEPARTMENT

EMPLOYEE (E ENAME Salary D REFTdept)

lt REFdept REF type on departmentgt

DEPARTMENT (D DNAMEEMNGR Refmngr)

lt REFmngr Ref type on EMPLOYEE to contain the ROWID of the department managergt

84

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 85: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice(in SQL3 and SQL2)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

For SQL2 Replace REF Types by foreign keys

85

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 86: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice (SQL3)

Q9 What are the names (and salaries) of the employeeshaving a salary more important than their managerrsquo s one

SELECT EName ESalary

FROM EMP E

WHERE ESalary gt EREFdeptREFmngr Salary

Or

Where EREFdept REFmngr Salary ltESAL

86

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 87: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Exercice SQL2

in SQL2 (replacing REF Types by foreign keys)

SELECT EName ESalary

FROM Employee E Employee F Department D

WHERE ED = DD AND

DEMNGR = FE AND ESalarygtFSalary

87

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 88: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

Discussion on REF type

Meaning of an algebraic projection on the correspondingattribute

SELECT refplane

FROM pilot pl

WHERE plpilname = sergersquo

A REF typed attribute contains (line) POINTERS not VALUES

no relational algebraic operators

just REFERENCING OPERATOR

88

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 89: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

REF type and ROWID initialization

CREATE TABLE flight(F Char(4)PIL reference PiloteREFPIL REF Pilote)

Insert into flight (IT100 1 Nice Paris 78(Select ampROW (p) From Pilot pl whereplpil= 1 ))NOTE ampROW (ref in Oracle) laquo Referencing operator raquo associated with ROWID In Oracle the clause laquo returning REF into raquo enables to get the ROWID of a

given tupleExample

insert into pilot values (1Serge Toulouse Returning Ref (pilot) into pil-ref)insert into flight values (IT100 1 NiceParis 78 pil-ref)

89

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 90: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

General comments

RAT (laquo Record at-a-time raquo) return in the SAT (laquo Set at-a-time raquo)

In ODMG REF is a persistent pointer which enriches the C++ pointer with its reverse pointer

In SQL31 REF is a line pointer (with no reverse)

2 Dereferencing operators are alternative to JOINS

In SQL3 two paradigms coexist within TABLEs (differencewith DATErsquos approach)

laquo VALUE raquo (algebraic operatorSQL)

laquo POINTERS raquo (REFDEREF operator)90

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 91: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL EVOLUTION and paradigms

RAT paradigm SAT paradigm

VALUE paradigmPOINTER paradigm

Application development

Logical Pointer(CURSOR)

(Set at a time)(Record at a time)

Line pointer(ROWID

withrefderef)

SQL2

SQL3

(Relational data model SQL23)

(C++ CODASYLDBTG ODMGhellip)

91OAT (Object-at-a time) paradigm

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 92: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

For next webchat

92

Select eQUESTIONSFrom students e

Where emood = lsquopositiversquo

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 93: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

EXTRA SLIDES

93

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 94: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT and CONSTRUCTOR

Example

Create table pilot of type pilot-t

Insert into pilot values

(pilot-t (1 Serge address-t (MBDS Sophia +3306060606))

Update pilot p set paddress = address-t (MBDS Sophia Antipolis 0606060606) where pname = lsquoSergersquo

94

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 95: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis (UCA)

SQL3 (Definition) ADT (abstract Data Type)

CREATE TYPE lt ADT namegt ltADT bodygt

ltADT Bodygt

ltOID optiongt = WITH OID (VISIBLE)

ltsubtype clausegt = UNDER ltsupertype clausegtltMultiple inheritancegt

ltmember listgtltcolumn definitiongt ltprivate or public attributesgtltfunction declarationgt lt public operatorsgtltoperator name listgt ltoverloaded operatorsgtltequals clausegt ltless-than clausegt ltorder definitionltcast clausegt lttype conversiongt

95

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96

Page 96: Copyright Big Data Pr Serge Miranda, MBDS, Univ de Nice ...mbds-fr.org/wp-content/uploads/2019/03/lectures/l6.pdf · Part2 : Foundation (kernel with abstract data types) Part3 : SQL/CLI

Copyright Big Data 2018 Pr Serge Miranda MBDS Univ de Nice Sophia Antipolis

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

LA CERTIFICATION DU COURS sur

httpswwwDATUMACADEMYCOM

96