关系对象数据库_第1页
关系对象数据库_第2页
关系对象数据库_第3页
关系对象数据库_第4页
关系对象数据库_第5页
已阅读5页,还剩44页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Silberschatz, Korth and Sudarshan9.1Database System Conceptsn嵌套关系n复杂类型与面向对象n复杂类型的查询n创建复杂类型值及对象n面向对象与对象关系数据库的比较Silberschatz, Korth and Sudarshan9.2Database System Conceptsn通过引入面向对象及处理复杂数据类型的构造来扩展关系数据模型.n允许元组属性具有复杂类型, 包括非原子值(如嵌套关系).n保持关系基础, 尤其是对数据的描述性存取, 同时扩展建模能力.n与现有关系语言向上兼容.Silberschatz, Korth and S

2、udarshan9.3Database System Conceptsn动机:允许非原子域 (原子 不可分割)非原子域例: 整数集合, 元组集合对含有复杂数据的应用可以更直观地建模n直观定义:在原先只允许出现原子值(标量)的地方可以出现关系 关系内的关系保持关系模型的数学基础违反第一范式Silberschatz, Korth and Sudarshan9.4Database System Conceptsn例如: 图书馆信息系统n每本书具有书名, 作者集合,出版商,关键词集合n非1NF 关系 booksSilberschatz, Korth and Sudarshan9.5Database S

3、ystem Conceptsn1NF version of booksflat-booksSilberschatz, Korth and Sudarshan9.6Database System Conceptsn假设flat-books 上有下列多值依赖:title authortitle keywordtitle pub-name, pub-branchn分解到4NF:(title, author)(title, keyword)(title, pub-name, pub-branch)Silberschatz, Korth and Sudarshan9.7Database System C

4、onceptsSilberschatz, Korth and Sudarshan9.8Database System Conceptsn4NF 设计使用户查询时需要连接.n通过连接4NF关系而定义的1NF视图 flat-books:使用户不必执行连接,但丢失了元组与文档的一一对应.具有大量冗余n嵌套关系表示法更自然.Silberschatz, Korth and Sudarshan9.9Database System Conceptsn对SQL的扩展支持复杂类型, 包括:集合与大对象类型嵌套关系是集合类型的例子结构类型类似符合属性的嵌套记录结构继承面向对象包含对象标识符和引用n我们主要基于SQ

5、L:1999标准介绍当前的数据库系统并没有完全实现但某些特性在主要商业数据库系统中都存在请参阅你的数据库系统的手册看看他支持什么我们也介绍一些SQL:1999中没有的特性这会明显指出Silberschatz, Korth and Sudarshan9.10Database System ConceptsnSet 类型(SQL:1999不支持)create table books (.keyword-set setof(varchar(20)nSet 是集合类型的一种. 其他集合类型包括Array (SQL:1999支持)E.g. author-array varchar(20) array10

6、可按通常方式存取数组元素: E.g. author-array1Multiset (SQL:1999不支持)无序集合, 其中一元素可出现多次嵌套关系是元组集合SQL:1999 支持元组数组Silberschatz, Korth and Sudarshan9.11Database System Conceptsn大对象类型clob: 字符大对象 book-review clob(10KB)blob: 二进制大对象 image blob(10MB) movie blob (2GB)nJDBC/ODBC 提供特别方法来以小片断形式存取大对象类似于存取操作系统文件应用获取大对象的定位符, 然后再用宿主

7、语言操作大对象Silberschatz, Korth and Sudarshan9.12Database System ConceptsnSQL中可声明并使用结构类型 create type Publisher as (name varchar(20), branch varchar(20) create type Book as (title varchar(20), author-array varchar(20) array 10, pub-date date, publisher Publisher, keyword-set setof(varchar(20)注: SQL:1999不支

8、持setof用array 存储作者使我们可以记录作者顺序n结构类型可用来创建表 create table books of Book类似于前面的嵌套关系books, 但对作者用的是array而非 setSilberschatz, Korth and Sudarshan9.13Database System Conceptsn结构类型可直接表示E-R图中的复合属性.nSQL:1999中还可用无名行类型来定义复合属性E.g. 可以省略Publisher 类型的声明, 而在类型Book 中用下列声明 publisher row (name varchar(20), branch varchar(20

9、)n类似地, 集合类型可直接表示E-R图中的多值属性.Silberschatz, Korth and Sudarshan9.14Database System Conceptsn可以直接创建表, 而不需创建一个中间类型例如, 表books 可如下定义: create table books (title varchar(20), author-array varchar(20) array10, pub-date date, publisher Publisher keyword-list setof(varchar(20)n方法可以作为结构类型定义的一部分: create type Empl

10、oyee as ( name varchar(20), salary integer) method giveraise (percent integer)n单独定义方法体 create method giveraise (percent integer) for Employee begin set self.salary = self.salary + (self.salary * percent) / 100; endSilberschatz, Korth and Sudarshan9.15Database System Conceptsn结构类型的值用构造函数创建E.g. Publis

11、her(McGraw-Hill, New York) 注: 值不是对象nSQL:1999 的构造函数E.g. create function Publisher (n varchar(20), b varchar(20)returns Publisherbeginset name=n;set branch=b;end每个结构类型都有一个缺省的无参数构造函数, 其他构造函数可根据需要定义nrow 类型的值可通过在括号中列出值来构造E.g. 给定row类型 row (name varchar(20), branch varchar(20)(McGraw-Hill,New York) 是上面类型的值

12、Silberschatz, Korth and Sudarshan9.16Database System Conceptsn构造Array值 array Silberschatz,Korth,SudarshannSet 值 (SQL:1999不支持)set( v1, v2, , vn)n创建books 关系的元组 ( Compilers, arraySmith,Jones, Publisher(McGraw-Hill,New York), set(parsing,analysis)n将上面这个元组插入到关系books 中 insert into booksvalues (Compilers,

13、arraySmith,Jones, Publisher(McGraw Hill,New York ), set(parsing,analysis)Silberschatz, Korth and Sudarshan9.17Database System Conceptsn假设有下列类型定义:create type Person (name varchar(20), address varchar(20)n利用继承定义student和 teacher类型 create type Student under Person (degree varchar(20), department varchar

14、(20) create type Teacher under Person (salary integer, department varchar(20)n子类型在类型声明中可以用overriding method代替代替method来重定义方法Silberschatz, Korth and Sudarshan9.18Database System ConceptsnSQL:1999 不支持多重继承n如果类型系统支持多值继承, 可以如下定义助教类型:create type Teaching Assistant under Student, Teachern为避免两个department 之间的

15、名字冲突可以重命名 create type Teaching Assistant under Student with (department as student-dept), Teacher with (department as teacher-dept)Silberschatz, Korth and Sudarshan9.19Database System Conceptsn表继承通过允许一个实体同时存在于多个表中, 使得对象可具有多个类型.nE.g. people 表: create table people of Personn可定义people 的子表students 和 tea

16、cherscreate table students of Student under peoplecreate table teachers of Teacher under peoplen子表(students 与teachers)中的每个元组隐含地存在于其超表(people)中n多重继承对于表也是可能的. create table teaching-assistants of Teaching Assistant under students, teachersSQL:1999不支持多重继承Silberschatz, Korth and Sudarshan9.20Database Sys

17、tem Conceptsn表继承对角色的建模很有用n允许一个值有多种类型, 而不必具有最具体类型(与类型继承不同).例如, 对象可以同时存在于子表students 和teachers 中, 而不必存在于某个定义在students 和 teachers之下的子表student-teachers 中对象可以获得/失去角色: 对应于从子表中插入/删除对象n注意注意: SQL:1999要求值具有最特殊类型因此上述讨论不适用于SQL:1999Silberschatz, Korth and Sudarshan9.21Database System Conceptsn对子表与超表的一致性要求超表(e.g.

18、people)中的每条元组最多只能对应于每个子表(e.g. students and teachers)中的一条元组SQL:1999中的附加约束:相互对应的所有元组 (即, 对继承来的属性有相同值) 必须是从一条元组(插入到一个表中)派生的. 即, 每个实体必须具有最具体类型在people 中不能有一条元组对应于students 和 teachers中的各一条元组Silberschatz, Korth and Sudarshan9.22Database System Conceptsn可选择的存储方式1.在子表中只存储局部属性和超表的主键继承来的属性通过与超表连接而得到2.每个表都保存所有继承

19、来的属性和局部定义的属性超表隐含地包含其子表中的所有元组 (的继承属性)可快速存取元组的所有属性: 不需连接如果实体必须具有最具体类型, 创建元组时只保存在一个表中H 否则可能有冗余Silberschatz, Korth and Sudarshan9.23Database System Conceptsn面向对象语言提供创建和引用对象的能力. n在SQL:1999中引用是针对元组的, 并且引用必须有作用域, 即, 只能指向特定表中的元组Silberschatz, Korth and Sudarshan9.24Database System Conceptsn例如定义类型Department 具

20、有字段 name 和head (对Person 类型的引用并以表people 作为作用域) create type Department( name varchar(20), head ref(Person) scope people)n可如下创建表departments create table departments of Departmentn可从类型声明中省略scope people, 而是在 create table语句中增加:create table departments of Department (head with options scope people)Silbersc

21、hatz, Korth and Sudarshan9.25Database System Conceptsn在Oracle中, 为创建带有引用值的元组, 可以先创建具有空引用的元组, 然后再利用作用于一元组变量的函数ref(p)单独对引用赋值n例如创建name为CS, head为John的系, 可用insert into departments values (CS, null)update departments set head = (select ref(p) from people as p where name=John) where name = CSSilberschatz, K

22、orth and Sudarshan9.26Database System ConceptsnSQL:1999 不支持ref() 函数, 而是要求用一特殊的属性来保存对象标识符n自引用属性的声明: 在create table语句中增加ref is子句: create table people of Person ref is oid system generated这里 oid 是属姓名, 而非关键字. n为取得对元组的引用, 在前面的查询中可用select p.oid 取代 select ref(p)Silberschatz, Korth and Sudarshan9.27Database

23、System ConceptsnSQL:1999 允许由用户生成对象标识对象标识符的类型必须作为被引用表的类型定义的一部分来指定, 且表定义必须指出该引用是用户生成的E.g. create type Person (name varchar(20) address varchar(20) ref using varchar(20) create table people of Person ref is oid user generatedn当创建元组时, 必须提供唯一的值给对象标识符(假设是第一个属性): insert into people values (01284567, John,

24、23 Coyote Run)Silberschatz, Korth and Sudarshan9.28Database System Conceptsn然后向departments 插入元组时即可使用该标识的值不必用单独的查询来获得标识: E.g. insert into departments values(CS, 02184567)n甚至可以用现有的主键值作为标识: 用ref from 子句并声明引用是derived create type Person (name varchar(20) primary key, address varchar(20) ref from(name)cre

25、ate table people of Person ref is oid derivedn向departments 插入元组时, 可用insert into departments values(CS,John)Silberschatz, Korth and Sudarshan9.29Database System Conceptsn求所有系的系主任的姓名和地址:select head name, head addressfrom departmentsn形如 “headname” 的表达式称为路径表达式路径表达式n路径表达式避免了显式的连接若head不是引用, 则需要连接departmen

26、ts 和people使查询的表达对用户更容易Silberschatz, Korth and Sudarshan9.30Database System Conceptsn求每本书的书名和出版商名. select title, from books 注意“点表示法”在访问复合属性(结构类型) publisher 的字段时的用法Silberschatz, Korth and Sudarshan9.31Database System Conceptsn利用关键字unnest可将集合值属性视为关系, 关系 books 具有数组值属性author-array 和集合值属性key

27、word-setn求所有具有关键词“database”的书 select titlefrom bookswhere database in (unnest(keyword-set)注: 上述语法在SQL:1999中是合法的, 但SQL:1999 唯一支持的汇集类型是array n对每本书及其每个作者查询 “title, author-name” select B.title, A from books as B, unnest (B.author-array) as ASilberschatz, Korth and Sudarshan9.32Database System Conceptsn利

28、用下标可访问数组的个别元素E.g. 如果知道某本书有三个作者, 可以写出:select author-array1, author-array2, author-array3from bookswhere title = Database System ConceptsSilberschatz, Korth and Sudarshan9.33Database System Conceptsn将嵌套关系转换成具有较少(或没有)关系值属性的形式, 称为unnesting.nE.g. select title, A as author, as pub_name, pub

29、lisher.branch as pub_branch, K as keyword from books as B, unnest(B.author-array) as A, unnest (B.keyword-list) as KSilberschatz, Korth and Sudarshan9.34Database System ConceptsnNesting 与 unnesting正相反, 创建汇集值属性n注: SQL:1999不支持nestingnNesting可以用与合计一样的方式进行, 只是用函数set() 代替合计函数来创建集合n对关系flat-books 的属性keywor

30、d 进行nesting :select title, author, Publisher(pub_name, pub_branch) as publisher, set(keyword) as keyword-listfrom flat-booksgroupby title, author, publishern同时对authors 和keywords进行nesting : select title, set(author) as author-list, Publisher(pub_name, pub_branch) as publisher, set(keyword) as keyword

31、-listfrom flat-booksgroupby title, publisherSilberschatz, Korth and Sudarshan9.35Database System Conceptsn创建嵌套关系的另一种方法: 在select 子句中使用子查询select title, ( select author from flat-books as M where M.title=O.title) as author-set, Publisher(pub-name, pub-branch) as publisher, (select keyword from flat-boo

32、ks as N where N.title = O.title) as keyword-setfrom flat-books as On在嵌套查询中可用 order by 子句得到有序汇集从而与前面方法不同, 可以创建数组Silberschatz, Korth and Sudarshan9.36Database System ConceptsnSQL:1999支持函数与过程函数/过程可用 SQL本身编写, 或用外部程序设计语言函数对专门的数据类型如图像和地理对象尤其有用例如用函数检查多边形的重叠, 或者比较图像的相似度有些数据库支持表值函数表值函数, 即返回关系作为结果nSQL:1999还支持

33、丰富的命令构造, 包括循环, if-then-else, 赋值n许多数据库系统对SQL做了不同于SQL:1999的专有的过程式扩展Silberschatz, Korth and Sudarshan9.37Database System Conceptsn定义一个函数, 对给定书名, 返回该书的作者数目(针对4NF关系模式books4 和authors). create function author-count(name varchar(20) returns integer begin declare a-count integer; select count(author) into a-

34、count from authors where authors.title=name return a=count; endn求所有由多个作者写的书的书名.select namefrom books4where author-count(title) 1Silberschatz, Korth and Sudarshan9.38Database System Conceptsn方法可视为与结构类型相关联的函数它们具有隐含的第一个参数self, 设置成在其上方法被调用的结构类型的值方法的代码可以利用self 变量引用结构类型值的属性E.g. self.aSilberschatz, Korth a

35、nd Sudarshan9.39Database System Conceptsn函数author-count 也可写成过程:create procedure author-count-proc (in title varchar(20), out a-count integer) begin select count(author) into a-count from authors where authors.title = title endn过程可利用call语句从SQL过程或嵌入式SQL调用.E.g. 从SQL 过程declare a-count integer;call autho

36、r-count-proc(Database systems Concepts, a-count);nSQL:1999 允许多个同名的函数/过程 (称为名字重载重载), 只要参数数目不同, 或者参数类型不同Silberschatz, Korth and Sudarshan9.40Database System ConceptsnSQL:1999 允许使用用其他语言(如C或 C+)写的函数及过程n声明外部语言过程及函数create procedure author-count-proc(in title varchar(20), out count integer)language Cextern

37、al name /usr/avi/bin/author-count-proccreate function author-count(title varchar(20)returns integerlanguage Cexternal name /usr/avi/bin/author-countSilberschatz, Korth and Sudarshan9.41Database System Conceptsn外部语言函数/过程的好处: 对许多操作更高效, 表达能力更强n缺点实现函数的代码可能需要加载到数据库系统中并在数据库系统的地址空间中执行存在无意中破坏数据库结构的风险安全性风险,

38、允许用户存取未授权数据存在替代方法, 可以提供好的安全性但性能较差当效率比安全性更重要时才直接在数据库系统的空间执行Silberschatz, Korth and Sudarshan9.42Database System Conceptsn为处理安全性问题使用sandbox 技术 即, 使用一种类似Java的安全的语言, 它不能用来存取/损坏数据库代码的其他部分或者, 在一个单独的进程中运行外部语言函数/过程, 不存取数据库进程的内存通过进程间通信交换参数和结果n两者都有性能开销n许多数据库系统支持上述两种方法, 以及在数据库系统地址空间中直接执行Silberschatz, Korth and

39、 Sudarshan9.43Database System ConceptsnSQL:1999 支持丰富的过程构造n复合语句形如begin end, 在begin和end之间可能包含多条SQL语句.复合语句中可声明局部变量nWhile与 repeat 语句declare n integer default 0;while n 10 do set n = n+1end whilerepeat set n = n 1until n = 0end repeatSilberschatz, Korth and Sudarshan9.44Database System ConceptsnFor循环允许在所

40、有查询结果上迭代例如: 求Perryridge分行的所有余额总和 declare n integer default 0; for r as select balance from account where branch-name = Perryridge do set n = n + r.balance end forSilberschatz, Korth and Sudarshan9.45Database System Conceptsn条件语句 (if-then-else)E.g. 对三类账户的每一类求余额总和 (即balance =1000 and = 5000)if r.balance 1000 then set l = l + r.balanceelseif r.balance 5000 then set m = m + r.balanceelse set h = h + r.balanceend if nSQL:1999还支持 case 语句(与 C 的case语句类似)n例外条件的发出, 并声明例外处理程序declare out_of_s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论