44
SQL SQL - - 99 99 XQUERY: an XML Query Language XQUERY: an XML Query Language INF3100/INF4100, V’2005, W7-A Chapter 9 Edited By M. Naci Akkøk 3/3-2003, 1/3-2004 and 23/02-2005. Based upon slides by Pål Halvorsen (5/3-2002). Contains slides made by Arthur M. Keller, Vera Goebel

SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

SQLSQL--9999XQUERY: an XML Query LanguageXQUERY: an XML Query Language

INF3100/INF4100, V’2005, W7-AChapter 9

Edited By M. Naci Akkøk 3/3-2003, 1/3-2004 and 23/02-2005.Based upon slides by Pål Halvorsen (5/3-2002).

Contains slides made byArthur M. Keller, Vera Goebel

Page 2: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 2

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

OverviewOverview

SQL-99user-defined types (UDTs)methods for UDTsdeclarationsreferencesoperations

A brief example of XML query languages: XQuery

Page 3: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 3

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQL DevelopmentSQL DevelopmentSQL-86 SQL-89 (SQL1) SQL-92 (SQL2):

executable DDLouter joincascaded update and deletetemporary tableset operations: union, intersection, differencedomain definitions in schemesnew built-in data types dynamic SQL via PREPARE andEXECUTE statementstransaction consistency levelsdeferred constraint lockingscrolled cursorsSQL diagnostics

SQL-99 (SQL3): SQL-92 + extensions

SQL1SQL2

SQL3

NOTE 2:NOTE 2:we are focusing on some of these extensions today

NOTE 1:NOTE 1:SQL-99 contains the functions from SQL-92

Page 4: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 4

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:UserUser--Defined TypesDefined Types

Relations are still the core abstraction. Classes from ODL are “translated” into User-Defined Types (UDTs).

SQL allows UDTs that play a dual role:1. They can be the types of relations (tables), i.e.,

the type of their tuple (sometimes called a row type).2. They can be the type of an attribute in a relation.

Page 5: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 5

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:Defining Defining UDTsUDTs

UDTs are analogous to ODL class declarations, butkey declarations are not part of the UDT – it is part of the table declarationrelationships are not properties – it must be represented by own tables

A simple form of UDTs consists ofkeyword CREATE TYPEnamekeyword ASa parenthesized, comma-separated list of attribute-type paira comma-separated list of methods including argument and return type

Syntax: CREATE TYPE T AS( < list of attribute-type pair>)< list of methods>;

Page 6: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 6

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:BarBar––BeerBeer––Sell (BBS) Example: Defining Sell (BBS) Example: Defining UDTsUDTs

CREATE TYPE BarType AS(

name CHAR(20),addr CHAR(20)

);

CREATE TYPE BeerType AS(

name CHAR(20),manf CHAR(20)

);

NOTE 1:NOTE 1:keyword CREATE TYPE

NOTE 2:NOTE 2:a name of the UDT

NOTE 3:NOTE 3:keyword AS

NOTE 4:NOTE 4:parenthesized, comma-separated list of attribute-type pair

NOTE 5:NOTE 5:additionally we may have methods(will be added later)

Page 7: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 7

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:Creating Tables Creating Tables –– II

UDTs do not declare relations, but we might declare one (or more) relations whose tuples are the type of an UDT.A simple form of relations defined from a UDT consists of

keyword CREATE TABLEnamekeyword OFname of UDT

Syntax: CREATE TABLE S OF T

A relation must declare a key as it is not part of the UDTSyntax: CREATE TABLE S OF T (

PRTIMARY KEY(<list of key attributes>));

Page 8: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 8

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:BBS Example: Creating TablesBBS Example: Creating Tables

CREATE TYPE BarType AS( name CHAR(20),

addr CHAR(20));CREATE TYPE BeerType AS( name CHAR(20),

manf CHAR(20));CREATE TABLE Bars OF BarType( PRIMARY KEY (name));CREATE TABLE Beers OF BeerType( PRIMARY KEY (name));

NOTE 1:keyword OF and name of UDTs are used in place of element lists in CREATE TABLEstatements

NOTE 2:primary key is defined by the keywords PRIMARY KEY followed by a parenthesized, comma-separated list of key attributes

NOTE 3:other elements of a table declaration may be added similarly, e.g., foreign keys, tuple based constrains, etc., which apply to this table only, not UDT

NOTE 4:usually we have one relation per UDT, but we may have several

Page 9: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 9

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:References References –– II

If a table is created using a UDT, we may have a reference column serving as an identity

it can serve as a primary keycan be a system generated, unique value

To refer to tuples in a table with a referece column, an attribute may have as type a reference to another type.

If T is a UDT, then REF(T) is the type of a reference to a T object.Unlike OODBS, references are values that can be seen by queries.

Page 10: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 10

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:References References –– IIII

For a reference attribute to be able to refer to a relation, it must be referenceable.A table is made referenceable by including a clause in the table declaration (this not part of the UDT).Syntax: REF IS <attribute name> <generated>The <attribute name> will serve as the object identifierThe <generated> is telling how the id is generated, either:

1. SYSTEM GENERATED, the DBMS maintains an unique value in this column for each tuple

2. DERIVED, the DBMS use the primary key of the relation to produce unique values for each tuple

Page 11: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 11

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:BBS Example: References BBS Example: References –– II

CREATE TYPE BarType AS (name CHAR(20),addr CHAR(20),bestSeller REF(BeerType) SCOPE Beers

);CREATE TYPE BeerType AS (

name CHAR(20),manf CHAR(20)

);CREATE TABLE Bars OF BarType (

PRIMARY KEY (name));CREATE TABLE Beers OF BeerType (

REF IS beerID SYSTEM GENERATEDPRIMARY KEY (name)

);

NOTE 1:NOTE 1:bestSeller is a reference to a BeerType object

NOTE 2:NOTE 2:bestSeller must refer to objects in the Beers relation whose type is BeerType

NOTE 3:NOTE 3:the relation Beers must be referenceable

NOTE 4:NOTE 4:the “ID” is system generated

NOTE 5:NOTE 5:only single references is possible this way, not set

Page 12: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 12

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:BBS Example: References BBS Example: References –– IIII

CREATE TYPE BarType AS( name CHAR(20),

addr CHAR(20));

CREATE TYPE BeerType AS( name CHAR(20),

manf CHAR(20));

CREATE TYPE MenuType AS( bar REF(BarType),

beer REF(BeerType));

NOTE 1:Bars sell beers (and beers are sold at bars), but we cannot directly represent this SET relationship in type bar and beer as in ODL

NOTE 3:if the relationship has properties as price, we even in ODL must have an own class

NOTE 2:we need an own relation to represent such sets, with references to the two types (possibly with a scope)

Bars BeersSell

price

Sell

Page 13: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 13

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:References References –– IIIIII

References may be given a scope, i.e., the name of the relation to whose tuples are referred toSyntax: S REF(T) SCOPE R -- (an attribute S of type REF(T) referring to a tuple in relation R)If no scope is given, the reference can go to tuples of any relation of type TExample

CREATE TYPE MenuType AS (bar REF(BarType) SCOPE Bars,beer REF(BeerType) SCOPE Beers,price FLOAT

);

NOTE:NOTE:Bars and Beers are relations defined using the BarType and BeerType, respectively

Page 14: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 14

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:Methods Methods –– II

UDTs can have associated methods working on an object whose type is the UDT (applied on tuples)

Similar to Persistent Stored Modules (PSM), which are general purpose functions allowed to be stored together with the schema and used in SQL (described in Chapter 8), but methods are

declared in the UDT using a METHOD clausedefined separate in a CREATE METHOD statement

Page 15: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 15

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:Methods Methods –– II II

There is a special tuple variable SELF that refers to that object to which the method is applied, i.e., can use SELF.a to access the object attribute aIn the method declaration, arguments need a mode, like IN, OUT, or INOUT, but the mode does not appear in the definition.Many methods will take no arguments (relying on SELF)All methods return a value of some typeA method is applied using “dot”, e.g., t.updatePrice(...)

Page 16: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 16

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:Methods: DeclarationMethods: Declaration

A declaration of a method for a UDT consists ofkeyword METHODname of the methodkeyword RETURNSthe return type

Declaration syntax: METHOD <name> RETURNS <return type>;

Page 17: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 17

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:Methods: Definitions Methods: Definitions

A declaration of a method for a UDT consists ofkeywords CREATE METHODname of the method including arguments and their typekeyword RETURNS and the return type keyword FOR and the name of the UDT in which the method is declaredbody of the method (as PSM functions)

Definition syntax (body): CREATE METHOD <name> RETURNS <return type> FOR <name of UDT>BEGIN

<method body>END

Page 18: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 18

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 Example Methods:99 Example Methods:Declarations and Definitions Declarations and Definitions –– I I

Example:CREATE TYPE MenuType AS( bar REF(BarType) SCOPE Bars,

beer REF(BeerType) SCOPE Beers,price FLOAT

)METHOD updatePrice( IN p float) RETURNS BOOLEAN;

CREATE METHOD updatePrice( p float) RETURNS BOOLEAN FOR MenuTypeBEGIN

<body>END;

NOTE 1:Declaration in UDT

NOTE 2:Definition separately, outside the UDT

NOTE 3:parameters, mode only in declaration

NOTE 4:the body is written in the same language as the PSM functions, e.g., SQL/PSM used in the book

NOTE 5:bar, beer unnecessary, can use built-in SELF

NOTE 6:p necessary, as it is used to change the value of the price attribute, e.g., p is added to SELF.price

Page 19: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 19

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:New OperationsNew Operations

All appropriate SQL operations applying to tables defined using UDTs are allowed, but there are also some new features:

using referencesaccessing UDT attributescreating UDT objectsorder relationships

Page 20: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 20

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 99 –– New Operations: New Operations: Following References Following References –– II

If x is a value of type REF(T), then x refers to some tuple t of type TThe attributes of tuple t can be obtained by using the -> operator

essentially as in Cif x is a reference to tuple t and a is an attribute in t, then x->a is the value if attribute a in t

Example: Find the beers served at “Joe’s”SELECT beer->nameFROM SellsWHERE bar->name = 'Joe''s';

NOTE 1:Sells is a table with menuType as type

NOTE 3:single-quoted strings

NOTE 2:the attributes of a tuple is accessed using the -> operator

Page 21: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 21

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 99 –– New Operations: New Operations: Following References Following References –– IIII

The tuple t can be obtained by using the DEREF operator if xis a referenceExample: Find the bars (all attributes) serving “Bud”

SELECT DEREF(bar)FROM SellsWHERE beer->name = 'Bud';

NOTE 1:Bar is reference to a tuple in table Bars

NOTE 2:DEREF(bar) gets the referenced tuples

NOTE 3:SELECT bar, without DEREF, would return only system-generated values serving as the ID of the tuples – not the information in the tuples self

SELECT barFrom SellsWhere beer->name = ‘Bud’;

Page 22: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 22

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 99 –– New Operations: New Operations: Accessing UDT AttributesAccessing UDT Attributes

A tuple defined by an UDT is analogous to an object – not a list of components corresponding to the attributes of an UDTExample:the relation bars is defined using the UDT barType

this UDT has two attributes, i.e., name and addr, a tuple t in bars has only one component, i.e., the object itself

Every UDT has implicitly defined observer methods for each attribute. x() is the name of the observer method for an attribute xreturns the value of attribute x in the UDTis applied as all other methods on this UDT, i.e., using “dot”if t is of type T and x is an attribute of T, then t.x() is the value of xin t

Page 23: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 23

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 99 –– New Operations: New Operations: Creating Data ElementsCreating Data Elements

Generator methods create objects of type T:same name as the UDT itself, i.e., T()takes no argumentsinvoked without being applied to objectsreturns an object of type T with no values in the various components

Page 24: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 24

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 99 –– New Operations: New Operations: Updating Data ElementsUpdating Data Elements

Mutator methods update attributes in objects of type T:for each attribute x in T, there is a mutator method x(v)when applied to an object T, x(v) changes the value of xto v

Note: both mutator (x(v)) and observer (x()) methods have the same name, but only a mutator method has a parameter

Page 25: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 25

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 99 –– New Operations Example:New Operations Example:Creating and Updating Data ElementsCreating and Updating Data Elements

Example: PSM procedure inserting new bars into the Bars relationCREATE PROCEDURE insertBar (

IN n CHAR(20),IN a CHAR(20)

)DECLARE newBar BarType;BEGIN

SET newBar = BarType();newBar.name(n);newBar.addr(a);INSERT INTO Bars VALUES(newBar);

END;

NOTE 2:NOTE 2:declaration of a variable of type BarType

NOTE 3:NOTE 3:newBar is assigned a value of an empty BarType object using the BarType() generator method

NOTE 4:NOTE 4: we apply mutator methods for the attributes in BarType UDT, i.e,

and , on the name(n) addr(a) newBarobject using “dot” notation

NOTE 5:NOTE 5: we insert the object newBar of type BarType into the table Bars. NB! Simpler ways may exist to insert objects

NOTE 1:NOTE 1:the UDT BarType has two attributes, i.e., name and addr, which are parameters

Page 26: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 26

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 99 –– New Operations: New Operations: Comparing Objects Comparing Objects –– II

There are no operations to compare two objects whose type is some UDT by default, i.e, we cannot

eliminate duplicatesuse WHERE clausesuse ORDER BY clauses

SQL-99 allows to specify comparison or ordering using CREATE ORDERING statements for UDTs

Page 27: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 27

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 99 –– New Operations: New Operations: Comparing Objects Comparing Objects –– II II

Equality for an UDT named T:CREATE ORDERING FOR T EQUALS ONLY BY STATE(all corresponding components have the same value)

Apply all comparison operators for an UDT named T:CREATE ORDERING FOR T ORDERING FULL BY RELATIVE WITH F(all comparison operatots - <, <=, >, >=, =, and <> - may be applied on two objects using an integer function F which must be implemented separately)

Example: < : F(x1, x2) < 0 if x1 < x2> : F(x1, x2) > 0 if x1 > x2= : F(x1, x2) = 0 if x1 = x2etc.

Page 28: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 28

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:UDTsUDTs (revisited) (revisited) –– Type of a ColumnType of a Column

A UDT can also be the type of a column.Example:Let’s create an address type to use in bars (replacing the string)

CREATE TYPE AddrType AS (street CHAR(30),city CHAR(20),zip INTEGER

);

CREATE TYPE BarType AS (name CHAR(20),addr AddrType

);

Problem: how can we sort all bars alphabetically? Need a way to compare the objects

NOTE 1:NOTE 1:the addr attribute of the UDT BarType has changed to an own UDT – composite types

Page 29: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 29

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 99 –– New Operations:New Operations:Comparing Objects (revisited) Comparing Objects (revisited) –– alphabetical orderingalphabetical ordering

First, the UDT AddrType:CREATE ORDERING FOR AddrTypeORDER FULL BY RELATIVE WITH AddrComp;

CREATE FUNCTION AddrComp (IN x1 AddrType,IN x2 AddrType

) RETURNS INTEGERIF x1.city() < x2.city() THEN RETURN(-1)ELSEIF x1.city() > x2.city() THEN RETURN(1)ELSEIF x1.street() < x2.street() THEN RETURN(-1)ELSEIF x1.street() > x2.street() THEN RETURN(1)ELSERETURN(0) END IF;

NOTE 1:NOTE 1:all comparison operators may be appliedNOTE 2:NOTE 2:comparison is performed in function AddrCompNOTE 3:NOTE 3:we first compare city, if equal we look at street

NOTE 4:NOTE 4: if x1.a > x2.a return 1

NOTE 5:NOTE 5: if x1.a < x2.a return -1NOTE 6: if all x1.a = x2.a return 0NOTE 7:NOTE 7: has to use observer methods to get value

Page 30: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 30

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 99 –– New Operations: New Operations: Comparing Objects (revisited) Comparing Objects (revisited) –– alphabetical orderingalphabetical ordering

Second, the UDT BarType:CREATE ORDERING FOR BarTypeORDER FULL BY RELATIVE WITH BarComp;

CREATE FUNCTION BarComp (IN x1 BarType,IN x2 BarType

) RETURNS INTEGERIF x1.name() < x2.name() THEN RETURN(-1)ELSEIF x1.name() > x2.name() THEN RETURN(1)ELSEIF x1.addr() < x2.addr() THEN RETURN(-1)ELSEIF x1.addr() > x2.addr() THEN RETURN(1)ELSERETURN(0) END IF;

NOTE 1:NOTE 1:all comparison operators may be applied

NOTE 2:NOTE 2:we first compare name, if equal we look at addr

NOTE 3:NOTE 3:as the addr itself is a UDT, it will again use the its own comparison function AddrComp

Page 31: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 31

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99 BBS Example: Using Methods 99 BBS Example: Using Methods –– I I

NOTE 1:NOTE 1:tip is given in percent

NOTE 2:NOTE 2:the value returned is the price, found by using SELF, increased by p percent (FLOAT)

NOTE 3:NOTE 3:create table sells fromUDT MenuType

Example: add method for retrieving price including tipCREATE TYPE MenuType AS (

bar REF(BarType) SCOPE Bars,beer REF(BeerType) SCOPE Beers,price FLOAT

)METHOD priceTip (IN p float) RETURNS FLOAT;

CREATE METHOD priceTip (p float) RETURNS FLOAT FOR MenuTypeBEGIN

RETURN (1 + p) * SELF.price;END;CREATE TABLE Sells OF MenuType;

Page 32: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 32

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SQLSQL--99:99:BBS Example: Using Methods BBS Example: Using Methods –– IIII

Example:find beers and price with and without tip on “Joe’s” barSELECT s.beer->name(),s.price(),s.priceTip(15)FROM Sells sWHERE s.bar->name() = ‘Joe’’s’

NOTE 1:NOTE 1:Renamingallowed

11

NOTE 2:NOTE 2:since beer and bar are references we have to use

22

22

NOTE 3:NOTE 3:bar is a reference to an object whose type is an UDT. However, the value

33

44

4455 55

the -> operator returned by the name() observer method is a text string. Thus, NO comparison operators have to be defined – use only traditional text comparison

NOTE 4:NOTE 4:since Sells objects have an UDT type and beer and bar are references to objects whose types are UDTs, we must use observer methods to retrieve the attribute values

NOTE 5:NOTE 5:methods are applied using “dot”notation

Page 33: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

XML Query Languages:XML Query Languages:XQUERYXQUERY

Page 34: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 34

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

Querying XML DataQuerying XML Data

“The goal of the XML Query WG is to produce a data model for XML documents, a set of query operators on that data model, and a query language based on these query operators.“

XML query languages: XPATH, XPOINTER, and XQUERY

XQUERY (see: http://www.w3.org/TR/xquery/):

is an emerging standard for querying XML documents

strongly influenced by OQL

XQUERY is a functional language in which a query is represented as an expression (opposed to OQL and SQL which are declarative)

XQUERY expressions can be nested

filters can strip out fields

grouping

Page 35: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 35

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

XQUERY XQUERY –– I I XQUERY provides a FLWR expression:

F – FOR: associates with variables, creating an ordered sequence of tuples drawn form the cartesian product of the variables. It iterates through a sequence of individual nodes out of the selected collection, in order, one at a timeL – LET: binds a variable directly to an entire expression – to the set of nodes in the selected collectionW – WHERE: predicates used on bound variables, used as a filter for the tuples generated by the FOR and LET clausesR – RETURN: contains an expression tat is used to construct the result from the whole FLWR expression. Invoked for every tuple generated by the FOR and LETclauses, but after eliminating any tuples in the WHEREclause

Page 36: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 36

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

XQUERY:XQUERY:FLWR expressions FLWR expressions –– I I

FLWR expressions: (FORexpr | LETexpr)+ WHEREclause? RETURNexpr

FORexpr: FOR variable IN expression (, variable IN expression )*LETexpr: LET variable := expression (, variable := expression )*WHEREexpr: WHERE expression RETURNexpr: RETURN expression

NOTE 1:NOTE 1:FOR and / or LET appear one or more times

NOTE 2:NOTE 2:WHERE clauses are optional

NOTE 3:NOTE 3:a RETRUN clause is always present

Page 37: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 37

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

XQUERY:XQUERY:FLWR Examples FLWR Examples –– I I

Example 1:LET $a := (1, 2, 3)RETURN <out>{$a}</out>

Example 2:FOR $a IN (1, 2, 3)RETURN <out>{$a}</out>

Output:<out>1 2 3</out>

Output:<out>1</out><out>2</out><out>3</out>

NOTE 1a:NOTE 1a:the variable $a is bound to the expression(1, 2, 3). LET clause generates one tuple containing the variable binding of $a

NOTE 1b:NOTE 1b:one might add tags in the output, i.e., in the RETURN clause

NOTE 2:NOTE 2:the variable $a is associated with the expression (1, 2, 3)from which the variable bindings of $a will be drawn, i.e., $a will be processed in such a way that the value of $a will be bound for each element in the expression – in this case three times

Page 38: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 38

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

XQUERY:XQUERY:FLWR Examples FLWR Examples –– II II

Example 3:FOR $a IN (1, 2),

$b IN (a, b)RETURN <out>

<a>{$a}</a> <b>{$b}</b></out>

Output:<out>1 2 3

<a>1</a> <b>a</b><a>1</a> <b>b</b><a>2</a> <b>a</b><a>2</a> <b>b</b>

</out>

NOTE 3a:NOTE 3a:we may have multiple FOR clauses, each variable associated with an expression

NOTE 3b:NOTE 3b:the tuples are drawn from the cartesianproduct of the sequence returned in FOR, i.e., cartesian product of $a and $b.

NOTE 3c:NOTE 3c:the order of the tuples are the order of which they were formed – from left to right and variable $a before $b

Page 39: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 39

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

XQUERY:XQUERY:FLWR expressions FLWR expressions –– IIII

FOR and LET clauses operates on setsSets of elements can be described by paths, consisting of:

1. URL or file name, e.g.,$ba IN document(“bars.xml”) – “bars.xml” contain data for $ba

2. elements forming a path in the semi-structured data graph, e.g., //BAR/NAME – start at any BAR node and go to a NAME child

3. ending condition of the form the path[<sub-elements conditions, @attributes, and values>], e.g.,//BAR/BEER[NAME = “Bud”] – beer elements in a bar named “Bud”//BAR[@TYPE = “Sports”] – bar elements whose type is “Sports”

4. ....

Page 40: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 40

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

XQUERY:XQUERY:BBS Example BBS Example –– II

<?XML VERSION = "1.0" STANDALONE = "no"?>

<!DOCTYPE Bars SYSTEM "bar.dtd">

<BARS><BAR type = "sports">

<NAME>Joe's</NAME><BEER><NAME>Bud</NAME>

<PRICE>2.50</PRICE></BEER><BEER><NAME>Miller</NAME>

<PRICE>3.00</PRICE></BEER></BAR><BAR type = "sushi">

<NAME>Homma's</NAME><BEER><NAME>Sapporo</NAME>

<PRICE>4.00</PRICE></BEER></BAR> ...

</BARS>

Page 41: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 41

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

XQUERY:XQUERY:BBS Example BBS Example –– IIII

Example: find the names of “sports bars” serving “Bud”FLWR Query:FOR $ba IN document(“bars.xml”)//BAR[@type = "sports"],WHERE $ba/BEER/[NAME = “Bud”]

RETURN <out>$ba/NAME/text()</out>;

NOTE 1:NOTE 1:$ba is assosiated with data present in the “bars.xml”file

NOTE 2:NOTE 2:Start at BAR nodes, i.e., select only those elements for $ba

NOTE 3:NOTE 3:Further reduce the number of elements to only those bars which is a “sports” bar

NOTE 4:NOTE 4:select only those bars from the collection $ba that have beer named “bud”

NOTE 5: NOTE 5: return the name of the bar

NOTE 6: NOTE 6: the text() function retrieves the text (name) between the name-tags inside the bar-tag

Page 42: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 42

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

XQUERY:XQUERY:BBS Example BBS Example –– IIIIII

Query: find the names of “sports bars” serving “Bud”FOR $ba IN document(“bars.xml”)//BAR[@type = "sports"],WHERE $ba/BEER/[NAME = “Bud”]RETURN <out>$ba/NAME/text()</out>;

XML-file containing data (bars.xml):<?XML VERSION = "1.0" STANDALONE = "no"?><!DOCTYPE Bars SYSTEM "bar.dtd"><BARS>

<BAR type = "sports"><NAME>Joe's</NAME><BEER><NAME>Bud</NAME><PRICE>2.50</PRICE></BEER><BEER><NAME>Miller</NAME><PRICE>3.00</PRICE></BEER>

</BAR><BAR type = "sports">

<NAME>Mary's</NAME><BEER><NAME>Miller</NAME><PRICE>3.50</PRICE></BEER>

</BAR><BAR type = "sushi">

<NAME>Homma's</NAME><BEER><NAME>Sapporo</NAME><PRICE>4.00</PRICE></BEER>

</BAR> ...</BARS>

11

11

11

22

22

33

Output:<out>Joe’s</out>

33

Page 43: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 43

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

SummarySummary

SQL-99user-defined types (UDTs) –both as relation and component typesmethods for UDTs –user defined, observer, generator, and mutatordeclarationsreferences –direct references, referencable tables, reference followingcomparison operations – must be defined for UDTs

XML Query example: XQUERY

Page 44: SQL-99 XQUERY: an XML Query Language · INF3100/4100 – Database Systems Page 3 Department of Informatics, University of Oslo, Norway M. Naci Akkøk, Spring 2005 DMMS – Distributed

INF3100/4100 – Database SystemsPage 44

Department of Informatics, University of Oslo, NorwayDMMS – Distributed Multimedia Systems GroupM. Naci Akkøk, Spring 2005

A word onA word onPointers and ReferencesPointers and References

Pointers in C, C++ and references in SQL 99 are related concepts.

If ‘printing’ the contents of X (or memory address A00F 7700) will print out 5…… then ‘printing’ the contents of RX (or memory address 8000 F400) will print out A00F 7700…… and ‘printing’ RX->X or DEREF(RX) will print 5.

MEMORYCONTENT

A00F 7700

5X

RX

MEMORYADDRESS

Var.Name

8000 F400

A00F 7700