Download presentation
Presentation is loading. Please wait.
Published byShon George Modified over 9 years ago
2
1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams
3
2 What are Data Model? uComputer simulation of the real world. uTo abstract, denote and transact the information in the real world.
4
3 Data Model should : uSimulate the real world comparatively truly uBe easy to understand uBe easy to be implemented by computer
5
4 Abstraction and Modeling Two steps of the abstraction and modeling: 1.The objective objects in the real world is abstracted to a concept model. 2.Concept model is converted to a data model that supported by some DBMS. The concept model is a middle layer between the real world and the machine world. 现实世界 机器世界 概念模型 数据模型 客观对象 信息世界
6
5 The classification of the models uConcept model Also called information model. Modeling data from the viewpoint of users. a middle layer between the real world and the machine world. Mainly used for a database design. The language in which the database designers and users communicate with each other. It should be simple, clear and easy to understand by users.
7
6 uData model Modeling data from the viewpoint of computer. Mainly used for a DBMS realization. Traditional data models Hierarchical Model Network Model Relational Model Main factors of the data model Data structure Data operation Integrity constraints
8
7 数据模型的组成要素 u 数据结构 w 指所研究数据集合及数据间的联系 w 是对系统静态特性的描述 u 数据操作 w 对数据库中各类数据允许执行的操作及有关的操作规则 检索、更新(包括插入、删除、修改) w 是对系统动态特性的描述 u 数据的约束条件 w 一组数据及其联系所具有的制约规则,用以限定符合数 据模型的数据库状态以及状态的变化,以保证数据的正 确、有效、相容
9
8 E-R Model uThe concept model is represented by Entity-Relationship ( E-R ) diagrams. uThe E/R model wDescribes kinds of data and how they connected. wCan be converted to relational model later.
10
9 概念模型基本概念 u 实体( Entities) w 客观存在并可相互区别的事物,可具体,可抽象 例:职工、学生、系 u 属性( Attribute ) w 实体具有的某一特性 例:学生实体有学号、姓名、性别、出生年份等属性 u 码( Key ) w 唯一标识实体的属性集 例如 学号是学生实体的码 u 域 ( Domain ) w 某属性的取值范围 u 实体集( Entity Set ) w 同型实体的集合 如 全体学生
11
10 概念模型的基本概念 u 实体间的联系( Relationship ) w 现实世界中事物内部以及事物之间的联系在信息世 界中反映为实体内部的联系和实体之间的联系 例 张山选修数据库课程。 学生实体和课程之间通过选修联系 u 两个实体之间的联系: w 一对一联系 (1:1) w 一对多联系 (1:n) w 多对一联系 (n:1) w 多对多联系 (m:n)
12
11 Entity Sets uEntity = “thing” or object. uEntity set = collection of similar entities. wSimilar to a class in object-oriented languages. uAttribute = property of (the entities of) an entity set. wAttributes are simple values, e.g. integers or character strings.
13
12 E/R Diagrams uIn an entity-relationship diagram: wEntity set = rectangle. wAttribute = oval, with a line to the rectangle representing its entity set.
14
13 Example uEntity set Stars has two attributes, name and address. uEach star has values for these two attributes. Stars name address
15
14 Relationships uA relationship connects two or more entity sets. uIt is represented by a diamond, with lines to each of the entity sets involved.
16
15 Example Filmtype P41 The arrow pointing to entity set studios indicates that each movie is owned by a unique studio. Studios address name Stars address name Movies title length year Stars_in Owns
17
16 Relationship Set uThe current “value” of an entity set is the set of entities that belong to it. wExample: the set of all stars in our database. uThe “value” of a relationship is a set of lists of currently related entities, one from each of the related entity sets.
18
17 Example uFor the relationship Stars_ in, we might have a relationship set like: MoviesStars Basic InstinctSharon Stone Total RecallArnold Schwarzenegger Total RecallSharon Stone
19
18 Multiway Relationships uSometimes, we need a relationship that connects more than two entity sets.
20
19 A Typical Relationship Set StudiosMoviesstars Universal StudioKing KongJack Black
21
20 Many-Many Relationships uIn a many-many relationship, an entity of either set can be connected to many entities of the other set. wE.g., a star can appear in many movies; a movie can have more than one star.
22
21 Many-One Relationships uSome binary relationships are many -one from one entity set to another. wEach entity of the first set is connected to at most one entity of the second set. wBut an entity of the second set can be connected to zero, one, or many entities of the first set.
23
22 Example uOwns, from Movies to Studios is many- one. wA movie is owned by at most one studio. wBut a studio owns any number of movies.
24
23 One-One Relationships uIn a one-one relationship, each entity of either entity set is related to at most one entity of the other set. uExample: Relationship Runs between entity sets Studios and Presidents. wA president can run only one studio and a studio has only one president.
25
24 Representing “Multiplicity” uShow a many-one relationship by an arrow entering the “one” side. uShow a one-one relationship by arrows entering both entity sets.
26
25 实体联系图示 一对一、一对多、多对一、多对多
27
26 Attributes on Relationships uSometimes it is useful to attach an attribute to a relationship. uThink of this attribute as a property of tuples in the relationship set.
28
27 Example Moviesstars Contracts salary It is appropriate to associate a salary with the (star, movie, studio) triple in the relationship set for the Contracts relationship. studios
29
28 Keys uA key is a set of attributes for one entity set such that no two entities in this set agree on all the attributes of the key. wIt is allowed for two entities to agree on some, but not all, of the key attributes. uWe must designate a key for every entity set. uKeys in E/R Diagrams: Underline the key attribute(s)
30
29 Example: a Multi-attribute Key Courses dept number hoursroom Note that hours and room could also serve as a key, but we must select only one key.
31
30 Design Techniques 1.Avoid redundancy. 2.Don’t use an entity set when an attribute will do.
32
31 Avoiding Redundancy uRedundancy occurs when we say the same thing in two or more different ways. uRedundancy wastes space and (more importantly) encourages inconsistency. wThe two instances of the same fact may become inconsistent if we change one and forget to change the other.
33
32 Example: Good This design gives the name of each studio exactly once. MoviesStudios Owns titlenameaddr year
34
33 Example: Bad MoviesStudios Owns title This design states the name of each studio twice: as an attribute and as a related entity. nameaddr year StudioName
35
34 Example: Bad This design repeats the studio’s address once for each movie and loses the address if there are temporarily no movie for a studio. Movies titleyearStudioName StudioAddr
36
35 Entity Sets Versus Attributes uAn entity set should satisfy at least one of the following conditions: wIt is more than the name of something; it has at least one nonkey attribute. or wIt is the “many” in a many-one or many-many relationship.
37
36 Example: Good MoviesStudio Owns title Studio deserves to be an entity set because of the nonkey attribute addr. Movies deserves to be an entity set because it is the “many” of the many-one relationship Owns. nameaddr
38
37 Example: Good Movies title There is no need to make the studio an entity set, because we record nothing about studios besides their name. StudioName
39
38 Example: Bad MoviesStudios Owns title Since the Studios is nothing but a name, and is not at the “many” end of any relationship, it should not be an entity set. name
40
39 两个实体型间的联系 班级 班级 - 班长 班长 1 1 1:1 联系 班级 组成 学生 1 n 1:n 联系 课程 选修 学生 m n m:n 联系
41
40 多个实体型间的联系 u 多个实体型间的一对多联系 w 若实体集 E1 , E2 ,... , En 存在联系,对于实体集 Ej ( j=1 , 2 ,... , i-1 , i+1 ,... , n )中的给定实体,最多只和 Ei 中的一个 实体相联系,则 Ei 与 E1 , E2 ,... , Ei-1 , Ei+1 ,... , En 之间是 一对多联系 w 实例:课程、教师与参考书三个实体型 u 多个实体型间的一对一联系 u 多个实体型间的多对多联系 课 程课 程 教 员教 员参考书 讲 授讲 授 1 m n
42
41 课堂思考 u 三个实体间的联系 u 判断联系种类 供应商 项 目项 目零 件零 件 供 应供 应 m np
43
42 同一实体集内各实体间的联系 u 一对多联系 w 实例 职工实体集内部具有领导与被领导的联系 某一职工(干部) “ 领导 ” 若干名职工,一个职工 仅被另外一个职工直接领导 这是一对多的联系 u 一对一联系 u 多对多联系 职 工 领导 1 n
44
43 Other Example 矩形:表示实体集 菱形:表示联系集 线:连接实体集与联系集或属性与实体集 椭圆:表示属性 下划线:主码属性
45
44 学生实体例 u 学生实体及属性 学生 学号 姓名 性别 出生年月 入学时间 系
46
45 联系及其属性例 供应商 供应 项 目 零 件 供应量 课程 选修 学生 m n 成绩
47
46 实例练习 u 实体 w 系、班级、学生、教研室、教师、课程 u 给出概念模型: E - R 图
48
47 Reading Guide uRequired: 2.2,2.5 uRecommended: 2.3,2.8, 《数据库系统概论》第一章
49
48 Exercises To manage the materials of a factory, please give the E-R diagrams. Such entity sets are involved as below: uWarehouse: with attributes as Number, area and telephone uAccessory : with attributes as number, Name, Specification, price and description uSupplier: with attributes as Number, name, addr, telephone and account uProject : with attributes as number and budget uEmployee : with attributes as number, name and age The relationships among these entity sets are listed as below: uA kind of accessory can be deposited in more than one warehouse. A warehouse can accommodate more than one kind of accessory. uThere can be more than one employee in a warehouse, and a employee can work in only one warehouse. uSome employee can leader others. uA supplier can supply various accessories to various projects. A project can use accessories supplied by various suppliers. A kind of accessory can be supplied by various supplier.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.