Create table statement:CREATE TABLE ProfessorsPSSN varchar20 CHECKPSSN like ''''P%'''',Name nvarchar50,Age int CHECKAge >= 22,Ranking int CHECKRanking > 0,Research nvarchar50 CHECKResearch = ''''
Trang 1Database Systems - DBI202
Teacher: VU THANH PHONG
Table of contents: Page ASSIGNMENT REPORT
Trang 2TOPIC ……….3
I Introduction to the problem ……….……3
1 Describe the problem ……….3
2 Management goals ……… 4
II Entity Relationship Model ……….4
1 Identify entity sets, attributes ……….4
2 Make E/R diagram ………5
III Data Requirements Specification ……… 6
1 Relational model ………6
2 Database Diagram ………15
3 Trigger – Procedure – SQL Query ………16
Trang 3TOPIC 3 Consider the following set of requirements for a database that is used to keep track information about a university.
I Introduction to the problem (write the user requirement).
1 Describe the problem.
A university has a need to build a database to manage professors and manage projects done by students:
- Professors have an SSN, a name, an age, a rank, and
a research specialty Projects have a project number, a sponsor name (e.g., NSF), a starting date, an ending date, and a budget Graduate students have an SSN, a name, an age, and a degree program (e.g., M.S or Ph.D.)
- Each project is managed by one professor (known as the project’s principal investigator) Each project is worked on by one or more professors (known as the project’s co-investigators) Professors can manage and/or work on multiple projects Each project is worked
on by one or more graduate students (known as the project’s research assistants)
- When graduate students work on a project, a
professor must supervise their work on the project Graduate students can work on multiple projects
- Departments have a department number, a department name, and a main office Departments have a professor (known as the chairman) who runs the department Professors work in one or more departments
- Graduate students have one major department in whichthey are working on their degree
Trang 4- Each graduate student has another, more senior graduate student who advises him or her on what courses
to take Design and draw an ER diagram that captures theinformation about the university Use only the basic ER model here; that is, entities, relationships, and attributes Be sure to indicate any key and
II Entity Relationship Model (E/R model).
1 Identify entity sets, attributes.
- Based on the description of the problem and the management goal, we can give some entities and the properties of that entity as follows:
Professors: ID, Name, Age, Ranking, Research Departments: ID, Name, Office.
Students: ID, Name, Age, Degree.
Projects: ID, Name, StartingDate, EndingDate,
Budget
- Relation between entity:
+ Professors run Department
+ Professors work in Department
+ Professors manage Project
+ Professors work on Project
+ Professors supervise Project, Student
+ Students work on Projects
Trang 5+ Students major Department
+ Senior Graduate introduce Graduate Students
2 Make E/R diagram.
Trang 6III Data Requirements Specification (Data dictionary).
1 Relational model (Converted from E/R model).
Trang 7Create table statement:
CREATE TABLE Professors(
PSSN varchar(20) CHECK(PSSN like 'P%'),
Name nvarchar(50),
Age int CHECK(Age >= 22),
Ranking int CHECK(Ranking > 0),
Research nvarchar(50) CHECK(Research = 'Master' or Research = 'Doctor'),
) StartingDate Date
EndingDate Date
Budget Float 0 Budget >= 0
Trang 8PR05 P05 Elementary Japanese 2022-10-01 2022-11-01 0
Create table statement:
CREATE TABLE Projects(
ProjectNumber varchar(20) CHECK(ProjectNumber like 'PR
Budget float CHECK(Budget >= 0) DEFAULT 0,
Primary key (ProjectNumber),
FOREIGN KEY (PSSN) REFERENCES Professors(PSSN)
)
Trang 9Create table statement:
CREATE TABLE Departments (
DNumber int CHECK(DNumber > 0),
PSSN varchar(20) CHECK(PSSN like 'P%'),
DName nvarchar(50),
Trang 10Office nvarchar(50),
Primary key (DNumber),
FOREIGN KEY (PSSN) REFERENCES Professors(PSSN)
)
d TABLE STUDENTS
Attribute
s Data type Default Check Key/Index/Constrains
FOREIGN KEY (DNumber) REFERENCES Departments(DNumber) Name Nvarchar(50)
Trang 11
Create table statement:
CREATE TABLE Students (
SSN varchar(20) CHECK(SSN like 'SE%'),
SupSSN varchar(20) CHECK(SupSSN like 'SE%'),
DNumber int CHECK(DNumber > 0),
) SSN Varchar(20
Example:
Trang 12PR02 SE160369
Create table statement:
CREATE TABLE WorkOnPRJS (
ProjectNumber varchar(20) CHECK(ProjectNumber like 'PR
%'),
SSN varchar(20) CHECK(SSN like 'SE%'),
Primary key (SSN, ProjectNumber),
FOREIGN KEY (ProjectNumber) REFERENCES
r
Varchar(20 )
ProjectNumbe
r like 'PR%'
Primary Key, FOREIGN KEY (ProjectNumber) REFERENCES Projects(ProjectNumb er)
Example:
Trang 13P02 PR02
Create table statement:
CREATE TABLE WorkOnPROS (
PSSN varchar(20) CHECK(PSSN like 'P%'),
ProjectNumber varchar(20) CHECK(ProjectNumber like 'PR%'),Primary key (PSSN, ProjectNumber),
FOREIGN KEY (PSSN) REFERENCES Professors(PSSN),
FOREIGN KEY (ProjectNumber) REFERENCES
) SSN Varchar(20
Example:
Trang 14Create table statement:
CREATE TABLE Supervise (
PSSN varchar(20) CHECK(PSSN like 'P%'),
ProjectNumber varchar(20) CHECK(ProjectNumber like 'PR%'),SSN varchar(20) CHECK(SSN like 'SE%'),
Primary key (PSSN, SSN, ProjectNumber),
FOREIGN KEY (PSSN) REFERENCES Professors(PSSN),
FOREIGN KEY (ProjectNumber) REFERENCES
Primary Key, FOREIGN KEY (DNumber) REFERENCES Departments(DNumber)
Example:
Trang 15Create table statement:
CREATE TABLE WorkIn (
PSSN varchar(20) CHECK(PSSN like 'P%'),
DNumber int CHECK(DNumber > 0),
Hourss int CHECK(Hourss >= 0) DEFAULT 0,
Primary key (PSSN, DNumber),
FOREIGN KEY (PSSN) REFERENCES Professors(PSSN),
FOREIGN KEY (DNumber) REFERENCES Departments(DNumber))
2 Database diagram
Trang 163 Trigger – Procedure – SQL Query
b Display the number of professor that is working in each department.
Query Statement:
SELECT DNumber, DName, COUNT( P PSSN) AS NumOfProfessor
Trang 17FROM Departments D INNER JOIN WorkIn WI ON DNumber
WI DNumber
INNER JOIN Professors P ON WI.PSSN = P PSSN
GROUP BY DNumber, DName
c Display all students that doesn’t have supervise student Query Statement:
SELECT SSN, DNumber, Name, Age, Degree
FROM Students
WHERE SupSSN is null
2 TRIGGER
a Trigger for insert on Projects table
Usage: If the row that user insert into Projects table has
Ending Date that not after the Starting Date The trigger will print the notification and rollback for avoiding this insert Otherwise, the row that user insert will be inserted normally
Create Trigger Statement:
CREATE TRIGGER startDate_endDate_check on Projects
Trang 18SELECT @SDATE = StartingDate , @EDATE = EndingDate
FROM INSERTED
DECLARE @SYEAR INT , @SMONTH INT , @SDAY INT
SELECT @SYEAR = YEAR( @SDATE ), @SMONTH = MONTH( @SDATE ), @SDAY
DAY( @SDATE )
DECLARE @EYEAR INT , @EMONTH INT , @EDAY INT
SELECT @EYEAR = YEAR( @EDATE ), @EMONTH = MONTH( @EDATE ), @EDAY
b Trigger for insert on Students table
Usage: If the row that user insert into Students table has
Supervise Student that has degree is not Senior Graduate The trigger will print the notification and rollback for avoiding
Trang 19this insert Otherwise, the row that user insert will be inserted normally.
Create Trigger Statement:
CREATE TRIGGER SUP_STU_TRIG ON STUDENTS
FOR INSERT
AS
BEGIN
DECLARE @SUP_SSN VARCHAR ( 20 )
SELECT @SUP_SSN = SupSSN
FROM STUDENTS
DECLARE @SUP_DEGREE NVARCHAR ( 30 )
SELECT @SUP_DEGREE = DEGREE
c Trigger for insert on WorkIn table
Usage: The professor that manage the department need to have
the highest ranking (Rank 1 > Rank 2) in department If the row that user insert into WorkIn table has professor has higher ranking than department's manager The trigger will print the notification and rollback for avoiding this insert.Otherwise, the row that user insert will be inserted
normally
Create Trigger Statement:
CREATE TRIGGER check_WorkIn ON WORKIN
FOR INSERT
AS
BEGIN
Trang 20DECLARE @DNUM INT , @IPSSN VARCHAR ( 20 )
SELECT @DNUM = DNUMBER , @IPSSN = PSSN
FROM INSERTED
DECLARE @ManagerNum VARCHAR ( 20 )
SELECT @ManagerNum = PSSN
FROM DEPARTMENTS
WHERE DNumber = @DNUM
DECLARE @IRANKING INT
SELECT @IRANKING = Ranking
FROM Professors
WHERE PSSN = @IPSSN
DECLARE @ManagerRanking INT
SELECT @ManagerRanking = Ranking
Usage: Normally, if user want to delete a Project they need
to delete in all table that referenced to Projects table Therefore, we create this procedure that help user can delete
a project by the Project Number in one statement
Create procedure statement:
CREATE PROCEDURE DELETE_PRO_BY_KEY @PRONUM varchar ( 20 )
AS
BEGIN
DELETE FROM WorkOnPROS
Trang 21WHERE ProjectNumber = @PRONUM
DELETE FROM Supervise
WHERE ProjectNumber = @PRONUM
DELETE FROM WorkOnPRJS
WHERE ProjectNumber = @PRONUM
DELETE FROM Projects
WHERE ProjectNumber = @PRONUM
END
GO
b Procedure that helping user Insert a project into the Projects table
Usage: Normally, if user want to insert a Project they need
to insert the professor if PSSN not exist in the Professor table Therefore, we create this procedure that help user caninsert a project into Projects table in one statement At thesame time, we put the insert statement in TRANSACTION and in block TRY so if any error happen the insert will not be inserted to table for avoiding wrong data in database
Create procedure statement:
CREATE PROCEDURE INSERT_PRO
Trang 22SET NOCOUNT ON
BEGIN TRAN
BEGIN TRY
DECLARE @CHECK INT
SELECT @CHECK = COUNT( PSSN ) FROM Professors
GROUP BY PSSN
HAVING PSSN = @PSSN
IF ( @CHECK is not null) BEGIN
INSERT INTO Projects
VALUES ( @projNum , @PSSN , @projName , @startDate ,
@endDate , 0 )
END ELSE BEGIN INSERT INTO Professors
VALUES ( @PSSN , @profName , @profAge , @ranking ,
@research )
INSERT INTO Projects
VALUES ( @projNum , @PSSN , @projName , @startDate , @endDate , 0 )