- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Repetition of Information.(姓名,身份证号,出生日期) Inability to represent certain information.(姓名,出生 日期)
Avoid redundant(冗余) data Ensure that relationships among attributes are represented (代表性) Facilitate the checking of updates for violation(违背) of database integrity constraints(完整性).
example
Relation schema: Lending-schema = (branch-name, branch-city, asset, customer-name, loan-number, amount) 存在以下4个问题:
插入异常:例如要插入一个新的银行信息,如果该银行没有 客户,就无法插入该银行信息。(tell me why?) 删除异常:如果删除某银行的所有客户信息,则该银行的信 息(branch_name, branch-city, asset )记录就要被删除。 ( 因为customer_name是主属性,删除了客户名称,整个 元组就不存在了。) 数据冗余:一个branch-name就可以唯一地确定一个 branch-city和asset 。而在该关系模式中,如果一个银行有1 万个客户,该银行的branch-city和asset就要重复1万次。 修改复杂:假设某银行有5万客户,如果该银行的branchcity和asset branch-city和asset变动的话,则需要对5万个 元组中全部的branch-city和asset信息进行修改。
Pitfalls in Relational Database Design
Relational database design requires that we find a “good” collection of relation schemas. A bad design may lead to
Now 5 minutes
, cno score
Movie(title,year,length,filmTpye,studioName,starName)中,
Functional Dependencies (Cont.)
包含键码的属性集成为“超键码”superkey,因此, 每个键码都是超键码,但是,某些超键码不是键码。
Those attributes functionally determines all other attributes of the relation.这些属性函数决定该关系的所有 其他属性。 No proper subset of {A1,A2, …,An} functionally determines all other attributes of R. i.e, a key must be minimal. {A1,A2, …,An}的任何真子集都不能函数决定R的 所有其他属性。也就是说,键码必须是最小的。
Functional Dependencies (Cont.)
例如:student_course(sno,cno,score) (sno,cno) score and sno score 思考:在关系 键码是什么?如何证明?
有时在一个关系中有多个键码,这样的话,通常要指定其中的 一个键码为主键码(primary key),在商业数据库系统中,主 键码的选择会影响某些实现细节。 自学下一节3.5.3 Superkeys
Design Goals:
Decomposition(分解)
在关系模式的设计有缺陷时,我们通过更详细地分析这些 缺陷引起地问题,引入“分解”的思想,即把一个关系模 式分解成两个更小的模式来消除某些问题。
Decompose the relation schema Lending-schema into: Branch-schema = (branch-name, branch-city,assets) Loan-info-schema = (customer-name, loan-number, branchname, amount)
Decomposition(分解)
•All attributes of an original schema (R) must appear
in the decomposition (R1, R2):
R = R1 R2
•Lossless-join decomposition.无损连接
For all possible relations r on schema R r = R1 (r) R2 (r)
Functional Dependencies
如果R的两个元组在属性A1,A2, …,An上一致,则它们在另 一个属性B上也应该一致。这种依赖正式记作A1,A2, …,An B,称为B函数依赖于A1,A2, …,An,也可以说 “A1,A2, …,An函数决定B”。
例如关系 Movie(title,year,length,filmTpye,studioName,starName) 我们可以合理地断言关于Movie的几个函数依赖。
从以上分解中,我们不难看到:如此将一个关系模式分解 成两个关系模式后:
减少了冗余 消除了插入异常 消除了删除异常 修改简单
Decomposition(分解)
并不是任意地将一个关系模式分解成两个关系模式就一定会 产生好的关系模式,关系模式的分解也可能带来一些新的问 题。 例如:将Lending-schema 分解成: Branch-customer = (branch-name, customer-name) Loan-info = (branch-name, loan-number,amount) 如此分解,既不能解决以前存在的问题,还会带来新的异 常(anomalies)。 究竟应该如何分解才能消除异常?在关系分解中应该遵循的 基本原则是什么?
(title,year) length
(title,year) studioName
(title,year) fileTpye
由于三个依赖的左边完全相同,都是(title,year) 我门可以用 简写的形式把它们汇总在一行中: (title,year) length, fileType,studioName
Functional Dependencies (Cont.)
P119 example 3.20
P120 figure 3.27 另一方面,我们观察到,如下表述
(title,year) starName
就是错误的,它不是函数依赖。 若属性Y不函数依赖于属性X,则记为X Y 练习:考虑一个关于美国人的关系:包括姓名、社会保险号、 街道地址、城市、州、邮政编码、地区码以及电话号码。你认 为有哪些函数依赖?
First Normal Form
A relational schema R is in first normal form if the domains of all attributes of R are atomic 如果一个关系模式R的所有属性都是不可分的基本数 据项,则称关系R属于第一范式。 记作R 1NF. 第一范式是对关系模式的一个最起码的要求。 不满足第一范式的数据库模式不能称为关系数据库。 当然并不是满足第一范式的关系模式就一定是一个好 的关系模式。仍然可能存在很多的问题。
each relation is in good form the decomposition is a lossless-join decomposition functional dependencies 函数依赖 multivalued dependencies 多值依赖
Our theory is based on:
Functional Dependencies (Cont.)
A functional dependency is a generalization(概括) of the notion(概念) of a key. 从函数依赖的角度出发来认识关系的键码(keys of relation) We say a set of one or more attributes {A1,A2, …,An} is a key for a relation if :如果一个和多个属性{A1,A2, …,An} 满足如下条件,我们就称该集合为关系R的键码。
平凡函数依赖在任何关系中都成立 E.g.在任何关系中都成立
customer-name, loan-number customer-name customer-name customer-name
In general, is trivial if
Example of Non Lossless-Join Decomposition
Decomposition of R = (A, B) R1 = (A) R2 = (B)
A B 1 2 1 r A B 1 2
A(r)
A B 1 2 1 2
B(r)
A (r)
B (r)
Functional Dependencies
Constraints on the set of legal relations. y=2x+5 x=1 则 y=7 学号->学生 Require that the value for a certain set of attributes determines uniquely the value for another set of attributes. A functional dependency is a generalization (概括) of the notil — Devise(设计出) a Theory for the Following