Assignment: Dbi202 - Database System of Dorm FPT University
Assignment: Dbi202 - Database System of Dorm FPT University
ASSIGNMENT
DBI202 – DATABASE SYSTEM OF DORM
FPT UNIVERSITY
STUDENT NAME: PHAM NGOC HOA | SE05740
STUDENT NAME: NGUYEN HAI NAM | SE05817
TEACHER: NGUYEN QUYNH CHI
DBI202
TABLE OF CONTENTS
I) INTRODUCE THE PROBLEM…………………………………………………………………………………………….
1) DESCRIBE THE PROBLEM……………………………………………………………………………………………………….
2) MANAGEMENT OBJECTIVES…………………………………………………………………………………………………..
Request:
Daily, guard need to caculate the total sutdent fined, caculate the total money.
Guard need check day student check-in, check out, day report item broken.
Monthly, guard need to count and display student not pay money for room.
Monthly, guard need cacule total amount collected.
Guard can check information of student.
Guard can check room is emty for student register.
Guard can check item broken in dom, in room to call fixer.
Check student register in room valid or invalid
Check room valid in condition special when student register in room.
2) MANAGEMENT OBJECTIVES
Manage student and change of students.
Manage penalize and calculate money fined.
Manage room for student register.
Manage items broken to fix.
Important output
Total money for fined of each student.
Count number room is empty.
Count number room have items broken.
Entity ENTITY
Relationship Relationship
Connectivity (force) = 1
Connectivity = N
LINK: HTTPS://GOO.GL/U4X3JO
Code:
--create table guard
CREATE TABLE GUARD(
GuardID INT PRIMARY KEY,
[First Name] NVARCHAR(45) NOT NULL,
[Last Name] NVARCHAR(45) NOT NULL,
Email NVARCHAR(255) CHECK(Email LIKE '%@fpt.edu.vn')NOT NULL,
Phone VARCHAR(11) UNIQUE CHECK(Phone LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-
9][0-9][0-9]' OR Phone LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-
9]')
)
Example:
Code:
--create table student
CREATE TABLE STUDENT(
StudentID VARCHAR(7) PRIMARY KEY NOT NULL CHECK(StudentID LIKE 'SE[0-9][0-9][0-9]
[0-9][0-9]' OR StudentID LIKE 'SB[0-9][0-9][0-9][0-9][0-9]' OR StudentID LIKE
'HE[0-9][0-9][0-9][0-9][0-9]'),
GuardID INT FOREIGN KEY REFERENCES dbo.GUARD(GuardID),
[First Name] NVARCHAR(45) NOT NULL,
[LastName] NVARCHAR(45) NOT NULL,
Email NVARCHAR(255) CHECK(Email LIKE '%@fpt.edu.vn') NOT NULL,
Phone CHAR(11) UNIQUE CHECK(Phone LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
[0-9][0-9]' OR Phone LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-
9]'),
Gender CHAR(1) DEFAULT 'M' CHECK(Gender IN('F','M')) NOT NULL,
Course CHAR(3) CHECK(Course LIKE 'K[0-9][0-9]') NOT NULL,
[Role] NVARCHAR(30) DEFAULT N'Thành Viên' CHECK(Role IN (N'Thành Viên',
N'Trưởng Phòng')) NOT NULL ,
Country NVARCHAR(50) DEFAULT N'Việt Nam' NOT NULL
)
Example:
Student Guard First Last Email Phone Gender Course Role Contry
ID ID Name Name
SE06155 1 Tù Bùi Anh [email protected] 0169927 M K12 Thành Việt
m 1212 Viên Nam
HE13001 3 Công Lê Thành [email protected] 0969981 M K13 Thành Việt
u.vn 028 Viên Nam
SE05627 3 Hiệp Đỗ Quang hiepdq05627/2fpt.edu. 0164908 M K12 Trưởng Việt
vn 8188 Phòng Nam
SE05740 5 Hòa Phạm Ngọc [email protected] 0968038 M K12 Trưởng Campuc
714 Phòng hia
Code:
--create table room
CREATE TABLE ROOM(
RoomID CHAR(5) PRIMARY KEY CHECK(RoomID LIKE '[A-F][1-5][0-9][0-9][0-9]' OR RoomID
LIKE '[A-F][1-5][0-9][0-9][0-9][0-9]'),
[Name Dom] VARCHAR(6) CHECK([Name Dom] IN ('A','B','C','D','F')),
[Floor] SMALLINT CHECK([Floor] BETWEEN 1 AND 5) NOT NULL ,
[Location] NVARCHAR(5) CHECK(Location IN (N'Trái', N'Phải')) NOT NULL,
[Number Bed] SMALLINT CHECK([Number Bed] BETWEEN 1 AND 12) NOT NULL,
Gender CHAR(1) DEFAULT 'M' CHECK(Gender IN('F','M')) NOT NULL,
[Status] SMALLINT DEFAULT 1 CHECK(Status BETWEEN 0 AND 1) NOT NULL
)
Example:
Code:
--create table manager room
CREATE TABLE [MANAGER ROOM](
RoomID CHAR(5) FOREIGN KEY REFERENCES dbo.ROOM(RoomID) CHECK(RoomID LIKE '[A-F][1-
5][0-9][0-9][0-9]' OR RoomID LIKE '[A-F][1-5][0-9][0-9][0-9][0-9]'),
GuardID INT FOREIGN KEY REFERENCES dbo.GUARD(GuardID) NOT NULL,
[Name Dom] CHAR(1) CHECK([Name Dom] IN ('A','B','C','D','F')) NOT NULL,
Price VARCHAR(10) CHECK(Price LIKE '%VND') NOT NULL
)
Example:
Code:
--create table register
CREATE TABLE REGISTER(
RoomID CHAR(5) FOREIGN KEY REFERENCES dbo.ROOM(RoomID) CHECK(RoomID LIKE '[A-F][1-
5][0-9][0-9][0-9]' OR RoomID LIKE '[A-F][1-5][0-9][0-9][0-9][0-9]'),
Example:
Code:
--create table items
CREATE TABLE ITEMS(
ItemID INT PRIMARY KEY NOT NULL,
[Name] NVARCHAR(50) NOT NULL,
Price INT NOT NULL,
Content NVARCHAR(255) NOT NULL
)
Example:
Code:
--create table fixdetail
CREATE TABLE [FIX DETAIL](
RoomID CHAR(5) CHECK(RoomID LIKE '[A-F][1-5][0-9][0-9][0-9]' OR RoomID LIKE '[A-F]
[1-5][0-9][0-9][0-9][0-9]') FOREIGN KEY REFERENCES dbo.ROOM(RoomID),
ItemID INT FOREIGN KEY REFERENCES dbo.ITEMS(ItemID),
Fixer NVARCHAR(90),
[Date Report] DATETIME CHECK([Date Report] <= GETDATE()) NOT NULL,
[Date Fix] DATETIME CHECK([Date Fix]<= GETDATE()),
Quantity INT NOT NULL
)
Example:
Code:
--create table penalize
CREATE TABLE PENALIZE(
Example:
Code:
--create table student penalize
CREATE TABLE [STUDENT PENALIZE](
StudentID VARCHAR(7) CHECK(StudentID LIKE 'SE[0-9][0-9][0-9][0-9][0-9]' OR
StudentID LIKE 'SB[0-9][0-9][0-9][0-9][0-9]' OR StudentID LIKE 'HE[0-9][0-9][0-9]
[0-9][0-9]') FOREIGN KEY REFERENCES dbo.STUDENT(StudentID),
PenalizeID INT FOREIGN KEY REFERENCES dbo.PENALIZE(PenalizeID),
[Date] DATETIME NOT NULL CHECK(Date<=GETDATE()),
Quantity SMALLINT DEFAULT 1 NOT NULL
)
Example:
B. STUDENT
C. PANALIZE
D. ROOM
E. ITEMS
F. 3 NORMAL FORM
BEFORE AFTER
Because, the DomName and Floor can get by RoomID (ex: a room’s name A207. A207
the first word mean this room from DomA. A207 the first number mean this room from second
Floor). We can get Gender of each room by Student’s gender. So we can drop entities to get 3NF.
G. FULL DIAGRAM
V. SQL COMMAND
I using Microsoft SQL Server 2016, this server build intelligent, mission-critical applications using
a scalable, hybrid database platform that has everything built in—from in-memory performance and
advanced security to in-database analytics.
A. QUERY USING ORDER BY
Code:
SELECT *
FROM dbo.STUDENT
ORDER BY GuardID ASC
Result:
We use query containing ORDER BY to sort the list ascending or descending by the values of a
domain. SELECT * FROM STUDENT command give us all record in Student table and then sort the
records ascending by GuardID which is id of guard
Result:
Result:
We count number of the room is empty of all dom from room table. We have to use function
COUNT() with parameter is RoomID and condition is Status = 0 which is count the number bed is
empty.
Code:
SELECT TOP 2 p.*
FROM dbo.PENALIZE p
ORDER BY p.Price DESC
Result:
Shows another example of query using aggregate functions, which is TOP command.
We use this command to find out top of two penalize in Penalize table and sort them by the price.
Result:
Assume that have four boy student want to live together and they require that in floor two in
dom a. So, guard have write query display name of room enough that require. Firstly, we have to create
table temporary to save RoomID, Number Room, Number Bed, Name Dom, Floor, Status and gender
from Room table. Then, we have to use that temporaty table, display number room and count number
bed when status equal to 0 meaning is that number bed is empty. Moreover, we have to check that
room for men and GROUP BY with Number Room, and HAVING count the number bed have to more
than 3.
Result:
If guard want to display these student doesn’t deposit in the dorm. Firstly, we will return
studentID were deposit in the dorm so, Check price equal to 1 from Register table. Then we display
student deposit in the dorm by way display information of student if StudentId not is appear studentId
we return above.
Result:
Then guard solute issue I talk example uses having and group by, the the student want to
register the room that valid that require. So we have to number of bed to student can register, we can
uses LIKE combination A205 %, % it’s meaning that any letter.
Result:
H. STORE PROCEDURE
Code:
CREATE PROC Information_Student
@mssv VARCHAR(7)
AS
BEGIN
SELECT s.StudentID, s.LastName +' '+s.[First Name] AS 'Full Name', s.Phone, s.Email,
s.Role, r.RoomID, COUNT(sp.PenalizeID) AS 'Number Penalize'
FROM dbo.STUDENT s
LEFT OUTER JOIN dbo.REGISTER r ON r.StudentID = s.StudentID
LEFT OUTER JOIN dbo.[STUDENT PENALIZE] sp ON sp.StudentID = s.StudentID
WHERE s.StudentID = @mssv
GROUP BY s.StudentID, s.LastName, s.[First Name], s.Phone, s.Email, s.Role, r.RoomID
END
Result:
Enter the Student Code, print the student's information, how many rooms are in the room, and
how many times the student owes the student's debt.
I. TRIGGER
Code:
CREATE TRIGGER Check_Register
ON dbo.REGISTER
AFTER INSERT , UPDATE
AS
BEGIN
DECLARE @roomid VARCHAR(7)
SELECT @roomid = (SELECT Inserted.RoomID FROM Inserted)
IF (SELECT r.Status FROM dbo.ROOM r WHERE @roomid = r.RoomID) = 1
BEGIN
DELETE dbo.REGISTER WHERE @roomid = RoomID
PRINT 'Not empty'
END
END
THE END