Download presentation
Presentation is loading. Please wait.
Published byMarsha Taylor Modified over 8 years ago
1
Chapter 2 The Relational Model
2
2-2 In This Chapter You Will Learn What a data model is and what its uses are The terminology of the relational model How tables are used to represent data Properties of database tables How to identify candidate, primary, and foreign keys The meaning of entity integrity (實體完整限制式) and referential integrity (參照完整限制式)
3
2-3 What Is A Data Model? An integrated collection of concepts for describing data, relationships between data, and constraints on the data. A data model attempts to represent the operation of a company or part of the company, that you wish to model. It provides the essential concepts and notations that allow database designers and end-users to communicate their understanding of the company unambiguously and accurately.
4
2-4 Three Components Of A Data Model A Structure Part How the database is to be constructed A Manipulative Part Types of data operations that are allowed Updating or retrieving data Changing the structure of the database A Set Of Integrity Rules Ensures that the data is accurate
5
2-5 Terminology of Relational Model Relation (關聯、關係) Table with columns and rows Columns ~ Attributes Rows ~ Individual records We use tables to hold information about the objects that we want to represent in the database. Attribute (屬性) Named column of a table Can appear in any order, and the table will still be the same
6
2-6 A case example : StayHome Video Rental Company The information on branches ( 分店 ) is represented by the Branch relation, with columns for attributes branchNo, street, city, state, zipCode, and mgrStaffNo. The information on staff is represented by the Staff relation, with columns for attributes staffNo, name, position, salary, and branchNo. Terminology of Relational Model
7
2-7 An example of the Branch and Staff relations
8
2-8 Domain (定義域) A set of allowable (possible) values for one or more attributes Every attribute in a relational database is associated with a domain May be distinct for each attribute. Or, two or more attributes may be associated with the same domain Terminology of Relational Model
9
2-9 Domain Example (For Some Attributes of Branch Relation
10
2-10 Tuple (值組) A record of a relation In the Staff relation, each record contains five values, one for each attribute. A Relational Database A collection of normalized tables with distinct table names. Terminology of Relational Model
11
2-11 Seven Properties of Relational Tables 1. 1.Table name is distinct from all other table names in the database. 2. 2.Each column has a distinct name in a table. 3. 3.Each cell of a table contains exactly one value. To store several telephone number for a single branch in a single cell is wrong (No repeating groups.) A table satisfying this property is said to be normalized or in first normal ( 正規化 ) form.
12
2-12 Seven Properties of Relational Tables 4. 4.Values of a column are all from the same domain 5. 5.Each record is distinct. There are no duplicate records. 6. 6.Order of columns has no significance (To users and programmers) 7. 7.Order of records has no significance. (To users and programmers)
13
2-13 Relational Keys Candidate key (候選鍵) Primary key (主鍵) Foreign key (外部鍵、外來鍵、外鍵)
14
2-14 Candidate Key (候選鍵) A column, or a set of columns, that uniquely identifies a record within a table. Uniqueness (唯一性) : In a table, the value of the candidate key uniquely identifies a record Irreducibility (不可減少性,最小性 ) : No proper subset of the candidate key has the uniqueness property The key cannot be made smaller or simpler for unique identification There may be many candidate keys for a table When a key consists of more than one column, we call it a composite key (組合鍵).
15
2-15 Candidate Key (候選鍵) Can city be a candidate key? Can zipCode be a candidate key? Can branchNo be a candidate key? Can mgrStaffNo be a candidate key? Branch relation
16
2-16 Candidate Key (候選鍵) Can actorNo be a candidate key? Role Table The characters played by actors in videos Can catalogNo be a candidate key? actorNocatalogNo? actorNo + catalogNo? actorNo+ character? actorNo + character?
17
2-17 Keys Primary Key (主鍵) The candidate key selected to identify records uniquely for a given table. A table always has a primary key. The Worst Case: The entire set of columns serve as the primary key
18
2-18 Rules To Select Primary Key With minimal number of attributes Less likely to have its values changed (or never changed) Less likely to lose uniqueness in the future Always have values With fewest characters (for those with textual attribute) With the smallest maximum value (for numerical attribute) Easiest to use from the users’ point of view
19
2-19 Foreign Key (外部鍵、外索引) A column, or set of columns, within one table that matches the primary key of some (possibly same) table. When a column appears in more than one table, its appearance represents a relationship between the records of the two tables
20
2-20 Relational Keys Parent/Home Table Primary Key Foreign Key Child Table
21
2-21 Functions of Keys 1.Who is the manager of the branch at Portland, OR ? 2.Who are the managers of the branch at Seattle, WA ? 3. Who is Sally Adams’s manager ?
22
2-22 Relational Keys of COMPANY Database (PK and FK)
23
2-23 Representing Relational Databases (The StayHome Database) Branch ( branchNo, street, city, state, zipCode, mgStaffNo) Staff ( staffNo, name, position, salary, branchNo) Video ( catalogNo, title, category, dailyRental, price, directorNo) Director ( directorNo, directorName) Actor ( actorNo, catalogNo) Role ( actorNo, catalogNo, character) Member ( memberNo, fName, lName, address) Registration ( branchNo, memberNo, staffNo, dateJoined) RentalAgreement ( rentalNo, dateOut, dateReturn, memberNo, videoNo) VideoForRent ( videoNo, available, catalogNo, branchNo) data example data example
24
2-24 Data example of StayHome DB (1)
25
2-25 Data example of StayHome DB (2)
26
2-26 Relational Integrity (關聯完整限制式、關聯的完整性) Ensures that the data of a database is accurate Constraints that apply to the entire database Entity Integrity (實體完整限制式) Referential Integrity (參照完整限制式) Before defining these two constraints, let’s look at Null ( 虛值) Business Rules
27
2-27 Null (虛值、空值) Represents a value for a column that is currently unknown or is not applicable ( 不適用 ) for a record. Deals with incomplete or exceptional data. Examples A branch temporarily without a manager
28
2-28 Business Rules (企業規則) Specify additional constraints that the data must satisfy. Represented in the form of rules that define or constrain some operations of the organization. Example A member can only rent a maximum of 10 videos at any one time
29
2-29 Entity Integrity (實體完整限制式、實體的完整性) Applies to the primary key of a base table. In a base table, no column of a primary key can be null. A base table is a named table whose records are physically stored in the database. Note: A view is a ‘virtual table’ that does not actually exist in the database but is generated by the DBMS from some underlying base tables dynamically.
30
2-30 Referential Integrity (參照完整限制式、參照的完整性) If foreign key exists in a table, either the foreign key value of a record in the table must match a candidate key value of some record in its parent/home table, or the foreign key value must be wholly null. Example: Not possible to create a staff record with branch number B300 (since B300 does not exist.) Can create a new staff record with a null branch number if null value is allowed
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.