Aprovechando XML dentro de la base de datos con SQL Server 2005
description
Transcript of Aprovechando XML dentro de la base de datos con SQL Server 2005
Aprovechando XML dentro de la base de datos con SQL Server 2005
2
SQL Server 2005 para desarrolladores Solid Quality Learning
Adolfo [email protected]
Microsoft Regional Director - http://msdn.microsoft.com/isv/rd Mentor Solid Quality Learning - http://www.solidqualitylearning.com Fundador, Costa Rica User Group .NET - http://www.crug.net Orador INETA Latinoamérica - http://www.ineta.org/latam Blog - http://www.wiernik.net
Jose Ricardo [email protected]
En Microsoft desde 1998 Desde el 2003 - Regional Program Manager SQL Server Latinoamérica
3
SQL Server 2005 para desarrolladores Solid Quality Learning
Series de Webcasts Introducción a SQL Server 2005 para desarrolladores
Viernes, 22 de Julio de 2005 06:00 p.m.(GMT)http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277969&Culture=es-MX
Nuevas características del lenguaje T-SQL en SQL Server 2005Lunes, 25 de Julio de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277973&Culture=es-MX
Aprovechando XML dentro de la base de datos con SQL Server 2005Viernes, 29 de Julio de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277975&Culture=es-MX
Programando SQL Server 2005 con el CLR – Integración SQL-CLRLunes, 01 de Agosto de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277977&Culture=es-MX
Nuevas características en ADO.NET 2.0Viernes, 05 de Agosto de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277978&Culture=es-MX
4
SQL Server 2005 para desarrolladores Solid Quality Learning
.NET FrameworkCommon Language Runtime IntegrationUser-defined AggregatesUser-defined Data TypesUser-defined FunctionsSQL Server .NET Data ProviderExtended Triggers
Data TypesManaged SQL TypesNew XML DatatypeVarchar (MAX) Varbinary (MAX)
XMLXQUERY Support XML Data Manipulation Language FOR XML EnhancementsXML Schema (XSD) Support MSXML 6.0 (Native).Net XML Framework
Full-text SearchIndexing of XML Datatype
MDAC SNACMicrosoft Installer base setup
ADO.NET 2.0Notification SupportObject Model enhancements
SQL Client .NET Data ProviderServer Cursor SupportAsynchronous ExecutionSystem.Transactions
SecuritySeparation of Users and SchemaData encryption primitives
AdministrationSQL Management Objects (SMO)Analysis Management Objects
(AMO)Replication Management Objects
(RMO)T-SQL
Recursive QueriesCommon Table ExpressionsPIVOT – UNPIVOT OperatorsAPPLY OperatorException Handling
SQL Server EngineSQL Service BrokerHTTP Support (Native HTTP)Multiple Active Result Sets (MARS)Snapshot Isolation Level
Reporting ServicesMultiple Output Formats Parameters (Static, Dynamic,
Hierarchical)Bulk Delivery of Personalized
ContentSupport Multiple Data Sources STS (Web Parts, Doc Libraries)Visual Design ToolCharting, Sorting, Filtering, Drill-
ThroughScheduling, CachingComplete Scripting EngineScale Out architectureOpen XML Report Definition
Notification ServicesSQL Server Mobile Edition
Nuevas Características para Desarrollo
5
SQL Server 2005 para desarrolladores Solid Quality Learning
Agenda
Why bring XML and Relational together? Microsoft SQL Server 2000 Recap Microsoft SQL Server 2005 XML
server-side support• Native XML storage• XML schema support• XML Querying and updating• Publishing with FOR XML• Shredding with nodes()
6
SQL Server 2005 para desarrolladores Solid Quality Learning
File SystemFile SystemFile SystemFile System
XMLXMLX
ML
XM
LXMLXML
XM
LX
ML
XML and Relational Data Today
7
SQL Server 2005 para desarrolladores Solid Quality Learning
XML
Self-describing:
<doc id="d1">This is an <important>example</important>.</doc>
Complex data• Trees, recursive, graph• Structured Data: highly regular, homogeneous structures• Semi-structured Data: heterogeneous, sparse data• Markup Data: documents/content markup
Document ordering Schema/Type system
• Schema-less• Optional Schema: semi-structured, structured
Extensible• Annotations, multiple schemas (late binding)
8
SQL Server 2005 para desarrolladores Solid Quality Learning
Relational Data
Tabular data format with a priori fixed schema Schema and data are kept separate:CREATE TABLE T (name nvarchar(50), age int)INSERT INTO T(name, age) VALUES ('Zaphod', 42)
Structured Data: highly regular, homogeneous structures Express relationships through
• Referential constraints (foreign keys)• Queries and Views
Provides efficient repurposing of information
9
SQL Server 2005 para desarrolladores Solid Quality Learning
XML Scenarios
Data Exchange• Business-to-business (B2B), business-to-consumer (B2C), application-to-
application (A2A)• XML is ubiquitous, extensible, platform independent transport format
Document Management• Office XML Documents• Data/view separation: Documents, style sheets, transforms
Messaging• Simple Object Access Protocol (SOAP)
Mid-Tier Collaboration Ad-hoc modeling
• storing objects with sparse and multi-valued properties that do not fit well in the traditional relational schemata
→→Transport, Store, and Query XML dataTransport, Store, and Query XML data
10
SQL Server 2005 para desarrolladores Solid Quality Learning
Why Bring XML and Relational Data Together?
XML for relational dataXML for relational data
Rel Rel XML XML
Native Native XML Store XML Store in RDBMSin RDBMS
+ XML dt+ XML dt+ XML dt+ XML dt
XML, relational and semi-structured dataXML, relational and semi-structured data
DocumentMgmt
Rel Rel XML XML
XML in SQL Server 2000XML in SQL Server 2000(incl SQLXML)(incl SQLXML)
XML in SQL Server 2005XML in SQL Server 2005(incl SQLXML)(incl SQLXML)
Native XML andNative XML andsemi-structured semi-structured
data DBMSdata DBMS
11
SQL Server 2005 para desarrolladores Solid Quality Learning
SQL Server as Integrated Data Platform
Provides core DB services to BOTH XML and Relational Data:• Base Services
• Concurrency Control• Recovery• Declarative Query and Update language• Execution Engine and Optimizer
• Tools to repurpose and combine data• Shared management and deployment• Integration with BI tools
12
SQL Server 2005 para desarrolladores Solid Quality Learning
XML or Relational?
Data Characteristics XML Relational
Flat Structured Data
Hierarchical Structured Data
Not First Class: PK-FK with cascading delete
Semi-structured Data Not First Class
Mark-up Data Not First Class: FTS
Order preservation Not First Class
Recursion (Recursive query)
13
SQL Server 2005 para desarrolladores Solid Quality Learning
XML and Relational!
Scenarios XML Relational
Relational Data Exchange Use as transport, shred to relational
Storage and Query
Document Management Use as markup, store natively
Provides framework to manage collections and relationships; provides Full-text search
Semi-structured Data Represent semi-structured parts
Represent structured parts
Message audit Store natively Used for querying over promoted properties
Object serialization Store natively Used for querying over promoted properties
14
SQL Server 2005 para desarrolladores Solid Quality Learning
SQL Server 2000Technologies
Server support• FOR XML: generate XML from tables• OpenXML: generate relational rowset from XML
Mid-tier support• XML views (annotated mapping schemas XSD)• Templates • UpdateGrams/BulkLoad
Access methods• HTTP SOAP (via mid-tier ISAPI)• ADO, OLE DB; ADO.NET
15
SQL Server 2005 para desarrolladores Solid Quality Learning
XML Datatype
Native SQL type• Use for column, variable or parameter
Can represent:• XML 1.0 documents• XML 1.0 fragments (0 to n element nodes and text
nodes at top)• Can be constrained by XML Schema collection
Queryable with XQuery Updateable with XML-DML XML Indexing Well-formed and validation checks
16
SQL Server 2005 para desarrolladores Solid Quality Learning
XML Datatype: Storage
Primary Storage• As LOB (2 GB per instance) • Efficient binary representation:
• Primary goal: efficient parsing and serialization• ca. 20% compression over Unicode textual representation
• XML encoding transformed to UTF-16 Secondary Storage (for Query Optimization)
• Via Primary and Secondary Indices• Primary XML index: 2X of XML data• PATH XML index: 0.5 of XML data
CAST from/to varbinary, (n)varchar, and CLR UDT
17
SQL Server 2005 para desarrolladores Solid Quality Learning
XML Schema Support
XML Schema (World Wide Web Consortium [W3C] standard)• Rich mechanism for type definitions and validation
constraints• Can be used to constrain XML documents
Benefits of typed data • Guarantees shape of data• Allows storage and query optimizations
XML type system• Store XML schemas in system metadata• Does not preserve annotations
18
SQL Server 2005 para desarrolladores Solid Quality Learning
XQuery and XML-DML in SQL Server 2005
Subset of XQuery implemented Is aligned with July 2004 XQuery working draft Added XML Data Modification Applies to single XML data type instance Methods on XML data type:
• query(), value(), exist(), modify(), nodes() Use SQL to iterate over collection of instances
(XML-typed column) Can refer to relational data Take advantage of Schema-collection information
to operate on typed XML data Will make use of XML indices for optimization
19
SQL Server 2005 para desarrolladores Solid Quality Learning
XML Query
XQuery: query XML documents and data• Standards-based: W3C working draft
In document 123, return section heading of section 3 and later
SELECT id, xDoc.query('for $s in
/doc[@id = 123]//sec[@num >= 3] return <topic>{data($s/heading)}</topic>
') FROM docs
20
SQL Server 2005 para desarrolladores Solid Quality Learning
XQuery: query()
Creates new, untyped XML data type instance
SELECT T.x.query(‘declare namespace n = “urn:example” for $s in /n:doc[@id = 123]//n:sec[@num >= 3] return <topic>{data($s/n:heading)}</topic>’)FROM T
21
SQL Server 2005 para desarrolladores Solid Quality Learning
XQuery: value()
Extracts an XQuery value into the SQL value and type space
Expression has to statically be a singleton String value of atomized XQuery item is cast to SQL type
SELECT T.x.value( ‘(/n:doc[@id = 123]//n:sec[@num >= 3]/@num)[1]’, ’int’)FROM T
22
SQL Server 2005 para desarrolladores Solid Quality Learning
XQuery: exist()
Returns 1 if the XQuery expression returns at least one item, 0 otherwise
select * from T where T.x.exist(‘/a/b[@id=42]’)=1
23
SQL Server 2005 para desarrolladores Solid Quality Learning
sql:column()/sql:variable()
Map SQL value and type into XQuery values and types in context of XQuery or XML-DML
sql:variable(): accesses a SQL variable
sql:column(): accesses another column value
declare @value int set @value=42select * from T where T.x.exist(‘/a/b[@id=sql:variable(“@value”)]’)=1
tables: T(key int, x xml), S(key int, val int)
select * from T join S on T.key=S.keywhere T.x.exist(‘/a/b[@id=sql:column(“S.val”)]’)=1
24
SQL Server 2005 para desarrolladores Solid Quality Learning
XML Indices
Create XML index on XML columnCREATE PRIMARY XML INDEX idx_1 ON docs (xDoc)
Creates secondary indexes on tags, values, paths
Speeds up queries• Results can be served directly from index• Entire query is optimized
• Same award winning cost based optimizer• Indexes are used as available
Indices can be ALTERed and DROPed
25
SQL Server 2005 para desarrolladores Solid Quality Learning
Secondary XML Indices
FOR VALUE – useful for queries such as //city[.=“Bellevue”]
FOR PATH – useful for Path expressions: avoids joins by mapping paths to hierarchical index (HID) numbers. Example: /person/address/zip
FOR PROPERTY – useful for “property extraction” scenario. A bunch of properties (e.g. <prop>123</prop>) are being stored in an XML column. Relational part of query (or other XQuery method) has found XML instance already.
26
SQL Server 2005 para desarrolladores Solid Quality Learning
Shredding and Publishing XML
Server-side:• Publishing: FOR XML• Shredding: nodes() method or OpenXML
Client-side:• SQLXML 4.0 XML Bulkload object• ADO.NET DataSet• SQL Server Integration Services
27
SQL Server 2005 para desarrolladores Solid Quality Learning
FOR XML and OpenXML
Backward compatible with SS2K FOR XML
• New directive TYPE returns XML data type • Nested FOR XML• Assignment to XML data type• Support for new data types• New PATH mode• WITH XMLNAMESPACES (for FOR XML and XQuery)• Requires explicit cast of CLR UDT to XML• element-centric raw mode• inline XSD for raw and auto • NULL as xsi:nil or absence for element-centric modes
OpenXML• Based on MSXML 3.0 engine• XML overflow column• New types [n]varchar(max), varbinary(max), XML
28
SQL Server 2005 para desarrolladores Solid Quality Learning
XQuery: nodes()
Provides OpenXML-like functionality on XML data type column in SQL Server 2005
Returns a row per selected node Each row contains a special XML data type instance
that:• Has the context node set to the selected node• Preserves the original structure and types• Can only be used with the XQuery methods (not
modify()), count(*), and IS (NOT) NULL
29
SQL Server 2005 para desarrolladores Solid Quality Learning
Conclusions
Why bring XML and Relational together? Microsoft SQL Server 2000 Recap Microsoft SQL Server 2005 XML
server-side support• Native XML storage• XML schema support• XML Querying and updating• Publishing with FOR XML• Shredding with nodes()
30
SQL Server 2005 para desarrolladores Solid Quality Learning
Resources
SQL Server webpage: http://msdn.microsoft.com/SQL/2005/
XML and Databases whitepapers: http://msdn.microsoft.com/XML/BuildingXML/XMLandDatabase/
Online WebCasts: http://msdn.microsoft.com/sql/2005/2005webcasts/
Newsgroups & Forum: microsoft.private.sqlserver2005.xmlhttp://communities.microsoft.com/newsgroups/default.asp?ICP=sqlserver2005&sLCID=us http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=89
31
SQL Server 2005 para desarrolladores Solid Quality Learning
Series de Webcasts
Introducción a SQL Server 2005 para desarrolladoresViernes, 22 de Julio de 2005 06:00 p.m.(GMT)http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277969&Culture=es-MX
Nuevas características del lenguaje T-SQL en SQL Server 2005Lunes, 25 de Julio de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277973&Culture=es-MX
Aprovechando XML dentro de la base de datos con SQL Server 2005Viernes, 29 de Julio de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277975&Culture=es-MX
Programando SQL Server 2005 con el CLR – Integración SQL-CLRLunes, 01 de Agosto de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277977&Culture=es-MX
Nuevas características en ADO.NET 2.0Viernes, 05 de Agosto de 2005 06:00 p.m.(GMT) http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032277978&Culture=es-MX