Professional Documents
Culture Documents
Learning Objectives
Define each of the following database terms Relation Primary key
Foreign key
Referential integrity Field
Data type
Null value Discuss the role of designing databases in the analysis and design of an information system Learn how to transform an entity-relationship (ER) Diagram into an equivalent set of well-structured 9.2 relations
9.4
4
Logical Design
Based upon the conceptual data model Four key steps
1. Develop a logical data model for each known user interface for the application using normalization principles. Combine normalized data requirements from all user interfaces into one consolidated logical database model Translate the conceptual E-R data model for the application into normalized data requirements Compare the consolidated logical database design with the translated E-R model and produce one final logical database model for the application
2. 3. 4.
9.6
9.7
7
A named, two-dimensional table of data. Each relation consists of a set of named columns and an arbitrary number of unnamed rows Properties
Entries in cells are simple
Entries in columns are from the same set of values Each row is unique The sequence of columns can be interchanged without changing the meaning or use of the relation
9.8
9.9
9
2. Represent relationships
3. Normalize the relations 4. Merge the relations
9.10
10
9.11
11
9.12
12
13
9.14
14
9.15
15
9.16
16
9.17
17
9.18
18
that both take their values from the same primary key
9.19
19
9.20
20
9.21
21
Part 2 false? A superkey. If theres >1 key for a relation, one of the keys is chosen (by DBA) to be the primary key.
E.g., sid is a key for Students. (What about name?) The set {sid, gpa} is a superkey. Primary key can not have null value
22
If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references.
Can you name a data model w/o referential integrity?
23
What should be done if an Enrolled tuple with a non-existent student id is inserted? (Reject it!)
What should be done if a Students tuple is deleted?
Also delete all Enrolled tuples that refer to it. Disallow deletion of a Students tuple that is referred to. Set sid in Enrolled tuples that refer to it to a default sid. (In SQL, also: Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown or `inapplicable.)
ssn
name
lot
Employees
CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn))
25
Keys for each participating entity set (as foreign keys). This set of attributes forms a superkey for the relation.
CREATE TABLE Works_In( ssn CHAR(1), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)
26
dname budget
key constraint
Employees
on Manages.
Manages
Departments
CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)
we could instead
combine Manages and Departments.
did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)
28
If so, this is a participation constraint: the participation of Departments in Manages is said to be total (vs. partial). Every did value in Departments table must appear in a row of the Manages table (with a non-null ssn value!)
name
since
dname
lot did Manages budget
ssn
Employees
Departments
Works_In
since
29
Owner entity set and weak entity set must participate in a one-to-many
relationship set (1 owner, many weak entities). Weak entity set must have total participation in this identifying relationship set.
Employees
Policy
Dependents
31
32
When the owner entity is deleted, all owned weak entities must also be deleted.
CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)
33
pname
age
Dependents
Bad design
Key constraint on Policies would mean policy can only cover 1 dependent!
Dependents
Better design
policyid
Policies
cost
34
The key constraints allow us to combine Purchaser with CREATE TABLE Dependents ( Policies and pname CHAR(20), age INTEGER, Beneficiary policyid INTEGER, with PRIMARY KEY (pname, policyid). FOREIGN KEY (policyid) REFERENCES Policies, Dependents. Participation
ON DELETE CASCADE)
35
CREATE TABLE Policies ( policyid INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)
36
37
An Example
38
CREATE TABLE Student ( ID NUMBER, Fname VARCHAR2(20) NOT NULL, Lname VARCHAR2(20) NOT NULL, );
40
CREATE TABLE Student ( ID NUMBER PRIMARY KEY, Fname VARCHAR2(20) NOT NULL, Lname VARCHAR2(20) NOT NULL, );
Primary Key implies: * NOT NULL * UNIQUE. There can only be one primary key.
41
CREATE TABLE Students ( ID NUMBER, Fname VARCHAR2(20) NOT NULL, Lname VARCHAR2(20) NOT NULL, PRIMARY KEY(ID) ); Needed when the primary key is made up of two or more fields
42
Another Table
CREATE TABLE Studies( Course NUMBER, Student NUMBER );
45
Translating Entities
id birthday
Actor
address
name
Translating Entities
id birthday
Actor
address
name
birthday date,
address varchar2(100));
48
title
id
name
Actor
Acted In
Film
year
address
salary
type
General Rule: Create a table with the name of the relationship The table has columns for all of the relationship's attributes and for the keys of each entity participating in the relationship What is the primary key of the table? What foreign keys are needed?
49
birthday id name
title
Actor
year
address
salary
type
50
id
name
Employee
worker
Manages
address
51
General Rule for Option 1: Same as without key constraints, except that the primary key is defined differently
52
salary
id varchar2(20),
title varchar2(40), salary integer,
What primary and foreign keys are missing?
)
53
salary
salary
title varchar2(40),
year integer, primary key (title),
What 3 lines are missing?
)
55
56
salary
General Rule:
If has both participation and key constraint, use Option 2 from before.
salary
title varchar2(40),
year integer, id varchar2(20),
salary integer,
foreign key (id) references Director, primary key (title))
58
salary
59
phone number
create table award( name varchar2(40), year integer, money number(6,2), o_name varchar2(40),
Organization
Gives
Award year
60
picture
Actor
Director
create table MoviePerson( ... ) create table Actor(id varchar2(20), picture bfile,
primary key(id),
foreign key (id) references MoviePerson)) create table Director(...)
61
picture
Actor
Director
No table for MoviePerson! create table Actor(id varchar2(20), address varchar2(100), name varchar2(20), picture blob, primary key(id)); create table Director(...)
62
63
name
phone number
Translating Aggregation
picture
Actor Acted In Film title type salary
Organization
year