Download presentation
Presentation is loading. Please wait.
Published byTyler Lang Modified over 9 years ago
1
1 The Relational Data Model Tables Schemas Conversion from E/R to Relations
2
2 The things you should know… uThe basic principle of relational database is proposed by E.F.Codd in 1970. uThe first RDBMS production is System R uThe most popular RDBMS DB2, Oracle, Ingres, Sybase, Informix,SQL Server…
3
3 Basic of the Relational Model uThe relational model gives us a single way to represent data: as a two- dimensional table called a relation.
4
4 uAttributes ( 属性 ) wAttributes of a relation serve us as names for the columns of the relation. wUsually, the attributes describes the meaning of entries in the column below.
5
5 uSchemas (模式) wThe name of a relation and the set of attributes for a relation is called the schema for that relation. wWe show the schema for the relation with the relation name followed by a parenthesized list of its attributes.
6
6 uTuples (元组) The rows of a relation, other than the header row containing the attributes, are called tuple. For example: wRelations, however, are sets of tuples, and it is impossible for a tuple to appear more than once in a given relation.
7
7 uDomains (域) wThe relational model requires that each component of each tuple be atomic; that is, it must be of some elementary type such as integers or string. wEach attributes of a relation has particular elementary type, thus domain is decided.
8
8 uEquivalent Representations of a Relation wThe attributes of the relation can be reordered without change the relation.
9
9 uRelation Instance (关系实例) wA relation about movies is not static; rather, relations change over time. wIt is not common for the schema of a relation to change. wWe shall call a set of tuples for a given relation an instance of that relation.
10
10 An Example of Relation Instance Relation: Person (Name, Address, Telephone) Relation Instance: NameAddressTelephone Bob123 Main St555-1234 Bob128 Main St555-1235 Pat123 Main St555-1235 Harry456 Main St555-2221 Sally456 Main St555-2221 Sally456 Main St555-2223 Pat12 State St555-1235
11
11 More … uRelation (Instance) = a set of tuples uDatabase = collection of relations uRelation schema = relation name + attributes wExample: Movies (title, year, length, fileType) uDatabase schema = a set of all relation schemas wMovies(Title, Year, Length, FileType) wStar(Name, Age) wStudio(StudioName, Addr)
12
12 Name Addr Tel N1 A1 T1 N2 A2 T2 N3 A3 T3 N4 T4 N5 T5 T6 T7 Name Addr Tel N1 A1 T1 N1 A1 T2 N1 A1 T3. N1 A1 T7 N1 A2 T1 N1 A3 T1 N2 A1 T1 Tuple Domain Component Attribute
13
13 From E/R Diagrams to Relations The problems in this step are how to transform the entity, relationship between entities into relational models,how to decide the attributes and the keys of these relational models.
14
14 uEntity set wOne entity is transformed to one relation, the attributes of the entity are the attributes of the relational schema, and the key of the entity is the key of the relational schema. From E/R Diagrams to Relations ( Cont. ) Entity set -> relation. Attributes -> attributes.
15
15 Entity Set -> Relation Relation: Stars(name, address) Stars name address
16
16 Relationship (1) 1:1 relationship can be transformed into one relation schema, the keys of the entities related to this relationship, and the attributes of this relationship all are transformed into attributes of the relation schema, the keys of each entity related to this relationship are the candidate key of the relation schema. Also, the relationship can be combined with the relation schema corresponding to one entity related to this relationship, in this case, the key of entity in the other end and the attribute of the relationship must be added into the relation; From E/R Diagrams to Relations ( Cont. )
17
17 (2) Relationship 1:n can be treated as in (2), except that if it is transformed into one independent relation schema, the key of it will be the key in end ‘n’. (3) Relationship m:n is transformed into one independent relation schema, the keys of every entities related to this relationship and the attributes of this relationship are all transformed into attributes of the relation schema, the key of the relation schema is the group of the keys of every entities related to this relationship. (4) Multi-relationship among three or more entities can be treated as in (3); (5) Relation schema with the same key ca be combined. From E/R Diagrams to Relations ( Cont. )
18
18 uRelationship -> Relation Owns (title, year, studioname) Stars-in (title, year, starName)
19
19 Combining Relations uOK to combine into one relation: 1.The relation for an entity-set E 2.The relations for many-one relationships of which E is the “many.”
20
20 Example Movies(title, year,length,filmType) and Owns (title, year, studioname) combine to Movies(title, year,length,filmType,studioname).
21
21 Risk with Many-Many Relationships uCombining Movies with Star would be a mistake. It leads to redundancy, as: Redundancy titleyearlengthfilm typestudioNamestarName Star Wars Mighty Ducks Wayne’s World 1977 1991 1992 124 104 95 color Fox Disney Paramount Carrie Fisher Mark Hamill Harrison Ford Emilio Estevez Dana Carvey Mike Meyers
22
22 E-R 图向关系模型的转换原则 一个实体转换为一个关系模式,实体的属性就 是关系的属性,实体的码就是关系的码 对实体间的联系 一个 1:1 联系可以转换为一个独立的关系模式,也可 以与任意对应的关系模式合并 一个 1:n 联系可以转换为一个独立的关系模式,也可 以与 n 端对应的关系模式合并 一个 m:n 联系转换为一个关系模式 三个或三个以上实体间的一个多元联系可以转换为 一个关系模式 具有相同码的关系模式可以合并 中文:
23
23 如公司部门管理系统的 E-R 图及其转换成的关系模式 部门项目 职工 电话 包括 承担 n 1 n1 办公室号,面积项目号,预算费 参与 m n 办公室 包含 1n 包括 n 1 部门号,预算费, 领导人职工号 电话号码,说明 职工号,姓名,办公电话 分担任务 部门 ( 部门号,部门预算费,领导人职工号 ) 职工 ( 职工号,姓名,办公电话,部门号 ) 办公室 ( 办公室号,面积,部门号 ) 项目 ( 项目号,项目预算费,部门号 ) 电话 ( 电话号码,说明,办公室号 ) 项目承担情况 ( 职工号,项目号,分担任务 )
24
24 Handling Weak Entity Sets uRelation 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. uA supporting relationship is redundant and yields no relation (unless it has attributes).
25
25 Example LoginsHostsAt name Hosts(hostName, location) Logins(loginName, hostName, billTo) At(loginName, hostName, hostName2) Must be the same billTo At becomes part of Logins location
26
26 Subclasses: Three Approaches 1.Object-oriented : One relation per subset of subclasses, with all relevant attributes. 2.Use nulls : One relation; entities have NULL in attributes that don’t belong to them. 3.E/R style : One relation for each subclass: wKey attribute(s). wAttributes of that subclass.
27
27 Example Beers Ales isa name manf color
28
28 Object-Oriented namemanf BudAnheuser-Busch Beers name manfcolor Summerbrew Pete’sdark Ales Good for queries like “find the color of ales made by Pete’s.”
29
29 E/R Style namemanf Bud Anheuser-Busch Summerbrew Pete’s Beers name color Summerbrew dark Ales Good for queries like “find all beers (including ales) made by Pete’s.”
30
30 Using Nulls namemanf color Bud Anheuser-Busch NULL Summerbrew Pete’s dark Beers Saves space unless there are lots of attributes that are usually NULL.
31
31 Exercise uRequired: Convert the E-R diagram you designed in last exercise into a relational database schema.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.