数据库系统概论PPT教程第十六章 XML数据库.ppt

上传人:仙人指路1688 文档编号:2926078 上传时间:2023-03-03 格式:PPT 页数:69 大小:1.15MB
返回 下载 相关 举报
数据库系统概论PPT教程第十六章 XML数据库.ppt_第1页
第1页 / 共69页
数据库系统概论PPT教程第十六章 XML数据库.ppt_第2页
第2页 / 共69页
数据库系统概论PPT教程第十六章 XML数据库.ppt_第3页
第3页 / 共69页
数据库系统概论PPT教程第十六章 XML数据库.ppt_第4页
第4页 / 共69页
数据库系统概论PPT教程第十六章 XML数据库.ppt_第5页
第5页 / 共69页
点击查看更多>>
资源描述

《数据库系统概论PPT教程第十六章 XML数据库.ppt》由会员分享,可在线阅读,更多相关《数据库系统概论PPT教程第十六章 XML数据库.ppt(69页珍藏版)》请在三一办公上搜索。

1、,An Introduction to Database System,数据库系统概论An Introduction to Database System第十六章 XML数据库,第十六章 XML数据库,16.1 概述16.2 XML简介16.3 XML数据模型16.4 SQL/XML16.5 小结,An Introduction to Database System,16.1 概述,XML(eXtended Markup Language)可扩展的标记语言用户可以定义自己的标记,用来描述文档的结构W3C(World Wide Web Consortium)在1998年制定的一项标准,用于网上数

2、据交换 是标准通用标记语言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/XML16.5 小结,An Introduction to Database System,16.2 XML简介,一、XML说明(XML declaratio

3、n)对XML文档处理的环境和要求的说明必须在文档的第一行 例如:,An Introduction to Database System,二、元素,元素(element)是XML文档的主要组成部分标记名区分大、小写开始于字母或下划线(_),后面可以跟任意长度的字母、数字、句点(.)、连接符(-)、下划线或冒号 起始标记形式是,终止标记形式是嵌套有且只有一个根元素,An Introduction to Database System,元素(续),Beijing Library Database System Concepts 26.50 Kaily Jone Silen Smith Introdu

4、ction 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文档的应用程序提供信息。组成:处理指令名称和

5、数据其格式为。例如,An Introduction to Database System,五、注释,注释以结束注释可以在XML文档的任何地方插入,An Introduction to Database System,六、良构的XML文档,良构的(well-formed)XML文档是指:文档的构造从语法上都是正确的只有一个顶层元素,即根元素至少包含一个元素,即文档中必须有根元素所有的起始标记都有与之对应的终止标记,或者使用空元素速记语法所有的标记都正确的嵌套每一个元素的所有属性具有不同属性名,An Introduction to Database System,七、实体,用来定义重复使用的文档内

6、容格式为:例如引用实体的格式为&实体名当XML处理器遇到字符串&DW时就用字符串Data Warehouse代替该实体DW,An Introduction to Database System,第十六章 XML数据库,16.1 概述16.2 XML简介16.3 XML数据模型16.4 SQL/XML16.5 小结,An Introduction to Database System,XML数据模型,XML数据是自描述的、不规则的,可以用图模型来表示,An Introduction to Database System,图16.1中的XML数据的图模型表示,元素标识名,属性值对应集合,元素文本内

7、容,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数据模型(续),这四种模型都采用树结构。,An Introduction to Database System,表16.1 XML数据模型的比较,16.3 XML数据模型,16.3.1 XML D

8、TD16.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中定义属性的格式为:属性类型字符串类型

9、枚举类型缺省声明#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 DTD16.3.2 XML Schema,An Introduction to Database System,XML Schema,定义XML模式的另外两个标准:XML S

10、chemaDocument Content Descriptors(DCDs)XML Schema用XML来定义其文档的模式,支持对结构和数据类型的定义,An Introduction to Database System,XML Schema(续),XML Schema的例子 例8,An Introduction to Database System,XML Schema(续),An Introduction to Database System,表16.2 DTD和XML Schema的特征比较,XML Schema更为完善DTD简便易用,XML Schema(续),良结构的文档有效的文档

11、无效的文档,An Introduction to Database System,第十六章 XML数据库,16.1 概述16.2 XML简介16.3 XML数据模型16.4 SQL/XML16.5 小结,An Introduction to Database System,16.4 SQL/XML,SQL2003标准增加了对XML的支持,定义了数据库语言SQL与XML结合的方式,扩展的部分称为SQL/XML,An Introduction to Database System,SQL/XML(续),An Introduction to Database System,表16.3 SQL/XML

12、中的主要关键词及功能分类,SQL/XML(续),SQL/XML定义了新的数据类型XML数据类型,以及一组函数,An Introduction to Database System,XML和关系数据间的双向转换,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 bookstor

13、e(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 typ

14、e),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元素,可以指定元素名字例3SELECT e.id,XMLELEMENT(NAME Emp,e.fname|e.lname)AS resultFROM employees e;,An Introduction to Data

15、base System,XML发布函数(续),An Introduction to Database System,假设关系表employees如下:,查询返回的结果是:,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

16、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,返回的结果可能是:,X

17、ML发布函数(续),二、XMLAttributes函数 为构建的元素生成一组属性仅在XMLElement函数中使用,An Introduction to Database System,XML发布函数(续),An Introduction to Database System,例6SELECT e.id,XMLELEMENT(NAME Emp,XMLATTRIBUTES(e.id,e.lname AS name)AS resultFROM employees e;返回的结果可能是:,XML发布函数(续),三、XMLNamespaces给出名空间Namespaces 例7SELECT e.id,

18、XMLELEMENT(NAME admi:employee,XMLNAMESPACES(http:/AS admi),XMLATTRIBUTES(e.workdept AS admi:department),e.lastname)AS resultFROM employees eWHERE e.job=ANALYST;,An Introduction to Database System,XML发布函数(续),An Introduction to Database System,可能的结果是:,XML发布函数(续),四、XMLForest函数生成一组元素,An Introduction to

19、Database System,XML发布函数(续),An Introduction to Database System,例8SELECT e.id,XMLFOREST(e.hire,e.dept AS department)AS resultFROM employees e;结果是:,XML发布函数(续),五、XMLConcat函数将两个以上的XML数据值串接起来例9SELECT e.id,XMLCONCAT(XMLELEMENT(NAME first,e.fname),XMLELEMENT(NAME last,e.lname)AS resultFROM employees e;,An I

20、ntroduction to Database System,XML发布函数(续),An Introduction to Database System,返回的结果可能是:,如果某个结果返回空值,则不出现在串接中。,XML发布函数(续),六、XMLAGG函数XMLAGG是一个聚集函数参数是一个XML数据类型的表达式,An Introduction to Database System,XML发布函数(续),例10SELECT XMLELEMENT(NAME Department,XMLATTRIBUTES(e.dept AS name),XMLAGG(XMLELEMENT(NAME emp,e

21、.lname)AS dept_list,COUNT(*)AS dept_countFROM employees eGROUP BY dept;,An Introduction to Database System,XML发布函数(续),An Introduction to Database System,假设关系表employees加上了2条记录,共4条记录:,XML发布函数(续),An Introduction to Database System,返回的结果是:,XML发布函数(续),ORDER BY子句指定返回值的串接顺序例11SELECT XMLELEMENT(NAME Departm

22、ent,XMLATTRIBUTES(e.dept AS name),XMLAGG(XMLELEMENT(NAME emp,e.lname)ORDER BY e.lname)AS“dept_list”,COUNT(*)AS dept_countFROM employees eGROUP BY dept;,An Introduction to Database System,XML发布函数(续),An Introduction to Database System,返回的结果是:,XML发布函数(续),七、XMLComment函数 生成注释例12XMLCOMMENT(This is a comme

23、nt)结果生成,An Introduction to Database System,XML发布函数(续),八、XMLPI函数生成处理指令例13XMLPI(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嵌入S

24、QL的机制,对存储在关系数据库中的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

25、 Introduction to Database System,XML提取函数(续),An Introduction to Database System,返回的结果可能是:,Xquery中的return子句有构造XML的功能,XML提取函数(续),二、XMLTable函数从XML数据中提取信息,返回结果是一个二维的关系表(列的定义用XPath路径表达式),An Introduction to Database System,XML提取函数(续),例15 选出价格低于80的英文书,显示书名、作者名、出版年份和价格(返回关系表的格式)。SELECT T.title,T.author,T.yea

26、r,T.priceFROM bookstore AS B,XMLTABLE(for$bib in$doc/biblang/text()=English,$book in$bib/bookWHERE$book/price 80RETURN$bookPASSING B.bib AS docCOLUMNS 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

27、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/XML16.5 小结,An Introduction to Database System,16.5 小结,SQL/XML基于关系模型允许在SQL查询中直接使用XML发布函数生成XML结构对传统的SQL做了扩展 SQL/XML既提供了从关系数据创建XML的功能,又能很好的适应已有的SQL环境,An Introduction to Database System,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号