第十六章XML数据库.ppt
An Introduction to Database System,中国人民大学信息学院,数据库系统概论 An Introduction to Database System 第十六章 XML数据库,An Introduction to Database System,第十六章 XML数据库,16.1 概述 16.2 XML简介 16.3 XML数据模型 16.4 SQL/XML 16.5 小结,An Introduction to Database System,16.1 概述,XML(eXtended Markup Language) 可扩展的标记语言 用户可以定义自己的标记,用来描述文档的结构 W3C(World Wide Web Consortium)在1998年制定的一项标准,用于网上数据交换 是标准通用标记语言SGML的一个子集,An Introduction to Database System,概述(续),XML在SGML和HTML的基础上发展起来的 相对于HTML,XML具有如下的一些特点: 更多的结构和语义 可扩展性 自描述性 数据与显示分离 简洁性,An Introduction to Database System,第十六章 XML数据库,16.1 概述 16.2 XML简介 16.3 XML数据模型 16.4 SQL/XML 16.5 小结,An Introduction to Database System,16.2 XML简介,一、XML说明(XML declaration) 对XML文档处理的环境和要求的说明 必须在文档的第一行 例如: ,An Introduction to Database System,二、元素,元素(element)是XML文档的主要组成部分 标记名 区分大、小写 开始于字母或下划线(_),后面可以跟任意长度的字母、数字、句点(.)、连接符(-)、下划线或冒号 起始标记形式是,终止标记形式是 嵌套 有且只有一个根元素,An Introduction to Database System,元素(续), Beijing Library Database System Concepts 26.50 Kaily Jone Silen Smith Introduction to XML 18.80 Kaily Jone A Query language for XML 26.50 Kaily Jone 图16.1 一个XML文档示例,An Introduction to Database System,三、属性,描述元素的有关信息 格式为: 如。 属性值必须出现在单引号或双引号中 一个元素可以有任意多个属性,每个属性取不同的属性名,An Introduction to Database System,四、处理指令,处理指令(processing instructions)是为使用一段特殊代码而设计的标记 为处理XML文档的应用程序提供信息。 组成:处理指令名称和数据 其格式为。例如,An Introduction to Database System,五、注释,注释以结束 注释可以在XML文档的任何地方插入,An Introduction to Database System,六、良构的XML文档,良构的(well-formed)XML文档是指: 文档的构造从语法上都是正确的 只有一个顶层元素,即根元素 至少包含一个元素,即文档中必须有根元素 所有的起始标记都有与之对应的终止标记,或者使用空元素速记语法 所有的标记都正确的嵌套 每一个元素的所有属性具有不同属性名,An Introduction to Database System,七、实体,用来定义重复使用的文档内容 格式为: 例如 引用实体的格式为&实体名 当XML处理器遇到字符串&DW时就用字符串Data Warehouse代替该实体DW,An Introduction to Database System,第十六章 XML数据库,16.1 概述 16.2 XML简介 16.3 XML数据模型 16.4 SQL/XML 16.5 小结,An Introduction to Database System,XML数据模型,XML数据是自描述的、不规则的,可以用图模型来表示,图16.1中的XML数据的图模型表示,元素标识名,属性值对应集合,元素文本内容,An Introduction to Database System,XML数据模型(续),XML数据 VS 半结构化数据 (1) XML中存在参照 (2) XML中的元素是有序的 (3) XML中可以将文本与元素混合 (4) XML包含许多其他的内容 XML数据模型:XML Information Set,Xpath1.0 Data Model,DOM model和XML Query Data Model,An Introduction to Database System,XML数据模型(续),这四种模型都采用树结构。,表16.1 XML数据模型的比较,An Introduction to Database System,16.3 XML数据模型,16.3.1 XML DTD 16.3.2 XML Schema,An Introduction to Database System,16.3.1 XML DTD,文档类型定义DTD(Document Type Descriptors) 用来描述XML文档的结构,类似于模式的概念 DTD定义内容 元素、属性 元素出现的次序、出现的次数 如何相互嵌套 其他详细信息,An Introduction to Database System,一、元素的定义,DTD定义元素的格式为: 例1 例2 例3 例4 例5 ,An Introduction to Database System,二、属性的定义,DTD中定义属性的格式为: 属性类型 字符串类型 枚举类型 缺省声明 #REQUIRED,表示该属性在XML文件中必须出现 #IMPLIED,表示该属性在XML文件中可以缺省 声明缺省属性值 例6 ,An Introduction to Database System,属性的定义(续),通常把DTD存储在一个后缀为.dtd的外部文件里 例7 一个XML DTD 示例 ,An Introduction to Database System,16.3 XML数据模型,16.3.1 XML DTD 16.3.2 XML Schema,An Introduction to Database System,XML Schema,定义XML模式的另外两个标准: XML Schema Document Content Descriptors(DCDs) XML Schema 用XML来定义其文档的模式,支持对结构和数据类型的定义,An Introduction to Database System,XML Schema(续),XML Schema的例子 例8 ,An Introduction to Database System,XML Schema(续),表16.2 DTD和XML Schema的特征比较,XML Schema更为完善 DTD简便易用,An Introduction to Database System,XML Schema(续),良结构的文档 有效的文档 无效的文档,An Introduction to Database System,第十六章 XML数据库,16.1 概述 16.2 XML简介 16.3 XML数据模型 16.4 SQL/XML 16.5 小结,An Introduction to Database System,16.4 SQL/XML,SQL2003标准增加了对XML的支持,定义了数据库语言SQL与XML结合的方式,扩展的部分称为SQL/XML,An Introduction to Database System,SQL/XML(续),表16.3 SQL/XML中的主要关键词及功能分类,An Introduction to Database System,SQL/XML(续),SQL/XML定义了新的数据类型XML数据类型,以及一组函数,XML和关系数据间的双向转换,An Introduction to Database System,16.4 SQL/XML,16.4.1 XML数据类型 16.4.2 XML发布函数 16.4.3 XML提取函数,An Introduction to Database System,16.4.1 XML数据类型,原生(Native)SQL数据类型:XML数据类型,用“XML”表示 例1 创建一个书店表bookstore,其中bib列的类型是XML。 CREATE TABLE bookstore(id INT PRIMARY KEY,bib XML);,An Introduction to Database System,XML数据类型(续),例2 可以将一个XML文档加入书店bookstore中。 INSERT INTO bookstore VALUES (1492, xmlparse (' English ') );,An Introduction to Database System,XML数据类型(续),XMLCast 将某种SQL预定义类型的数据转换为一种XML类型 将某种XML类型的数据转换为其他的SQL预定义类型或XML类型 格式 XMLCAST ( value-expression AS type ),An Introduction to Database System,16.4 SQL/XML,16.4.1 XML数据类型 16.4.2 XML发布函数 16.4.3 XML提取函数,An Introduction to Database System,16.4.2 XML发布函数,XML发布函数的作用:从关系数据创建XML结构 一、XMLElement函数 创建一个XML元素,可以指定元素名字 例3 SELECT e.id,XMLELEMENT(NAME “Emp“,e.fname | ' ' | e.lname) AS “result“ FROM employees e;,An Introduction to Database System,XML发布函数(续),假设关系表employees如下:,查询返回的结果是:,An Introduction to Database System,XML发布函数(续),嵌套使用 例4 SELECT e.id,XMLELEMENT( NAME “Emp“, XMLELEMENT (NAME “name“,e.lname ), XMLELEMENT (NAME “hiredate“,e.hire ) ) AS “result“ FROM employees e;,An Introduction to Database System,XML发布函数(续),查询返回的结果是:,An Introduction to Database System,XML发布函数(续),参数是子查询 例5 SELECT e.id,XMLELEMENT ( NAME “Emp“, XMLELEMENT (NAME “name“,e.lname), XMLELEMENT (NAME “dependants“, (SELECT COUNT (*) FROM dependants d WHERE d.parent=e.id) ) ) AS “result“ FROM employees e;,An Introduction to Database System,XML发布函数(续),返回的结果可能是:,An Introduction to Database System,XML发布函数(续),二、XMLAttributes函数 为构建的元素生成一组属性 仅在XMLElement函数中使用,An Introduction to Database System,XML发布函数(续),例6 SELECT e.id,XMLELEMENT (NAME “Emp“, XMLATTRIBUTES (e.id,e.lname AS “name“) ) AS “result“ FROM employees e; 返回的结果可能是:,An Introduction to Database System,XML发布函数(续),三、 XMLNamespaces 给出名空间Namespaces 例7 SELECT e.id,XMLELEMENT(NAME “admi:employee“, XMLNAMESPACES('http:/www.admi.com' AS “admi“), XMLATTRIBUTES(e.workdept AS “admi:department“), e.lastname ) AS “result“ FROM employees e WHERE e. job='ANALYST';,An Introduction to Database System,XML发布函数(续),可能的结果是:,An Introduction to Database System,XML发布函数(续),四、XMLForest函数 生成一组元素,An Introduction to Database System,XML发布函数(续),例8 SELECT e.id,XMLFOREST (e.hire,e.dept AS “department“) AS “result“ FROM employees e; 结果是:,An Introduction to Database System,XML发布函数(续),五、XMLConcat函数 将两个以上的XML数据值串接起来 例9 SELECT e.id,XMLCONCAT ( XMLELEMENT (NAME “first“,e.fname), XMLELEMENT (NAME “last“,e.lname) )AS “result“ FROM employees e;,An Introduction to Database System,XML发布函数(续),返回的结果可能是:,如果某个结果返回空值,则不出现在串接中。,An Introduction to Database System,XML发布函数(续),六、XMLAGG函数 XMLAGG是一个聚集函数 参数是一个XML数据类型的表达式,An Introduction to Database System,XML发布函数(续),例10 SELECT XMLELEMENT (NAME “Department“, XMLATTRIBUTES (e.dept AS “name“), XMLAGG (XMLELEMENT (NAME “emp“,e.lname) ) AS “dept_list“, COUNT(*) AS “dept_count“ FROM employees e GROUP BY dept;,An Introduction to Database System,XML发布函数(续),假设关系表employees加上了2条记录,共4条记录:,An Introduction to Database System,XML发布函数(续),返回的结果是:,An Introduction to Database System,XML发布函数(续),ORDER BY子句指定返回值的串接顺序 例11 SELECT XMLELEMENT (NAME “Department“, XMLATTRIBUTES (e.dept AS “name“), XMLAGG (XMLELEMENT (NAME “emp“,e.lname) ORDER BY e.lname) ) AS “dept_list”, COUNT(*) AS “dept_count“ FROM employees e GROUP BY dept;,An Introduction to Database System,XML发布函数(续),返回的结果是:,An Introduction to Database System,XML发布函数(续),七、XMLComment函数 生成注释 例12 XMLCOMMENT('This is a comment') 结果生成,An Introduction to Database System,XML发布函数(续),八、XMLPI函数 生成处理指令 例13 XMLPI(NAME “includeFile“,'/POs/template.hls') 结果生成 ,An Introduction to Database System,16.4 SQL/XML,16.4.1 XML数据类型 16.4.2 XML发布函数 16.4.3 XML提取函数,An Introduction to Database System,16.4.3 XML提取函数,一、XMLQuery函数 XML数据的查询语言 SQL/XML提供将XQuery嵌入SQL的机制,对存储在关系数据库中的XML数据可以用XQuery进行查询 返回的数据类型为XML,An Introduction to Database System,XML提取函数(续),例14选出价格低于80的所有英文书,返回价格和作者名 SELECT XMLQUERY( for $bib in $doc/biblang/text()='English', $book in $bib/book WHERE $book/price $book/price,$book/author/name ' PASSING bib AS “doc“ RETURN CONTENT) AS “result“ FROM bookstore;,An Introduction to Database System,XML提取函数(续),返回的结果可能是:,Xquery中的return子句有构造XML的功能,An Introduction to Database System,XML提取函数(续),二、XMLTable函数 从XML数据中提取信息,返回结果是一个二维的关系表(列的定义用XPath路径表达式),An Introduction to Database System,XML提取函数(续),例15 选出价格低于80的英文书,显示书名、作者名、出版年份和价格(返回关系表的格式)。 SELECT T.title,T.author,T.year,T.price FROM bookstore AS B, XMLTABLE( 'for$bib in $doc/biblang/text()='English', $book in $bib/book WHERE $book/price 80 RETURN $book' PASSING B.bib AS “doc“ COLUMNS “title“ VARCHAR(100) PATH './title', “year“ INTEGER PATH './year', “price“ DOUBLE PATH './price', “author“ VARCHAR(30) PATH './author/name' ) AS T;,An Introduction to Database System,XML提取函数(续),返回的可能结果是:,An Introduction to Database System,XML提取函数(续),三、 XMLExists 谓词 检查XQuery查询在XML数据上的执行结果是否为非空 如果查询结果是一个空序列,则谓词返回假,否则为真,An Introduction to Database System,XML提取函数(续),例16 查询标了价格的所有书目。 SELECT id,bib FROM bookstore WHERE XMLEXISTS(' $doc/bib/book/price' PASSING bib AS “doc“);,An Introduction to Database System,第十六章 XML数据库,16.1 概述 16.2 XML简介 16.3 XML数据模型 16.4 SQL/XML 16.5 小结,An Introduction to Database System,16.5 小结,SQL/XML 基于关系模型 允许在SQL查询中直接使用XML发布函数生成XML结构 对传统的SQL做了扩展 SQL/XML既提供了从关系数据创建XML的功能,又能很好的适应已有的SQL环境,