1、Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,11/7/2009,#,一对象关系数据库基本概念,1.1对象关系数据模型,Object-RelationalDataModels,对象关系模型ORDBM,Extend,therelationaldatamodelbyincludingobjectorientation,面向对象,andconstructstodealwithaddeddatatypes.,Allow
2、attributesoftuplestohavecomplextypes,includingnon-atomic,valuessuchasnestedrelations.,Preserve,relationalfoundations,inparticularthedeclarativeaccesstodata,whileextendingmodelingpower.,Upwardcompatibility,向上兼容,withexistingrelationallanguages.,对象关系数据库管理系统ORDBMS,基于对象关系数据模型的数据库管理系统,关系模型设计面临障碍:关系模型仅支持非
3、常有限的数据类型,难以描述许多客,观存在的复杂结构,(记录结构,多值属性,继承),这些复杂结构E-R模型本身是能很好的支持的,但在形成相应关系模式结构时被强,制转换为简单的SQL数据类型,将传统关系模型扩展到对象关系模型是为了想要使用面向对象特征的关系数据库,用户提供一个移植途径,以更好地满足应用的实际需要,SQL标准SQL1999版本(以及SQL2003),特别增加了对象关系扩展描述部分,注意,大多数数据库产品都只支持SQL标准的一个子集,需要实际查阅相关的用户,手册,1.2复杂数据类型,动机Motivation:,Permitnon-atomicdomains(indivisible,不可
4、分的,),Exampleofnon-atomicdomain:setofintegers,orsetof,tuples,Allowsmoreintuitivemodelingforapplicationswith,complexdata,直观说明Intuitivedefinition:,allow,relationswheneverweallowatomic(scalar)values,比如:relationswithinrelations,Retains,保留mathematicalfoundationofrelationalmodel,Violates,违反firstnormalform.
5、例子:,关系看着属性值,传统处理方式:,表示数据对象,传统方式存在的不足:,不足分析,Example:libraryinformationsystem(图书馆信息系统),Eachbookhas,title,asetofauthors,Publisher,and,asetofkeywords,Non-1NFrelation,books:,结构:,内容:,多值属性,记录结构属性,多值属性,1.2复杂数据类型,Removeawkwardness笨拙offlat-books扁平关系byassumingthat,thefollowingmultivalueddependencieshold:,titl
6、eauthor,title,title,keyword,pub-name,pub-branch,Decomposeflat-doc扁平文档内容into4NFusingtheschemas:,(由4NF分解算法),authors(title,author),keywords(title,keyword),book4(title,pub-name,pub-branch),传统方式表示的不足,4NFdesignrequiresuserstoinclude,joins,intheirqueries.,1NFrelationalviewflat-booksdefinedbyjoinof4NFrelati
7、ons:,eliminatestheneedforuserstoperformjoins,butlosestheone-to-onecorrespondencebetweentuplesanddocuments.,Andhasalargeamountofredundancy,新方式表示的优点,Nestedrelationsrepresentationismuchmorenaturalhere.,二SQL支持的复杂数据类型,ExtensionstoSQLtosupportcomplextypesinclude:,Collection,and,largeobject,types,Nestedrel
8、ationsareanexampleofcollectiontypes,Structured,types,Nestedrecordstructureslikecompositeattributes,Inheritance,Object,orientation,Includingobjectidentifiersandreferences,OurdescriptionismainlybasedontheSQL:1999standard,Notfully,implementedinanydatabasesystemcurrently,Butsome,featuresarepresentineach
9、ofthemajorcommercial,databasesystems,Readthemanualofyourdatabasesystemtoseewhatit,supports,2.1SQL-结构类型,创建:User-definedrowtypes,(可以利用已定义结构定义新的结构),createtypeCustomerTypeas(,nameName,addressAddress,dateOfBirthdate),notfinal,使用:Canthencreateatablewhoserowsareauser-definedtype,createtablecustomerofCustom
10、erType,2.1SQL-结构类型,结构定义时可申明方法:Can,add,amethoddeclarationwithastructuredtype.,method,ageOnDate,(onDatedate),returnsintervalyear,方法体需要单独编写:Methodbodyisgiven,separately,.,createinstancemethod,ageOnDate,(onDatedate),returnsintervalyear,forCustomerType,begin,return,onDate-self.dateOfBirth,;,当前日期减去出生日期,en
11、d,注:,SELF,referstothistuple,instance,指出该方法在CustomerType的实例上执行,方法的使用方式:Wecannowfindtheageofeachcustomer:,selectname.lastname,ageOnDate,(current_date),fromcustomer,注:Dot是系统提供的,缺省方法,!,补充:,另一例子,2.1SQL-结构类型,.,10.15,10.17,P.451(example9.21:EndtoMethod.),declaration,Separately,weneedtodefinethemethodhouseN
12、umber()asthe,form:,CREATEMETHODhouseNumber()RETURNSCHAR(10),FORAddressType,BEGIN,definition,END;,2.2SQL-继承类型,子类继承:,Suppose,thatwehavethefollowingtypedefinitionforpeople:,createtypePerson,(namevarchar(20),addressvarchar(20),Usinginheritance,todefinethestudentandteachertypes,createtypeStudent,underPer
13、son,(degreevarchar(20),departmentvarchar(20),createtypeTeacher,underPerson,(salaryinteger,departmentvarchar(20),多态性:Subtypescan,redefinemethods,byusingoverridingmethodin,placeofmethodinthemethoddeclaration,2.2SQL-继承类型,不支持:SQL:1999andSQL:2003,donotsupport,multipleinheritance,假想的类型继承:,Ifourtypesystems
14、upportsmultipleinheritance,wecandefineatype,forteachingassistantasfollows:,createtypeTeachingAssistant,underStudent,Teacher,Toavoidaconflictbetweenthe,twooccurrences,ofdepartmentwecan,renamethem,createtypeTeachingAssistant,under,Studentwith(departmentas,student_dept,),分情况选择继承,Teacherwith(departmenta
15、s,teacher_dept,Student,),类型的属性名,2.3SQL-数组和多重集合类型,数组构造器,Arrayconstruction,:,arraySilberschatz,Korth,Sudarshan,多重集合构造器,Multisetsconstruction,:,multisetsetcomputer,database,SQL,产生复杂结构元组值,Tocreateatuple,ofthetypedefinedbythebooksrelation:,(Compilers,arraySmith,Jones,newPublisher(McGraw-Hill,NewYork),mul
16、tisetparsing,analysis),注:,new,是指调用Publisher构造函数(和调用参数)为Publisher属性创建一个值,,Publisher构造函数可用createfunction定义,如p.242的name构造函数定义。,插入复杂结构元组到表中,Toinserttheprecedingtupleintotherelationbooks:,insertintobooks,values,(Compilers,arraySmith,Jones,newPublisher(McGraw-Hill,NewYork),.multisetparsing,analysis),2.4SQ
17、L嵌套集合体上的操作,含义:Thetransformationofanestedrelationintoaformwithfewer(orno),较少或,没有,relation-valuedattributes,关系值属性,iscalled,unnesting,.,例子:E.g.(设有嵌套关系模式,books,),selecttitle,Aasauthor,publisher.nameaspub_name,publisher.branchaspub_branch,K.keyword,frombooksasB,unnest,(B.author_array)asA(author),unnest,(B.keyword_set)asK(keyword),查询结果为平面关系:,flat-books,






