You are on page 1of 40

Database Lecture Notes Mapping ER Diagrams to Tables 1

Dr. Meg Murray mcmurray@kennesaw.edu

The Physical Relational Model


Tables
Map to Entities

Attributes
Map to Columns

Identifiers
Map to Primary Keys

Relationships
Map to Foreign Keys

Relation
A relation is a two-dimensional table that has specific characteristics The table dimensions, like a matrix, consist of rows and columns

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

Characteristics of a Relation
Rows contain data about an entity Columns contain data about attributes of the entity Cells of the table hold a single value All entries in a column are of the same kind Each column has a unique name The order of the columns is unimportant The order of the rows is unimportant No two rows may be identical
KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

A Sample Relation
EmployeeNumber 100 101 104 107 FirstName Mary Jerry Alex Megan LastName Abernathy Cadley Copley Jackson

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

A Nonrelation Example
Cells of the table hold multiple values EmployeeNumber Phone 100 335-6421, 454-9744 101 215-7789 104 610-9850 107 299-9090
KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

LastName Abernathy Cadley Copley Jackson

A Nonrelation Example
No two rows may be identical
EmployeeNumber 100 101 104 100 107
KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

Phone 335-6421 215-7789 610-9850 335-6421 299-9090

LastName Abernathy Cadley Copley Abernathy Jackson

Terminology
Synonyms Table File Relation Row Record Tuple Column Field Attribute

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

A Key
A key is one (or more) columns of a relation that is (are) used to identify a row

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

Uniqueness of Keys
Unique Key Data value is unique for each row. Consequently, the key will uniquely identify a row. Nonunique Key Data value may be shared among several rows. Consequently, the key will identify a set of rows.

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

A Composite Key
A composite key is a key that contains two or more attributes For a key to be unique, often it must become a composite key

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

Composite Key Example


To identify a family member, you need to know a FamilyID, a FirstName, and a Suffix (e.g., Jr.) The composite key is: (FamilyID, FirstName, Suffix) One needs to know the value of all three columns to uniquely identify an individual

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

A Candidate Key
A candidate key is called candidate because it is a candidate to become the primary key A candidate key is a unique key

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

A Primary Key
A primary key is a candidate key chosen to be the main key for the relation If you know the value of the primary key, you will be able to uniquely identify a single row

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

A Surrogate Key
A Surrogate Key is a unique, numeric value that is added to a relation to server as the Primary Key Surrogate Key values have no meaning to users and are usually hidden of forms, queries and reports A Surrogate Key is often used in place of a composite primary key
KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

Surrogate Key Example


If the Family Member Primary Key is FamilyID, FirstName, Suffix, it would be easier to append and use a surrogate key of FamMemberID FamilyID, FirstName and Suffix remain in the relation

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

Relationships Between Tables


A table may be related to other tables For example
An Employee works in a Department A Manager controls a Project

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

A Foreign Key
To preserve relationships, you may need to create a foreign key A foreign key is a primary key from one table placed into another table The key is called a foreign key in the table that received the key

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

Foreign Key Example


Project
Primary Key

Manager
MgrID MgrName
Foreign Key

ProjID ProjName

MgrID

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

Foreign Key Example


Department
Primary Key

Employee
EmpID DeptID
Foreign Key

DeptID DeptName

Location

EmpName

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

Foreign Key Example

DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall

6-21

Primary and Foreign Keys


STANDARD REPRESENTATION: The primary keys of the relations are underlined and any foreign keys are in italics like below:
DEPARTMENT (DepartmentName, BudgetCode, ManagerName) EMPLOYEE (EmployeeNumber, EmployeeName, DepartmentName)

[if you do not have italics, the foreign key is shown double underlined]

DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall

3-22

Transforming a Data Model into a Relational Design

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

Mapping Entities to Tables

Representing an Entity as a Table


Primary key is designated by key symbol

ITEM (ItemNumber, Description, Cost, ListPrice, QuantityOnHand)

KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) 2008 Pearson Prentice Hall

Select the Primary Key


Unique identifier May be single or composite The ideal primary key is short, numeric and fixed Can NEVER be null

DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall

6-26

Surrogate Key
Supplied identifier generated by a DBMS
Assigned when a row is created They never change In simpler DBMS they are simply a sequential numbering scheme

Use if no good candidate for primary key Surrogate keys meet the ideal, but have no meaning to users
DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall 6-27

Surrogate Key
In the ITEM table, ItemNumber is a surrogate key The data type of Identity shows this in this example it is SQL SERVER identifier

Specify Candidate (Alternate) Keys


Candidate keys are alternate identifiers of unique rows in a table The terms candidate key and alternate key are synonymous

They are used to ensure uniqueness


Can be null
DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall 6-29

Specify Candidate (Alternate) Keys

In the AKn.m notation, n is the number of the alternate key, and m is the column number in that alternate key
DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall 6-30

Data Dictionary
Description of data objects - the meta data Usually presented in a table form with the following information:
Attribute name and table in which it is contained Data type Whether it can be null Whether it is a primary key Whether it is a foreign key
If so the corresponding table

Whether it has to be unique Default Value Data domain/constraints


6-31

DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall

6-32

Specify Column Properties: Null Status


Null status indicates whether or not the value of the column can be NULL

DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall

6-33

Specify Column Properties: Data Type


Generic Data Types:
CHAR(n) VARCHAR(n) DATE TIME MONEY INTEGER DECIMAL

DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall

6-34

Specify Column Properties: SQL Server Data Types

DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall

6-35

Specify Column Properties: Oracle Data Types

DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall

6-36

Specify Column Properties: Default Value


A default value is the value supplied by the DBMS when a new row is created

DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall

6-37

Specify Column Properties: Data Constraints


Data constraints are limitations on data values:
Domain constraint - Column values must be in a given set of specific values Range constraint - Column values must be within a given range of values Intrarelation constraint Column values are limited by comparison to values in other columns in the same table Interrelation constraint - Column values are limited by comparison to values in other columns in other tables
DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall 6-38

VERIFY NORMALIZATION

DAVID M. KROENKES DATABASE PROCESSING, 10th Edition 2006 Pearson Prentice Hall

6-39

Normalization
We havent talked about this yet just note it is a check of efficiency of the tables created The tables should be normalized based on the data model

6-40

You might also like