Tải bản đầy đủ (.pdf) (26 trang)

Cơ Sở Dữ Liệu Quản Lý Thư Viện.pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.2 MB, 26 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

DBI202 – DATABASE SYSTEM OF DORMFPT UNIVERSITY

STUDENT NAME: NGUYỄN TRẦN VIỆT HƯNG | SE12XXX

STUDENT NAME: TRẦN TRUNG HIẾU | SE12XXX

TEACHER: …………

<b>ASSIGNMENT</b>

</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">

TABLE 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)………

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

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

I)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.

<b>Request: </b>

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 OBJECTIVESManage player and change of player.Manage salary and calculate salary each month.Manage contract for player.

Manage market value for player.

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

II)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:

- <b>Student: StudentID, GuardID, First Name, Last Name, Email, Phone, Gender, Course, Role, </b>

- <b>Register: RoomID, StudentID, Check-In, CheckPrice.</b>

- <b>Room: RoomID , Name Dom, Floor, Location, Number Bed, Gender, Status.</b>

- <b>Fix Detail: RoomID, ItemID, Fixer, Date Report, Date Dix, Quantity.</b>

- Items: <b>ItemID</b>, Name, Content, Price.

- <b>Student Penalize: StudentID, PenalizeID, Date, Quantity.</b>

- <b>Penalize: PenalizeID, Name, Price.</b>

- <b>Guard: GuardID, First Name, Last Name, Email, Phone.</b>

- <b>Manager room: RoomID, GuardID, </b>Name Dom, Price.2) SET-UP ENTITY – RELATIONSHIP

* Some symbols used in the modelKey / identifier attributeAttribute description / descriptionEntity

Weak entityRelationshipConnectivity (force) = 1

3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 3

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

Connectivity = N

3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 4

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

LINK: 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

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

1) DATABASE AND TABLE

<b>A.CREATE DATABASE PROJECTDBI202</b>

--create database

CREATE DATABASE PROJECTDBI202

<b>B.CREATE TABLE GUARD</b>

<b>Column NameData TypeDefaultCheckKey/ Index/ Constraint</b>

First Name Nvarchar(45) Not nullLast name Nvarchar(45) Not nullEmail Nvarchar(255) @fpt.edu.vn Not nullPhone 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

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

C. <b>CREATE TABLE STUDENT</b>

<b>Column NameData TypeDefaultCheckKey/ Index/ Constraint</b>

StudentID Varchar(7) SE|SB|HExxxxx PK, Not null

First Name Nvarchar(45) Not nullLast Name Nvarchar(45) Not nullEmail 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 LIKE9][0-9][0-9][0-9]'OR StudentID LIKE'SB[0-9][0-9][0-9][0-9][0-9]'OR

'SE[0-9][0-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)

Example:

<b>First Name</b>

<b>Last Name</b>

<b>EmailPhone Gender Course RoleContry</b>

SE06155 1 Tù Bùi Anh

ViênViệt NamHE13001 3 Công Lê Thành 0969981 M K13 Thành Việt 3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 7

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

<b>First Name</b>

<b>Last Name</b>

<b>EmailPhone Gender Course RoleContry</b>

SE05627 3 Hiệp Đỗ Quang hiepdq05627/2fpt.edu.vn

PhòngViệt NamSE05740 5 Hòa Phạm Ngọc 0968038

<b>A.</b>)<b>CREATE TABLE ROOM</b>

<b>Column NameData TypeDefaultCheckKey/ Index/ Constraint</b>

RoomID Char(5) A|B|C|D|Fxxxxx Primary keyName Dom Varchar(6) A|B|C|D|F Not null

Location Nvarchar(5) ‘Trái’ or ‘Phải’ Not nullNumber Bed Smallint 1 to 12 Not nullGender Char(1) M ‘F’ or ‘M’ Not nullStatus 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]'ORRoomID 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 )

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

<b>B.CREATE TABLE MANAGER ROOM</b>

<b>Column NameData TypeDefaultCheckKey/ Index/ Constraint</b>

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-

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

<b>Column NameData TypeDefault CheckKey/ Index/ Constraint</b>

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-

3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 9

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

StudentID 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 NULL

<b>D.CREATE TABLE ITEMS</b>

<b>Column NameData TypeDefaultCheckKey/ Index/ Constraint</b>

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-MT7 Toilet 500000 Toilet KOHLER Highline Classic White

3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 10

</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">

3 Giường 3000000 Giường tầng sắt Ký túc xá FPT – Hòa Phát

<b>E.CREATE TABLE FIX DETAIL</b>

<b>Column NameData TypeDefaultCheckKey/ Index/ Constraint</b>

RoomID Char(5) A|B|C|D|Fxxxxx FK reference ROOM(RoomID)

--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( ),

<b>F.CREATE TABLE PENALIZE</b>

<b>Column NameData TypeDefaultCheckKey/ Index/ Constraint</b>

3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 11

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

Code:

--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

<b>G. CREATE TABLE STUDENT PENALIZE</b>

<b>Column NameData TypeDefaultCheckKey/ Index/ Constraint</b>

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]'ORStudentID 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)

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

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

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 14

</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="16">

C. 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

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

Room entity have 7 attributes.To find this ROOM need information is NameDom, Floor, Location of room. Gender to know gender of Student this room, CheckNumberBed 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

</div><span class="text_page_counter">Trang 18</span><div class="page_container" data-page="18">

F. 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

</div><span class="text_page_counter">Trang 19</span><div class="page_container" data-page="19">

3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 18

</div><span class="text_page_counter">Trang 20</span><div class="page_container" data-page="20">

V. SQL COMMAND

I using Microsoft SQL Server 2016, this server build intelligent, mission-critical applications usinga 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 Players

order by MarketValue desc

Result:

We use query containing ORDER BY to sort the list ascending or descending by the values of a domain. SELECT * FROM Players command give us all record in Players table andthen sort the records descending by MarketValue which is value of players

B. QUERY USING INNER JOIN

Code:

select s.PlayerID, s.Name , c.CountryID

from Players s inner join Countries c on c.CountryID = s.CountryIDwhere c.CountryID = 6

3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 19

</div><span class="text_page_counter">Trang 21</span><div class="page_container" data-page="21">

In 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 tonumber 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

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 = ‘Bayern Munich’ .

Code:

SELECT TOP 2 p.*FROM Players p

ORDER BY p.MarketValue DESC

3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 20

</div><span class="text_page_counter">Trang 22</span><div class="page_container" data-page="22">

Shows 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 MarketValue.

D. QUERY USING THE GROUP BY AND HAVING CLAUSES

Code:

select c.ClubID , c.Name ,

sum(p.MarketValue) as 'TotalClubValue'from Players p , Clubs c

where p.ClubID = c.ClubIDgroup by c.ClubID , c.Namehaving sum(p.MarketValue) >= all (

select sum(p1.MarketValue) from Players p1 , Clubs c1where p1.ClubID = c1.ClubIDgroup by c1.ClubID , c1.Name)

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 cwhere p.ClubID = c.ClubID

3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 21

</div><span class="text_page_counter">Trang 23</span><div class="page_container" data-page="23">

and c.LeaguesID = l.LeaguesIDand l.LeaguesID not in (

select l1.LeaguesIDfrom Leagues l1

where l1.Name in ('League 1','Bundesliga','La Liga'))

This SQL query selects the PlayerID and Name of players who belong to clubs that are not associated with leagues named 'League 1', 'Bundesliga', or 'La Liga'. It achieves this by joining the Players table with the Clubs and Leagues tables and checking clubs that are not in these specified leagues.

F. QUERY THAT USES PARTIAL MATCHING IN THE WHERE CLAUSE

Code:

select p.*from Players p where p.ClubID = 1and p.Name LIKE 'Joe%'

3/22/2024 DBI202 – DATABASE SYSTEM OF DORM FPT UNIVERSITY 22

</div>

×