IMG-LOGO
×

Giáo Trình Môn Học

Khoá học lập trình SQL

Kiến Thức Core SQL Server

[Video] Hướng dẫn cách xác định kiểu dữ liệu, primary key, foreign key trong SQL - My SQL - SQL Server [Video] Hướng dẫn thao tác nhiểu bảng - select - multi table - join, inner join trong SQL Server [Video] Hướng dẫn phân tích CSDL - phân tích bảng, column trong bản - Học lập trình SQL, SQL Server [Video] Hướng dẫn tìm hiểu create, alter , insert table và bản chất primary key, foreign key - SQL Server [Video] Hướng dẫn tìm hiểu select, and, in, like, not in,group by .. having, order by asc, desc - SQL Server [Video] Quản lý sinh viên (create, alter, update, insert, select) - Lập trình SQL Server

View SQL Server

[Video] Hướng dẫn tìm hiểu view trong SQL Server Bài Giảng: Hướng dẫn tìm hiểu view + proc trong SQL - Lập trình SQL Server

Store & Procedure

[Video] Hướng dẫn tìm hiểu proc procedure trong SQL Server - Lập trình SQL Server, MySQL Bài Giảng: Xây dự hệ thống đưa đón học sinh bằng xe bus của Trường Học - Lập trình SQL Server [Video] Xây dựng CSDL quản lý điểm danh aptech - Lập trình SQL Server/MySQL

Trigger

[Video] Học Trigger qua ví dụ - quản lý nhà trên phố - Lập trình SQL Server [Video] Hướng dẫn học trigger qua bài quản lý điểm danh Aptech - Lập trình SQL Server [Video] Bài tập - Quản lý nhân khẩu - Lập trình SQL Server - MySQL




Trang Chủ SQL Server/MySQL Bài tập - Thiết kế CSDL quản lý sở thú - SQL Server

Bài tập - Thiết kế CSDL quản lý sở thú - SQL Server

by GokiSoft.com - 17:37 30/10/2021 4,144 Lượt Xem

Xây dựng CSDL phục vụ cho sở thú gồm các bảng như sau

1. Bảng Room (Thông tin từng chuồng trong sở thú)

- id: kiểu dữ liệu int

- name: nvarchar(20)

- max: int -> số lượng động vật tối đa

2. Thông tin động vật (Animal)

- id: int

- name: nvarchar(50)

- age: int -> tuổi của động vật

- buy_at: datetime -> ngày mua động vật vể sở thú

- room_id: int -> mã chuồng đang nuôi động vật

3. Loại thức ăn (FoodType)

- id: int

- name: nvarchar(50)

- price: float -> giá tiền / 1 kg hoặc item của sản phẩm

- amount: float -> số lượng còn lại trong sở thú

4. Quản lý thức ăn (FoodAnimal)

- food_id: int

- animal_id: int


Yêu cầu:

1. Tạo bảng trên

2. Tạo primary key & foreign key cho từng bảng -> Không tạo trong câu lệnh create table

3. Thêm mỗi bảng 5 bản ghi dữ liệu

4. Xem thông tin động vật gồm các trường sau: tên chuồng, tên động vật, tuổi, ngày mua về

5. Xem thông tin những chuồng có số động vật đang lưu vượt quá max của chuồng đó

6. Xem thông tin những chuồng còn so khả năng lưu thêm động vật vào

7. Viết proc có tham số là @animalId -> cho phép xem được thông tin loại thức ăn của động vật này.

8. Viết trigger intead of -> cho phép xóa 1 loại thức ăn trong bảng FoodType

Bình luận



Chia sẻ từ lớp học

Võ Như Việt [C2010L]

Ngày viết: 17:20 20/04/2021



create database CSDL_Quan_Ly_So_Thu

use CSDL_Quan_Ly_So_Thu

-- tao bang ROOm--
create table Room(
	id int primary key identity(1,1),
	name nvarchar(20),
	max int
)
-- tao bang Animal--
create table Animal(
	id int primary key identity(1,1),
	name nvarchar(50),
	age int,
	buy_at datetime,
	room_id int,
	constraint fk_room_id foreign key (room_id) references Room(id)
)
-- tao bang loai Food--
create table FoodType(
	id int primary key,
	name nvarchar(50),
	price float,
	amount float
)
-- tao bang FoodAnimal--
create table FoodAnimal(
	food_id int,
	animal_id int
	primary key( food_id, animal_id)
	constraint fk_food_id foreign key (food_id) references FoodType(id),
	constraint fk_animal_id foreign key (animal_id) references Animal(id)
)
--2. da tao nhu tren--

--3. them 5 ban ghi moi Bang--
insert into Room(name,max)
values
('Chuong 1', 20),
('Chuong 2', 10),
('Chuong 3',15),
('Chuong 4',25),
('Chuong 5',30)

select * from Room

insert into Animal(name,age,buy_at,room_id)
values
('Voi',11,'2021-4-5',1),
('Su Tu',12,'2021-4-5',3),
('Khi',13,'2021-4-5',5),
('Ngua Van',14,'2021-4-5',4),
('Huou Cao Co',15,'2021-4-5',2)

select * from Animal

insert into FoodType(id,name,price,amount)
values
(1,'Co 4 la',10000,11),
(2,'Thit Heo',50000,12),
(3,'Co 5 la',40000,13),
(4,'Co luck luck',20000,14),
(5,'Co Cao Co',30000,15)

insert into FoodAnimal(food_id,animal_id)
values
(1,5),
(2,2),
(3,4),
(4,1),
(5,3)

--xem cac du lieu--
select *  from Room
select *  from Animal
select *  from FoodType
select *  from FoodAnimal

--4. xem thong tin gom cac Truong : ten chuong, ten dong vat, tuoi, ngay mua ve

select Room.name 'Ten Chuong' , Animal.name'Ten Dong Vat',Animal.age'Tuoi',Animal.buy_at'Ngay Mua Ve'
from Room,Animal
where Room.id = Animal.room_id

--5. Xem Thông tin những chuồng có số động vật đang lưu vượt quá max của chuồng--

select Room.name 'Ten Chuong', Room.name'Ten Chuong', count(Animal.room_id) 'So Luong Chuong'
from Room,Animal
where Room.id = Animal.id
group by Room.id, Room.name
having count(Animal.room_id) > 2

--6.Xem Thông tin những chuồng con co kha nang luu them dong vat--

select Room.name 'Ten Chuong', Room.name'Ten Chuong', count(Animal.room_id) 'So Luong Chuong'
from Room,Animal
where Room.id = Animal.id
group by Room.id, Room.name
having count(Animal.room_id) < 30


--7.Viet proc cho phep xem thong tin loai thuc an cua dong vat--
create proc Proc_FoodAnimal_check 
	@animalld int
as
begin
	select Animal.name,Animal.age,FoodType.name,Animal.id
	from Animal,FoodType,FoodAnimal
	where Animal.id = FoodAnimal.animal_id
		and FoodAnimal.food_id = FoodType.id
		and Animal.id [email protected]
end

exec Proc_FoodAnimal_check 3 

--8 .trigger --

create trigger trigger_inteadOfDelete_FoodType on FoodType
instead of delete
as
begin
	delete from FoodAnimal where food_id in (select id from deleted)
	delete from FoodType where id in (select id from deleted)
end

delete from FoodType where id = 1


hieuvm0512 [community,C2010L]

Ngày viết: 17:23 18/04/2021



create database zootopia 

use zootopia

create table Room
(
ID int primary key identity(1,1),
name nvarchar(25),
chuong int,
check (chuong<10)
)

create table Animal
(
ID int primary key identity(1,1),
name nvarchar(50),
age int,
buy_at datetime,
room_id int
)

create table FoodType
(
ID int primary key identity(1,1),
name nvarchar(50),
price float,
amount float
)

create table FoodAnimal
(
food_id int not null,
animal_id int not null
)

drop table FoodAnimal
alter table Animal
add constraint FK_RID foreign key(room_id) references Room(ID)

alter table FoodAnimal
add constraint PK_A Primary key(food_id,animal_id)

alter table FoodAnimal
add constraint FK_Food foreign key(food_id) references FoodType(ID)


insert into Room(name,chuong)
values
('Chuong cho',9),
('Chuong bo',2),
('Chuong lon',5),
('Chuong ga',9),
('Chuong ngua',4)

insert into Animal(name,age,buy_at,room_id)
values
('Corgi',5,'2021-01-05 18:00:00',1),
('Belge',8,'2021-02-05 17:30:00',1),
('Doberman',2,'2021-02-05 15:25:00',1),
('MooMoo',10,'2021-03-05 07:00:00',2),
('COWard',11,'2021-03-06 08:00:00',2),
('Peppa',2,'2021-01-09 19:00:00',3),
('Egypt',5,'2021-03-05 14:00:00',4),
('Big Black Cock',1,'2021-01-06 06:00:00',4),
('EggMachine',1,'2021-01-06 09:00:00',4),
('Al',12,'2021-01-18 16:00:00',5)

insert into FoodType(name,price,amount)
values
('Gieng',5.000,3.4),
('Gung',10.000,5.4),
('Weed',500.000,2.5),
('SaOt',10.000,4.3)

insert into FoodAnimal(food_id,animal_id)
values
(1,1),
(1,2),
(1,3),
(2,7),
(2,8),
(2,9),
(3,4),
(3,5),
(4,6),
(4,10)

drop table FoodAnimal
select *from Animal
alter table FoodAnimal
add constraint FK_Fad foreign key(animal_id) references Animal(ID)

select Room.name 'Ten chuong', Animal.name 'Ten Dong Vat', Animal.age 'Tuoi', Animal.buy_at 'Ngay mua'
from Animal,Room
where Room.ID=Animal.room_id

select Room.ID 'Ma Chuong', Room.name 'Ten chuong', Count(Animal.room_id) 'So luong'
from Animal,Room
where Room.ID=Animal.room_id
group by Room.name,Room.ID
Having Count(Animal.room_id)>2

select Room.ID 'Ma Chuong', Room.name 'Ten chuong', Count(Animal.room_id) 'So luong'
from Animal,Room
where Room.ID=Animal.room_id
group by Room.name,Room.ID
Having Count(Animal.room_id)<10

create proc LTA 
@animalid int
as
select Animal.name 'Ten Vat Nuoi', FoodType.name 'Loai Thuc An'
from Animal,FoodAnimal,FoodType
where Animal.ID = FoodAnimal.animal_id
and FoodType.ID = FoodAnimal.food_id
and @animalid = FoodAnimal.animal_id

exec LTA 7

drop proc LTA

create trigger Xoa_Food on FoodType

instead of delete
as
begin
delete from FoodAnimal where food_id in (select id from deleted)
delete from FoodType where ID in (select id from deleted)
end

drop trigger Xoa_Food
delete from FoodType
where ID = 1


select*from Animal
Select*from Room
Select*from FoodType
Select*from FoodAnimal



Vũ Ngọc Văn [community,C2010L]

Ngày viết: 14:45 18/04/2021



create database db0417_QLsoThu

use db0417_QLsoThu

create table Room (
	id int not null,
	name nvarchar(20),
	max int
)

create table Animal (
	id int not null,
	name nvarchar(50),
	age int,
	buy_at datetime,
	room_id int
)

create table FoodType (
	id int not null,
	name nvarchar(50),
	price float,
	amount float
)

create table FoodAnimal (
	food_id int not null,
	animal_id int not null
)



alter table Room
add primary key (id)

alter table Animal
add primary key (id)

alter table FoodType
add primary key (id)

alter table FoodAnimal
add primary key (food_id, animal_id)



alter table Animal
add foreign key (room_id) references Room(id)

alter table FoodAnimal
add foreign key (food_id) references FoodType(id)

alter table FoodAnimal
add foreign key (animal_id) references Animal(id)



insert into Room
values
	(1, 'Chuong 1', 5),
	(2, 'Chuong 2', 1),
	(3, 'Chuong 3', 10),
	(4, 'Chuong 4', 12),
	(5, 'Chuong 5', 21)

insert into FoodType
values
	(21, 'Thit bo', 100000, 50),
	(22, 'Thit lon', 50000, 100),
	(23, 'Thit ga', 70000, 75),
	(24, 'Tao', 50000, 150),
	(25, 'Co', 5000, 250)
	
insert into Animal
values
	(31, 'Ho cai', 3, '2021-01-01 08:00:00', 2),
	(32, 'Ho duc', 5, '2021-03-12 08:35:00', 2),
	(33, 'Da dieu', 6, '2021-04-01 08:00:00', 3),
	(34, 'Huou', 4, '2021-04-10 16:00:00', 5),
	(35, 'Su tu cai', 1, '2021-04-15 08:00:00', 4)

insert into FoodAnimal
values
	(21, 31),
	(23, 32),
	(24, 33),
	(25, 34),
	(22, 35)



select Room.id 'IdChuong', Room.name 'TenChuong', Animal.name 'TenDongVat', Animal.age 'Tuoi', Animal.buy_at 'NgayMuaVe'
from Room join Animal on Room.id = Animal.room_id
order by Room.id

select Room.*, count(Animal.room_id) 'SoLuongCan', count(Animal.room_id) - Room.max 'SoLuongConThieu'
from Room join Animal on Room.id = Animal.room_id
group by Room.id, Room.name, Room.max
having count(Animal.room_id) > Room.max

select Room.*, count(Animal.room_id) 'SoLuongCan', Room.max - count(Animal.room_id) 'SoLuongConThua'
from Room join Animal on Room.id = Animal.room_id
group by Room.id, Room.name, Room.max
having count(Animal.room_id) < Room.max



create proc proc_view_foodType @animalID int
as
	select Animal.id 'Animal_ID', Animal.name 'Animal_Name', FoodType.*
	from FoodType join FoodAnimal on FoodType.id = FoodAnimal.food_id
	join Animal on FoodAnimal.animal_id = Animal.id
	where Animal.id = @animalID
go

exec proc_view_foodType '34'

create trigger delete_in_FoodType on FoodType
instead of delete
as
	delete from FoodAnimal where food_id in (select id from deleted)
	delete from FoodType where id in (select id from deleted)
go

select * from FoodType

delete from FoodType where id = 23

select * from FoodAnimal


Cao Tuấn Minh [community,C2010L]

Ngày viết: 21:17 17/04/2021



create table Room (
	RoomID int identity(1,1),
	RoomName nvarchar(20),
	Max int 
)

create table Animal (
	AnimalID int identity(1,1),
	AnimalName nvarchar(50),
	AnimalAge int,
	BuyDate datetime,
	RoomID int
)

create table FoodType (
	FoodID int identity(1,1),
	FoodName nvarchar(50),
	Price float,
	Amount float
)

create table FoodManagement (
	FoodID int,
	AnimalID int
)

alter table Room
add primary key (RoomID);

alter table Animal
add primary key (AnimalID);

alter table FoodType
add primary key (FoodID);

alter table Animal
add foreign key (RoomID) references Room(RoomID);

alter table FoodManagement
add foreign key (FoodID) references FoodType(FoodID);

alter table FoodManagement
add foreign key (AnimalID) references Animal(AnimalID);

insert into Room(RoomName, Max)
values
('Room A', 20),
('Room B', 40),
('Room C', 15),
('Room D', 35),
('Room E', 10)

select* from Room
select* from Animal
select* from FoodType
select* from FoodManagement

insert into Animal(AnimalName, AnimalAge, BuyDate, RoomID)
values
('Monkey', 4, '2020-10-10', 4),
('Cheetah', 6, '2020-11-20', 1),
('Flamingo', 1, '2020-04-18', 2),
('Rhino', 7, '2020-10-07', 5),
('Giraffe', 2, '2020-05-30', 3)

insert into FoodType(FoodName, Price, Amount)
values
('Banana', 4000000.500, 20),
('Meat', 2000000.500, 44),
('Seed', 100000.500, 22),
('Rats', 5000000.500, 40),
('Herbs', 500000.500, 35)


insert into FoodManagement(FoodID, AnimalID)
values
(1, 1),
(2, 2),
(3, 3),
(4, 4),
(5, 5)

select Room.RoomName, Animal.AnimalName, Animal.AnimalAge, Animal.BuyDate
from Room, Animal
where Animal.RoomID = Room.RoomID

create proc Procedure_Check_FoodType
@animalID int
as
begin
	select Animal.AnimalName, FoodType.FoodName, FoodType.Price, FoodType.Amount
	from Animal, FoodType, FoodManagement
	where Animal.AnimalID = FoodManagement.AnimalID
		and FoodManagement.FoodID = FoodType.FoodID
		and Animal.AnimalID = @animalID
end

exec Procedure_Check_FoodType 3;

create trigger Foodtype_Delete 
on FoodType
instead of delete
as 
begin
	delete from 
end


nguyen hoang viet [community,C2009I]

Ngày viết: 16:46 09/03/2021

create database QuanLySoThu

use QuanLySoThu


create table Room(

Id int not null,

Name nvarchar(20),

Max int

)


create table Animal(

Id int not null,

Name nvarchar(50),

Age int,

Buy_At date,

Room_Id int

)


create table FoodType(

Id int not null,

Name nvarchar(50),

Price float,

Amount float

)


create table FoodAnimal(

Food_Id int not null,

Animal_Id int not null

)


alter table Room

add constraint PK_Room primary key (Id)


alter table Animal

add constraint PK_Animal primary key (Id)


alter table FoodType

add constraint PK_Food_Type primary key (Id)


alter table Animal

add constraint FK_Animal foreign key (Room_Id) references Room(Id)


alter table FoodAnimal

add constraint Fk_Food_Id foreign key (Food_Id) references FoodType(Id)


alter table FoodAnimal

add constraint FK_Animal_Id foreign key (Animal_Id) references Animal(Id)


insert into Room(Id,Name,Max)

values

(1,'Cho',10),

(2,'Meo',15),

(3,'Ngua',5),

(4,'Ho',3),

(5,'Voi',7)


insert into Animal(Id, Name, Age,Buy_At, Room_Id)

values

(1, 'A', 5, '2020-10-10 10:00:00', 3),

(2, 'B', 3, '2021-03-02 11:00:00', 1),

(3, 'C', 10, '2020-12-12 05:00:00', 5),

(4, 'D', 4, '2021-02-05 16:30:00', 4),

(5, 'E', 7, '2020-05-06 18:42:06', 2)


insert into FoodType(Id, Name, Price, Amount)

values 

(3, 'Thuc an cho', 100000, 3428),

(2, 'Thuc an meo', 200000, 584),

(1, 'Thuc an ngua', 150000, 3648),

(5, 'Thuc an ho', 250000, 8694),

(4, 'Thuc an voi', 50000, 3578)



insert into FoodAnimal(Food_Id, Animal_Id)

values

(1, 3),

(2, 2),

(3, 1),

(4, 5),

(5, 4)


select room.Name as RoomName, Animal.Name as AnimalName, Animal.Age, animal.Buy_At 

from Room, Animal

where Animal.Room_Id = room.id


select room.Id, Room.Max, Room.Name, COUNT(Animal.Room_Id) as SoDongVat

from Animal, Room 

where Animal.Room_Id = room.id

group by room.Id, Room.Max, Room.Name having COUNT(Animal.Room_Id) > max(room.Max)


select room.Id, Room.Max, Room.Name, COUNT(Animal.Room_Id) as SoDongVat

from Animal, Room 

where Animal.Room_Id = room.id

group by room.Id, Room.Max, Room.Name having COUNT(Animal.Room_Id) < max(room.Max)


create proc Proc_Thong_Tin_Thuc_An

@animalId int

as

begin

select * from FoodType, FoodAnimal

where FoodType.Id = FoodAnimal.Food_Id and @animalId = FoodType.Id

end


exec Proc_Thong_Tin_Thuc_An 4


create trigger trigger_delete_food on FoodType

instead of delete

as

begin

delete from foodanimal where food_id in (select id from deleted)

delete from FoodType where id in (select id from deleted)

end 


delete from FoodType where id = 1


Đã sao chép!!!