Bạn được yêu cầu xây dựng database cho hệ thống đưa đón học sinh bằng xe bus cho một trường học có nghiệp vụ như sau
- Bảng học viên gồm các trường : id tự tăng, Tên, địa chỉ, họ tên bố, me, sđt của bố, số điện thoại của mẹ, ngày sinh, giới tính, id địa điểm đón xe.
- Thông tin xe bus : id tự tăng, biển số xe, loại xe, số ghế ngồi, id tài xế
- Tài xế: id tự tăng, tên, sđt, giới tính, địa chỉ
- Lộ trình xe đi : id xe bus, id địa điểm đón
- Địa điểm đón xe : id tự tăng, địa chỉ
Yêu cầu thiết kế hệ thống trên.
Thêm mỗi bảng 5 bản ghi
Tạo Proc xem thông tin lộ trình đi của xe bus : tài xế, biển số xe, địa chỉ đón.
Tạo Proc xem thông tin sinh viên theo biển số xe.
Tao View xem thông tin sinh viên gồm : Tên SV, giới tính, địa chỉ đón
Đánh index (nonclustered index) cho column họ tên bố trên bảng học viên
Tạo trigger cho phép xoá địa chỉ đón trong bảng : Địa điểm đón xe
Theo dõi cập nhật nội dung học trên Youtube & Facebook
TRẦN VĂN ĐIỆP [Teacher]
Ngày viết: 14:52 04/03/2021
-- Tao database
create database bt1844
-- Active database
use bt1844
-- Tao tables
create table student (
id int primary key identity(1,1),
fullname nvarchar(50),
address nvarchar(200),
father_name nvarchar(50),
father_phone nvarchar(20),
mother_name nvarchar(50),
mother_phone nvarchar(20),
birthday date,
gender nvarchar(12),
position_id int
)
create table bus (
id int primary key identity(1,1),
bus_no nvarchar(12),
type nvarchar(20),
seats_num int,
driver_id int
)
create table driver (
id int primary key identity(1,1),
fullname nvarchar(50),
phone_number nvarchar(12),
address nvarchar(200),
gender nvarchar(12)
)
create table bus_travel (
bus_id int,
position_id int,
primary key (bus_id, position_id)
)
create table position (
id int primary key identity(1,1),
address nvarchar(200)
)
-- foreign key
alter table student
add constraint fk_student_position foreign key (position_id) references position(id)
alter table bus
add constraint fk_bus_driver foreign key (driver_id) references driver (id)
alter table bus_travel
add constraint fk_bus_travel_position foreign key (position_id) references position (id)
alter table bus_travel
add constraint fk_bus_travel_bus foreign key (bus_id) references bus (id)
-- insert into data
insert into position (address)
values
('1. A'),
('2. B'),
('3. C'),
('4. D'),
('5. E')
insert into driver (fullname, phone_number, address, gender)
values
('TRAN VAN A', '123', 'A', 'Nam'),
('TRAN VAN B', '123', 'B', 'Nam'),
('TRAN VAN C', '123', 'C', 'Nam'),
('TRAN VAN D', '123', 'D', 'Nam'),
('TRAN VAN E', '123', 'E', 'Nam')
insert into student (fullname, address, father_name, father_phone, mother_name, mother_phone, birthday, gender, position_id)
values
('Hoc sinh A', 'Ha Noi', 'A1', '123', 'A2', '233', '2018-02-03', 'Nam Dinh', 1),
('Hoc sinh B', 'Ha Noi', 'B1', '123', 'B2', '233', '2018-01-03', 'Nam Dinh', 2),
('Hoc sinh C', 'Ha Noi', 'C1', '123', 'C2', '233', '2018-03-03', 'Nam Dinh', 3),
('Hoc sinh D', 'Ha Noi', 'D1', '123', 'D2', '233', '2018-06-03', 'Nam Dinh', 1),
('Hoc sinh E', 'Ha Noi', 'E1', '123', 'E2', '233', '2018-09-03', 'Nam Dinh', 2)
insert into bus (bus_no, driver_id, seats_num, type)
values
('R001', 1, 20, 'A'),
('R002', 2, 20, 'A'),
('R003', 3, 20, 'A'),
('R004', 4, 20, 'A'),
('R005', 5, 20, 'A')
insert into bus_travel (bus_id, position_id)
values
(1, 1),
(1, 2),
(1, 4),
(1, 5),
(2, 2),
(2, 3),
(2, 5)
-- Proc
create proc proc_view_travel_by_bus_no
@busNo nvarchar(20)
as
begin
select driver.fullname 'Driver Full Name', bus.bus_no, position.address
from driver, bus, position, bus_travel
where driver.id = bus.driver_id
and bus.id = bus_travel.bus_id
and position.id = bus_travel.position_id
and bus.bus_no = @busNo
end
exec proc_view_travel_by_bus_no 'R001'
create proc proc_view_student_by_bus_no
@busNo nvarchar(20)
as
begin
select student.fullname, student.address, student.birthday, student.gender, student.father_name, student.father_phone, student.mother_name, student.mother_phone, bus.bus_no, position.address 'DC Don', driver.fullname
from student, bus, driver, position, bus_travel
where driver.id = bus.driver_id
and bus.id = bus_travel.bus_id
and position.id = bus_travel.position_id
and student.position_id = position.id
and bus.bus_no = @busNo
end
exec proc_view_student_by_bus_no 'R001'
create view view_student
as
select student.fullname, student.address, student.birthday, student.gender, student.father_name, student.father_phone, student.mother_name, student.mother_phone, position.address 'DC Don'
from student, position
where student.position_id = position.id
select * from view_student
create nonclustered index index_father_name on student (father_name)
-- trigger
create trigger trigger_delete on position
instead of delete
as
begin
delete from student where position_id in (select id from deleted)
delete from bus_travel where position_id in (select id from deleted)
delete from position where id in (select id from deleted)
end
select * from position
select * from student -- Uu tien du lieu student.
select * from bus_travel
delete from position where id = 1
Trinh Huy Hung [community,C2009I]
Ngày viết: 17:27 02/03/2021
create database ManagementBus
use ManagementBus
create table Student(
StudentId int identity(1, 1) primary key,
StudentName nvarchar(50),
Address nvarchar(100),
FatherName nvarchar(50),
MotherName nvarchar(50),
FatherPhoneNumber nvarchar(20),
MotherPhoneNumber nvarchar(20),
Birthday date,
Gender nvarchar(50),
LocationId int
)
create table Bus(
BusId int identity(1, 1) primary key,
License nvarchar(50),
Type nvarchar(50),
Seat int,
DriverId int
)
create table Driver(
DriverId int identity(1, 1) primary key,
DriverName nvarchar(50),
PhoneNumber nvarchar(20),
Gender nvarchar(50),
Address nvarchar(100)
)
create table Route(
BusId int,
LocationId int
Constraint PK_Route primary key (BusId, LocationId)
)
create table Location(
LocationId int identity(1, 1) primary key,
Address nvarchar(100)
)
alter table Student
add foreign key (LocationId) references Location(LocationId)
alter table Route
add foreign key (BusId) references Bus(BusId)
alter table Route
add foreign key (LocationId) references Location(LocationId)
alter table Bus
add foreign key (DriverId) references Driver(DriverId)
insert into Location(Address)
values
('So 1A'),
('So 2A'),
('So 1B'),
('So 1C'),
('So 2B')
insert into Student(StudentName, Address,FatherName,MotherName,FatherPhoneNumber,MotherPhoneNumber,Birthday,Gender,LocationId)
values
('Nguyen Van AAA','Chung Cu A1','Nguyen Van AA','Nguyen Thi A','012345678','012365487','2010-05-03','Nam',1),
('Le Van BBB','Chung Cu B1','Le Van BB','Le Thi B','012345679','012365489','2010-05-06','Nam',3),
('Nguyen Van DDD','Chung Cu A1','Nguyen Van DD','Nguyen Thi D','012345677','012365477','2010-02-03','Nam',1),
('Nguyen Van CCC','Chung Cu A2','Nguyen Van CC','Nguyen Thi C','012345676','012365486','2009-05-03','Nam',2),
('Nguyen Thi ABC','Chung Cu B2','Nguyen Van AB','Le Thi C','012345672','012365482','2011-05-03','Nu',6)
insert into Driver(DriverName, PhoneNumber, Gender, Address)
values
('Hoang','098745632','Nam','KTX1'),
('Long','098745631','Nam','KTX2'),
('Ngoc','098745633','Nu','KTX3'),
('Tuan','098745634','Nam','KTX4'),
('Phuong','098745635','Nam','KTX5')
insert into Bus(License, Type, Seat, DriverId)
values
('AB-123-456','Ngoi', 16, 1),
('AB-123-654','Ngoi', 32, 2),
('AB-789-456','Ngoi', 16, 3),
('AB-987-456','Ngoi', 32, 4),
('AB-456-789','Ngoi', 16, 5)
insert into Route(BusId, LocationId)
values
(1,1),
(1,2),
(2,3),
(2,4),
(3,6)
create proc proc_view_Route_Bus
@BusID int
as
begin
select Driver.DriverName, Bus.License, Location.Address
from Driver, Bus, Location, Route
where Driver.DriverId=Bus.DriverId and Route.LocationId=Location.LocationId and Bus.BusId=Route.BusId and [email protected]
end
exec proc_view_Route_Bus 1
create proc proc_view_student_licenseBus
@License nvarchar(50)
as
begin
select Student.StudentName, Student.Gender, Location.Address
from Student, Location, Bus, Route
where Student.LocationId=Location.LocationId and Bus.BusId=Route.BusId and Route.LocationId=Location.LocationId and [email protected]
end
exec proc_view_student_licenseBus 'AB-123-456'
create clustered index CLI_StudentFatherName on Student(FatherName)
create trigger trigger_instead_of_Address on Location
instead of delete
as
begin
delete from Student where LocationId in (select LocationId from deleted)
delete from Route where LocationId in (select LocationId from deleted)
delete from Location where LocationId in (select LocationId from deleted)
end
select * from Student
select *from Location
select * from Route
select * from Bus
select * from Driver
delete from Location where LocationId=6
Lê Sĩ Tuyển [community,C2009I]
Ngày viết: 16:43 02/03/2021
Create Database bai10
go
use bai10
go
--Create Table--
Create table HocVien(
id int identity(1,1) not null,
Student_Name Nvarchar(100) ,
address_std Nvarchar(max),
Dad_Name Nvarchar(100),
Mom_Name Nvarchar(100),
Dad_Phone Nvarchar(20),
Mom_Phone Nvarchar(20),
date_birth Datetime,
gender Nvarchar(10),
id_address_bus int not null
)
Create Table Bus_Information(
Bus_ID int identity(1,1) not null,
Bus_Number Nvarchar(20),
Category_Bus Nvarchar(100),
Seating_Bus int,
Driver_id int not null
)
Create Table Driver(
id int identity(1,1) not null,
Driver_Name Nvarchar(100),
Driver_Phone Nvarchar(20),
gender Nvarchar(10),
Driver_address Nvarchar(max)
)
Create Table Bus_Route(
Bus_ID int not null,
id_address_bus int not null
)
Create Table Bus_address(
id int identity(1,1) not null,
address_bus Nvarchar(max)
)
--insert data--
insert into HocVien(Student_Name,address_std,Dad_Name,Mom_Name,Dad_Phone,Mom_Phone,date_birth,gender,id_address_bus)
values
('Nguyen Van A','Hoang Mai','Nguyen Van R','Pham Thi O','01234567891','01234567891','2002-12-06','Male',5),
('Nguyen Van X','Hoang Mai','Nguyen Van T','Pham Thi H','01234567891','01234567891','2002-12-06','Male',6),
('Nguyen Van Q','Hoang Mai','Nguyen Van Y','Pham Thi K','01234567891','01234567891','2002-12-06','Male',7),
('Nguyen Van Z','Hoang Mai','Nguyen Van U','Pham Thi L','01234567891','01234567891','2002-12-06','Male',8),
('Nguyen Van W','Hoang Mai','Nguyen Van I','Pham Thi M','01234567891','01234567891','2002-12-06','Male',9)
insert into Bus_Information(Bus_Number,Category_Bus,Seating_Bus,Driver_id)
values
('29A-H17-5678','Porsche',40,1),
('30A-H18-5678','Lamborghini',40,2),
('36A-H19-5778','PhanTom',60,3),
('88A-H20-5878','Lexus',50,4),
('45A-H21-5578','toyota',20,5)
insert into Driver(Driver_Name,Driver_Phone,gender,Driver_address)
values
('Tran Van A','0371465787','Male','Linh Nam'),
('Tran Van B','0371465797','Male','Nam Dinh'),
('Tran Van C','0371465757','Male','Ha Tinh'),
('Tran Van D','0371465767','Male','Thanh Hoa'),
('Tran Van E','0371465787','Male','Nghe An')
insert into Bus_Route(Bus_ID,id_address_bus)
values
(1,5),
(2,6),
(3,7),
(4,8),
(5,9)
insert into Bus_address(address_bus)
values
('Hoang Mai'),
('Ba Dinh'),
('Cau Giay'),
('Linh Nam'),
('Hai Ba Trung')
select *from HocVien
select *From Bus_Information
select*from Driver
select *from Bus_Route
select *from Bus_address
--Create PRoc---
Alter Proc ST_Information_Route_Bus
as
begin
select Driver.Driver_Name,Bus_Information.Bus_Number,Bus_address.address_bus
from Driver,Bus_Information,Bus_address
where Driver.id=Bus_address.id and Driver.id=Bus_Information.Driver_id
end
exec ST_Information_Route_Bus
Create proc ST_Information_STD_BusNumber
@BusNumber Nvarchar(50)
as
begin
select HocVien.Student_Name,HocVien.address_std,HocVien.Dad_Name,HocVien.Mom_Name,HocVien.Dad_Phone,HocVien.Mom_Phone,
HocVien.date_birth,HocVien.id_address_bus,Bus_Information.Bus_Number,@BusNumber as'Bien So xe'
from HocVien,Bus_Information,Bus_address,Bus_Route
where HocVien.id_address_bus=Bus_Route.id_address_bus
and Bus_Information.Bus_ID=Bus_Route.Bus_ID
and Bus_Route.id_address_bus=Bus_address.id
and [email protected]
end
exec ST_Information_STD_BusNumber '29A-H17-5678'
--VIEW--
Alter view Information_STD
as
select HocVien.Student_Name,HocVien.gender,Bus_Route.id_address_bus,Bus_address.address_bus
from HocVien,Bus_address,Bus_Route
where Bus_Route.id_address_bus=Bus_address.id
select *from Information_STD
--index--
create clustered index CLID_Dad_Name on HocVien(Dad_Name)
--trigger--
Create Trigger delete_Address on Bus_address
instead of delete
as
begin
delete from Bus_address where address_bus in (select address_bus from deleted)
end
delete from Bus_address
Nguyễn Hữu Hiếu [T2008A]
Ngày viết: 11:46 16/12/2020
create database hethongduadonhocsinh
use hethongduadonhocsinh
create table diadiemdonxe (
id int primary key identity(1,1),
address nvarchar(200)
)
create table taixe(
id int primary key identity(1,1),
ten nvarchar(50),
sdt nvarchar(25),
gioitinh nvarchar(10),
diachi nvarchar(100)
)
create table thongtinxebus(
id int primary key identity(1,1),
bienxo nvarchar(20),
loaixe nvarchar(20),
soghe int,
id_taixe int references taixe(id)
)
create table lotrinhxedi(
id int primary key identity(1,1),
id_diadiemdon int references diadiemdonxe(id)
)
create table sinhvien(
id int primary key identity(1,1),
ten nvarchar(50),
diachi nvarchar(100),
tenbome nvarchar(50),
id_diadiemdonSV int references diadiemdonxe(id)
)
insert into diadiemdonxe (address)
values
('Cau Giay'),
('Thanh Xuan'),
('Hoan Kiem'),
('Thanh Nhan'),
('Cau Giay 2')
insert into taixe(ten,sdt,gioitinh,diachi)
values
('Nguyen A', '03434','Nam','Ha Noi'),
('Nguyen B', '0425','Nu','Ha Noi 2'),
('Nguyen C', '04348','Nam','Ha Noi 3'),
('Nguyen D', '09998278','Nam','Ha Noi 4'),
('Nguyen E', '09998278','Nam','Ha Noi 2')
insert into thongtinxebus(bienxo,loaixe,soghe,id_taixe)
values
('22345','Toyota',20,2),
('26545','Toyota 2',10,1),
('22424','Toyota 2',30,1),
('146535','Toyota 3',40,3),
('42324','Toyota 3',30,4)
insert into lotrinhxedi(id_diadiemdon)
values (1), (2), (2), (3), (3), (5)
insert into sinhvien(ten,diachi,tenbome,id_diadiemdonSV)
values
('Nguyen Con A','Ha Noi','Nguyen Bo A',2),
('Nguyen Con B','Ha Noi 2','Nguyen Bo B',1),
('Nguyen Con C','Ha Noi 3','Nguyen Bo B',2),
('Nguyen Con D','Ha Noi 4','Nguyen Bo B',3),
('Nguyen Con E','Ha Noi 5','Nguyen Bo B',4)
--Proc xem thông tin lộ trình đi của xe bus : tài xế, biển số xe, địa chỉ đón
CREATE PROC PROC_LOTRINHXE
@ID_XE INT
AS
BEGIN
SELECT taixe.ten, thongtinxebus.bienxo, diadiemdonxe.address
from taixe, thongtinxebus, diadiemdonxe
where taixe.id = thongtinxebus.id_taixe and diadiemdonxe.id = thongtinxebus.id_taixe
and thongtinxebus.id = @ID_XE
END
exec PROC_LOTRINHXE 1
--Tạo Proc xem thông tin sinh viên theo biển số xe.
create proc proc_SV_theo_biensoxe2
@biensoxe nvarchar(20)
as
begin
select sinhvien.ten, sinhvien.id_diadiemdonSV, thongtinxebus.bienxo
from sinhvien, thongtinxebus
where
and thongtinxebus.bienxo = @biensoxe
end
exec proc_SV_theo_biensoxe2 26545
select * from thongtinxebus
select * from sinhvien
Nguyễn Xuân Mai [T2008A]
Ngày viết: 22:21 06/12/2020
create database Hethongduadon
use Hethongduadon
create table student(
id int primary key identity(1,1),
name nvarchar(50),
address nvarchar(100),
dad_name nvarchar(50),
mom_name nvarchar(50),
phone_dad int,
phone_mom int,
birthdate date,
gender nvarchar(20),
id_pickupad int references pickup(id)
)
insert into student(name, address, dad_name,mom_name,phone_dad,phone_mom,birthdate,gender, id_pickupad)
values
('Nguyen A', 'Nguyen Chi Thanh', 'Nguyen B', 'Nguyen C', 0901234567, 0811234567,'2001/03/06','Nu',1),
('Tran A', 'Hoang Dao Thuy', 'Tran B', 'Tran C', 0901234678, 0811234678,'2001/12/06','Nam',3),
('Le A', 'Hoang Minh Giam', 'Le B', 'Le C', 0901234234, 0811567567,'2002/06/15','Nu',3),
('Hoang A', 'Lieu Giai', 'Hoang B', 'Hoang C', 0901237767, 0815884567,'2002/12/26','Nu',1),
('Ngo A', 'Giang Vo', 'Ngo B', 'Ngo C', 0811232867, 0811484567,'2003/11/10','Nam',5)
select * from student
create table bus(
id int primary key identity(1,1),
bienso nvarchar(20),
type_of_car nvarchar(30),
seat_no int,
id_driver int references driver(id)
)
insert into bus(bienso, type_of_car, seat_no, id_driver)
values
('30A12345', 'Toyota', '48', 5),
('30B34567', 'Hyundai', '47', 3),
('30C23456', 'Ford', '16', 4),
('30D45678', 'Isuzu', '29', 1),
('30E56789', 'Toyota', '16', 2)
select * from bus
create table driver(
id int primary key identity(1,1),
name nvarchar(50),
gender nvarchar(30),
phone_no int,
address nvarchar(100)
)
insert into driver(name, gender, phone_no, address)
values
('Nguyen Van A', 'Nam', 0901231233, 'Ha Dong'),
('Tran Van A', 'Nam', 0901236488, 'My Dinh'),
('Le Van A', 'Nam', 0901234497, 'Hai Ba Trung'),
('Hoang Van A', 'Nam', 0901234977, 'Ly Thuong Kiet'),
('Ngo Van A', 'Nam', 0811232956, 'Doi Can')
select * from driver
create table route(
id_bus int references bus(id),
id_pickupad int references pickup(id)
)
insert into route
values
(3,1),
(4,5),
(1,2),
(2,3),
(5,4)
select * from route
create table pickup(
id int primary key identity(1,1),
address nvarchar(100)
)
insert into pickup(address)
values
('Nguyen Chi Thanh'),
('Tran Hung Dao'),
('Tran Duy Hung'),
('Lang Ha'),
('Le Van Luong')
select * from pickup
CREATE PROC route_info
AS
BEGIN
select driver.name, bus.bienso, pickup.address
from driver, bus, pickup, route
where bus.id_driver=driver.id and bus.id = route.id_bus and route.id_pickupad = pickup.id
group by driver.name, bus.bienso, pickup.address
END
EXEC route_info
CREATE PROC student_info
@bienso nvarchar(20)
AS
BEGIN
select student.*, bus.bienso
from student, bus,route
where student.id_pickupad = route.id_pickupad and route.id_bus = bus.id and bus.bienso = @bienso
END
drop proc student_info
EXEC student_info '30A12345'
create view student_info2
AS
SELECT student.name, student.gender, student.address
FROM student
select * from student_info2
create clustered index ci_dadname on Student (dad_name)
create trigger TG_pickup_delete on pickup
for delete
as
begin
if (select address from deleted) <> ''
begin
print N'Do not delete pickup address'
rollback transaction
end
end