[理学]南开大学数据库原理课件lecture2-RelationalDataModel.ppt_第1页
[理学]南开大学数据库原理课件lecture2-RelationalDataModel.ppt_第2页
[理学]南开大学数据库原理课件lecture2-RelationalDataModel.ppt_第3页
[理学]南开大学数据库原理课件lecture2-RelationalDataModel.ppt_第4页
[理学]南开大学数据库原理课件lecture2-RelationalDataModel.ppt_第5页
已阅读5页,还剩46页未读 继续免费阅读

下载本文档

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

文档简介

The Relational Data Model,Tables Schemas Conversion from E/R to Relations,Database Modeling & Implementation,Database Model (E/R, ODL),Ideas,Physical storage,Diagrams (E/R),Tables: column names: attributes rows: tuples,Complex file organization and index structures.,Relational Schema,ER Model vs. Relational Model,Both are used to model data ER model has many concepts entities, relations, attributes, etc. well-suited for capturing the app. requirements not well-suited for computer implementation (does not even have operations on its structures) Relational model has just a single concept: relation world is represented with a collection of tables well-suited for efficient manipulations on computers,An Example of a Relation,Name Price Category Manufacturer gizmo $19.99 gadgets GizmoWorks Power gizmo $29.99 gadgets GizmoWorks SingleTouch $149.99 photography Canon MultiTouch $203.99 household Hitachi,tuples,Attribute names,Table name,Products:,Domains,Each attribute has a type Must be atomic type (why? see later) Called domain Examples: Integer String Real ,Schemas,The Schema of a Relation: Relation name plus attribute names E.g. Product(Name, Price, Category, Manufacturer) In practice we add the domain for each attribute The Schema of a Database A set of relation schemas E.g. Product(Name, Price, Category, Manufacturer), Vendor(Name, Address, Phone), . . . . . . .,Instances,Relational schema = R(A1,Ak): Instance = relation with k attributes (of “type” R) values of corresponding domains Database schema = R1(), R2(), , Rn() Instance = n relations, of types R1, R2, ., Rn,Schemas vs. instances (very important, make sure you know the difference),Example,Relational schema:Product(Name, Price, Category, Manufacturer) Instance:,Updates,The database maintains a current database state. Updates to the data: 1) add a tuple 2) delete a tuple 3) modify an attribute in a tuple Updates to the data happen very frequently. Updates to the schema: relatively rare. Rather painful. Why?,Schemas and Instances,Analogy with programming languages: Schema = type Instance = value Important distinction: Database Schema = stable over long periods of time Database Instance = changes constantly, as data is inserted/updated/deleted,Two Mathematical Definitions of Relations,Relation as Cartesian product Tuple = element of string x int x string x string E.g. t = (gizmo, 19, gadgets, GizmoWorks) Relation = subset of string x int x string x string Order in the tuple is important ! (gizmo, 19, gadgets, GizmoWorks) (gizmo, 19 , GizmoWorks, gadgets) No attributes,Relation as a set of functions Fix the set of attributes A=name , price, category, manufacturer A tuple = function t:A Domains Relation = set of tuples E.g. Order in a tuple is not important Attribute names are important,name gizmo, price 19, category gadgets, manufacturer gizmoWorks,Translating ER Diagram to Rel. Design,Basic cases entity set E = relation with attributes of E relationship R = relation with attributes being keys of related entity sets + attributes of R Special cases combining two relations translating weak entity sets translating is-a relationships and subclasses,Stock price,An Example,Entity Sets to Relations,Product,name,category,price,Product: Name Category Price gizmo gadgets $19.99,Relationships to Relations,makes,Company,Product,name,category,Stock price,name,Relation Makes (watch out for attribute name conflicts) Product-name Product-Category Company-name Starting-year gizmo gadgets gizmoWorks 1963,Start Year,price,Relationship to Relation: Another Example,Drinkers,Beers,Likes,Likes(drinker, beer),Favorite,Favorite(drinker, beer),Married,husband,wife,Married(husband, wife),name,addr,name,manf,Buddies,1,2,Buddies(name1, name2),Combining Two Relations,makes,Company,Product,name,category,Stock price,name,No need for Makes. Just modify Product: name category price StartYear companyName gizmo gadgets 19.99 1963 gizmoWorks,Start Year,price,Combining Relations,It is OK to combine the relation for an entity-set E with the relation R for a many-one relationship from E to another entity set. Example: Drinkers(name, addr) and Favorite(drinker, beer) combine to make Drinker1(name, addr, favoriteBeer).,Risk with Many-Many Relationships,Combining Drinkers with Likes would be a mistake. It leads to redundancy, as:,name addr beer Sally 123 Maple Bud Sally 123 Maple Miller,Handling Weak Entity Sets,University,Team,affiliation,number,sport,name,Relation Team: Sport Number Affiliated University mud wrestling 15 Montezuma State U.,- need all the attributes that contribute to the key of Team - dont need a separate relation for Affiliation. (why ?),Handling Weak Entity Sets,Relation for a weak entity set must include attributes for its complete key (including those belonging to other entity sets), as well as its own, nonkey attributes. A supporting (double-diamond) relationship is redundant and yields no relation.,Another Example,Logins,Hosts,At,name,name,Hosts(hostName) Logins(loginName, hostName, time) At(loginName, hostName, hostName2),time,Translating Subclass Entities,Option #1: the OO Approach,4 tables: each object can only belong to a single table Product(name, price, category, manufacturer) EducationalProduct( name, price, category, manufacturer, ageGroup, topic) SoftwareProduct( name, price, category, manufacturer, platforms, requiredMemory) EducationalSoftwareProduct( name, price, category, manufacturer, ageGroup, topic, platforms, requiredMemory) All names are distinct,Option #2: the E/R Approach,Product(name, price, category, manufacturer) EducationalProduct( name, ageGroup, topic) SoftwareProduct( name, platforms, requiredMemory) No need for a relation EducationalSoftwareProduct Unless, it has a specialized attribute: EducationalSoftwareProduct(name, educational-method) Same name may appear in several relations,Option #3: The Null Value Approach,Have one table: Product ( name, price, manufacturer, age-group, topic, platforms, required-memory, educational-method) Some values in the table will be NULL, meaning that the attribute not make sense for the specific product. Too many meanings for NULL,Translating Subclass Entities: The Rules,Three approaches: Object-oriented : each entity belongs to exactly one class; create a relation for each class, with all its attributes. E/R style : create one relation for each subclass, with only the key attribute(s) and attributes attached to that E.S.; entity represented in all relations to whose subclass/E.S. it belongs. Use nulls : create one relation; entities have null in attributes that dont belong to them.,Example,Beers,Ales,isa,name,manf,color,Object Oriented,name manf Bud Anheuser-Busch Beers name manf color Summerbrew Petes dark Ales,Beers,Ales,isa,name,manf,color,E/R Style,name manf Bud Anheuser-Busch Summerbrew Petes Beers name color Summerbrew dark Ales,Beers,Ales,isa,name,manf,color,Using Nulls,name manf color Bud Anheuser-Busch NULL Summerbrew Petes dark Beers,Beers,Ales,isa,name,manf,color,Comparisons,O-O approach good for queries like “find the color of ales made by Petes.” Just look in Ales relation. E/R approach good for queries like “find all beers (including ales) made by Petes.” Just look in Beers relation. Using nulls saves space unless there are lots of attributes that are usually null.,数据模型三要素,数据结构 所研究的对象类型的集合。 通常按数据结构的类型来命名数据模型。 数据操作 是对数据库中各种对象(型)的实例允许执行的操作集合 数据的约束条件 数据及其联系应具有的制约和依赖规则,层次模型(Hierarchical Model),层次模型的数据结构 用树形结构来表示实体之间联系的模型叫层次模型 层次数据库系统的典型代表是IBM公司的IMS系统,层次数据库实例,D9,计算机,王平,D2,机械系,张风,S22,材料,S21,机制,S92,系统结构,S91,软件,080,许超,教授,077,正刚,副教授,076,刘同,讲师,070,杨岭,教授,109,秦涛,教授,082,戴月,副教授,045,高风,讲师,8325,8317,8306,8340,8321,8316,c26,.,c24,.,c25,.,c22,.,.,.,.,.,.,.,系,专业,学生,教师,层次模型的数据操作,基本操作是记录的插入、删除、修改和检索 层次模型支持三种检索 按层次顺序检索 按层次路径检索 在当前父记录内检索 插入操作 指定插入路径 指定插入记录的父记录 删除操作 指定删除记录 修改操作 在I/O区中准备好修改记录,然后找到被修改记录替换,层次模型的数据约束,记录之间的关联关系只限于一对多联系 采用虚拟记录型来反映多对多联系,例如学生与选课之间的多对多联系 它的全部数据必须以有序树的形式组织起来,任一记录型至多只能有一个父记录型,只有一个记录型没有父结点 如何表达父亲与子女和母亲与子女之间的关系,网状模型(Network Model),网状模型的数据结构: 用有向图结构表示实体类型及实体间联系的数据模型 网状数据模型的典型代表是DBTG系统,学号,姓名,年龄,性别,课程号,名称,学号,课程号,成绩,学生 s,课程 c,学生成绩单 s-sc,课程成绩单 c-sc,学生选课 sc,系的概念,网状模型以系结构为基础,它是系的集合,网状数据库实例,S1 C1 A,S1 C2 A,S2 C1 A,S2 C3 A,S3 C1 A,S3 C2 A,S4 C1 A,S4 C2 A,S4 C3 A,S1,S2,S3,S4,C1,C2,C3,网状模型的数据操作,网状模型的数据操作分为对于数据记录的操作和对于系(set)的操作 对于记录的操作包括检索、插入、删除与修改 对于系的操作包括 把一个记录连入一个系值中 使一个记录脱离它所在的某个系值 将一个记录从一个系值转入另一个系值,网状模型的数据约束,支持记录码的概念 在任何时候任何一个系值中至多只有一个主记录 任何一个记录在同一系型中至多只能参与其中的一个系值 某个学生记录属于计算机系,就不能再属于物理系 可以支持双亲记录和子女记录之间的某些约束条件,关系模型概述,单一的数据结构-关系 任何一个关系数据库都是由若干张互相关联的表组成 关系模式和关系 关系操作 查询操作:选择、投影、连接、除、并、交、差 更新操作:增加、删除、修改 关系操作的特点是集合操作方式 关系的三类完整性约束 实体完整性、参照完整性和用户定义的完整性,实体完整性(Entity Integrity),若属性A是基本关系R的主属性,则属性A不能取空值 例如: 选修(学号,课程号,成绩) 人(身份证号,姓名,家庭住址,出生日期,.) 实体应该是可区分的,主码是区分实体的唯一性标识,因此不能为空(即不能不知道)。,参照完整性(Referential Integrity),设F是基本关系R的一个或一组属性,但不是关系R的码。如果F与基本关系S的主码KS相对应,则称F是基本关系R的外码(foreign Key). S(sno,name,age,sex,deptno), D(deptno,deptname) S(sno,name,age), C(cno,cname), SC(sno,cno,grad

温馨提示

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

评论

0/150

提交评论