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

Code sql bài toán quản lí cửa hàng thời trang sql server

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 (92.07 KB, 28 trang )

Mục Lục
Code Sql bài toán quản lí cửa hàng thời trang Sql server
Create database cuahangthoitrang...........................................................2
--Tạo Bảng...................................................................................... 2
create table khachhang......................................................................2
create table nhanvien......................................................................... 2
create table sanpham.........................................................................3
create table hangsx...........................................................................3
create table hoadonnhap.....................................................................3
create table hoadonxuat.....................................................................4
create table nv_sdt............................................................................ 4
create table kh_sdt............................................................................ 4
create table hoadonxuat_chitiet............................................................4
create table hoadonnhap_chitiet...........................................................5
create table kho_chitiet......................................................................5
--Tạo khóa chính................................................................................ 5
--Tạo khóa phụ.................................................................................6
--Tạo các CK.................................................................................... 8
--Tạo các Constraint...........................................................................8
--Tạo các Index.................................................................................9
--Nhập bản ghi.................................................................................. 9
--Xoa truong thu 2 o moi bang.............................................................14
--Thêm trường vừa xóa.....................................................................15
--Cập nhập nhân viên........................................................................ 16
--update nhanvien.............................................................................16
---Tăng giá sản phảm có mã 03 lên 300000.............................................16
---Chuyển số nhân viên có công việc a thành c........................................17
---Lấy sđt của nhân viên là nữ.............................................................17


--Hiện ra khách hàng mua nhiều hàng nhất,tên loại hàng,tổng tiền...............17


--Hiện hãng sản phẩm được bán nhiều nhất...........................................17
--Đếm số nhân viên theo giới tính........................................................18
--Đếm số khách hàng theo giời tính......................................................18
--Đếm số khách hàng........................................................................18
--Tạo View..................................................................................... 19
--Tạo procedure thêm vào bảng...........................................................20
---Gọi các Stored Procedure vừa tạo.....................................................21
---Tạo các Stored Procedure xóa dữ liệu................................................22
--Tạo Procedure tìm khách hàng lớn tuổi nhất.........................................22
--Tìm khách hàng theo mã khách hàng...................................................22
--Tìm nhân viên theo mã nhân viên........................................................23
--Tìm khách hàng theo giới tính...........................................................23
--Tìm nhân viên theo giới tính..............................................................23
--Nhập mã nhân viên hiện ra tuổi, hệ số lương.......................................24
--Tạo view hiện tuổi nhỏ nhất............................................................24
--Tạo View màu sắc mua nhiều nhất....................................................24
--Dem so luong sp các hãng sx.............................................................25
--HoaDonXuatMotNhanVien...............................................................26
--Tao proc tinh tong SP trong mot kho....................................................26


Create database cuahangthoitrang
use cuahangthoitrang
drop database cuahangthoitrang

go
--Tạo Bảng
create table khachhang
(
MaKH varchar (10) not null ,

HoTen nvarchar (50),
NgaySinh datetime,
DiaChi nvarchar(50),
GioiTinh varchar(3),)

create table nhanvien
(
MaNV varchar(10) not null,
HoTen nvarchar(50),
NgaySinh datetime,
GioiTinh varchar(3),
DiaChi nvarchar (50),
HSL float,
ChucVu varchar(10),
)


create table sanpham
(
MaSP varchar (10) not null,
TenSP nvarchar(50),
MauSac varchar(20),
MoTa nvarchar(50),
MaHSX varchar (10),
MaKho varchar(10),)

create table hangsx
(
MaHSX varchar(10) not null,
TenSX nvarchar (50),)


create table kho(
MaKho varchar(10) not null,
TenKho nvarchar (50),
DiaChi nvarchar (50),
SDT varchar (11),
MaNV varchar (10) not null,)

create table hoadonnhap
(
MaHDN varchar(10) not null,
NgayLap datetime,
MaNV varchar (10) not null,)


create table hoadonxuat
(
MaHDX varchar (10) not null,
NgayLap datetime ,
MaNV varchar (10) not null,
MaKH varchar (10) not null,)

create table nv_sdt
(
MaNV varchar (10) not null,
SDT varchar (11) not null,)

create table kh_sdt
(
MaKH varchar (10) not null,

SDT varchar (11) not null,)

create table hoadonxuat_chitiet
(
MaHDX varchar (10) not null,
MaSP varchar (10) not null,
DGX int,
SoLuong int,)


create table hoadonnhap_chitiet
(
MaHDN varchar (10) not null,
MaSP varchar (10) not null,
DGN int,
SoLuong int,)

create table kho_chitiet
(
MaKho varchar (10) not null,
MaSP varchar (10) not null,
SoLuong int,)

--Tạo khóa chính
alter table khachhang
add constraint PK_MaKH primary key (MaKH)

alter table nhanvien
add constraint PK_MaNV primary key (MaNV)
alter table sanpham

add constraint PK_MaSP primary key (MaSP)

alter table hoadonnhap
add constraint PK_MaHDN primary key (MaHDN)


alter table hoadonxuat
add constraint PK_MaHDX primary key (MaHDX)

alter table kho
add constraint PK_MaKho primary key (MaKho)

alter table hangsx
add constraint PK_MaHSX primary key (MaHSX)

alter table nv_sdt
add constraint PK_SDT primary key (MaNV,SDT)

alter table kh_sdt
add constraint PK_SDTKH primary key (MaKH,SDT)

--Tạo khóa phụ
alter table sanpham
add constraint FK_MaHSX foreign key (MaHSX) references hangsx(MaHSX)

alter table kho
add constraint FK_MaNV foreign key (MaNV) references nhanvien (MaNV)

alter table hoadonnhap
add constraint FK_MaNVHDN foreign key (MaNV) references nhanvien (MaNV)



alter table hoadonxuat
add constraint FK_MaNVHDX foreign key (MaNV) references nhanvien (MaNV)

alter table hoadonxuat
add constraint FK_MaKH foreign key (MaKH) references khachhang (MaKH)
alter table hoadonnhap_chitiet
add constraint FK_MaHDNCT foreign key (MaHDN) references
hoadonnhap(MaHDN)

alter table hoadonnhap_chitiet
add constraint FK_MaSPNCT foreign key (MaSP) references sanpham(MaSP)

alter table hoadonxuat_chitiet
add constraint FK_MaHDXCT foreign key (MaHDX) references hoadonxuat
(MaHDX)

alter table hoadonxuat_chitiet
add constraint FK_MaSPXCT foreign key (MaSP) references sanpham (MaSp)

alter table kho_chitiet
add constraint FK_MaKhoCT foreign key (MaKho) references kho(MaKho)

alter table kho_chitiet
add constraint FK_MaSPKCT foreign key (MaSP) references sanpham(MaSP)


--Tạo các CK
alter table khachhang

add constraint GTKH check (GioiTinh in ('Nam','Nữ'))
alter table nhanvien
add constraint GTNV check (GioiTinh in ('Nam','Nữ'))
alter table hoadonxuat
add constraint ngaylap check (year(ngaylap)>2015)

--Tạo các Constraint
alter table khachhang
add constraint unique_makh unique (MaKH)

alter table nhanvien
add constraint unique_manv unique (MaNV)

alter table sanpham
add constraint unique_masp unique (MaSP)

alter table hangsx
add constraint unique_mahang unique (MaHSX)

alter table kho
add constraint unique_makho unique (MaKho)

alter table hoadonnhap


add constraint unique_mahdn unique (MaHDN)

alter table hoadonxuat
add constraint unique_mahdx unique (MaHDX)


alter table nv_sdt
add constraint unique_sdtnv unique (SDT)
alter table kh_sdt
add constraint unique_sdtkh unique (SDT)

--Tạo các Index
create index idx_tenkh on khachhang(HoTen)
create index idx_tennx on nhanvien (HoTen)
create index idx_tensp on sanpham (TenSP)
create index idx_mausp on sanpham (MauSac)
create index idx_ctkho on kho_chitiet (MaKho)

--Nhập bản ghi
insert into khachhang values ('KH01','Ngô Hồng Quân','12-12-1996','Hà
Nội','Nam')
insert into khachhang values ('KH02','Nguyễn Quang Hợp','12-12-1996','Hà
Nội','Nam')
insert into khachhang values ('KH03','Đào Thị Lệ','02-10-1996','Hà Nội','Nữ')
insert into khachhang values ('KH04','Nguyễn Văn A','04-05-1989','Quảng
Ninh','Nam')
insert into khachhang values ('KH05','Trần Gia B','03-02-2000','Hà Nam','Nữ')


insert into khachhang values ('KH06','Đào Hương Hoa','05-11-1980','Cà
Mau','Nữ')
insert into khachhang values ('KH07','Nguyễn Thu Trang','11-08-1999','Hà
Nội','Nữ')
insert into khachhang values ('KH08','Trần Anh Minh','01-10-1996','Bắc
Giang','Nam')
insert into nhanvien values ('NV01','Nguyễn Mạnh Hưng','10-10-1995','Nam','Hà

Nội','2.5','a')
insert into nhanvien values ('NV02','Nguyễn Hồng Hạnh','10-20-1996','Nữ','Hà
Nội','2.5','b')
insert into nhanvien values ('NV03','Phạm Thanh Hương','12-31-1998','Nữ','Bắc
Giang','2.5','a')
insert into nhanvien values ('NV04','Trần Văn Vinh','01-10-1990','Nam','Hưng
Yên','2.5','b')
insert into nhanvien values ('NV05','Vũ Quốc Toản','11-20-1988','Nam','Hải
Phòng','2.5','a')
insert into nhanvien values ('NV06','Tạ Hữu Hoàng','02-15-1998','Nam','Hà
Nội','2.5','a')
insert into nhanvien values ('NV07','Vũ Thu Thủy','06-10-1994','Nữ','Hà
Nội','2.5','a')
insert into nhanvien values ('NV08','Hoàng Quốc Thiện','05-20-1992','Nam','Hà
Nội','2.5','b')
insert into hangsx values ('H01','LV')
insert into hangsx values ('H02','D&G')
insert into hangsx values ('H03','Pumma')
insert into hangsx values ('H04','Lacoste')
insert into hangsx values ('H05','Adidas')


insert into kho values ('KHO1','Kho Áo','Nguyễn Trãi','0132549688','NV01')
insert into kho values ('KHO2','Kho Quần','Ngọc Lâm','0132549699','NV03')
insert into kho values ('KHO3','Kho Túi','Trần Hưng Đạo','0132549456','NV05')
insert into kho values ('KHO4','Kho Giày','Phố Huế','0132549677','NV06')
insert into kho values ('KHO5','Kho Váy','Thái Hà','0132543456','NV07')

insert into sanpham values ('SP01','Áo sơmi','Xanh','FreeSize','H01','KHO1')
insert into sanpham values ('SP02','Quần legging','Đỏ','FreeSize','H03','KHO2')

insert into sanpham values ('SP03','Quần Jogger','Tím','FreeSize','H05','KHO2')
insert into sanpham values ('SP04','Túi xách','Vàng','FreeSize','H02','KHO3')
insert into sanpham values ('SP05','Giày LV','Lục','FreeSize','H01','KHO4')
insert into sanpham values ('SP06','Váy ','Lam','FreeSize','H02','KHO5')
insert into sanpham values ('SP07','Váy xẻ','Chàm','FreeSize','H04','KHO5')
insert into sanpham values ('SP08','Giày Adi','Cam','FreeSize','H04','KHO4')

insert into nv_sdt values ('NV01','01234567899')
insert into nv_sdt values ('NV02','01234567888')
insert into nv_sdt values ('NV03','01234567877')
insert into nv_sdt values ('NV04','01234567866')
insert into nv_sdt values ('NV05','01234567855')
insert into nv_sdt values ('NV06','01234567844')
insert into nv_sdt values ('NV07','01234567833')
insert into nv_sdt values ('NV08','01234567822')


insert into kh_sdt values ('KH01','01245678900')
insert into kh_sdt values ('KH02','01245678911')
insert into kh_sdt values ('KH03','01245678922')
insert into kh_sdt values ('KH04','01245678933')
insert into kh_sdt values ('KH05','01245678944')
insert into kh_sdt values ('KH06','01245678955')
insert into kh_sdt values ('KH07','01245678966')
insert into kh_sdt values ('KH08','01245678977')

insert into hoadonnhap values ('HD01','02-11-2016','NV02')
insert into hoadonnhap values ('HD02','03-22-2016','NV02')
insert into hoadonnhap values ('HD03','04-07-2016','NV02')
insert into hoadonnhap values ('HD04','05-08-2016','NV04')

insert into hoadonnhap values ('HD05','08-09-2016','NV04')
insert into hoadonnhap values ('HD06','11-20-2016','NV08')
insert into hoadonnhap values ('HD07','12-22-2016','NV08')
insert into hoadonnhap values ('HD08','12-22-2016','NV04')

insert into hoadonxuat values ('HD01','01-02-2016','NV01','KH01')
insert into hoadonxuat values ('HD02','02-03-2016','NV02','KH02')
insert into hoadonxuat values ('HD03','04-05-2016','NV03','KH03')
insert into hoadonxuat values ('HD04','05-06-2016','NV04','KH04')
insert into hoadonxuat values ('HD05','06-07-2016','NV05','KH05')
insert into hoadonxuat values ('HD06','07-08-2016','NV06','KH06')


insert into hoadonxuat values ('HD07','09-10-2016','NV07','KH07')
insert into hoadonxuat values ('HD08','11-12-2016','NV08','KH08')

insert into hoadonnhap_chitiet values ('HD01','SP01','100000','20')
insert into hoadonnhap_chitiet values ('HD02','SP02','200000','50')
insert into hoadonnhap_chitiet values ('HD03','SP03','150000','40')
insert into hoadonnhap_chitiet values ('HD04','SP04','500000','30')
insert into hoadonnhap_chitiet values ('HD05','SP05','500000','10')
insert into hoadonnhap_chitiet values ('HD06','SP06','200000','30')
insert into hoadonnhap_chitiet values ('HD07','SP07','300000','40')
insert into hoadonnhap_chitiet values ('HD08','SP08','400000','60')

insert into hoadonxuat_chitiet values ('HD01','SP01','120000','5')
insert into hoadonxuat_chitiet values ('HD02','SP02','220000','5')
insert into hoadonxuat_chitiet values ('HD03','SP03','190000','3')
insert into hoadonxuat_chitiet values ('HD04','SP04','570000','2')
insert into hoadonxuat_chitiet values ('HD05','SP05','550000','1')

insert into hoadonxuat_chitiet values ('HD06','SP06','230000','2')
insert into hoadonxuat_chitiet values ('HD07','SP07','350000','6')
insert into hoadonxuat_chitiet values ('HD08','SP08','470000','2')

insert into kho_chitiet values ('KHO1','SP01','100')
insert into kho_chitiet values ('KHO2','SP02','100')
insert into kho_chitiet values ('KHO3','SP03','100')
insert into kho_chitiet values ('KHO1','SP04','100')


insert into kho_chitiet values ('KHO2','SP05','100')
insert into kho_chitiet values ('KHO5','SP06','100')
insert into kho_chitiet values ('KHO4','SP07','100')
insert into kho_chitiet values ('KHO4','SP08','100')

--Xoa truong thu 2 o moi bang
alter table khachhang
drop column HoTen
alter table nhanvien
drop column HoTen
alter table sanpham
drop column TenSP
alter table hangsx
drop column TenSX
alter table kho
drop column TenKho
alter table hoadonnhap
drop column NgayLap
alter table hoadonxuat
drop column NgayLap

alter table nv_sdt
drop column SDT
alter table kh_sdt
drop column SDT
alter table hoadonxuat_chitiet


drop column MaSP
alter table hoadonnhap_chitiet
drop column MaSP
alter table kho_chitiet
drop column MaSP

--Thêm trường vừa xóa
alter table khachhang
add HoTen nvarchar (50)
alter table nhanvien
add HoTen nvarchar (50)
alter table sanpham
add TenSP nvarchar (50)
alter table hangsx
add TenSX nvarchar (50)
alter table kho
add TenKho nvarchar (50)
alter table hoadonnhap
add NgayLap datetime
alter table hoadonxuat
add NgayLap datetime
alter table nv_sdt
add SDT varchar (11)

alter table nv_sdt
modify column SDT varchar(11) not null
alter table kh_sdt


add SDT varchar (11)
alter table kh_sdt
modify column SDT varchar(11) not null
alter table hoadonxuat_chitiet
add MaSP varchar (10)
alter table hoadonnhap_chitiet
add MaSP varchar (10)
alter table kho_chitiet
add MaSP varchar (10)

--Cập nhập nhân viên
Update nhanvien
set ChucVu=N'Kho'
where ChucVu=N'b'

--update nhanvien
set HSL='3'
where GioiTinh=N'Nữ'

---Tăng giá sản phảm có mã 03 lên 300000
update hoadonnhap_chitiet
set DGN = DGN + 300000
where MaSP=N'SP03'



---Chuyển số nhân viên có công việc a thành c
update nhanvien
set ChucVu='c'
where ChucVu='a'

---Lấy sđt của nhân viên là nữ
select SDT,HoTen
from nhanvien,nv_sdt
where nhanvien.MaNV=nv_sdt.MaNV
and GioiTinh='Nữ'

--Hiện ra khách hàng mua nhiều hàng nhất,tên loại hàng,tổng tiền
select HoTen,TenSP,SoLuong,(SoLuong*DGX) as TongTien
from khachhang,hoadonxuat,hoadonxuat_chitiet,sanpham
where khachhang.MaKH=hoadonxuat.MaKH
and

hoadonxuat_chitiet.MaHDX=hoadonxuat.MaHDX

and

hoadonxuat_chitiet.MaSp=sanpham.MaSp

order by (TongTien)desc

--Hiện hãng sản phẩm được bán nhiều nhất
select TenSX,sum(Soluong)as TongBanDuoc
from hoadonxuat,hoadonxuat_chitiet,sanpham,hangsx
where hoadonxuat.MaHDX=hoadonxuat_chitiet.MaHDX



and hoadonxuat_chitiet.MaSP=sanpham.MaSp
and sanpham.MaHSX=hangsx.MaHSX
group by hangsx.MaHSX,TenSX
order by TongBanDuoc desc

--Đếm số nhân viên theo giới tính
select GioiTinh, COUNT(GioiTinh) as SoNV
from nhanvien
group by GioiTinh

--Đếm số khách hàng theo giời tính
select GioiTinh,COUNT(GioiTinh)as SoKH
from khachhang
group by GioiTinh
order by SoKH

--Đếm số khách hàng
select COUNT(MaKH)as sokh from khachhang
---Khách hàng ở Hà Nội
select *
from khachhang
where DiaChi='Hà Nội'
--Hiện sản phẩm có giá trị nhập lớn nhất
select TenSP,(DGN*SoLuong) as GiaTri,SoLuong
from sanpham,hoadonnhap_chitiet
where sanpham.MaSP=hoadonnhap_chitiet.MaSP


Group by TenSP,SoLuong,DGN

order by GiaTri desc
--Tạo View
create view hangspbannhieunhat
as
select top 1 TenSX,sum(Soluong)as TongBanDuoc
from hoadonxuat,hoadonxuat_chitiet,sanpham,hangsx
where hoadonxuat.MaHDX=hoadonxuat_chitiet.MaHDX
and hoadonxuat_chitiet.MaSP=sanpham.MaSp
and sanpham.MaHSX=hangsx.MaHSX
group by hangsx.MaHSX,TenSX
order by TongBanDuoc desc

create view mausacmuanhieunhat
as
select top 1 MauSac,(COUNT(hoadonxuat_chitiet.MaSP)*SoLuong) as SoLanMua
from sanpham,hoadonxuat_chitiet
where sanpham.MaSP=hoadonxuat_chitiet.MaSP
group by MauSac,sanpham.MaSP,SoLuong
order by SoLanMua desc

create view sokhachhangmuanhieunhat
as
select top 1 HoTen,Sum(SoLuong)as SoHangBanDuoc,(SoLuong*DGX)as
DoanhThu


from hoadonxuat,hoadonxuat_chitiet,nhanvien
where hoadonxuat.MaHDX=hoadonxuat_chitiet.MaHDX
and hoadonxuat.MaNV=nhanvien.MaNV
group by HoTen, SoLuong, DGX

order by (DoanhThu) desc

create view khachhangoHN
as
select *
from khachhang
where DiaChi='Hà Nội'
create view sanphamnhapnhieunhat
as
select top 1 TenSP,(DGN*SoLuong) as GiaTri,SoLuong
from sanpham,hoadonnhap_chitiet
where sanpham.MaSP=hoadonnhap_chitiet.MaSP
Group by TenSP,SoLuong,DGN
order by GiaTri desc

--Tạo procedure thêm vào bảng
create procedure SP_themsp
(@MaSP varchar (10), @TenSP nvarchar (50),@MauSac nvarchar (10),@MoTa
nvarchar(50),@MaHSX varchar(10),@MaKho varchar(10))
with recompile, encryption
as


insert into sanpham values
(@MaSP,@TenSP,@MauSac,@MoTa,@MaHSX,@MaKho)
create procedure SP_hoadonnhap
(@MaHDN varchar(10),@NgayLap datetime,@MaNV varchar(10))
with recompile, encryption
as
insert into hoadonnhap values (@MaHDN,@NgayLap,@MaNV)


create procedure SP_nhanvien
(@MaNV varchar(10),@HoTen nvarchar(50),@NgaySinh datetime,@GioiTinh
varchar(3),@DiaChi nvarchar(50),@HSL float,@ChucVu nvarchar(10))
with recompile,encryption
as
insert into nhanvien values (@MaNV,@HoTen
,@NgaySinh,@GioiTinh,@DiaChi,@HSL,@ChucVu)

---Gọi các Stored Procedure vừa tạo
exec SP_hoadonnhap @MaHDN='',@NgayLap='',@MaNV=''
exec SP_nhanvien
@MaNV='',@HoTen='',@NgaySinh='',@GioiTinh='',@DiaChi='',@HSL='',@Chu
cVu=''
exec SP_themsp
@MaSP='',@TenSP='',@MauSac='',@MoTa='',@MaHSX='',@MaKho=''


---Tạo các Stored Procedure xóa dữ liệu
create procedure P_nhanvien
(@HSL float)
with recompile,encryption
as
delete from nhanvien
where @HSL=''

--Tạo Procedure tìm khách hàng lớn tuổi nhất
create procedure SP_khlontuoinhat
with recompile,encryption
as

select top 1 HoTen, (year(getdate())-year(NgaySinh)) as Tuoi
from khachhang
group by HoTen,NgaySinh
order by Tuoi desc

--Tìm khách hàng theo mã khách hàng
create procedure SP_search
(@MaKH varchar(10))
with recompile,encryption
as
select*
from khachhang
where MaKH=@MaKH
exec SP_search @MaKH='KH02'


--Tìm nhân viên theo mã nhân viên
create procedure SP_search1
(@MaNV varchar(10))
with recompile,encryption
as
select*
from nhanvien
where MaNV=@MaNV
exec SP_search1 @MaNV='NV01'

--Tìm khách hàng theo giới tính
create procedure SP_timkhgt
(@GioiTinh varchar (3))
with recompile,encryption

as
select*
from khachhang
where GioiTinh=@GioiTinh
exec SP_timkhgt @GioiTinh="Nam"

--Tìm nhân viên theo giới tính
create procedure SP_timnvgt
(@GioiTinh varchar (3))
with recompile,encryption
as


select*
from nhanvien
where GioiTinh=@GioiTinh
exec SP_timnvgt @GioiTinh="Nam"

--Nhập mã nhân viên hiện ra tuổi, hệ số lương
create procedure SP_HienTTNV
(@MaNV varchar (10))
with recompile,encryption
as
select HoTen, datepart(yy,getdate()) - year(NgaySinh) as Tuoi,HSL
from nhanvien
where MaNV=@MaNV
exec SP_HienTTNV @MaNV='NV06'

--Tạo view hiện tuổi nhỏ nhất
create view tuoilonnhat

as
select YEAR(getdate())-YEAR(nhanvien.NgaySinh) as Tuoi,HoTen
from nhanvien
where YEAR(getdate())-YEAR (nhanvien.NgaySinh) = (select
MAX(YEAR(getdate())-year(nhanvien.NgaySinh))from nhanvien)

--Tạo View màu sắc mua nhiều nhất
create view mausacduocmuanhieunhat
as


×