Professional Documents
Culture Documents
S01 Lab01 ER Into Create Database Tables Stepbystep
S01 Lab01 ER Into Create Database Tables Stepbystep
(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
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
Department Entity
Attributes:
- DName
- DCode
- DOffice
- DPhone
- College
Course Entity
Attributes:
- CName
- CDescription
- CNum
- SemesterHours
- Level
4/10
Step by Step: ER Model and Relational Mapping
5/10
Step by Step: ER Model and Relational Mapping
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);
Department Table
Primary Key: DCode
Alter table Department
Add Constraint pk_dep Primary key (DCode);
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);
9/10
Step by Step: ER Model and Relational Mapping
10/1