The clubalso needs to ensure that the player cannot sign a contract with two or more other clubs.Information of the club include: ClubID, Name of the club Clubs also needs to manage numb
Trang 1DBI202 – DATABASE SYSTEM OF DORM
FPT UNIVERSITY
STUDENT NAME: NGUYỄN TRẦN VIỆT HƯNG | SE12XXX
STUDENT NAME: TRẦN TRUNG HIẾU | SE12XXX
TEACHER: …………
ASSIGNMENT
Trang 2TABLE OF CONTENTS
I) INTRODUCE THE PROBLEM………
1) DESCRIBE THE PROBLEM………
2) MANAGEMENT OBJECTIVES………
II) ENTITY – RELATIONSHIP – ER………
1) DIFINITION ENTITY – ATTRIBUTE………
2) SET-UP ENTITY – RELATIONSHIP………
III) DATA DICTIONARY………
1) DATABASE AND TABLE……….………
IV) ENTITY RELATIONSHIP DIAGRAM (ERD)………
1) GUARD………
2) STUDENT………
3) PANALIZE………
4) ROOM………
5) ITEMS………
6) NORMAL FORM………
V) SQL COMMAND 1) QUERY USING ORDER BY………
2) QUERY USING INNER JOIN………
3) QUERY USING AGGREGATE FUNCTIONS………
4) QUERY USING THE GROUP BY AND HAVING CLAUSES………
5) QUERY THAT USES A SUB-QUERY AS A RELATION………
6) QUERY THAT USES PARTIAL MATCHING IN THE WHERE CLAUSE………
7) QUERY THAT USES A SELF-JOIN………
8) STORE PROCEDURE………
9) TRIGGER………
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 1
Trang 3I) INTRODUCE THE PROBLEM
1) DESCRIBE THE PROBLEM
Nowadays, football becomes one of the world’s most popular sports To be able to create such a wonderful sport, it is can not be ignored to mention the players who are dedicating themselves to this sport But, it is still have not database system for manage player After the actual survey, the result are
as follows:
Every player is managed by an agent, an agent can manage many players
Each player have their own country
Player can only play for one club through contract, each club have many players
Information for contract include: StartDate, EndDate, Salary of player
Clubs records information about player, including: name, age, height, weight, position The clubalso needs to ensure that the player cannot sign a contract with two or more other clubs.Information of the club include: ClubID, Name of the club
Clubs also needs to manage number of salary to calculate salary by week or month Each club can only participate one league, but a league can have many clubs
Location of the league depend on countries
Request:
Club need to check market value to have player in their team Otherwise, club can check contract include: start date, end date to guarantee that they can not sign two contracts.Monthly, club need caculate total amount of salary
Club can check information of player
2) MANAGEMENT OBJECTIVES
Manage player and change of player
Manage salary and calculate salary each month
Manage contract for player
Manage market value for player
Important output
Total salary for player each month
Count date of the contract
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 2
Trang 4II) ENTITY – RELATIONSHIP – ER
1) DIFINITION ENTITY – ATTRIBUTE
Base on the problem description and management objectives, we can present several entities and attributes of the entity as follow:
- Student: StudentID, GuardID, First Name, Last Name, Email, Phone, Gender, Course, Role,
Country
- Register: RoomID, StudentID, Check-In, CheckPrice.
- Room: RoomID , Name Dom, Floor, Location, Number Bed, Gender, Status.
- Fix Detail: RoomID, ItemID, Fixer, Date Report, Date Dix, Quantity.
- Items: ItemID, Name, Content, Price
- Student Penalize: StudentID, PenalizeID, Date, Quantity.
- Penalize: PenalizeID, Name, Price.
- Guard: GuardID, First Name, Last Name, Email, Phone.
- Manager room: RoomID, GuardID, Name Dom, Price
2) SET-UP ENTITY – RELATIONSHIP
* Some symbols used in the model
Key / identifier attribute
Attribute description / description
Trang 5Connectivity = N
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 4
Trang 6LINK: HTTPS://GOO.GL/U4X3JO
III) DATA DICTIONARY
Just for example on some tables (other table are similar, you have to define all the tables in your database) Note: to run the query you have to define the table 1 first then go to the side tables much3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 5
Trang 71) DATABASE AND TABLE
A CREATE DATABASE PROJECTDBI202
create database
CREATE DATABASE PROJECTDBI202
B CREATE TABLE GUARD
Column Name Data Type Default Check Key/ Index/ Constraint
First Name Nvarchar(45) Not null
Last name Nvarchar(45) Not null
Email Nvarchar(255) @fpt.edu.vn Not null
Phone Varchar(11) 0 to 9 Unique
Code:
create table guard
CREATE TABLE GUARD(
GuardID INT PRIMARY KEY,
[First Name] NVARCHAR( )45NOT NULL,
[Last Name] NVARCHAR( )45NOT NULL,
Email NVARCHAR(255)CHECK(Email LIKE'%@fpt.edu.vn')NOT NULL,
Phone VARCHAR( )11UNIQUE 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:
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 6
Trang 8C CREATE TABLE STUDENT
Column Name Data Type Default Check Key/ Index/ Constraint
StudentID Varchar(7) SE|SB|HExxxxx PK, Not null
First Name Nvarchar(45) Not null
Last Name Nvarchar(45) Not null
Email Nvarchar(255) @fpt.edu.vn Not null
Gender Char(1) M ‘F’ or ‘M’ Not null
Role Nvarchar(30) Thành
Viên
‘Thành Viên’
or ‘Trưởng Phòng’
Not null
Country Nvarchar(50) VietNam Not Null
Code:
create table student
CREATE TABLE STUDENT(
StudentID VARCHAR( )7PRIMARY KEYNOT NULLCHECK(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( )45NOT NULL,
[LastName] NVARCHAR( )45NOT NULL,
Email NVARCHAR(255)CHECK(Email LIKE'%@fpt.edu.vn') NOT NULL,
Phone CHAR( )11UNIQUE 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( )30DEFAULTN'Thành Viên'CHECK Role( IN (N'Thành
Viên',N'TrưởAng Phòng'))NOT NULL ,
Country NVARCHAR( )50DEFAULTN'Việt Nam'NOT NULL
SE06155 1 Tù Bùi Anh tubase06155@gmail.co
m
01699271212
ViênViệt NamHE13001 3 Công Lê Thành congltthe13001@fpt.ed 0969981 M K13 Thành Việt 3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 7
Trang 9SE05627 3 Hiệp Đỗ Quang hiepdq05627/2fpt.edu.
vn
01649088188
PhòngViệt NamSE05740 5 Hòa Phạm Ngọc thaycacac@fpt.edu.vn 0968038
714
PhòngCampuchia
A.)CREATE TABLE ROOM
Column Name Data Type Default Check Key/ Index/ Constraint
RoomID Char(5) A|B|C|D|Fxxxxx Primary key
Name Dom Varchar(6) A|B|C|D|F Not null
Location Nvarchar(5) ‘Trái’ or ‘Phải’ Not null
Number Bed Smallint 1 to 12 Not null
Gender Char(1) M ‘F’ or ‘M’ Not null
Status Char(1) 1 ‘1’ or ‘0’ Not null
Code:
create table room
CREATE TABLE ROOM(
RoomID CHAR( )5PRIMARY 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( )6CHECK([Name Dom] IN ( , , ,'A' 'B' 'C' 'D' 'F', )),
[Floor] SMALLINT CHECK([Floor] BETWEEN AND ) NOT NULL , 1 5
[Location] NVARCHAR( )5CHECK Location( IN (N'Trái' N'Pha, Ai')) NOT NULL,
[Number Bed] SMALLINT CHECK([Number Bed] BETWEEN AND 1 12 ) NOT NULL,
Gender CHAR( )1 DEFAULT'M'CHECK(Gender IN( ,'F' 'M')) NOT NULL,
[Status] SMALLINT DEFAULT CHECK Status 1 ( BETWEEN AND ) NOT NULL 0 1
Trang 10B CREATE TABLE MANAGER ROOM
Column Name Data Type Default Check Key/ Index/ Constraint
RoomID Char(5) A|B|C|D|Fxxxxx FK reference ROOM(RoomID)
GuardID Int FK reference GUARD(GuardID) Not nullName Dom Char(1) A|B|C|D|F Not null
Price Varchar(10) %VND Not null
Code:
create table manager room
CREATE TABLE [MANAGER ROOM](
RoomID CHAR( )5FOREIGN 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( )NOTNULL,
[Name Dom] CHAR( )1CHECK([Name Dom] IN ( , , , ,'A' 'B' 'C' 'D' 'F')) NOT NULL,
Price VARCHAR( )10CHECK(Price LIKE'%VND') NOT NULL
C CREATE TABLE REGISTER
Column Name Data Type Default Check Key/ Index/ Constraint
RoomID Char(5) A|B|C|D|Fxxxxx FK reference ROOM(RoomID)StudentID Varchar(7) SE|SB|HExxxxx FK reference STUDENT (StudentID)Check-in Datetime < =Getdate() Not null
Check Price Smallint 1 0 or 1 Not null
Code:
create table register
CREATE TABLE REGISTER(
RoomID CHAR( )5FOREIGN 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]'),
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 9
Trang 11StudentID VARCHAR( )7FOREIGN KEY REFERENCES dbo.STUDENT StudentID ( )
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]')
NOT NULL,
[Check-in] DATETIME CHECK([Check-in] <=GETDATE()) NOT NULL,
[Check Price] SMALLINT DEFAULT CHECK( 1 [Check Price] BETWEEN AND ) NOT 0 1
D CREATE TABLE ITEMS
Column Name Data Type Default Check Key/ Index/ Constraint
Code:
create table items
CREATE TABLE ITEMS(
ItemID INT PRIMARY KEYNOT NULL,
[Name] NVARCHAR( )50 NOT NULL,
Price INTNOT NULL,
Content NVARCHAR(255)NOT NULL
)
Example:
5 Bóng điện 120000 Bóng đèn LED tuýp T8 N01 60/10W nhựa Rạng
Đông
6 Bình Nóng Lạnh 1800000 Bình nước nóng 30L Aiston AN30RS-MT
7 Toilet 500000 Toilet KOHLER Highline Classic White
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 10
Trang 12ItemID Name Price Content
3 Giường 3000000 Giường tầng sắt Ký túc xá FPT – Hòa Phát
E CREATE TABLE FIX DETAIL
Column Name Data Type Default Check Key/ Index/ Constraint
RoomID Char(5) A|B|C|D|Fxxxxx FK reference ROOM(RoomID)
Fixer Nvarchar(90)
Data report Datetime <= Getdate() Not null
Date Fix Datetime <= Getdate()
Code:
create table fixdetail
CREATE TABLE [FIX DETAIL](
RoomID CHAR( )5CHECK(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 INTNOT NULL
F CREATE TABLE PENALIZE
Column Name Data Type Default Check Key/ Index/ Constraint
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 11
Trang 13Code:
create table penalize
CREATE TABLE PENALIZE(
PenalizeID INT PRIMARY KEY,
[Name] NVARCHAR(255)NOT NULL,
Price INTNOT NULL
)
Example:
2 Hút thuốc trong ký túc xá 50000
4 Nuôi động vật trong ký túc xá 500000
G CREATE TABLE STUDENT PENALIZE
Column Name Data Type Default Check Key/ Index/ Constraint
StudentID Varchar(7) SE|SB|HExxxxx FK reference
STUDENT(StudentID)
PENALIZE(PenalizeID)Date Datetime <= Getdate() Not null
Code:
create table student penalize
CREATE TABLE [STUDENT PENALIZE](
StudentID VARCHAR( )7CHECK(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] DATETIMENOT NULLCHECK Date( <=GETDATE()),
Quantity SMALLINT DEFAULT 1 NOT NULL
Trang 14StudentIDPenalizeID Date Quantity
IV ENTITY RELATIONSHIP DIAGRAM (ERD)
A GUARD
This is the Guard entity, the root of whole
diagram Tournament entity has 5
attributes
The attribute GuardID also is the primary
key of this entity Each guard has Name
and contact Name is FirstName and
LastName Guard’s contact is Email and
Phone
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 13
Trang 15B STUDENT
This is the Student entity This has 10
attributes
Each student have StudentID is Primary
key Contaxt of Student has Phone number
as Phone, Email Student’s Information has
FirstName, LastName Role, Country,
Gender, Course Last, each Student is
Guarded by Guards so this have GuardID is
foriegn key
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 14
Trang 16C PANALIZE
Student could violate the rule do need
have Penalize Entity It Know as Name of
rule be violate PenalizeID is Primary key
to insert to database The last, Price is
penaties
D ROOM
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 15
Trang 17Room entity have 7 attributes.
To find this ROOM need information is
NameDom, Floor, Location of room Gender
to know gender of Student this room, Check
NumberBed to know Number of Student
this Room Status as (Full or not), The Last,
RoomID to insert to database
E ITEMS
Each Student as a room hava many ITEM so this is ITEM entity
ITEM has 3 attributes This is ItemID is Primary key to mark many the same Items in different room, Name of Item and Content as (need fix or not)
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 16
Trang 18F 3 NORMAL FORM
Because, the DomName and Floor can get by RoomID (ex: a room’s name A207 A207the first word mean this room from DomA A207 the first number mean this room from secondFloor) We can get Gender of each room by Student’s gender So we can drop entities to get 3NF
G FULL DIAGRAM
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 17
Trang 193/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 18
Trang 20V 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
B QUERY USING INNER JOIN
Code:
select s.PlayerID, s.Name , c.CountryID
from Players s inner join Countries c on c.CountryID = s.CountryID
where c.CountryID = 6
Result:
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 19
Trang 21In this query, we use INNER JOIN
command to find out who is the Players
have country is ‘Argentina’ Focus on
details, we inner join Player table with
Country table, choose the rows adapts
condition that the CountryID must equal to
number written Then we select columns
PlayerID and Name from Players table
and Check Country from Country table
where the CountryID is equal to 6
C QUERY USING AGGREGATE FUNCTIONS
Code:
select c.ClubID , c.Name , sum(p.MarketValue) as 'TotalClubValues'
from Players p , Clubs c
where c.ClubID = p.ClubID
and c.ClubID = 3
group by c.ClubID , c.Name
Result:
This is query to count total player’s values of a club name ‘Bayern Munich’ We need
to use function Sum() with parameter is MarketValue and condition is clubID = 3 or c.Name =
Trang 22Shows another example of query using aggregate functions, which is TOP command.
We use this command to find out top of two Players in Players table and sort them by the
from Players p , Clubs c
where p.ClubID = c.ClubID
group by c.ClubID , c.Name
having sum(p.MarketValue) >= all (
select sum(p1.MarketValue)
from Players p1 , Clubs c1
where p1.ClubID = c1.ClubID
group by c1.ClubID , c1.Name
)
Result:
This SQL query selects the ClubID, Name, and total market value of clubs along with their players It groups the results by ClubID and Name, calculating the total market value for each club Then, it display clubs whose total market value is greater than or equal to the total market value of all clubs
E QUERY THAT USES A SUB-QUERY AS A RELATION
Code:
select p.PlayerID, p.Name
from Players p , Leagues l , Clubs c
where p.ClubID = c.ClubID
3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 21