Download presentation
Presentation is loading. Please wait.
1
End of SQL XML April 22 th, 2002
2
Null Values If x=Null then 4*(3-x)/7 is still NULL If x=Null then x=“Joe” is UNKNOWN Three boolean values: –FALSE = 0 –UNKNOWN = 0.5 –TRUE = 1
3
Null Value Logic C1 AND C2 = min(C1, C2) C1 OR C2 = max(C1, C2) NOT C1 = 1 – C1 SELECT * FROM Person WHERE (age < 25) AND (height > 6 OR weight > 190) Semantics of SQL: include only tuples that yield TRUE
4
Null Values Unexpected behavior: SELECT * FROM Person WHERE age = 25 Some Persons are not included !
5
Testing for Null Can test for NULL explicitly: –x IS NULL –x IS NOT NULL SELECT * FROM Person WHERE age = 25 OR age IS NULL Now it includes all Persons
6
Outerjoins Explicit joins in SQL: Product(name, category) Purchase(prodName, store) SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName Same as: SELECT Product.name, Purchase.store FROM Product, Purchase WHERE Product.name = Purchase.prodName But Products that never sold will be lost !
7
Left Outerjoins Left outer joins in SQL: Product(name, category) Purchase(prodName, store) SELECT Product.name, Purchase.store FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName
8
NameCategory Gizmogadget CameraPhoto OneClickPhoto ProdNameStore GizmoWiz CameraRitz CameraWiz NameStore GizmoWiz CameraRitz CameraWiz OneClick- ProductPurchase
9
Outer Joins Left outer join: –Include the left tuple even if there’s no match Right outer join: –Include the right tuple even if there’s no match Full outer join: –Include the both left and right tuples even if there’s no match
10
XML
11
More Facts About XML Every database vendor has an XML page: –www.oracle.com/xml –www.microsoft.com/xml –www.ibm.com/xml Many applications are just fancier Websites But, most importantly, XML enables data sharing on the Web – hence our interest
12
What is XML ? From HTML to XML HTML describes the presentation: easy for humans
13
HTML Bibliography Foundations of Databases Abiteboul, Hull, Vianu Addison Wesley, 1995 Data on the Web Abiteboul, Buneman, Suciu Morgan Kaufmann, 1999 Bibliography Foundations of Databases Abiteboul, Hull, Vianu Addison Wesley, 1995 Data on the Web Abiteboul, Buneman, Suciu Morgan Kaufmann, 1999 HTML is hard for applications
14
XML Foundations… Abiteboul Hull Vianu Addison Wesley 1995 … Foundations… Abiteboul Hull Vianu Addison Wesley 1995 … XML describes the content: easy for applications
15
XML eXtensible Markup Language Roots: comes from SGML –A very nasty language After the roots: a format for sharing data Emerging format for data exchange on the Web and between applications
16
XML Applications Sharing data between different components of an application. Archive data in text files. EDI: electronic data exchange: –Transactions between banks –Producers and suppliers sharing product data (auctions) –Extranets: building relationships between companies Scientists sharing data about experiments.
17
Web Services A new paradigm for creating distributed applications? Systems communicate via messages, contracts. Example: order processing system. MS.NET, J2EE – some of the platforms XML – a part of the story; the data format.
18
XML Syntax Very simple: <db> <book> <title>Complete Guide to DB2</title> <author>Chamberlin</author> </book> < > <title>Transaction Processing</title> <author>Bernstein</author> < >Newcomer</author> </book> <publisher> <name>Morgan Kaufman</name> <state>CA</state> </publisher> </db>
19
XML Terminology tags: book, title, author, … start tag:, end tag: start tags must correspond to end tags, and conversely
20
XML Terminology an element: everything between tags –example element: Complete Guide to DB2 –example element: elements may be nested empty element: abbreviated an XML document has a unique root element well formed XML document: if it has matching tags Complete Guide to DB2 Chamberlin
21
The XML Tree db book publisher titleauthor titleauthor namestate “Complete Guide to DB2” “Chamberlin”“Transaction Processing” “Bernstein”“Newcomer” “Morgan Kaufman” “CA” Tags on nodes Data values on leaves
22
More XML Syntax: Attributes Complete Guide to DB2 Chamberlin 1998 Complete Guide to DB2 Chamberlin 1998 price, currency are called attributes
23
Replacing Attributes with Elements Complete Guide to DB2 Chamberlin 1998 55 USD Complete Guide to DB2 Chamberlin 1998 55 USD attributes are alternative ways to represent data
24
“Types” (or “Schemas”) for XML Document Type Definition – DTD Define a grammar for the XML document, but we use it as substitute for types/schemas Will be replaced by XML-Schema (will extend DTDs)
25
An Example DTD PCDATA means Parsed Character Data (a mouthful for string) <!DOCTYPE db [ ]> <!DOCTYPE db [ ]>
26
More on DTDs: Attributes <!DOCTYPE db [... <!ATTLIS book price CDATA #REQUIRED language CDATA #IMPLIED> ]> <!DOCTYPE db [... <!ATTLIS book price CDATA #REQUIRED language CDATA #IMPLIED> ]> Complete Guide to DB2 Chamberlin … Complete Guide to DB2 Chamberlin … The type: CDATA = string ID = a key IDREF = a foreign key others=rarely used Default declaration: #REQUIRED=required #IMPLIED=optional #FIXED=fixed (rarely used)
27
DTDs as Grammars Same thing as: A DTD is a EBNF (Extended BNF) grammar An XML tree is precisely a derivation tree XML Documents that have a DTD and conform to it are called valid db ::= (book|publisher)* book ::= (title,author*,year?) title ::= string author ::= string year ::= string publisher ::= string db ::= (book|publisher)* book ::= (title,author*,year?) title ::= string author ::= string year ::= string publisher ::= string
28
More on DTDs as Grammars <!DOCTYPE paper [ ]> <!DOCTYPE paper [ ]> … XML documents can be nested arbitrarily deep
29
XML for Representing Data John 3634 Sue 6343 Dick 6363 John 3634 Sue 6343 Dick 6363 row name phone “John”3634“Sue”“Dick”63436363 persons XML: persons
30
XML vs Data Models XML is self-describing Schema elements become part of the data –Relational schema: persons(name,phone) –In XML,, are part of the data, and are repeated many times Consequence: XML is much more flexible XML = semistructured data
31
Semi-structured Data Explained Missing attributes: Repeated attributes John 1234 Joe John 1234 Joe no phone ! Mary 2345 3456 Mary 2345 3456 two phones !
32
Semistructured Data Explained Attributes with different types in different objects Nested collections (no 1NF) Heterogeneous collections: – contains both s and s John Smith 1234 John Smith 1234 structured name !
33
XML Data v.s. E/R, ODL, Relational Q: is XML better or worse ? A: serves different purposes –E/R, ODL, Relational models: For centralized processing, when we control the data –XML: Data sharing between different systems we do not have control over the entire data E.g. on the Web Do NOT use XML to model your data ! Use E/R, ODL, or relational instead.
34
Data Sharing with XML: Easy Data source (e.g. relational Database) Application Web XML
35
Exporting Relational Data to XML Product(pid, name, weight) Company(cid, name, address) Makes(pid, cid, price) productcompany makes
36
Export data grouped by companies GizmoWorks Tacoma gizmo 19.99 … Bang Kirkland gizmo 22.99 … … GizmoWorks Tacoma gizmo 19.99 … Bang Kirkland gizmo 22.99 … … Redundant representation of products
37
The DTD
38
Export Data by Products Gizmo GizmoWorks 19.99 Tacoma Bang 22.99 Kirkland … OneClick … Gizmo GizmoWorks 19.99 Tacoma Bang 22.99 Kirkland … OneClick … Redundant Representation of companies
39
Which One Do We Choose ? The structure of the XML data is determined by agreement, with our partners, or dictated by committees –Many XML dialects (called applications) XML Data is often nested, irregular, etc No normal forms for XML
40
Storing XML Data We got lots of XML data from the Web, how do we store it ? Ideally: convert to relational data, store in RDBMS Much harder than exporting relations to XML (why ?) DB Vendors currently work on tools for loading XML data into an RDBMS
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.