当前位置:
文档之家› 数据库系统原理-第二章 关系数据模型
数据库系统原理-第二章 关系数据模型
• 高级数据模型
– The Entity/Relationship Model – Object Definition Language
Page 3
2.1 数据模型概述
• 关系数据模型简介,例如:描述电影信息的 movies关系 movies(title, year, length, genre)
Page 5
2.1 数据模型概述
<movies> <movie title=“Gone With the Wind”> <year> 1939 </year> <length 231 </length> <genre> drama </genre> movies </movie> title year length <movie title=“Star Wars”> <year> 1977 </year> Gone With the Wind 1939 231 <length 124 </length> Star Wars 1977 124 <genre> sciFi </genre> Wayne’s Word 1992 95 </movie> <movie title=“Wayne’s Word”> <year> 1992 </year> <length 95 </length> <genre> comedy </genre> </movie> </movies>
Page 18
2.2.7 关系上的键
Example:
Movies ( title, year, length, genre, studioName, starName ) – {title, year} 是键吗? – {title, year, starName}
成绩 ( 学号, 课号, 成绩 )
Page 15
2.2.5 关系的等价表示
• Relations are sets of tuples, not lists of tuples.
– 在关系中,元组的排列顺序是无关的. – 任一元组在关系中只能出现一次.
• We can reorder the tuples of a relation, without changing the relation. • We can reorder the attributes of a relation, without changing the relation. • P13/Fig. 2-4.
Page 21
2.2.8 数据库模式示例
关系数据库模式:
– Customer (custid, name, prov, city, phone, unit)
– Product (prodid, factory, type, spec, price, desc)
– Salesman (empid, idno, name, gender, phone, deptid)
Page 9
length 231 124 95
genre drama sciFi comedy
2.2.1 属性
• Attribute:
– 关系的列称之为属性. – 关系的每个属性有一个名称, 描述所在列的语义. – 一个关系中的两个属性不可重名.
Movies
title Attributes (column headers) Gone With the Wind Star Wars Wayne’s World
– 数据结构
• relational model = tables; • network and hierarchical models = graphs/trees.
– 操作 – 约束
Page 2
2.1 数据模型概述
• 一些重要的数据模型:
– 关系数据模型, 包括对象关系模型的拓展; – 半结构化数据模型, 包括 XML(扩展标记语言) 和相关的标准; – 网状(Network)模型 与层次( Hierarchical )模型
– Studio(name, address, presC#)
– cert# -- 证书号 – MovieExec – 电影制片
Page 20
2.2.8 数据库模式示例
• 为某商品销售公司构作一个销售业务数据库系统,该系统至少应管理 以下内容: – 公司分为多个下属部门 (Department) ,如“江苏销售部”,“上 海销售部”等。每个部门有若干销售员 (Salesman) ,每个销售员 有唯一的员工号 ( 如“ E0025”) 和身份证号,也包含姓名、性别、 出生日期、电话等信息。一个销售员仅属于一个部门,且每个部 门有一个销售员担任部门经理。 。 – 系统应管理客户信息 (Customer) ,包含:名称、省、市、单位名 称、电话等。 – 系统应管理所有销售的商品 (Product):制造商(如“春兰”、“海 尔” ) 、种类 ( 如“摩托车”、“空调机” ) 、规格 (如“ MT125” 型 摩托车、“RE1500”型空调)、价格、功能及性能描述。 – 该系统用销售订单(Sales order)表示销售业务。每个订单有一个订 单号和签订日期,且对应一个客户和一个销售员。一个订单至少 销售一种产品 (Salesitem),并可销售多种产品,且每种产品应确 定其销售数量和单价,以计算销售金额。
Page 10
year 1939 1977 1992
length 231 124 95
genre drama sciFi comedy
2.2.2 模式
• 关系模式 = 关系名+ 属性集.
– Example: Movies (title, year, length, genre) or Movies (title: string, year: integer, length: integer, genre: string)
第2章 关系数据模型
2.1 数据模型概述 2.2 关系模型基础 2.3 在SQL中定义关系模式 2.4 代数查询语言
2.5 关系上的约束
Page 1
2.1 数据模型概述 • 数据模型(data model)是用于描述数据或信 息的符号. • 在信息世界内采用的是概念模型.
• 在数据库中采用的数据模型主要由三部分 组成:
genre drama sciFi comedy
Tuples (rows)
Star Wars Wayne’s World
Page 13
2.2.3 元组
• 元组的表达
– 单独表示一个元组时,常用逗号隔开各个分量, 并用圆括号括起来. – Example: (‘Star Wars’, 1977, 124, ‘sciFi’) – 当单独表示元组时,属性名不出现。 – We should always use the order in which the attributes were listed in the relation schema.
Page 16
2.2.6 关系实例
• The schema of a relation is relatively static, while the tuples change over time. • Relation instances:
– 一个给定关系在某一时刻的元组的集合称为 该关系的一个实例. – 关系的实例是随时间变化的. – The set of tuples that are in the relation “now” is “current instance(当前实例)”. – 数据库中常常只保存关系的当前实例。
movies title Gone With the Wind Star Wars Wayne’s Word year length genre drama sciFi comedy 1939 231 1977 124 1992 95
Page 4
2.1 数据模型概述
半结构化模型简介: • 半结构化数据类似树或图 • 半结构化模型采用XML(eXtensive Makeup Language, 可扩展标记语言)描 述
– Department (deptid, name, headerid)
– Salesorder (orderno, signdate, empid, custid)
– Salesitem quantity)
(orderno,lineo,Page 22prodid,
unitprice,
2.2 关系模型基础
• Exercise:
p15 2.2.1 课内练习
Page 23
2.3 在SQL中定义关系模式
• SQL:Structured Query Language
• SQL的主要内容:
– DQL (select):DQL is primarily a query language, for getting information from a database. – DML (insert, delete, update) – DDL (create, drop, alter, ...):SQL also includes a data-definition component for describing database schemas. – DCL (grant, revoke, ...)
• 改变关系模式中属性的顺序, 不影响关系的 语义.
– Example: Movies (year, title, length, genre)