SQL Server 2005: Extending the Type System with XML.

Slides:



Advertisements
Similar presentations
Introduction to Structured Query Language (SQL)
Advertisements

SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
The XML data type in Microsoft SQL Server. The XML Type Microsoft SQL Server offers a special data type – XML – Used in tables, etc. – Basically a (long)
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Module 17 Storing XML Data in SQL Server® 2008 R2.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Using XML in SQL Server 2005 NameTitleCompany. XML Overview Business Opportunity The majority of all data transmitted electronically between organizations.
CNUG Day of.NET: October 30th SQL Server 2005: A Developers Introduction Shawn Wildermuth Magenic Technologies, Inc.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Database Technical Session By: Prof. Adarsh Patel.
Sofia, Bulgaria | 9-10 October Using XQuery to Query and Manipulate XML Data Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Introduction to MySQL Lab no. 10 Advance Database Management System.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
14-1 Goals of Database Design A database should provide for efficient storage, update, and retrieval of data. A database should be reliable—the stored.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Indexes / Session 2/ 1 of 36 Session 2 Module 3: Types of Indexes Module 4: Maintaining Indexes.
Module 3 Designing and Implementing Tables. Module Overview Designing Tables Working with Schemas Creating and Altering Tables.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Session 11 Creating Tables and Using Data Types. RDBMS and Data Management/Session 11/2 of 40 Session Objectives Define the data types and list the categories.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Visual Programing SQL Overview Section 1.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
SQL SERVER DAYS 2011 Table Indexing for the.NET Developer Denny Cherry twitter.com/mrdenny.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Session 1 Module 1: Introduction to Data Integrity
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
SQL Basics Review Reviewing what we’ve learned so far…….
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Getting started with Accurately Storing Data
Rob Gleasure robgleasure.com
Rob Gleasure robgleasure.com
CS320 Web and Internet Programming SQL and MySQL
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Insert, Update and the rest…
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Data Definition and Data Types
The Basics of Data Manipulation
CS3220 Web and Internet Programming SQL and MySQL
Data Definition Language
CS3220 Web and Internet Programming SQL and MySQL
Structured Query Language Path from Unorganized to Organized….
Presentation transcript:

SQL Server 2005: Extending the Type System with XML

Who I Am Shawn Wildermuth C# MVP INETA Speaker Author of “Pragmatic ADO.NET”; Editor of This Presentation can be found at: – Shawn Wildermuth C# MVP INETA Speaker Author of “Pragmatic ADO.NET”; Editor of This Presentation can be found at: –

Agenda The XML Datatype Using the XML Type Methods What is the Type System Can’t I use CLR Types? Typed XML Extending the Typed System with XML XML Indexes The XML Datatype Using the XML Type Methods What is the Type System Can’t I use CLR Types? Typed XML Extending the Typed System with XML XML Indexes

The XML DataType New to SQL Server 2005 – Native support for XML data – Not stored as text blobs – XML treated as first-class type – Has some limitations – Support in-place editing New to SQL Server 2005 – Native support for XML data – Not stored as text blobs – XML treated as first-class type – Has some limitations – Support in-place editing

The XML DataType (2) Using the XML datatype – Can be used in procedural code: – Can be gathered from Old Style XML Syntax: – Can be used in Table/View Declarations: Using the XML datatype – Can be used in procedural code: – Can be gathered from Old Style XML Syntax: – Can be used in Table/View Declarations: xml = ' ' = (SELECT * FROM Person.Contact FOR XML AUTO) CREATE TABLE Team ( TeamID int identity not null, TeamDoc xml DEFAULT ' ' NOT NULL )

The XML DataType (3) Using the XML datatype: – Inserting/updating XML with Strings: Using the XML datatype: – Inserting/updating XML with Strings: -- Insert a couple of records INSERT INTO Team (TeamDoc) VALUES (' ');

The XML DataType (4) Limitations – No conversion to and from text/ntext – Only strings can cast to XML type – Cannot be used in GROUP BY’s – Cannot be in Distributed or Materialized Views – Cannot used primary or foreign keys – Cannot be uniquely constrained – Only 32 XML Columns per Table – Only 128 Levels of Hierarchy supported Limitations – No conversion to and from text/ntext – Only strings can cast to XML type – Cannot be used in GROUP BY’s – Cannot be in Distributed or Materialized Views – Cannot used primary or foreign keys – Cannot be uniquely constrained – Only 32 XML Columns per Table – Only 128 Levels of Hierarchy supported

The XML DataType The XML Type has five methods: – Query: Used to find subresults – Exist: Used to find matches The XML Type has five methods: – Query: Used to find subresults – Exist: Used to find matches SELECT TeamDoc.query('/Team/Players/Pitcher') FROM Team SELECT Count(*) FROM Team WHERE = 1

The XML DataType Methods (2) – Value: Used to find a value of a result – Nodes: Used to return nodes collections  (We won’t be covering this in the interest of the schedule) – Value: Used to find a value of a result – Nodes: Used to return nodes collections  (We won’t be covering this in the interest of the schedule) SELECT 'nvarchar(max)') as FirstPitcher FROM Team FirstPitcher John Smoltz

The XML DataType Methods (3) – Modify: Used to do in-place changes  MS Specific XQuery Extensions for Modification –insert –delete – Modify: Used to do in-place changes  MS Specific XQuery Extensions for Modification –insert –delete UPDATE Team SET TeamDoc.modify(' insert as last into (/Team/Players)[1] ') WHERE = 1 UPDATE Team SET TeamDoc.modify(' delete Wright"] ') WHERE = 1

The XML DataType Methods (4) – Modify: Used to do in-place changes  replace with (like update)  Can use full XQuery syntax as needed – Modify: Used to do in-place changes  replace with (like update)  Can use full XQuery syntax as needed UPDATE Team SET TeamDoc.modify (' replace value of with "Starter" ') UPDATE Team SET TeamDoc.modify (' replace value of with ( if = "Closer") then "Starter" else "Closer") ')

SQL Server Type System Type system has been static – varchar, int, bit, datetime, etc. – Could only create types by reduction Type system has been static – varchar, int, bit, datetime, etc. – Could only create types by reduction EXEC sp_addtype N'age', N'tinyint', N'not null' GO CREATE RULE age_range >= 0 <=140 GO EXEC sp_bindrule N'age_range', N'age' GO

SQL Server Type System (2) SQL Server 2005 Supports two extensions – Managed Types  Limited to 8000 bytes in size  Good for including behavior  Must deal with security issues of managed code  No Good Inter Object Searching Story  No Good Indexing Story SQL Server 2005 Supports two extensions – Managed Types  Limited to 8000 bytes in size  Good for including behavior  Must deal with security issues of managed code  No Good Inter Object Searching Story  No Good Indexing Story

SQK Server Type System (3) SQL Server 2005 Supports two extensions – Typed XML  Size not an issue  No behavior can be added  Just data limits security issues  XQuery/XPath for sub-searching  Supports XML Indexing SQL Server 2005 Supports two extensions – Typed XML  Size not an issue  No behavior can be added  Just data limits security issues  XQuery/XPath for sub-searching  Supports XML Indexing

Using Typed XML Uses Schema Collections for different Types – Uses new DDL syntax to add XML Type Uses Schema Collections for different Types – Uses new DDL syntax to add XML Type CREATE XML SCHEMA COLLECTION BaseballSchema AS ' '

Using Typed XML (2) Once registered, can use as XML type: xml(BaseballSchema) = ' ' (1 row(s) affected)

Using Typed XML (3) Validation Happens at Assignment xml(BaseballSchema) -- Won’t work as role isn’t defined = ' ' XML Validation: Attribute 'role' is not permitted in this context. Location:

Using Typed XML (4) Works just as well as columns in Tables CREATE TABLE Team ( TeamID int identity not null, TeamDoc xml(BaseballSchema) ) INSERT INTO Team (TeamDoc) VALUES (' ')

Using Typed XML (5) Searching can then include XML searches – Just like non-typed XML, but faster Searching can then include XML searches – Just like non-typed XML, but faster SELECT Smoltz"]') FROM Team

Managing Typed XML Schemas Schema Collections – Can contain more than one XSD – Include new schema with ADD: Schema Collections – Can contain more than one XSD – Include new schema with ADD: ALTER XML SCHEMA COLLECTION BaseballSchema ADD ' '

Managing Typed XML Schemas (2) Both XSD’s can now be used INSERT INTO Team (TeamDoc) VALUES (' ') INSERT INTO Team (TeamDoc) VALUES ('<Score HomeTeam="Braves" AwayTeam="RedSox" HomeScore="5" AwayScore="4" />')

Managing Typed XML Schemas (3) Cannot drop schemas if used – ALTER does not let you drop an XSD – Cannot drop entire collection until not in use Cannot drop schemas if used – ALTER does not let you drop an XSD – Cannot drop entire collection until not in use ALTER TABLE Team DROP COLUMN TeamDoc GO DROP XML SCHEMA COLLECTION BaseballSchema GO CREATE XML SCHEMA COLLECTION BaseballSchema AS '...' GO ALTER TABLE Team ADD TeamDoc xml (BaseballSchema)

Managing Typed XML Schemas (4) Caveats – May need to workout versioning  Can do this with Convert(xml(sometype), oldtype)  Only works if the new schema is superset  May need to do xslt to do real conversions  Or; support both versions Caveats – May need to workout versioning  Can do this with Convert(xml(sometype), oldtype)  Only works if the new schema is superset  May need to do xslt to do real conversions  Or; support both versions

XML Indexing XML Columns can have Indexes – Can have Primary and secondary indexes – Primary improves simple node searches XML Columns can have Indexes – Can have Primary and secondary indexes – Primary improves simple node searches CREATE PRIMARY XML INDEX IXML_Teams ON Team (TeamDoc) SELECT * FROM TEAM WHERE TeamDoc.exist("\Team\Pitcher")

XML Indexing (2) Secondary Indexes – Add for specific types of searches  PATH –On the path and value columns of the primary index to make path-specific exist() method calls more efficient  PROPERTY –Builds an index on the PK, path and value columns of the primary index to make value() method calls more efficient Secondary Indexes – Add for specific types of searches  PATH –On the path and value columns of the primary index to make path-specific exist() method calls more efficient  PROPERTY –Builds an index on the PK, path and value columns of the primary index to make value() method calls more efficient TeamDoc.exist('/ = "John Smoltz"]')

XML Indexing (3) Secondary Indexes – Add for specific types of searches  VALUE –Builds an index on the value and path of the primary index to make node based exist() method calls more efficient Secondary Indexes – Add for specific types of searches  VALUE –Builds an index on the value and path of the primary index to make node based exist() method calls more efficient

XML Indexing (4) Creating Secondary Indexes – Use CREATE XML INDEX syntax  Add USING to specify primary index Creating Secondary Indexes – Use CREATE XML INDEX syntax  Add USING to specify primary index CREATE XML INDEX IXML_Team_Path ON Team (TeamDoc) USING XML INDEX IXML_Teams FOR PATH CREATE XML INDEX IXML_Team_Prop ON Team (TeamDoc) USING XML INDEX IXML_Teams FOR PROPERTY CREATE XML INDEX IXML_Team_Value ON Team (TeamDoc) USING XML INDEX IXML_Teams FOR VALUE

Questions?