Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Step by Step: ER Model and Relational Mapping

Step by Step to draw ER Model


1. (7.29) Consider the following set of requirements for a university database that is used to
keep track of students' transcripts. This is similar but not identical to the database shown in
Figure 1.2:

(a) The university keeps track of each student's name, student number, social security
number, current address and phone, permanent address and phone, birthdate, sex, class
(freshman, sophomore, ..., graduate), major department, minor department (if any), and
degree program (B.A., B.S., ..., Ph.D.). Some user applications need to refer to the city,
state, and zip of the student's permanent address, and to the student's last name. Both social
security number and student number have unique values for each student.
Student Entity
Attributes:
- SName: FName, LName
- SNum
- SSN
- CAddress: StreetAddr, City, State, Zip
- CPhone
- PAddress
- PPhone
- Birthdate
- Sex
- Class
- DegreeProgram
Relationship
- MajorDep
- MinorDep

(b) Each department is described by a name, department code, office number, office
phone, and college. Both name and code have unique values for each department.
Department Entity
Attributes:
- DName
- DCode
- DOffice
- DPhone
- College

1/10
Step by Step: ER Model and Relational Mapping

(c) Each course has a course name, description, course number, number of semester
hours, level, and offering department. The value of course number is unique for each
course.
Course Entity
Attributes:
- CName
- CDescription
- CNum
- SemesterHours
- Level
Relationship
- Dept_Course

(d) Each section has an instructor, semester, year, course, and section number. The section
number distinguishes different sections of the same course that are taught during the same
semester/year; its values are 1, 2, 3, ...; up to the number of sections taught during each
semester.
Section Entity (Weak Entity)
Attributes:
- Instructor
- PartialKey: Semester, Year, SNum
Relationship
- HadCourse

(e) A grade report has a student, section, letter grade, and numeric grade (0, 1, 2, 3, 4 for F,
D, C, B, A, respectively).
Grade Relationship (n-n)
Attributes:
- LetterGrade
- NumericGrade

There are 3 Entities: STUDENT, DEPARTMENT, COURSE and 1 weak entity SECTION

2/10
Step by Step: ER Model and Relational Mapping

There are 5 Relationship


- STUDENT and DEPARTMENT: MajorDept , MinorDept
- DEPARTMENT and COURSE: Dept_Course
- COURSE and SECTION (weak): Course_Sect
- STUDENT and SECTION: Stud_Sect (have 02 attribute: LetterGrade,
NumericGrade)

3/10
Step by Step: ER Model and Relational Mapping

Relational Mapping
Step 1: Mapping of Regular Entity Types
Student Entity
Attributes:
- SName: FName, LName
- SNum
- SSN
- CAddress: StreetAddr, City, State, Zip
- CPhone
- PAddress
- PPhone
- Birthdate
- Sex
- Class
- DegreeProgram
Relationship
- MajorDep
- MinorDep

STUDENT(FName, Lname, SNum, SSN, StreetAddr, City, State, Zip, Cphone,


Paddress, Pphone, Birthdate, Sex, Class, DegreeProgram)

Department Entity
Attributes:
- DName
- DCode
- DOffice
- DPhone
- College

DEPARTMENT(DName, DCode, DOffice, DPhone, College)

Course Entity
Attributes:
- CName
- CDescription
- CNum
- SemesterHours
- Level

COURSE(CName, CDescription, CNum, SemesterHours, Level)

4/10
Step by Step: ER Model and Relational Mapping

Step 2: Mapping of Weak Entity Types


Section Week Entity
Attributes:
- Instructor
- PartialKey: Semester, Year, SerialNum

SECTION(Instructor, ParitalKey, CNum)

Step 3: Mapping of Binary 1:1 Relation Types


(None)

Step 4: Mapping of Binary 1:N Relation Types


 STUDENT – DEPARTMENT: MajorDept, MinorDept STUDENT(FName,
Lname, SNum, SSN, StreetAddr, City, State, Zip, Cphone,
Paddress, Pphone, Birthdate, Sex, Class, DegreeProgram, MajorDept, MinorDept)

 DEPARTMENT – COURSE: Dept_Course


COURSE(CName, CDescription, CNum, SemesterHours, Level, DCode)

Step 5: Mapping of Binary N:N Relation Types

 STUDENT – SECTION: Stud_Sect


STUD_SECT(SNum, ParitalKey, CNum, LetterGrade, NumericGrade)

5/10
Step by Step: ER Model and Relational Mapping

Creating Database (SQL Server)


Description

STUDENT
FName Varchar(10)
LName Varchar(50)
SNum Char(8)
SSN Char(9)
StreetAddr Varchar(50)
City Varchar(50)
State Varchar(20)
Zip Varchar(8)
CPhone Varchar(10)
PAddress Varchar(50)
PPhone Varchar(10)
BirthDate DateTime
Sex Varchar(1) F, M
Class Varchar(10) freshman, …
DegreeProgram Varchar(4) B.A, B.S, Ph.D, …
MajorDept Varchar(4)
MinorDept Varchar(4)
Primary Key: SNum
Foreign Key: MajorDept - MinorDept

DEPARTMENT
DName Varchar(50)
DCode Varchar(4)
DOffice Varchar(6)
DPhone Varchar(10)
College Varchar(50)
Primary Key: DCode

COURSE
CName Varchar(50)
CDescription Varchar(200)
CNum Varchar(10)
SemesterHours Tinyint
Level Varchar(1) A, B, C,
DCode Varchar(4)
Primary Key: CNum
Foreign Key: DCode

6/10
Step by Step: ER Model and Relational Mapping

SECTION
Instructor Varchar(50)
PartialKey Varchar(13) Fall, Spring, … + 07, 08, … + 1,2,3, …
CNum Varchar(10)
Primary Key: (PartialKey, CNum)
Foreign Key: CNum

STUD_SECT
SNum Char(8)
PartialKey Varchar(13)
CNum Varchar(10)
LetterGrade Varchar(1) F, D, C, B, A
NumericGrade TinyInt 0, 1, 2, 3, 4
Primary Key: (SNum, PartialKey, CNum) Foreign
Key: SNum – (PartialKey, CNum)

1. Creating Database
CREATE DATABASE Univeristy ON PRIMARY
( NAME = 'University,
FILENAME = 'D:\DATA\Univeristy.mdf' ,
SIZE = 3072KB ,
MAXSIZE = UNLIMITED,
FILEGROWTH = 1024KB )
LOG ON
( NAME = 'University_log',
FILENAME = 'D:\DATA\University_log.ldf' ,
SIZE = 1024KB ,
MAXSIZE = 2048KB ,
FILEGROWTH = 10%)

2. Creating table
STUDENT
Create table STUDENT( FName
Varchar(10) Null, LName
Varchar(50) Null, SNum
Char(8) Not Null, SSN
Char(9) Not Null,
StreetAddr Varchar(50) Null,
City Varchar(50) Null,
State Varchar(20) Null, Zip
Varchar(8) Null, CPhone
Varchar(10) Null,
PAddress Varchar(50) Null,
PPhone Varchar(10) Null,
BirthDate DateTime Null, Sex
Varchar(1) Null,
Class Varchar(10) Null,
DegreeProgram Varchar(4) Null,
MajorDept Varchar(4) Null,
MinorDept Varchar(4) Null);

7/10
Step by Step: ER Model and Relational Mapping

DEPARTMENT
Create table DEPARTMENT(
DName Varchar(50) Null,
DCode Varchar(4) Not Null,
DOffice Varchar(6) Null,
DPhone Varchar(10) Null,
College Varchar(50) Null);

COURSE
Create table COURSE(
CName Varchar(50) Null,
CDescription Varchar(200) Null,
CNum Varchar(10) Not Null,
SemesterHours Tinyint Null, Level
Varchar(1) Null,
DCode Varchar(4) Null);

SECTION
Create table SECTION(
Instructor Varchar(50) Null,
PartialKey Varchar(13) Not Null,
CNum Varchar(10) Not Null);

STUD_SECT
Create Table STUD_SECT(
SNum Char(8) Not Null,
PartialKey Varchar(13) Not Null,
CNum Varchar(10) Not Null,
LetterGrade Varchar(1) Null,
NumericGrade TinyInt Not Null);

3. Creating Primary Key – Foreign Key


 Primary key
 Student Table
Primary Key: SNum
Alter table Student
Add Constraint pk_stu Primary key (SNum);

 Department Table
Primary Key: DCode
Alter table Department
Add Constraint pk_dep Primary key (DCode);

 Course Table Primary


Key: CNum Alter
table Course
Add Constraint pk_Crs Primary key (CNum);

8/10
Step by Step: ER Model and Relational Mapping

 Section Table
Primary Key: (PartialKey)
Alter table Section
Add Constraint pk_sec Primary key (PartialKey, CNum);

 Stud_Sect Table
Primary Key: (SNum, PartialKey, CNum)
Alter table Stud_Sect
Add Constraint pk_Stu_Sec Primary key (SNum, PartialKey, CNum);

 Foreign key
 Student Table to Department Table
Foreign Key: MajorDept – MinorDept
Alter table Student
Add Constraint fk_Stu_Dept_Major Foreign Key (MajorDept)
References Department(DCode);
Alter table Student
Add Constraint fk_Stu_Dept_Minor Foreign Key (MinorDept)
References Department(DCode);

 Course Table to Department Table


Foreign Key: DCode
Alter table Course
Add Constraint fk_Crs_Dept Foreign Key (DCode) References
Department(DCode);

 Section Table to Course Table


Foreign Key: CNum
Alter table Section
Add Constraint fk_Sec_Crs Foreign Key (CNum) References
Course(CNum);

 Stud_Sect Table to Student and Section


Foreign Key: SNum – (PartialKey, CNum)
Alter table Stud_Sect
Add Constraint fk_Stud_Sect_Stud Foreign Key (SNum) References
Student(SNum);
Alter table Stud_Sect
Add Constraint fk_ Stud_Sect_Sect Foreign Key (PartialKey, CNum)
References Section(PartialKey, CNum);

9/10
Step by Step: ER Model and Relational Mapping

10/1

You might also like