By GokiSoft.com| 15:33 16/05/2023|
SQL Server/MySQL

[Source Code] Quản lý khách sạn - nâng cao - Lập trình Sql Server - C2212I

Quản lý khách sạn - nâng cao - Lập trình Sql Server

Nội dung kiến thức:
	- union
	- where -> null, not null
	- Phân tích thiết kế CSDL
		- Yêu cầu đơn giản -> tập phân tích
		- Vẽ mô hình entity diagram & database diagram
Làm 1 bài tập overview kiến thức trên:
=================================================
Yêu cầu thiết kế database cho website: w3schools
	- Quản lý môn học
	- Quản lý chủ đề môn học
	- Quản lý nội dung của từng chủ đề

Bảng nào
Quan hệ giữa các bảng
Các column trong bảng
???

=================================================
1) Bảng quản lý môn học -> Subject
	id: int primary identity (1,1)
	tên môn học -> subject_name: nvarchar(150)
2) Bảng quản lý bài học -> Lesson
	id: int primary identity (1,1)
	tên bài học -> lesson_name: nvarchar(150)
	subject_id: foreign key -> Subject (id)
	tên tiêu đề -> title: nvarchar(150)
	nội dung -> content: ntext
	ngày tạo bài viết -> created_at: datetime
	ngày sửa bài viết -> updated_at: datetime

-- Tao tables
create table hotel (
	id int primary key identity(1,1),
	name nvarchar(150),
	address nvarchar(200),
	area float,
	owner_name nvarchar(50)
)
go

create table room (
	room_no nvarchar(20) primary key,
	id_hotel int references hotel (id),
	area float,
	type nvarchar(20),
	floor int
)
go

create table booking (
	id int primary key identity(1,1),
	room_no nvarchar(20) references room (room_no),
	checkin datetime,
	checkout datetime,
	amount int,
	price float
)
go

-- insert data
insert into hotel (name, area, address, owner_name)
values
('Hotel 1', 100, 'Ha Noi', 'TRAN VAN A'),
('Hotel 2', 200, 'Ha Noi', 'TRAN VAN B')
go

insert into hotel (name, area, address, owner_name)
values
('Hotel 3', 300, 'Ha Noi', 'TRAN VAN A')
go

insert into room (room_no, id_hotel, area, floor, type)
values
('HA01', 1, 50, 1, 'PRO'),
('HA02', 1, 50, 2, 'PRO'),
('HB01', 2, 80, 1, 'PRO'),
('HB02', 2, 30, 2, 'PRO'),
('HB03', 2, 90, 2, 'PRO')
go

insert into booking (room_no, price, amount, checkin, checkout)
values
('HA01', 2000000, 3, '2022-02-12 08:00:00', '2023-02-16 12:00:00'),
('HA02', 1000000, 2, '2022-03-12 08:00:00', '2023-03-16 12:00:00'),
('HA01', 2500000, 3, '2022-04-12 08:00:00', '2023-04-16 12:00:00'),
('HA01', 3000000, 3, '2022-05-12 08:00:00', '2023-05-16 12:00:00'),
('HA02', 1800000, 2, '2022-06-12 08:00:00', '2023-06-16 12:00:00')
go

-- Tên KS (hotel), địa chỉ (hotel), mã phòng (room), loại phòng (room), tầng (room), diem tich phong (room)
select hotel.name 'hotel name', hotel.address, room.room_no, room.type, room.floor, room.area 'Dien Tich Room'
from hotel left join room on hotel.id = room.id_hotel
go

-- Hien thi room co dien tich > 60
select hotel.name 'hotel name', hotel.address, room.room_no, room.type, room.floor, room.area 'Dien Tich Room'
from hotel left join room on hotel.id = room.id_hotel
where room.area > 60
go

-- Tên KS (hotel), địa chỉ (hotel), số phòng
select hotel.name 'hotel name', hotel.address, count(room.room_no) 'So Phong'
from hotel left join room on hotel.id = room.id_hotel
group by hotel.name, hotel.address
go

-- Them dieu kien so phong > 2
select hotel.name 'hotel name', hotel.address, count(room.room_no) 'So Phong'
from hotel left join room on hotel.id = room.id_hotel
group by hotel.name, hotel.address
having count(room.room_no) > 2
go

-- Thông kê theo dữ liệu : Tên KS (hotel), địa chỉ (address), diện tích phòng lớn nhất (max -> area room)
select hotel.name 'hotel name', hotel.address, max(room.area) 'MAX Dien Tich'
from hotel left join room on hotel.id = room.id_hotel
group by hotel.name, hotel.address
go

select hotel.name 'hotel name', hotel.address, count(room.room_no) 'So Phong'
from hotel left join room on hotel.id = room.id_hotel
group by hotel.name, hotel.address
having count(room.room_no) > 2
union
select hotel.name 'hotel name', hotel.address, count(room.room_no) 'So Phong'
from hotel left join room on hotel.id = room.id_hotel
group by hotel.name, hotel.address
having count(room.room_no) < 3

-- Xem thong tin du lieu
select hotel.name 'hotel name', hotel.address, room.room_no, room.type, room.floor, room.area 'Dien Tich Room'
from hotel left join room on hotel.id = room.id_hotel
go

-- Loai ban ghi ma type = null
select hotel.name 'hotel name', hotel.address, room.room_no, room.type, room.floor, room.area 'Dien Tich Room'
from hotel left join room on hotel.id = room.id_hotel
where room.type is not null
go

select hotel.name 'hotel name', hotel.address, room.room_no, room.type, room.floor, room.area 'Dien Tich Room'
from hotel left join room on hotel.id = room.id_hotel
where room.type is null
go
------------------------------------- chu de tiep theo ----------------------------

Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)