View
218
Download
0
Category
Tags:
Preview:
Citation preview
2
Chapter ObjectivesChapter Objectives
Learn the conceptual foundation of the Learn the conceptual foundation of the relational modelrelational model
Understand how relations differ from Understand how relations differ from nonrelational tablesnonrelational tables
Learn basic relational terminologyLearn basic relational terminology Learn the meaning and importance of Learn the meaning and importance of
keys, foreign keys, and related keys, foreign keys, and related terminology terminology
Understand how foreign keys represent Understand how foreign keys represent relationshipsrelationships
3
Chapter Objectives Chapter Objectives (continued)(continued)
Learn the purpose and use of surrogate Learn the purpose and use of surrogate keyskeys
Learn the meaning of functional Learn the meaning of functional dependenciesdependencies
Learn to apply a process for normalizing Learn to apply a process for normalizing relationsrelations
4
EntityEntity
An An entityentity is something of importance is something of importance to a user that needs to be to a user that needs to be represented in a databaserepresented in a database
An entity represents one theme or An entity represents one theme or topictopic
In an entity-relationship model In an entity-relationship model (discussed in Chapter 4), entities are (discussed in Chapter 4), entities are restricted to things that can be restricted to things that can be represented by a single tablerepresented by a single table
5
RelationRelation
A A relation relation is a two-dimensional table is a two-dimensional table that has specific characteristicsthat has specific characteristics
The table dimensions, like a matrix, The table dimensions, like a matrix, consist of rows and columnsconsist of rows and columns
6
Characteristics of a Characteristics of a RelationRelation
Rows contain data about an entityRows contain data about an entity Columns contain data about attributes of Columns contain data about attributes of
the entitythe entity Cells of the table hold a single valueCells of the table hold a single value All entries in a column are of the same All entries in a column are of the same
kindkind Each column has a unique nameEach column has a unique name The order of the columns is unimportantThe order of the columns is unimportant The order of the rows is unimportantThe order of the rows is unimportant No two rows may be identicalNo two rows may be identical
7
A Sample RelationA Sample Relation
EmployeeNumEmployeeNumberber
FirstNaFirstNameme
LastNamLastNamee
100100 MaryMary AbermanyAbermany
101101 JerryJerry CalderaCaldera
104104 AleaAlea CopleyCopley
107107 MuruganMurugan JacksoniJacksoni
8
A Nonrelation ExampleA Nonrelation Example
EmployeeNumEmployeeNumberber
PhonePhone LastNamLastNamee
100100 335-335-6421,6421,
454-9744454-9744
AbermanyAbermany
101101 215-7789215-7789 CalderaCaldera
104104 610-9850610-9850 CopleyCopley
107107 299-9090299-9090 JacksoniJacksoni
Cells of the table hold multiple values
9
EmployeeNumEmployeeNumberber
PhonePhone LastNamLastNamee
100100 335-6421335-6421 AbermanyAbermany
101101 215-7789215-7789 CalderaCaldera
104104 610-9850610-9850 CopleyCopley
100100 335-6421335-6421 AbermanyAbermany
107107 299-9090299-9090 JacksoniJacksoni
No two rows may be identical
A Nonrelation ExampleA Nonrelation Example
14
TerminologyTerminology
TableTable RowRow ColumnColumn
File or File or DatafileDatafile
RecordRecord FieldField
RelationRelation TupleTuple AttributeAttribute
Synonyms…
15
A KeyA Key
A A keykey is one (or more) columns is one (or more) columns of a relation that is (are) used to of a relation that is (are) used to identify a rowidentify a row
16
Uniqueness of KeysUniqueness of Keys
Unique KeyUnique Key Nonunique KeyNonunique Key
Data value is Data value is unique for each unique for each row.row.
Consequently, the Consequently, the key will uniquely key will uniquely identify a row.identify a row.
Data value may be Data value may be shared among shared among several rows.several rows.
Consequently, the Consequently, the key will identify a key will identify a set of rows.set of rows.
17
A Composite KeyA Composite Key
A A composite keycomposite key is a key that is a key that contains two or more attributescontains two or more attributes
For a key to be unique, often it For a key to be unique, often it must become a composite keymust become a composite key
18
Composite Key ExampleComposite Key Example
To identify a family member, you To identify a family member, you need to know a FamilyID, a need to know a FamilyID, a FirstName, and a Suffix (e.g., Jr.) FirstName, and a Suffix (e.g., Jr.)
The composite key is:The composite key is:
(FamilyID, FirstName, Suffix)(FamilyID, FirstName, Suffix) One needs to know the value of all One needs to know the value of all
three columns to uniquely identify three columns to uniquely identify an individualan individual
19
A Candidate KeyA Candidate Key
A A candidate keycandidate key is called is called “candidate” because it is a “candidate” because it is a candidate to become the primary candidate to become the primary keykey
A candidate key is a unique keyA candidate key is a unique key
20
A Primary KeyA Primary Key
A primary key is a candidate key A primary key is a candidate key chosen to be the main key for the chosen to be the main key for the relationrelation
If you know the value of the If you know the value of the primary key, you will be able to primary key, you will be able to uniquely identify a single rowuniquely identify a single row
21
Relationship NotationRelationship Notation
EMPLOYEE (EmployeeNumber, EMPLOYEE (EmployeeNumber, FirstName, LastName, FirstName, LastName, Department, Email, Phone)Department, Email, Phone)
DEPARTMENT (DeptName, DEPARTMENT (DeptName, BudgetCode, OfficeNumber)BudgetCode, OfficeNumber)
22
Specifying Primary Specifying Primary KeysKeys
EMPLOYEE (EMPLOYEE (EmployeeNumberEmployeeNumber, , FirstName, LastName, FirstName, LastName, Department, Email, Phone)Department, Email, Phone)
DEPARTMENT (DEPARTMENT (DeptNameDeptName, , BudgetCode, OfficeNumber)BudgetCode, OfficeNumber)
23
Relationships Between Relationships Between TablesTables
A table may be related to other A table may be related to other tablestables
For exampleFor example An Employee works in a DepartmentAn Employee works in a Department A Manager controls a ProjectA Manager controls a Project
24
A Foreign KeyA Foreign Key
To preserve relationships, you may To preserve relationships, you may need to create a need to create a foreign keyforeign key
A foreign key is a primary key from A foreign key is a primary key from one table placed into another tableone table placed into another table
The key is called a foreign key in the The key is called a foreign key in the table that received the keytable that received the key
25
Foreign Key ExampleForeign Key Example
PROJECTPROJECT
ProjIDProjID
ProjNameProjName
MgrIDMgrID
MANAGEMANAGERR
MgrIDMgrID
MgrNameMgrName
Foreign Key
Primary Key
PROJECT (ProjID, ProjName, MgrID)MANAGER (MgrID, MgrName)
26
DEPARTMEDEPARTMENTNT
DeptIDDeptID
DeptNameDeptName
LocationLocation
EMPLOYEEEMPLOYEE
EmpIDEmpID
DeptIDDeptID
EmpNameEmpNameForeign Key
Primary Key
Foreign Key ExampleForeign Key Example
DEPARTMENT (DeptID, DeptName, Location)
EMPLOYEE (EmpID, DeptID, EmpName)
27
Referential IntegrityReferential Integrity
Referential integrity states that every Referential integrity states that every value of a foreign key must match a value of a foreign key must match a value of an existing primary keyvalue of an existing primary key
For example (see previous slide)For example (see previous slide) If EmpID = 4 in EMPLOYEE has a DeptID If EmpID = 4 in EMPLOYEE has a DeptID
= 7 (a foreign key), a Department with = 7 (a foreign key), a Department with DeptID = 7 must exist in DEPARTMENTDeptID = 7 must exist in DEPARTMENT
The primary key value must exist before The primary key value must exist before the foreign key value is enteredthe foreign key value is entered
29
EQUIPMENT (SerialNumber, Type, AcquisitionCost, EmployeeNumber)
Suppose Equipment can be assigned to Employees. Primary key of EMPLOYEE is EmployeeNumber.
Constraint: EmployeeNumber in EQUIPMENT must exist in EmployeeNumber in EMPLOYEE
30
A Surrogate KeyA Surrogate Key
A A Surrogate KeySurrogate Key is a unique, is a unique, numeric value that is added to a numeric value that is added to a relation to serve as the Primary Keyrelation to serve as the Primary Key
Surrogate Key values have no Surrogate Key values have no meaning to users and are usually meaning to users and are usually hidden on forms, queries and reportshidden on forms, queries and reports
A Surrogate Key is often used in A Surrogate Key is often used in place of a composite primary keyplace of a composite primary key
31
Surrogate Key Surrogate Key ExampleExample
If the Family Member Primary Key is If the Family Member Primary Key is FamilyID, FirstName, Suffix, it would be FamilyID, FirstName, Suffix, it would be easier to append and use a surrogate key of easier to append and use a surrogate key of FamMemberIDFamMemberID
FamilyID, FirstName and Suffix remain in the FamilyID, FirstName and Suffix remain in the relationrelation
Referential Integrity:Referential Integrity:Use…Use… (FamMemberID) in School must exist in(FamMemberID) in School must exist in
(FamMemberID) in FamilyMember(FamMemberID) in FamilyMemberInstead of: Instead of:
(FamilyID, FirstName, Suffix) in School must exist in (FamilyID, FirstName, Suffix) in School must exist in
(FamilyID, FirstName, Suffix) in FamilyMember (FamilyID, FirstName, Suffix) in FamilyMember
32
Surrogate Key Surrogate Key ExampleExample
Landscaping company – has tables:Landscaping company – has tables:
PROPERTY (PROPERTY (StreetStreet, , CityCity, , StateState, , ZipZip))
PLANT (PLANT (ItemNumberItemNumber, VarietyName, , VarietyName, Price)Price)
SERVICE (SERVICE (InvoiceNumberInvoiceNumber, Date, , Date, TotalHours)TotalHours)
35
The RelationshipsThe Relationships
• A plant is sold for a particular A plant is sold for a particular property.property.
• A service is rendered for a A service is rendered for a particular property.particular property.
PROPERTY (PROPERTY (StreetStreet, , CityCity, , StateState, , ZipZip))
PLANT (PLANT (ItemNumberItemNumber, VarietyName, , VarietyName, Price, Price, StreetStreet, , CityCity, , StateState, , ZipZip))
SERVICE (SERVICE (InvoiceNumberInvoiceNumber, Date, , Date, TotalHours, TotalHours, Street Street , , CityCity, , StateState, , ZipZip))
36
The ConstraintsThe Constraints
(Street, City, State, Zip) in PLANT (Street, City, State, Zip) in PLANT
must exist in (Street, City, State, must exist in (Street, City, State, Zip) in PROPERTYZip) in PROPERTY
(Street, City, State, Zip) in (Street, City, State, Zip) in SERVICE SERVICE
must exist in (Street, City, State, must exist in (Street, City, State, Zip) in PROPERTYZip) in PROPERTY
37
Fixing the MessFixing the Mess
That’s a lot of duplication in each table.That’s a lot of duplication in each table.
Let’s add surrogate keys to the tables.Let’s add surrogate keys to the tables.
PROPERTY (PROPERTY (PropertyIDPropertyID, Street, City, , Street, City, State, Zip)State, Zip)
PLANT (PLANT (ItemNumberItemNumber, VarietyName, , VarietyName, Price, Price, PropertyIDPropertyID))
SERVICE (SERVICE (InvoiceNumberInvoiceNumber, Date, , Date, TotalHours, TotalHours, PropertyIDPropertyID))
38
The New ConstraintsThe New Constraints
PropertyID in PLANT must exist in PropertyID in PLANT must exist in PropertyID in PROPERTYPropertyID in PROPERTY
PropertyID in SERVICE must exist PropertyID in SERVICE must exist in PropertyID in PROPERTYin PropertyID in PROPERTY
Recommended