chapter 4chapter 4 logical database design and the ...mdamian/past/databasefa11/notes/...chapter...

40
Chapter 4 Chapter 4 Logical Database Design and the Relational Model 1 Objectives Objectives Define terms for the relational data model Define terms for the relational data model • Brief introduction to SQL Transform E-R diagrams to relations Transform E-R diagrams to relations Create tables with entity and relational integrity constraints constraints 2

Upload: others

Post on 06-Mar-2021

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Chapter 4Chapter 4Logical Database Design and

the Relational Model

11

ObjectivesObjectives

• Define terms for the relational data modelDefine terms for the relational data model

• Brief introduction to SQL

• Transform E-R diagrams to relations• Transform E-R diagrams to relations

• Create tables with entity and relational integrity constraintsconstraints

22

Page 2: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Steps in Database Problem Solving

Business ProblemStudy and Analyze

w/Team

Conceptual Model

Interviews & Integrated Model

(E-R)

L i l M d l

Transformation(Six Cases)

Logical Model(Relations)

Normalization(Three Steps)

Logical Model(3NF Relations)

(Three Steps)

33

IMPLEMENTATION

Advantages of Relational Modelg

• Can represent all kinds of information

• Based on Math (relations)

• Natural to people

• Relatively simple

• We know how to implement it fastp

44

Page 3: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Motivating ExampleMotivating Example

• Make a list of students in the class, keeping their ID,Make a list of students in the class, keeping their ID, name and phone number

55

Motivating ExampleMotivating Example

• Make a list of students in the class, keeping their ID,Make a list of students in the class, keeping their ID, name and phone number

• You’d probably come up with something like this:p y p g

ID Name Phone

Mik 111xx Mike 111

yy Elisa 222

• This is the basic structure of the relational model, a table or relation

66

table or relation

Page 4: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Extra AssumptionsExtra Assumptions

• You would not repeat the same row twiceYou would not repeat the same row twice

• No two rows have the same ID, but they may have the same name and phone numberp

ID Name Phone

Mik 111xx Mike 111

yy Elisa 222

• ID would be the PRIMARY KEY (PK).

77

Now add emails … (many!)Now add emails … (many!)

• Now you need to add the emails of each student, butNow you need to add the emails of each student, but you do not know how many emails

• Can you come up with a solution? Try it …y p y

88

Page 5: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Many Fieldsy

• Could come up with something like thisCould come up with something like this

ID Name Phone Email1 Email2

xx Mike 111 bad idea

yy Elisa 222 bad idea

• Above would not work very well. How many fields?– Wasted spaceWasted space– What if a student has more emails?– How to process it in my program?

99

Un-normalizedUn normalized

• Could also try this:

ID Name Phone Email

xx Mike 111 mk@ad comxx Mike 111 [email protected]

xx Mike 111 [email protected]

yy Elisa 222 [email protected]

• Problem is duplication, we are repeating the name and phone

yy Elisa 222 [email protected]

number in the second row– What if Mike changes his phone?

1010

• Later we will study normalization to solve this.

Page 6: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Now add emails … (many!)( y )• A much better way:

Email

ID Name Phone

StudentID Email

xx [email protected]

Student

xx Mike 111

yy Elisa 222

xx [email protected]

yy [email protected]

• Every StudentID on the second table needs a matching ID on the first table: StudentID is a FOREIGN KEYthe first table: StudentID is a FOREIGN KEY

• In a way, StudentID in the second table is a pointer or reference to the first table

1111

Formalizing: Relations• Definition: A relation is a named table of data

– Table is made up of rows (records or tuples), and columns (attributes or fields)

• Not all tables qualify as relations. Requirements:1 Every relation has a unique name1. Every relation has a unique name.2. Every attribute value is atomic (not multivalued, not

composite)3 E i i ( ’ h i h l h3. Every row is unique (can’t have two rows with exactly the

same values for all their fields)4. Attributes (columns) in tables have unique names( ) q5. The order of the columns is irrelevant6. The order of the rows is irrelevant

1212

By definition, all relations are in 1st Normal Form (1NF).

Page 7: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Correspondence with ER Modelp

• Relations (tables) correspond to entity types and to l i himany-to-many relationship types

• Rows correspond to entity instances and to many-to-l ti hi i tmany relationship instances

• Columns correspond to attributes

• NOTE: The word relation (in relational database) is

NOT the same as the word relationship (in ER model)

1313

Formalizing Key Fieldsg y

• Primary key (PK) – Minimal set of attributes that uniquely identifies a row,

chosen for referencing

Thi i h t th t ll i– This is how we can guarantee that all rows are unique

• Foreign key (FK)Set of attributes in a table that serves as a reference to the– Set of attributes in a table that serves as a reference to the primary key of another table

1414

Page 8: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Constraints Implied by the Keysp y y

• Entity Integrity Constraint– No attribute of the PK may be null

• Referential Integrity Constraint– For a FK, either all attributes are null, or the values appear

in the PK of a row of the referred tablein the PK of a row of the referred table

1515

Well-Structured Relations

• Bad designs have redundancy

• Well-structured relation: minimal redundancy

• Anomaly: error or inconsistency arising from bad y y gdesign when changing (Insert, Update, Delete) data

• We eliminate anomalies through Normalization

1616

Page 9: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Is Student a Well-Structured Relation?Is Student a Well Structured Relation?

Student

ID Name Phone Email

xx Mike 111 [email protected]

Mik 111 k@ dxx Mike 111 [email protected]

yy Elisa 222 [email protected]

1717

Review

• Conceptual ModelR l i l M d l• Relational Model

• Relation• Well Structured Relation• Well-Structured Relation• Primary Key• Foreign Keyg y• Entity Integrity Constraint• Referential Integrity Constraint

1818

Page 10: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Transforming E-R Into Relationsg

• Use a rectangle for each entity (table), with attributes inside rectangles, too– Can be vertical or horizontal– Primary key is underlined– Primary key is underlined

• Use arrows from Foreign key to Primary key

ID Name Phone ID

Name

StudentID

Email

Student Student Email

StudentID Email

Name

Phone

EmailEmail

1919

E-R vs. Relational

• Entities are represented by tablesB t t bl l t l ti hi lti l d– But tables may also represent relationships, or multivaluedattributes

• Foreign Keys used to relate table rows– Similar to relationships in E-R, but lower level

• Relational model is more concrete, lower level– Usually many more tables than entities

d d d b h i l l– Harder to understand by non-technical people– Directly implementable

2020

Page 11: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Brief Intro to SQLBrief Intro to SQL

2121

SQLQ

• Structured Query Language

• Standard Language for Relational Databases

• Every RDBMS implements SQL, with slight y gvariations, including

– Special data types

– Stored procedures

– Kinds of indexes, file organization ...g

2222

Page 12: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Basics

• Data Definition Language (DDL)– Create the database schema– CREATE / DROP / ALTER, …

• Data Manipulation Language (DML)– Manipulate the populated data– INSERT UPDATE DELETE SELECTINSERT, UPDATE, DELETE, SELECT, …

• Data Control Language (DCL)– Manage users permissions and suchManage users, permissions and such

2323

Conventions

• -- comments until end of line

• /* can also use C-style comments */

• SQL is case insensitive (except for data)

• But we usually type reserved words in ALL CAPS

• Use single quotes for ‘character constants’g q

2424

Page 13: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

CREATE TABLECREATE TABLE

CREATE TABLE table_name (field type constraints,field2 type2 ,CONSTRAINT name ...,...

);

CREATE TABLE Book (ISBN CHAR(9) PRIMARY KEY,Title VARCHAR(20) UNIQUE NOT NULL,Pages Integer

);

2525

Common Datatypesyp

• CHAR(n)fi d l th t i dd d ith t d– fixed length strings, padded with spaces at end

• VARCHAR(n)i bl l th t i b t l th– variable length strings, but no longer than n

• NUMERIC(prec, dec)– fixed precision numbers (not floats)fixed precision numbers (not floats)– prec(ision) is total number of digits– dec is how many after the decimal point

NUMERIC(3 2) l i 9 99– NUMERIC(3,2) max value is 9.99

• DATE, TIMESTAMPRepresent dates or specific points in time

2626

– Represent dates, or specific points in time

Page 14: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Common Constraints

• PRIMARY KEY

• NOT NULL

• UNIQUE

• REFERENCES (foreign key)

– After REFERENCES put name of table, then field in p ,

parenthesis– StudentId REFERENCES Student(Id)

• CHECK– Allows to specify a condition or predicate

2727

– CHECK(age>20)

CREATE TABLE ExampleCREATE TABLE Example

CREATE TABLE Student (Id CHAR(3) PRIMARY KEY,Name VARCHAR(20) NOT NULL,Age INT DEFAULT 20 CHECK(Age>0 AND AGE<100),G d C OGender CHAR NOT NULL,Deg_code CHAR(2) NOT NULL REFERENCES Degree(code),Major CHAR(3),Credits INTEGERCredits INTEGER

);

2828

Page 15: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

INSERT

• INSERT INTO table (fields)

VALUES (values)

• Character constants have single quotes ‘a’

INSERT INTO Student (Id,Name,Major,Age)VALUES (1, ‘Amy Cremer','CS',21);

2929

SELECT (Retrieving Data)( g )

• SELECT fields

FROM table

WHERE conditions

• Can use fields or expressions (a+3), * for all fields

• Conditions use normal operators (=,>) and are p ( , )combined with AND, OR, NOT

SELECT *FROM Student

SELECT Id,NameFROM StudentWHERE Major='CS'

3030

Page 16: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

More ExamplesMore Examples

SELECT Id,Name,Age+5, , gFROM StudentWHERE Major='CS' AND Gender=’F’

SELECT Id,Name,Age+5 AS AgeIn5FROM StudentWHERE Age>=20 OR Age <=10WHERE Age> 20 OR Age < 10

3131

DELETE

• DELETE FROM table DELETEFROM St d t

WHERE conditionsFROM StudentWHERE Id=1

• If no conditions, delete all data

• Does NOT delete the meta-data, ,

use DROP TABLE for that

3232

Page 17: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

UPDATE (Change Data)( g )

• UPDATE table

SET field=value

WHERE conditions

UPDATE StudentSET Name='Alfredo', Age=25SET Name Alfredo , Age 25WHERE Id=1;

UPDATE StudentSET Age=Age+1WHERE Id=1;

3333

;

Complete Examplep p

• Write CREATE TABLE statement

3434

Page 18: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

3535

Now Insert StatementsINSERT INTO Major(Id, Name) VALUES

('CS' 'Computer Science');( CS , Computer Science );

INSERT INTO Student(Id, Name, MajorId) VALUES(1, 'Paul Gordon', 'CS');

• Notice that field names and values match

( , au Go do , CS );

• Notice that field names and values match

• Notice that value of Student.MajorId needs to match a value of Major Idvalue of Major.Id

3636

Page 19: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Now You Tryy• Insert major for SWE, IT

d j i i S• Insert students majoring in SWE, IT

3737

3838

Page 20: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Basic SELECTS• Show all students

SELECT * FROM Student;

• Now you do all majorsy j

SELECT * FROM Major;

A d th Id d N f ll t d t j i i CS• And then Id and Name of all students majoring in CS

SELECT Id, Name FROM StudentWHERE Student.MajorId = 'CS‘;

3939

Updatesp• Change the student with Id 2 to be CS instead of SWE

UPDATE StudentSET MajorId = 'CS'WHERE Id=2 ;

• Change the name of the student with Id=3 to be Rich

4040

Page 21: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Deletes• Delete any student majoring in IT

DELETE FROM StudentWHERE MajorId = 'IT';

• Now you do – delete the IT major (from the table)

4141

From E-R to TablesFrom E R to Tables

4242

Page 22: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Six Cases of Transforming E-R Diagrams into Relations

1. Map Regular Entities

2. Map Binary Relationshipsp y p

3. Map Weak Entities

4 Map Associati e Entities4. Map Associative Entities

5. Map Unary Relationships

6. Map Ternary (and n-ary) Relationships

4343

1. Mapping Regular Entitiespp g gEMPLOYEE

SSN• Create a new table for each entity

R b d li h SSNName (First, Middle, Last){Emails}Date of Birth

• Remember to underline the identifier (Fig. 4-8)

• For composite attributes, map Date of Birth[Age]

For composite attributes, map only the basic pieces (Fig. 4-9)

• Derived attributes disappear

ID First Middle Last DoB

Employee• For multivalued attributes we need a new table (Fig. 4-10)

• We may need to create several

ID Email

Email

• We may need to create several tables for independent multivalued attributes

4444

Email

Page 23: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

You Try …yBOOK

ISBNISBNTitle{Authors}Format (Binding, NumPages, Dimensions, [Weight])

4545

Corresponding SQL

4646

Page 24: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Six Cases of Transforming E-R Diagrams into Relations

1. Map Regular Entities

2. Map Binary Relationshipsp y p

3. Map Weak Entities

4 Map Associati e Entities4. Map Associative Entities

5. Map Unary Relationships

6. Map Ternary (and n-ary) Relationships

4747

Mapping Binary Relationshipspp g y p

• One-to-Many P i k th id b f i k th– Primary key on the one side becomes a foreign key on the many side (Fig. 4-12).

• One-to-One – Primary key on the mandatory side becomes a foreign key

on the optional side (Fig. 4-14).

M t M• Many-to-Many– Create a new relation with the primary keys of the two

entities as its primary key (Fig. 4-13).p y y ( g )

4848

Page 25: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Fig. 4-12: Example of mapping a 1:M relationship(a) Relationship between customers and orders

[Primary key on the one side becomes a

Note the mandatory one

Fig. 4-12: (b) Mapping the relationship[Primary key on the one side becomes a

foreign key on the many side]

Again, no null value in the foreign key…this is because of the mandatory

4949

Foreign key

because of the mandatory minimum cardinality

One-to-Many Relationship

0STUDENT

IDN

PROGRAMIDN

>0Majors in

ID N

Name Name

Program

ID N M j

Student

ID Name

• For one-to-many (or one-

ID Name Major

CREATE TABLE Program (to-one) relationship, put a foreign key on the side that relates to one entity.

g (ID INT PRIMARY KEY,Name VARCHAR(20)

); y

• Put any relationship attributes on that table, too.

CREATE TABLE Student (ID INT PRIMARY KEY,Name VARCHAR(50),

5050

Major INT REFERENCES Program(ID));

Page 26: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Figure 4-14 Example of mapping a binary 1:1 relationship

) I h l ti hi (1 1)a) In_charge relationship (1:1)

5151

Often in 1:1 relationships, one direction is optional.

Fig. 4-14: (b) Resulting relations

mandatory

PK

optional

FK

5252

[Primary key on the mandatory side becomes a foreign key on the optional side]

Page 27: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Many-to-Many Relationship

0<STUDENT

IDPROGRAM

ID>0Majors in

Name Name

ProgramStudent

ID NameID Name

M j I

Student Program

MajorsIn

• For a many-to-many, we need a new table representing the relationship.

5353

• This table has Foreign Keys to both entities.

Figure 4-13 Example of mapping an M:N relationship

) C l t l ti hi (M N)a) Completes relationship (M:N)

The Completes relationship will need to become a separate relation

5454

p p p

Page 28: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Figure 4-13 Example of mapping an M:N relationship (cont.)

b) Th lti l tib) Three resulting relations

Composite primary key (CPK)Composite primary key (CPK)

Foreign key Foreign key

5555

You Try It …

0<PERSON

IDCOUNTRY

ID>Is Citizen

Name Name

5656

Page 29: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Six Cases of Transforming E-R Diagrams into Relations

1. Map Regular Entities

2. Map Binary Relationships p y p

3. Map Weak Entities

4 Map Associati e Entities4. Map Associative Entities

5. Map Unary Relationships

6. Map Ternary (and n-ary) Relationships

5757

3. Mapping Weak Entitiespp g

• A weak entity becomes a separate relation withA weak entity becomes a separate relation with a foreign key taken from the strong entity

• Primary key composed of:y y p– Partial identifier of weak entity

– Primary key of identifying relation (strong entity) (Fig. 4-11)

5858

Page 30: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Weak Entities

0<EMPLOYEE

SSNN

DEPENDENTNameD f Bi h

Has

Name Date of Birth

DependentEmployee

Employee Name DoBSSN Name

• Transform the strong entity normally

• For the weak entity, the PK becomes the identifier, plus the PK of the identifying entity

5959

You Try It …

0<

BOOKISBNTi l

CHAPTERNumber

Has

0TitleEdition

NumberTitle

6060

Page 31: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Six Cases of Transforming E-R Diagrams into Relations

1. Map Regular Entities

2. Map Binary Relationships p y p

3. Map Weak Entities

4 Map Associati e Entities4. Map Associative Entities

5. Map Unary Relationships

6. Map Ternary (and n-ary) Relationships

6161

4. Mapping Associative Entitiespp g

• Identifier Not Assigned – Default primary key for the association relation is

composed of the primary keys of the two entities (as in M:N relationship) (Fig. 4-15)M:N relationship) (Fig. 4 15)

• Identifier Assigned – It is natural and familiar to end-users.

– Default identifier may not be unique. (Fig. 4-16).

6262

Page 32: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Figure 4-16: Mapping an associative entity(a) Associative entity (SHIPMENT)

[Default primary key for the association

relation is assigned](a) Associative entity (SHIPMENT) relation is assigned]

(b) Three resulting relations

Primary key differs from foreign keys

6363

You Try It …

0 0

6464

Page 33: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Six Cases of Transforming E-R Diagrams into Relations

1. Map Regular Entities

2. Map Binary Relationships p y p

3. Map Weak Entities

4 Map Associati e Entities4. Map Associative Entities

5. Map Unary Relationships

6. Map Ternary (and n-ary) Relationships

6565

5. Mapping Unary Relationshipspp g U y p

• Same as other relationships, except that the FK maySame as other relationships, except that the FK may go to the same table.

• For one-to-many, the table has a reference to other y,rows of the same table (Fig. 4-17).

• For many-to-many, an extra table has two FKs, both y y, ,to the same table (Fig. 4-18).

6666

Page 34: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Figure 4-17 Mapping a unary 1:N relationship

(a) EMPLOYEE entity with unary relationship(a) EMPLOYEE entity with unary relationship

A recursive FK is a FK inFK is a FK in a relation that references the PK values of that same relation.

(b) EMPLOYEE relation with recursive foreign key

6767

Figure 4-18: Mapping a unary M:N relationship

(a) Bill-of-(a) Bill-of-materials relationships (M:N)

One table for the entity type.

O t bl f

(b) ITEM and COMPONENT

One table for an associative relation in which the primary key

relationsthe primary key has two attributes, both taken from the

6868

taken from the primary key of the entity.

Page 35: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

You Try It …

0EMPLOYEE

SSNBoss

0

Supervises

SSNName >

0

W kWorker

6969

You Try It …IsCorequisite

Requirer

>0COURSE

IDPre

Requirer

Required

>0

>0

IsPrerequisiteName >

0

Post

Required

7070

Page 36: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Six Cases of Transforming E-R Diagrams into Relations

1. Map Regular Entities

2. Map Binary Relationships p y p

3. Map Weak Entities

4 Map Associati e Entities4. Map Associative Entities

5. Map Unary Relationships

6. Map Ternary (and n-ary) Relationships

7171

6. Mapping Ternary Relationshipspp g y p

• One relation for each entity and one for the i i iassociative entity.

• Associative entity has foreign keys to each entity i th l ti hi (Fi 4 19)in the relationship (Fig. 4-19).

7272

Page 37: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Figure 4-19 Mapping a ternary relationship

a) PATIENT TREATMENT Ternary relationship witha) PATIENT TREATMENT Ternary relationship with associative entity

7373

Figure 4-19 Mapping a ternary relationship (cont.)

b) Mapping the ternary relationship PATIENT TREATMENT

A patient may receive a treatment

7474

once in the morning, then the same treatment in the afternoon.

Page 38: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

b) Mapping the ternary relationship PATIENT TREATMENT

Figure 4-19 Mapping a ternary relationship (cont.)

b) Mapping the ternary relationship PATIENT TREATMENT

Remember that the primary

key MUST be

This is why treatment date and time are included in

But this makes a very cumbersome

key

It would be better to create a

surrogate key like

7575

key MUST be unique

time are included in the composite primary key

key… surrogate key like Treatment#

Mi d E iMixed Exercises

7676

Page 39: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Practice Exercise

0<TV SERIES

IDTi l

EPISODENumberTitle

Has

TitleTitleFirstAiring Date

7777

Practice Exercise

COURSESECTION

Belongs TEACHERTeaches

0<COURSE

IDName

SecNoTerm(Semester, Year)

Belongs TEACHERSSNName

>0Teaches

7878

Page 40: Chapter 4Chapter 4 Logical Database Design and the ...mdamian/Past/databasefa11/notes/...Chapter 4Chapter 4 Logical Database Design and the Relational Model 1 Objectives • Define

Practice Exercise

7979

Next Topicp

• Next topic is the most important topic (theory) in this database management class.

• What is it?

• Normalization

8080