数据库系统概论第四版chp16

上传人:M****1 文档编号:569842896 上传时间:2024-07-31 格式:PPT 页数:69 大小:1.07MB
返回 下载 相关 举报
数据库系统概论第四版chp16_第1页
第1页 / 共69页
数据库系统概论第四版chp16_第2页
第2页 / 共69页
数据库系统概论第四版chp16_第3页
第3页 / 共69页
数据库系统概论第四版chp16_第4页
第4页 / 共69页
数据库系统概论第四版chp16_第5页
第5页 / 共69页
点击查看更多>>
资源描述

《数据库系统概论第四版chp16》由会员分享,可在线阅读,更多相关《数据库系统概论第四版chp16(69页珍藏版)》请在金锄头文库上搜索。

1、 中国人民大学信息学院 An Introduction to Database System数据库系统概论数据库系统概论An Introduction to Database System第十六章第十六章 XMLXML数据库数据库第十六章 XML数据库16.1 概述概述16.2 XML简介简介16.3 XML数据模型数据模型16.4 SQL/XML16.5 小结小结An Introduction to Database System16.1 概述XML(eXtended Markup Language) 可扩展的标记语言用户可以定义自己的标记,用来描述文档的结构W3C(World Wide W

2、eb 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/XML16.5 小结小结An Introduction to Database System1

3、6.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

4、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)是为使用一段特殊代码而设计的标记为处理

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

6、m七、实体用来定义重复使用的文档内容格式为: 例如引用实体的格式为&实体名当XML处理器遇到字符串&DW时就用字符串Data Warehouse代替该实体DWAn Introduction to Database System第十六章 XML数据库16.1 概述概述16.2 XML简介简介16.3 XML数据模型数据模型16.4 SQL/XML16.5 小结小结An Introduction to Database SystemXML数据模型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 ModelAn Introduction to Database SystemXML数据模型(续)这四种模型都采用树结构。An Introduction to Database System表16.1 XML数据模型的比较XMLInformati

8、on SetXpath 1.0Data ModelDOM 1.0Level 2XML QueryModel对象XML文档XML文档XML(或HTML)文档XML文档或部分的集合定义方式对XML语法项的附加描述对一组结点类型的数据结构和字符串值的描述一组对象接口的描述Constructor和accessor的功能描述是否要求DTD或XML Schema验证否否否是16.3 XML数据模型16.3.1 XML DTD16.3.2 XML SchemaAn Introduction to Database System16.3.1 XML DTD文档类型定义DTD(Document Type Des

9、criptors)用来描述XML文档的结构,类似于模式的概念DTD定义内容元素、属性元素出现的次序、出现的次数如何相互嵌套其他详细信息An Introduction to Database System一、元素的定义DTD定义元素的格式为:例1例2例3例4 例5An Introduction to Database System二、属性的定义DTD中定义属性的格式为:属性类型字符串类型枚举类型缺省声明n#REQUIRED,表示该属性在XML文件中必须出现n#IMPLIED,表示该属性在XML文件中可以缺省n声明缺省属性值例6An Introduction to Database System属

10、性的定义(续)通常把DTD存储在一个后缀为.dtd的外部文件里例7一个XML DTD 示例 An Introduction to Database System16.3 XML数据模型16.3.1 XML DTD16.3.2 XML SchemaAn Introduction to Database SystemXML Schema定义XML模式的另外两个标准:nXML SchemanDocument Content Descriptors(DCDs)XML Schema用XML来定义其文档的模式,支持对结构和数据类型的定义An Introduction to Database SystemX

11、ML Schema(续)XML Schema的例子例8 An Introduction to Database SystemXML Schema(续)An Introduction to Database System表16.2 DTD和XML Schema的特征比较Syntax in XMLNamespaceInclude &ImportBuilt-in typeUser-definedtypeDomain constraintExplicitNullDTDNoNoNo10NoNoNoXML SchemaYesYesYes37YesYesYesvXML Schema更为完善vDTD简便易用X

12、ML Schema(续)良结构的文档有效的文档无效的文档An Introduction to Database System第十六章 XML数据库16.1 概述概述16.2 XML简介简介16.3 XML数据模型数据模型16.4 SQL/XML16.5 小结小结An Introduction to Database System16.4 SQL/XMLSQL2003标准增加了对XML的支持,定义了数据库语言SQL与XML结合的方式,扩展的部分称为SQL/XMLAn Introduction to Database SystemSQL/XML(续)功 能SQL/XML中的关键词数据类型定义XML

13、强制数据类型转化XMLCAST字符串XMLXMLPARSEXML字符串XMLSERIALIZEXML发布函数关系数据XMLXMLELEMENT,(XMLNAMESPACES,XMLATTRIBUTES),XMLFOREST,XMLCONCAT,XMLAGG,XMLCOMMENT,XMLPIXML提取函数XMLXMLXMLQUERYXML关系XMLTABLEXML布尔值XMLEXISTSAn Introduction to Database System表16.3 SQL/XML中的主要关键词及功能分类SQL/XML(续)SQL/XML定义了新的数据类型XML数据类型,以及一组函数An Intr

14、oduction to Database SystemXML和关系数据间的双向转换16.4 SQL/XML16.4.1 XML数据类型数据类型16.4.2 XML发布函数发布函数16.4.3 XML提取函数提取函数An Introduction to Database System16.4.1 XML数据类型原生(Native)SQL数据类型:XML数据类型,用“XML”表示例1创建一个书店表bookstore,其中bib列的类型是XML。CREATE TABLE bookstore(id INT PRIMARY KEY,bib XML);An Introduction to Database

15、 SystemXML数据类型(续)例2可以将一个XML文档加入书店bookstore中。INSERT INTO bookstore VALUES (1492,xmlparse ( English ) );An Introduction to Database SystemXML数据类型(续)XMLCast将某种SQL预定义类型的数据转换为一种XML类型将某种XML类型的数据转换为其他的SQL预定义类型或XML类型格式XMLCAST ( value-expression AS type )An Introduction to Database System16.4 SQL/XML16.4.1 X

16、ML数据类型数据类型16.4.2 XML发布函数发布函数16.4.3 XML提取函数提取函数An Introduction to Database System16.4.2 XML发布函数XML发布函数的作用:从关系数据创建XML结构一、XMLElement函数创建一个XML元素,可以指定元素名字例3SELECT e.id,XMLELEMENT(NAME Emp,e.fname | | e.lname)AS resultFROM employees e;An Introduction to Database SystemXML发布函数(续)An Introduction to Database

17、 System假设关系表employees如下: Idfnamelnamehiredept1001JohnSmith2000-05-24Accounting1206BobMartin1996-02-01Shipping查询返回的结果是:IDResult1001John Smith1206Bob MartinXML发布函数(续)嵌套使用例4SELECT e.id,XMLELEMENT( NAME Emp,XMLELEMENT (NAME name,e.lname ),XMLELEMENT (NAME hiredate,e.hire ) ) AS result FROM employees e;

18、An Introduction to Database SystemXML发布函数(续)An Introduction to Database System查询返回的结果是:IDresult1001 Smith 2000-05-241206 Martin 1996-02-01XML发布函数(续)n参数是子查询例5SELECT e.id,XMLELEMENT ( NAME Emp,XMLELEMENT (NAME name,e.lname),XMLELEMENT (NAME dependants,(SELECT COUNT (*) FROM dependants d WHERE d.parent

19、=e.id) ) ) AS result FROM employees e;An Introduction to Database SystemXML发布函数(续)An Introduction to Database System返回的结果可能是:IDresult1001 Smith 3XML发布函数(续)二、XMLAttributes函数为构建的元素生成一组属性仅在XMLElement函数中使用An Introduction to Database SystemXML发布函数(续)An Introduction to Database System例6SELECT e.id,XMLELEM

20、ENT (NAME Emp,XMLATTRIBUTES (e.id,e.lname AS name) AS resultFROM employees e;返回的结果可能是:IDresult10011206XML发布函数(续)三、XMLNamespaces给出名空间Namespaces 例7SELECT e.id,XMLELEMENT(NAME admi:employee,XMLNAMESPACES(http:/ AS admi),XMLATTRIBUTES(e.workdept AS admi:department),e.lastname) AS resultFROM employees eW

21、HERE e. job=ANALYST;An Introduction to Database SystemXML发布函数(续)An Introduction to Database System可能的结果是:IDresult1130QUINTANA1140NICHOLLSXML发布函数(续)四、XMLForest函数生成一组元素An Introduction to Database SystemXML发布函数(续)An Introduction to Database System例8SELECT e.id,XMLFOREST (e.hire,e.dept AS department) AS

22、 resultFROM employees e;结果是:IDresult10012000-05-24Accounting12061996-02-01ShippingXML发布函数(续)五、XMLConcat函数将两个以上的XML数据值串接起来例9SELECT e.id,XMLCONCAT (XMLELEMENT (NAME first,e.fname),XMLELEMENT (NAME last,e.lname) )AS resultFROM employees e;An Introduction to Database SystemXML发布函数(续)An Introduction to D

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

24、*) AS dept_countFROM employees eGROUP BY dept;An Introduction to Database SystemXML发布函数(续)An Introduction to Database System假设关系表employees加上了2条记录,共4条记录:Idfnamelnamehiredept1000TomYates2001-08-02Accounting1001JohnSmith2000-05-24Accounting1205MaryOppenheimer2000-01-19Shipping1206BobMartin1996-02-01Shi

25、ppingXML发布函数(续)An Introduction to Database System返回的结果是:dept_listdept_countYatesSmith2OppenheimerMartin2XML发布函数(续)nORDER BY子句指定返回值的串接顺序例11SELECT XMLELEMENT (NAME Department,XMLATTRIBUTES (e.dept AS name),XMLAGG (XMLELEMENT (NAME emp,e.lname)ORDER BY e.lname) AS “dept_list”,COUNT(*) AS dept_countFROM

26、 employees eGROUP BY dept;An Introduction to Database SystemXML发布函数(续)An Introduction to Database System返回的结果是:dept_listdept_count Smith Yates 2 Martin Oppenheimer 2XML发布函数(续)七、XMLComment函数生成注释例12XMLCOMMENT(This is a comment)结果生成An Introduction to Database SystemXML发布函数(续)八、XMLPI函数生成处理指令例13XMLPI(NAM

27、E includeFile,/POs/template.hls)结果生成 An Introduction to Database System16.4 SQL/XML16.4.1 XML数据类型数据类型16.4.2 XML发布函数发布函数16.4.3 XML提取函数提取函数An Introduction to Database System16.4.3 XML提取函数一、XMLQuery函数XML数据的查询语言SQL/XML提供将XQuery嵌入SQL的机制,对存储在关系数据库中的XML数据可以用XQuery进行查询返回的数据类型为XMLAn Introduction to Database

28、SystemXML提取函数(续)例14选出价格低于80的所有英文书,返回价格和作者名SELECT XMLQUERY(for $bib in $doc/biblang/text()=English,$book in $bib/book WHERE $book/price 80 RETURN $book/price,$book/author/name PASSING bib AS doc RETURN CONTENT) AS result FROM bookstore;An Introduction to Database SystemXML提取函数(续)An Introduction to Da

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

30、BLE( 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 SystemXML提取函数(续)An Intr

31、oduction to Database System返回的可能结果是:TitleauthoryearpriceData on the WebSerge Abiteboul199915The Emperors New MindRoger Penrose198960XML提取函数(续)三、XMLExists谓词检查XQuery查询在XML数据上的执行结果是否为非空如果查询结果是一个空序列,则谓词返回假,否则为真An Introduction to Database SystemXML提取函数(续)例16查询标了价格的所有书目。SELECT id,bib FROM bookstore WHERE

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

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号