By GokiSoft.com| 16:58 30/05/2023|
SQL Server/MySQL

Bài tập - 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 BT1844

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 View 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ỉ đó

Liên kết rút gọn:

https://gokisoft.com/1844

Bình luận

avatar
Võ Như Việt [C2010L]
2021-04-26 16:45:16



create database XeBus
go

use XeBus
go

-- tao bang HocVien--
create table HocVien(
	Id int primary key identity(1,1) not null,
	TenHocVien nvarchar(50),
	Diachi_HV nvarchar(200),
	HoTenBo nvarchar(50),
	HoTenMe nvarchar(50),
	SDT_Bo nvarchar(11),
	SDT_Me nvarchar(11),
	NgaySinh_HV datetime,
	GioiTinh_HV nvarchar(10),
	Id_DiaDiemDon int not null
)
go
-- tao bang ThongTinXeBus--
create table ThongTinXeBus(
	Id_Xe int primary key identity(1,1) not null,
	BienSoXe nvarchar(10),
	LoaiXe nvarchar(20),
	SoGheNgoi int,
	Id_TaiXe int not null,
)
go
-- tao bang TaiXe--
create table TaiXe(
	Id_TaiXe int primary key identity(1,1) not null,
	TenTaiXe nvarchar(50),
	SDT_TaiXe nvarchar(11),
	GioiTich_TX nvarchar(10),
	DiaChi_TX nvarchar(300),
)
go
-- tao bang Lo Trinh Xe Di--
create table LoTrinhXe(
	Id_Xe int not null,
	Id_DiaDiemDon int not null
	primary key( Id_Xe, Id_DiaDiemDon)
)
go
--tao bang DiaDiemDon--
create table DiaDiemDonXe(
	Id_DiaDiemDon int primary key identity(1,1) not null,
	DiaChi nvarchar(200)
)
go
-- tao Foreign key--
alter table HocVien
add constraint FK_HocVien_ID_DiaDiemDon foreign key (Id_DiaDiemDon) references DiaDiemDonXe (Id_DiaDiemDon)
go

alter table ThongTinXeBus
add constraint FK_ThongTinXeBus_ID foreign key (Id_TaiXe) references TaiXe(Id_TaiXe)
go

alter table LoTrinhXe
add constraint FK_LoTrinhXe_DiaDiemDon foreign key (Id_DiaDiemDon) references DiaDiemDonXe (Id_DiaDiemDon)
go

alter table LoTrinhXe
add constraint FK_LoTrinhXe_Xe foreign key (Id_Xe) references ThongTinXeBus (Id_Xe)
go
-- Nhap 5 ban ghi--
insert into DiaDiemDonXe(DiaChi)
values
('123 Tran Dai Nghia'),
('135 Tran Dai Nghia'),
('353 Doi Can'),
('123 Doi Can'),
('123 Tran Nhan Tong')
go

insert into TaiXe(TenTaiXe,SDT_TaiXe,GioiTich_TX,DiaChi_TX)
values
('Tran Van A','012345678','Nam','123 O dau do'),
('Tran Van B','012345678','Nu','123 O dau do'),
('Tran Van C','012345678','Nam','123 O dau do'),
('Tran Van D','012345678','Nu','123 O dau do'),
('Tran Van E','012345678','Nam','123 O dau do')
go

insert into ThongTinXeBus(BienSoXe,LoaiXe,SoGheNgoi,Id_TaiXe)
values
('29D-22222','ToyoTa',16,1),
('29D-33333','Suzuki',17,4),
('29D-44444','Honda',18,2),
('29D-55555','Mazda',20,3),
('29D-11111','Mec',19,5)
go


insert into HocVien(TenHocVien,Diachi_HV,HoTenBo,HoTenMe,SDT_Bo,SDT_Me,NgaySinh_HV,GioiTinh_HV,Id_DiaDiemDon)
values
('Nguyen Van A','123A o dau day','Nguyen Van Bo A','Tran Van Me A','090123456','090654321','2000-1-1','Nam',1),
('Nguyen Van B','123B o dau day','Nguyen Van Bo B','Tran Van Me B','090123456','090654321','2000-2-2','Nu',2),
('Nguyen Van C','123C o dau day','Nguyen Van Bo C','Tran Van Me C','090123456','090654321','2000-3-3','Nam',4),
('Nguyen Van D','123D o dau day','Nguyen Van Bo D','Tran Van Me D','090123456','090654321','2000-4-4','Nu',3),
('Nguyen Van E','123E o dau day','Nguyen Van Bo E','Tran Van Me E','090123456','090654321','2000-5-5','Nam',5)
go

insert into LoTrinhXe(Id_Xe,Id_DiaDiemDon)
values
(1,5),
(2,4),
(3,1),
(4,2),
(5,3)
go

select * from DiaDiemDonXe
select * from TaiXe
select * from ThongTinXeBus
select * from HocVien
select * from LoTrinhXe
go

-- 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--
create proc Proc_vw_Thongtinlotrinhdixebus
as
begin
	select TaiXe.TenTaiXe'Tài Xế',ThongTinXeBus.BienSoXe'Biển Số Xe',DiaDiemDonXe.DiaChi'Địa Chỉ'
	from TaiXe,ThongTinXeBus,DiaDiemDonXe,LoTrinhXe
	where TaiXe.Id_TaiXe = ThongTinXeBus.Id_TaiXe
		and ThongTinXeBus.Id_Xe = LoTrinhXe.Id_Xe
		and LoTrinhXe.Id_DiaDiemDon = DiaDiemDonXe.Id_DiaDiemDon
end
go

exec Proc_vw_Thongtinlotrinhdixebus

--Tạo Proc xem thông tin sinh viên theo biển số xe--
alter proc Proc_vw_ThongTinSinhVien_TheoBienSoXe
		@BienSoXe nvarchar(10)
as
begin
	select distinct HocVien.TenHocVien,HocVien.Diachi_HV,HocVien.GioiTinh_HV,ThongTinXeBus.BienSoXe'Biển Số Xe'
	from HocVien,ThongTinXeBus,LoTrinhXe,DiaDiemDonXe
	where HocVien.Id_DiaDiemDon = DiaDiemDonXe.Id_DiaDiemDon
		and DiaDiemDonXe.Id_DiaDiemDon = LoTrinhXe.Id_DiaDiemDon
		and LoTrinhXe.Id_Xe = ThongTinXeBus.Id_Xe
		and ThongTinXeBus.BienSoXe = @BienSoXe
end
go

exec Proc_vw_ThongTinSinhVien_TheoBienSoXe '29D-22222'
go
--Tao View xem thông tin sinh viên gồm : Tên SV, giới tính, địa chỉ đón--

select distinct  HocVien.TenHocVien,HocVien.Diachi_HV,HocVien.GioiTinh_HV,DiaDiemDonXe.DiaChi
	from HocVien,DiaDiemDonXe
	where HocVien.Id_DiaDiemDon = DiaDiemDonXe.Id_DiaDiemDon
go
--Đánh index (nonclustered index) cho column họ tên bố trên bảng học viên--
create NONCLUSTERED INDEX nonclustered_index_HotenBo
ON HocVien(HoTenBo);
go
--Tạo trigger cho phép xoá địa chỉ đón trong bảng : Địa điểm đón xe



create trigger Trigger_Delete_diadiemdonxe on DiaDiemDonXe
instead of delete
as
begin
	delete from LoTrinhXe where Id_DiaDiemDon in (select Id_DiaDiemDon from deleted)
	delete from HocVien where Id_DiaDiemDon in (select Id_DiaDiemDon from deleted)
	delete from DiaDiemDonXe where Id_DiaDiemDon in (select Id_DiaDiemDon from deleted)
end
go

delete from DiaDiemDonXe where Id_DiaDiemDon = 1


avatar
TRẦN VĂN ĐIỆP [Teacher]
2021-03-04 07:52:39



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


avatar
Trinh Huy Hung [community,C2009I]
2021-03-02 10:27:06



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 Bus.BusId=@BusId
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 Bus.License=@License
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


avatar
Lê Sĩ Tuyển [community,C2009I]
2021-03-02 09:43:54



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 Bus_Information.Bus_Number=@BusNumber
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


avatar
Nguyễn Hữu Hiếu [T2008A]
2020-12-16 04:46:05



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


avatar
Nguyễn Xuân Mai [T2008A]
2020-12-06 15:21:49



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


avatar
hainguyen [T2008A]
2020-12-05 07:44:00



create database Bus_school

use Bus_school

create table student (
	id int primary key identity (1, 1),
	name nvarchar(50),
	address nvarchar(100),
	father_name nvarchar(50),
	mother_name nvarchar(50),
	f_phonenumber int,
	m_phonenumber int,
	birthday date,
	gender nvarchar(10),
	id_address int
)

create table bus (
	id int primary key identity (1, 1),
	number int,
	type nvarchar(30),
	seat int,
	id_driver int
)

create table driver (
	id int primary key identity (1, 1),
	name nvarchar(50),
	phonenumber int,
	gender nvarchar(10),
	address nvarchar(100)
)

create table road (
	id_bus int primary key identity (1, 1),
	id_address int
)

create table Diemdon (
	id int primary key identity (1, 1),
	address nvarchar(100)
)

alter table road
add constraint AD_address foreign key (id_address) references Diemdon(id)

alter table road
drop AD_address

alter table bus
add constraint PK_driver foreign key (id_driver) references driver(id)

alter table road
drop AD_address

alter table student
add constraint PK_address foreign key (id_address) references Diemdon(id)

alter table student
drop PK_address

alter table bus
add constraint PK_bus_road foreign key (id) references road(id_bus)

alter table road 
add id_address2 int

alter table road
add constraint PK_address_id foreign key (id_address2) references student(id)

insert into student(name, address, father_name, mother_name, f_phonenumber, m_phonenumber, birthday, gender, id_address)
values
('TRAN VAN A', 'HA NOI', 'TRAN VAN AA', 'NGUYEN THI A', 0123456789, 0198765432, '2000-01-01', 'Nam', 1),
('TRAN Thi A', 'HA NOI', 'TRAN VAN AB', 'NGUYEN THI AB', 0123346344, 0198774545, '2000-02-02', 'Nu', 2),
('TRAN VAN B', 'NAM DINH', 'TRAN VAN BB', 'NGUYEN THI BB', 0123534534, 0123545452, '2000-11-11', 'Nam', 3),
('TRAN VAN C', 'NAM DINH', 'TRAN VAN CC', 'NGUYEN THI C', 0124563463, 0123534524, '2001-05-01', 'Nam', 4),
('TRAN THI B', 'HA NOI', 'TRAN VAN BA', 'NGUYEN THI BA', 0123234273, 0198687576, '2000-02-04', 'Nu', 5)

insert into Diemdon(address)
values
('HA NOI'),
('NAM DINH'),
('HA NOI'),
('HA NOI'),
('NAM DINH')

insert into driver(name, phonenumber, gender, address)
values
('NGUYEN VAN A', 0123456789, 'Nam', 'HA NOI'),
('NGUYEN VAN B', 0138452638, 'Nam', 'NAM DINH'),
('NGUYEN THI A', 0124569235, 'Nu', 'HA NOI'),
('NGUYEN VAN C', 0123969989, 'Nam', 'NAM DINH'),
('NGUYEN VAN D', 0182369486, 'Nam', 'HA NOI')

insert into bus(number, type, seat, id_driver)
values
(001, 'TOYOTA', 20, 1),
(002, 'AUDI', 30, 2),
(003, 'TOYOTA', 22, 3),
(004, 'HONDA', 20, 4),
(005, 'TOYOTA', 30, 5)

insert into road(id_address)
values
(1),
(2),
(3),
(4),
(5)

select * from student
select * from bus
select * from driver
select * from road
select * from Diemdon

alter table bus
add constraint LK_address foreign key (id) references road(id_bus)

create proc LoTrinhXeBus 
as
begin
	select driver.name, bus.number, road.id_address
	from bus, driver, road
	where driver.id = bus.id_driver and road.id_bus = bus.id
end

exec LoTrinhXeBus 

create proc ThongTinSv
	@biensoxe int
as
begin
	select student.name, student.address, student.gender, bus.number
	from student, bus, road
	where student.id = road.id_address2 and bus.id = road.id_bus and bus.number = @biensoxe
end

exec ThongTinSv 1

create view Thong_Tin_sv
as
select student.name, student.gender, road.id_address
from student, road
where student.id = road.id_address2

select * from Thong_Tin_sv

create clustered index cr_fat on student (father_name)




avatar
vuong huu phu [T2008A]
2020-12-04 14:29:23



create database school_bus_transportation_system
use school_bus_transportation_system

------Bảng học viên 
create table student_bus (
student_id int identity (1,1) primary key,
student_name nvarchar(100),
Date_of_birth date ,
gender nvarchar(20),
student_address nvarchar (200),
student_father_name nvarchar(100),
student_mother_name nvarchar(100),
phone_number_mother nvarchar (20),
phone_number_father nvarchar (20),
id_don_xe int
)
go
-----Bảng Thông tin xe bus
create table Bus (
id int identity(1,1) primary key,
license_plates nvarchar (30),
range_of_vehicle nvarchar(50),
seating int ,
driver_id int 
)
go
-----Bảng Tài xế
create table driver(
driver_id int identity(1,1) primary key, 
driver_name nvarchar(100),
phone_number_driver nvarchar (20),
gender nvarchar(20),
driver_address nvarchar(200)
)
go
-----Bảng Lộ trình xe đi
create table Route_of_the_car(
bus_id int,
location_id int
primary key (bus_id,location_id)
)
go
-----Bảng Địa điểm đón xe 
create table Pick_up_location(
id int identity(1,1) primary key,
address nvarchar(200)
)
go

alter table student_bus 
add constraint fk_đia_diem foreign key (id_don_xe)
references Pick_up_location(id)

alter table Bus 
add constraint fk_tai_xe foreign key (driver_id) 
references driver(driver_id)

alter table Route_of_the_car 
add constraint fk_xe_bus foreign key (bus_id) 
references Bus(id)

alter table Route_of_the_car 
add constraint fk_điaiem_bus_don foreign key (location_id) 
references Pick_up_location(id)

insert into student_bus(student_name,student_address,Date_of_birth,gender,student_father_name,student_mother_name,phone_number_father,phone_number_mother,id_don_xe)
values
('A','Ton That Thuyet - Ha Noi','2001-07-13','Nam','AA','AAA','AA1234567890','AAA1234567890','1'),
('B','Nguyen Chi Thanh - Ha Noi','2001-07-13','Nam','BB','BBB','BB1234567890','BBB1234567890','2'),
('C','Nguyen Thai Hoc - Ha Noi','2001-07-13','Nu','CC','CCC','CC1234567890','CCC1234567890','3'),
('D','Hang cot - Ha Noi','2001-07-13','Nam','DD','DDD','DD1234567890','DDD1234567890','4'),
('E','Ton That Thuyet - Ha Noi','2001-07-13','Nam','EE','EEE','EE1234567890','EEE1234567890','1')

insert into Bus(license_plates,range_of_vehicle,seating,driver_id)
values
('30A-111.11','Honda','4','1'),
('30A-888.88','Honda','4','4'),
('30A-666.66','Honda','4','2'),
('30A-266.66','Honda','4','5'),
('30A-999.99','Honda','4','3')

insert into driver(driver_name , phone_number_driver , gender , driver_address)
values
('driver 1','8987112234','Nam','Bach Mai - Ha Noi'),
('driver 2','7987112234','Nam','Cua Bac - Ha Noi'),
('driver 3','6987112234','Nam','Phan Dinh Phung - Ha Noi'),
('driver 4','0587112234','Nam','Gia Thuy - Ha Noi'),
('driver 5','0487112234','Nam','Ngoc Lam - Ha Noi')

insert into Route_of_the_car (bus_id,location_id)
values
('1','1'),
('2','2'),
('3','3'),
('4','4'),
('5','5')

insert into Pick_up_location (address)
values
('Long Bien - Ha Noi'),
('Hoan Kiem - Ha Noi'),
('Ba Dinh - Ha Noi'),
('My Dinh - Ha Noi'),
('Thanh Xuan - Ha Noi')


select * from Bus
select * from driver
select * from Pick_up_location
select * from Route_of_the_car
 
 ------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.
create proc tt_lo_trinh
@id_xe int
as
begin
select driver.driver_name,Bus.license_plates,Pick_up_location.address
from driver,Bus,Pick_up_location,Route_of_the_car
where Bus.driver_id = driver.driver_id and Route_of_the_car.bus_id = Bus.id and Route_of_the_car .location_id = Pick_up_location.id and bus_id = @id_xe
end

exec tt_lo_trinh 1
------Tạo Proc xem thông tin sinh viên theo biển số xe.
alter proc tt_sv_bs_xe
@bsx nvarchar (20)
as
begin 
select student_bus.student_id,student_bus.student_name,student_bus.student_address,student_bus.Date_of_birth,student_bus.gender,student_bus.student_father_name,student_bus.student_mother_name,student_bus.phone_number_father,student_bus.phone_number_mother,student_bus.id_don_xe
from student_bus,driver,Bus,Pick_up_location,Route_of_the_car
where student_bus.id_don_xe = Pick_up_location.id and Bus.driver_id = driver.driver_id and Route_of_the_car.bus_id = Bus.id and Route_of_the_car .location_id = Pick_up_location.id and Bus.license_plates = @bsx
end

exec tt_sv_bs_xe '30A-111.11'

-----Tao View xem thông tin sinh viên
create view tt_sinh_vien
as
select student_bus.student_name ,student_bus.gender,Pick_up_location.address
from student_bus ,Pick_up_location,Bus,driver,Route_of_the_car
where student_bus.id_don_xe = Pick_up_location.id and Bus.driver_id = driver.driver_id and Route_of_the_car.bus_id = Bus.id and Route_of_the_car .location_id = Pick_up_location.id

select * from tt_sinh_vien

create clustered index ci_father
on student_bus(student_father_name)




avatar
Trần Văn Lâm [T2008A]
2020-12-04 05:53:19



create database simple_system
use simple_system
create table Student(
	id int primary key identity(1,1),
	name nvarchar(100),
	address nvarchar(200),
	name_father nvarchar(100),
	name_mother nvarchar(100),
	phoneNumer_father nvarchar(20),
	phoneNumber_mother nvarchar(20),
	Date_of_birth datetime,
	gender nvarchar(10),
	id_locationPickup int 
)
create table Bus(
	id int primary key identity(1,1),
	license_plate nvarchar(50),
	range_vehicle nvarchar(50),
	num_desk int,
	id_driver int
)
create table Driver(
	id int primary key identity(1,1),
	name nvarchar(100),
	phoneNumber nvarchar(20),
	gender nvarchar(20),
	address nvarchar(200)
)
create table Rout(
	id_bus int primary key identity(1,1),
	id_locationPickup int
)
create table LocationPickup(
	id int primary key identity(1,1),
	address nvarchar(200)
)

select * from Student
insert into Student(name,address,name_father,name_mother,phoneNumer_father,phoneNumber_mother,Date_of_birth,gender,id_locationPickup)
values
('Nguyen Van A','Linh Dam','Nguyen Tuan A','Tran Thi A','1234567','1234567',2002-05-05,'nam',1),
('Nguyen Van B','Linh Dam','Nguyen Tuan B','Tran Thi B','1234567','1234567',2002-05-05,'nam',3),
('Nguyen Van C','Linh Dam','Nguyen Tuan C','Tran Thi C','1234567','1234567',2002-05-05,'nam',2),
('Nguyen Van D','Linh Dam','Nguyen Tuan D','Tran Thi D','1234567','1234567',2002-05-05,'nam',1),
('Nguyen Van E','Linh Dam','Nguyen Tuan E','Tran Thi E','1234567','1234567',2002-05-05,'nam',5)
select * from Bus
insert into Bus(license_plate,range_vehicle,num_desk,id_driver)
values
('T3409','Mercedes',10,1),
('T3445','Mazda',12,2),
('T6756','Camry',18,4),
('T2123','Vinfast',20,2),
('T4564','Bungati',26,3)
select * from Driver
insert into Driver(name,phoneNumber,gender,address)
values
('Messi','12322','nam','My Dinh'),
('Mordic','12322','nam','My Dinh'),
('Tevez','12322','nam','My Dinh'),
('Neymar','12322','nam','My Dinh'),
('Ronaldo','12322','nam','My Dinh')
select * from Rout
insert into Rout(id_locationPickup)
values
(1),
(2),
(2),
(4),
(3)
insert into LocationPickup(address)
values
('Dong Da'),
('My Dinh'),
('Ton Duc Thang'),
('Dinh Tien Hoang'),
('Ton That Thuyet')
select Driver.name from Driver
select Bus.license_plate from Bus
select LocationPickup.address from LocationPickup
create proc proc_lo_trinh
as
begin
	select Driver.name from Driver
    select Bus.license_plate from Bus
    select LocationPickup.address from LocationPickup
end
exec proc_lo_trinh
create view thong_tin_sinh_vien
as
select Student.name, Student.gender, LocationPickup.address
from Student,LocationPickup
select * from thong_tin_sinh_vien
create clustered index ci_name_father on Student (name_father)


avatar
Do Trung Duc [T2008A]
2020-12-04 03:41:14



create database Bus_Symtem_Management
use Bus_Symtem_Management

create table Student(
Id int identity(1,1),
StudentName  nvarchar(100),
Address nvarchar(100),
ParentsName nvarchar(100),
ParentsPhone nvarchar(100),
Bithday date,
Id_TakePlace int,
constraint PK_Student primary key (Id)
)

create table BusInformation(
Id int primary key identity(1,1),
Number nvarchar(20),
Type nvarchar(100),
Seats int,
DriverId int,
)

create table DriverInformation(
DriverId int primary key identity(1,1),
DriverName nvarchar (100),
DriverPhone nvarchar (100),
DriverSex nvarchar (100),
DriverAddress nvarchar (200)
)

create table BusRoad(
id_busroad int primary key identity  (1,1),
AddressPlace nvarchar (200)
)

alter table BusRoad
drop  PK__BusRoad__3AF689CBBB74427A

alter table BusRoad
drop column id_busroad

alter table BusRoad
drop column AddressPlace

alter table BusRoad
add Id_TakePlace int


create table AddressPlaceDetail(
Id_AddressPlace int primary key identity  (1,1),
AddressPlace nvarchar (200)
)

alter table Student 
add constraint FK_Id_TakePlace_Student foreign key (Id_TakePlace) references AddressPlaceDetail(Id_AddressPlace)

alter table BusInformation
add constraint FK_DriverId_BusInformation foreign key (DriverId) references DriverInformation(DriverId)

alter table BusRoad
add constraint FK_id_busroad_BusRoad foreign key (id_busroad) references BusInformation(Id)

alter table BusRoad
drop FK_id_busroad_BusRoad

alter table BusRoad
add Bus_Id int 

alter table BusRoad
add constraint FK_Bus_Id_BusRoad foreign key (Bus_Id) references BusInformation(Id)

alter table BusRoad
add constraint FK_Id_TakePlace_BusRoad foreign key (Id_TakePlace) references AddressPlaceDetail(Id_AddressPlace)

--Them ban ghi
insert into  AddressPlaceDetail(AddressPlace)
values
('So 90 Cau Giay'),
('So 1 Linh Dam'),
('So 2 Hoang Mai'),
('So 9 Dong Da'),
('So 8 Tay Ho')
select *from AddressPlaceDetail

select *from Student
INSERT into Student(StudentName,Address,ParentsName,ParentsPhone,Bithday,Id_TakePlace)
values
('Do Trung Duc','Cau Giay,Ha Noi','Bo cua Duc','123456781','1991-12-20',1),
('Tran Van Diep','Linh Dam,Ha Noi','Bo cua Diep','123456782','1991-12-20',2),
('Nguyen Tuan Anh','Cau Giay,Ha Noi','Bo cua Tuan Anh','123456783','1991-12-20',1),
('Pham Hong Quang','Dong Da,Ha Noi','Bo cua Quang','123456784','1991-12-20',4),
('Le Minh Son','Cau Giay,Ha Noi','Bo cua Son','123456785','1991-12-20',5)


insert into DriverInformation(DriverName,DriverPhone,DriverSex,DriverAddress)
values
('Hoang Van A','123456789','Nam','Hanoi'),
('Hoang Van B','123456789','Nam','Hanoi'),
('Hoang Van B','123456789','Nam','Hanoi'),
('Hoang Van D','123456789','Nam','Hanoi'),
('Hoang Van E','123456789','Nam','Hanoi')

select * from DriverInformation

insert into BusInformation(Number,Type,Seats,DriverId)
values
('30K6523','FordTransit',16,1),
('30K6524','FordTransit',16,2),
('30K6525','Huyndai',29,3),
('30K6123','FordTransit',16,4),
('30K4226','Huyndai',29,5)

select *from BusInformation

insert into BusRoad(Bus_Id,Id_TakePlace)
values
(1,4),
(2,2),
(3,3),
(4,1),
(5,5)

select *from AddressPlaceDetail
select *from Student
select *from DriverInformation
select *from BusInformation
select *from BusRoad

create PROC xemthongtinlotrinhxebus
AS
BEGIN
	select DriverInformation.DriverName, BusInformation.Number, AddressPlaceDetail.AddressPlace
	FROM DriverInformation, BusInformation, AddressPlaceDetail, BusRoad
	where DriverInformation.DriverId = BusInformation.DriverId and BusInformation.Id = BusRoad.Bus_Id and BusRoad.Id_TakePlace = AddressPlaceDetail.Id_AddressPlace
END
exec xemthongtinlotrinhxebus

create Proc xemthongtinsinhvientheobiensoxe
   @biensoxe nvarchar(20)
AS
BEGIN 
	select Student.* , BusInformation.Number
	from Student, BusInformation, BusRoad
	where Student.Id_TakePlace = BusRoad.Id_TakePlace and BusRoad.Bus_Id = BusInformation.Id and BusInformation.Number =  @biensoxe
END

EXEC xemthongtinsinhvientheobiensoxe '30K6523'

--Tao View xem thông tin sinh viên gồm : Tên SV, giới tính, địa chỉ đón

alter table Student
add StudentSex nvarchar(10)

update Student set StudentSex =' Nam'

create view xemthongtinsinhvien 
AS
select Student.StudentName, Student.StudentSex, AddressPlaceDetail.AddressPlace
FROM Student inner join AddressPlaceDetail on Student.Id_TakePlace = AddressPlaceDetail.Id_AddressPlace

select * from  xemthongtinsinhvien 

--Đánh index (clustered index) cho column họ tên bố trên bảng học viên

create clustered Index ci_ParentsName on Student(ParentsName) --HOilaiThay

--Tao trigger khong cho phep xoa dia chi don xe don
create trigger khongxoadiadiemdon on AddressPlaceDetail
for delete
AS
BEGIN
	if (select AddressPlace from deleted) <> ''
	begin
	print N'‘Do not delete AddressPlace'
	rollback transaction 
	end
END

select * from AddressPlaceDetail
DELETE AddressPlace FROM AddressPlaceDetail WHERE Id_AddressPlace = 1 

select * from AddressPlaceDetail

select *from Student

create trigger nhapnamsinh on Student
for insert
as
BEgin
	if (select Bithday from inserted ) > '1990-1-1'
	begin
		print N'Nam sinh khong hop le'
		rollback transaction
		end
End

INSERT into Student(StudentName,Address,ParentsName,ParentsPhone,Bithday,Id_TakePlace)
values
('Testriger1','Cau Giay,Ha Noi','Bo cua triger1','123456781','1989-12-20',1)

INSERT into Student(StudentName,Address,ParentsName,ParentsPhone,Bithday,Id_TakePlace)
values
('Testriger2','Cau Giay,Ha Noi','Bo cua triger2','123456781','1994-12-20',1)