Presentation laddar. Vänta.

Presentation laddar. Vänta.

IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 1 IV1023 ht2013 Avancerad Datahantering med XML SQL/XML nikos dimitrakas 08-7904460 Rum 8522 Läsanvisningar.

Liknande presentationer


En presentation över ämnet: "IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 1 IV1023 ht2013 Avancerad Datahantering med XML SQL/XML nikos dimitrakas 08-7904460 Rum 8522 Läsanvisningar."— Presentationens avskrift:

1 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 1 IV1023 ht2013 Avancerad Datahantering med XML SQL/XML nikos dimitrakas Rum 8522 Läsanvisningar Kapitel 8.3 och 15 i kursboken

2 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 2 SQL/XML •Stöd för XML i relationsdatabaser enligt SQL 2003, SQL 2006 och SQL 2008 –SQL/XML: del av SQL-standarden –SQLX: gruppen bakom SQL/XML –SQLXML: Microsofts XML-tillägg för SQL •XML-datatyp •Komposition av XML (XML som resultat av SQL- SELECT-satser) •Stöd för exekvering av XQuery •Stöd för interoperabilitet mellan XQuery och SQL •Inget speciellt (ännu) om uppdatering av XML –XQuery Update Facility kom i mars 2011

3 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 3 XML-datatyp •Lagra XML, inte den serialiserade varianten (som skulle vara en vanlig CLOB) –Enligt XQuery sedan SQL 2006 •Varje cell är ett XML-dokument (eventuellt fragment) •Stöd för validering

4 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 4 SQL-funktioner •XMLELEMENT •XMLATTRIBUTES •XMLFOREST •XMLCONCAT •XMLCOMMENT •XMLPI •XMLNAMESPACES •XMLAGG •XMLTEXT •XMLDOCUMENT •XMLQUERY •XMLTABLE •XMLEXISTS •XMLSERIALIZE •XMLPARSE •XMLCAST •XMLVALIDATE

5 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 5 Exempeldata PERSON pidnameyearofbirth 1John Higgins1975 2Steven Hendry1973 3Mathew Stevens1982 4Ronnie O´Sullivan1980 5Ken Doherty1974 6Steve Davis1960 7Paul Hunter1983 8Neil Robertson1982 CAR licencenumbercolorbrandmodelyearowner ABC123blackNISSANCherry19951 CCD457blueFIATForza20012 DKL998greenSAAB9000C19983 RSQ199blackNISSANMicra19994 WID387redFIATNova20035 ROO197blueSAAB900i19823 TYD226blackNISSANCherry19901 PTF357redVOLVOV

6 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 6 Exempeldata •Kolumnen employments enligt följande DTD:

7 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 7 Exempeldata •Kolumnen employments enligt följande XML Schema:

8 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 8 Exempeldata pidemployments

9 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 9 XMLELEMENT •Skapar ett XML-element –Ange elementets namn efter NAME –Ange elementets innehåll SELECT XMLELEMENT(NAME "Person", name) FROM Person John Higgins Stephen Hendry Matthew Stevens Ronnie O'Sullivan Ken Doherty Steve Davis Paul Hunter Neil Robertson

10 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 10 XMLTEXT •Skapar en XML-textnod SELECT XMLELEMENT(NAME "Person", XMLTEXT(name)) FROM Person Samma som SELECT XMLELEMENT(NAME "Person", name) FROM Person

11 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 11 XMLDOCUMENT •Skapar en XML-dokumentnod SELECT XMLDOCUMENT(XMLELEMENT(NAME "Person", name)) FROM Person Samma som SELECT XMLELEMENT(NAME "Person", name) FROM Person

12 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 12 XMLATTRIBUTES •Skapar XML-attribut –Används inuti XMLELEMENT SELECT XMLELEMENT(NAME "Person", XMLATTRIBUTES(yearofbirth), name) FROM Person John Higgins Stephen Hendry Matthew Stevens Ronnie O'Sullivan Ken Doherty Steve Davis Paul Hunter Neil Robertson

13 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 13 XMLATTRIBUTES –Kan användas för att skapa flera attribut –Kan bestämma attributnamnen SELECT XMLELEMENT(NAME "Person", XMLATTRIBUTES(yearofbirth AS "FödelseÅr", (SELECT COUNT(*) FROM Car WHERE owner = pid) AS "AntalBilar"), name) FROM Person John Higgins Stephen Hendry Matthew Stevens Ronnie O'Sullivan Ken Doherty Steve Davis Paul Hunter Neil Robertson

14 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 14 XMLELEMENT utan innehåll –Behöver inte ha innehåll –Behöver inte ha attribut SELECT XMLELEMENT(NAME "Person") FROM Person SELECT XMLELEMENT(NAME "Person", XMLATTRIBUTES(yearofbirth, name)) FROM Person

15 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 15 XMLCONCAT •Slår ihop två XML-värden (kolumner) till ett –Varje XMLELEMENT skulle ge en kolumn (ett värde per rad) •Utan: SELECT XMLELEMENT(NAME "Namn", name), XMLELEMENT(NAME "FödelseÅr", yearofbirth) FROM Person John Higgins 1975 Stephen Hendry 1973 Matthew Stevens 1982 Ronnie O'Sullivan 1980 Ken Doherty 1974 Steve Davis 1960 Paul Hunter 1983 Neil Robertson 1982 Obs! resultatet har två kolumner

16 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 16 XMLCONCAT •Med: SELECT XMLCONCAT(XMLELEMENT(NAME "Namn", name), XMLELEMENT(NAME "FödelseÅr", yearofbirth)) FROM Person John Higgins 1975 Stephen Hendry 1973 Matthew Stevens 1982 Ronnie O'Sullivan 1980 Ken Doherty 1974 Steve Davis 1960 Paul Hunter 1983 Neil Robertson 1982 Resultatet har en kolumn

17 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 17 XMLFOREST •Skapar flera enkla element med en gång SELECT XMLFOREST(name AS "Namn", yearofbirth AS "FödelseÅr") FROM Person John Higgins 1975 Stephen Hendry 1973 Matthew Stevens 1982 Ronnie O'Sullivan 1980 Ken Doherty 1974 Steve Davis 1960 Paul Hunter 1983 Neil Robertson 1982

18 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 18 Nästla SELECT XMLELEMENT(NAME "Person", XMLATTRIBUTES(pid AS "ID"), XMLELEMENT(NAME "Info", XMLATTRIBUTES(name AS "Om"), XMLFOREST((SELECT count(*) FROM Car WHERE owner =pid) AS "Bilar", yearofbirth AS "År"))) FROM Person

19 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 19 XMLAGG •Aggregerar flera XML-värden till ett •En aggregatfunktion (som COUNT, SUM, etc.) –Antingen blir alla rader en grupp –Eller gruppering med GROUP BY SELECT XMLAGG(XMLELEMENT(NAME "Person", name)) FROM Person John Higgins Stephen Hendry Matthew Stevens Ronnie O'Sullivan Ken Doherty Steve Davis Paul Hunter Neil Robertson Obs! Hela resultatet är en rad och en kolumn (en cell)

20 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 20 XMLAGG •Och ett rotelement för att få well-formed XML SELECT XMLELEMENT(NAME "Folk", XMLAGG(XMLELEMENT(NAME "Person", name)))’ FROM Person John Higgins Stephen Hendry Matthew Stevens Ronnie O'Sullivan Ken Doherty Steve Davis Paul Hunter Neil Robertson

21 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 21 XMLAGG med GROUP BY •Ett resultat per grupp –Aggregerade vs grupperade kolumner (som vanligt) SELECT XMLELEMENT(NAME "Färg", XMLATTRIBUTES(color AS "Namn"), XMLAGG(XMLELEMENT(NAME "Bil", licencenumber))) FROM Car GROUP BY color ABC123 RSQ199 TYD226 CCD457 ROO197 DKL998 PTF357 WID387

22 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 22 XMLAGG med GROUP BY SELECT XMLELEMENT(NAME "Färg", XMLATTRIBUTES(color AS "Namn"), XMLAGG(XMLELEMENT(NAME "Bil", XMLFOREST(licencenumber AS "Regnr", name AS "Ägare")))) FROM Car, Person WHERE owner = pid GROUP BY color ABC123 John Higgins RSQ199 Ronnie O'Sullivan TYD226 John Higgins CCD457 Stephen Hendry ROO197 Ken Doherty DKL998 Matthew Stevens PTF357 Steve Davis WID387 Matthew Stevens

23 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 23 XMLAGG med GROUP BY SELECT XMLELEMENT(NAME "Bilfärger", XMLAGG(färgxml)) FROM (SELECT XMLELEMENT(NAME "Färg", XMLATTRIBUTES(color AS "Namn"), XMLAGG(XMLELEMENT(NAME "Bil", XMLFOREST(licencenumber AS "Regnr", name AS "Ägare")))) AS färgxml FROM Car, Person WHERE owner = pid GROUP BY color) AS inretabell ABC123 John Higgins RSQ199 Ronnie O'Sullivan TYD226 John Higgins CCD457 Stephen Hendry ROO197 Ken Doherty DKL998 Matthew Stevens PTF357 Steve Davis WID387 Matthew Stevens

24 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 24 Resultatet indenterat ABC123 John Higgins RSQ199 Ronnie O'Sullivan TYD226 John Higgins CCD457 Stephen Hendry ROO197 Ken Doherty DKL998 Matthew Stevens PTF357 Steve Davis WID387 Matthew Stevens

25 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 25 XMLCOMMENT •Skapar XML-kommentarer SELECT XMLCOMMENT('Vilket väder...') FROM Person

26 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 26 XMLCOMMENT dynamisk SELECT XMLELEMENT(NAME "Folk", XMLAGG(XMLCONCAT(XMLCOMMENT('Nu kommer elementet för person med pid=' CONCAT pid), XMLFOREST(name AS "Person")))) FROM Person John Higgins Stephen Hendry Matthew Stevens Ronnie O'Sullivan Ken Doherty Steve Davis Paul Hunter Neil Robertson

27 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 27 Resultatet indenterat John Higgins Stephen Hendry Matthew Stevens Ronnie O'Sullivan Ken Doherty Steve Davis Paul Hunter Neil Robertson

28 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 28 XMLPI •Skapar XML processing instructions SELECT XMLPI(NAME "hej", 'till="alla glada"') FROM Person

29 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 29 XMLNAMESPACES •Skapar namespaces inuti ett element –Anges i XMLELEMENT före XMLATTRIBUTES SELECT XMLELEMENT(NAME "iv1023:Data", XMLNAMESPACES('http://ns.kth.se/iv1023' AS "iv1023"), XMLFOREST(COUNT(*) AS "iv1023:Personer", (SELECT COUNT(*) FROM Car) AS "iv1023:Bilar")) FROM Person 8 8

30 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 30 XMLNAMESPACES DEFAULT SELECT XMLELEMENT(NAME "Data", XMLNAMESPACES(DEFAULT 'http://ns.kth.se/iv1023'), XMLFOREST(COUNT(*) AS "Personer", (SELECT COUNT(*) FROM Car) AS "Bilar")) FROM Person 8 8

31 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 31 XMLPARSE •Omvandlar ett serialiserat XML-värde till XML –DOCUMENT eller CONTENT XMLPARSE(DOCUMENT ' ') Obs! XML ' Obs! STRING

32 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 32 XMLSERIALIZE •Serialiserar ett XML-värde enligt en angiven datatyp –DOCUMENT eller CONTENT (default) –Kan generera XML-deklarationen XMLSERIALIZE(xmlvärde AS CLOB) XMLSERIALIZE(xmlvärde AS CLOB INCLUDING XMLDECLARATION)

33 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 33 XMLCAST •Omvandlar värden från/till XML XMLCAST(värde AS datatyp)

34 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 34 XMLVALIDATE •Validerar ett XML-värde enligt ett schema –Schemat kan anges i funktionsanropet –Schemat kan anges inuti XML-värdet •Returnerar det validerade XML-värdet –Eller ett fel XMLVALIDATE(xmlvärde ACCORDING TO xmlschema) XMLVALIDATE(xmlvärde)

35 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 35 XMLQUERY •Exekverar XQuery-uttryck –Returnerar XML SELECT XMLQUERY('for $a in (1, 2, 3) return element Nummer {$a}') FROM Person WHERE pid =

36 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 36 XMLQUERY –Kan skicka kolumnvärden som variabler SELECT name, XMLQUERY('for $a in return element Arbetsgivare {$a}' PASSING employments AS "e") FROM Person John Higgins ABB UPC Stephen Hendry ABB UPC Matthew Stevens UPC Ronnie O'Sullivan LKP STG Ken Doherty LKP ABB FFD STG Steve Davis ABB LKP FFD Paul Hunter FFD LKP Neil Robertson UPC ABB

37 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 37 XMLQUERY SELECT name, PASSING employments AS "e") AS AntalArbetsgivare FROM Person John Higgins 2 Stephen Hendry 2 Matthew Stevens1 Ronnie O'Sullivan2 Ken Doherty4 Steve Davis3 Paul Hunter2 Neil Robertson2

38 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 38 XMLTABLE •Skapar en tabell ur ett XQuery-resultat SELECT * FROM XMLTABLE('for $a in (2, 5, 9) return element Resultat {$a}') 2 5 9

39 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 39 XMLTABLE implicit join SELECT name, t2.* FROM Person, XMLTABLE('for $a in return element Arbetsgivare {$a}' PASSING employments AS "e" ) AS t2 John Higgins ABB John Higgins UPC Stephen Hendry ABB Stephen Hendry UPC Matthew Stevens UPC Ronnie O'Sullivan LKP Ronnie O'Sullivan STG Ken Doherty LKP Ken Doherty ABB Ken Doherty FFD Ken Doherty STG Steve Davis ABB Steve Davis LKP Steve Davis FFD Paul Hunter FFD Paul Hunter LKP Neil Robertson UPC Neil Robertson ABB

40 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 40 XMLTABLE kolumner SELECT name, t2.* FROM Person, XMLTABLE('for $a in $e//employment return $a' PASSING employments AS "e" COLUMNS arbetsgivare VARCHAR(10) PATH startdate DATE PATH enddate DATE PATH AS t2 NAME ARBETSGIVARE STARTDATE ENDDATE John Higgins ABB 08/20/ /28/2009 John Higgins UPC 04/15/ Stephen Hendry ABB 08/20/ /30/2003 Stephen Hendry UPC 08/01/ Stephen Hendry ABB 11/01/ Matthew Stevens UPC 01/10/ Ronnie O'Sullivan LKP 03/10/ /22/2010 Ronnie O'Sullivan STG 08/15/ Ken Doherty LKP 02/12/ /11/2003 Ken Doherty ABB 05/12/ /02/2003 Ken Doherty LKP 12/06/ /17/2005 Ken Doherty FFD 02/18/ /16/2008 Ken Doherty STG 06/02/ Steve Davis ABB 01/05/ /31/2005 Steve Davis LKP 01/15/ /22/2009 Steve Davis FFD 02/01/ Paul Hunter FFD 01/10/ /29/2008 Paul Hunter LKP 10/01/ /20/2010 Neil RobertsonUPC03/02/200610/30/2008 Neil RobertsonABB11/20/2008-

41 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 41 XMLTABLE SELECT name AS Namn, COUNT(*) AS AntalJobbJustNu FROM Person, XMLTABLE('for $a in return $a' PASSING employments AS "e" ) AS t2 GROUP BY name Namn AntalJobbJustNu John Higgins 1 Ken Doherty 1 Matthew Stevens 1 Ronnie O'Sullivan 1 Stephen Hendry 2 Steve Davis 1 Neil Robertson1 SELECT t2.* FROM Person, XMLTABLE('for $a in return $a' PASSING employments AS "e" ) AS t2 WHERE name = 'Stephen Hendry'

42 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 42 XMLTABLE SELECT name, startdate FROM Person, XMLTABLE('for $a in return $a' PASSING employments AS "e" COLUMNS arbetsgivare VARCHAR(10) PATH '.', startdate VARCHAR(10) PATH AS t2 WHERE arbetsgivare = 'UPC' John Higgins Stephen Hendry Matthew Stevens

43 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 43 XMLTABLE FOR ORDINALITY •FOR ORDINALITY –Speciell kolumn som numrerar raderna i resultatet av varje XMLTABLE-anrop SELECT name, ordnr, arbetsgivare FROM Person, XMLTABLE('for $a in distinct- return $a' PASSING employments AS "e" COLUMNS ordnr FOR ORDINALITY, arbetsgivare VARCHAR(5) PATH 'string()') AS t2 John Higgins 1 ABB Stephen Hendry 1 ABB Ronnie O'Sullivan 1 LKP Ken Doherty 1 LKP Ken Doherty 2 ABB Ken Doherty 3 FFD Steve Davis 1 ABB Steve Davis 2 LKP Paul Hunter 1 FFD Paul Hunter 2 LKP Neil Robertson1UPC

44 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 44 XMLEXISTS •Kollar om resultatet av ett XQuery-uttryck är tomt SELECT name FROM Person WHERE PASSING employments AS "e") John Higgins Stephen Hendry Ken Doherty Steve Davis Neil Robertson

45 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 45 Stöd i DBMS •Fullt stöd enligt SQL 2003 och delvis enligt SQL 2006 och SQL 2008 –IBM DB2 10 (inte riktigt fullt stöd för XPath) –Oracle 11g R2, 12c –PostgreSQL 9 (begränsat stöd) –OpenLink Virtuoso (begränsat stöd) –Sybase SQL Anywhere 12 (begränsat stöd) •Alternativa sätt –Microsoft

46 IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 46 Fortsättning •Quiz om SQL/XML •Lektionsuppgifter •Seminarieuppgifter (Inlupp 1)


Ladda ner ppt "IV1023 ht2013 nikos dimitrakas KTH/ICT/SCS 1 IV1023 ht2013 Avancerad Datahantering med XML SQL/XML nikos dimitrakas 08-7904460 Rum 8522 Läsanvisningar."

Liknande presentationer


Google-annonser