1. Trang chủ
  2. » Công Nghệ Thông Tin

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

28 724 4

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 28
Dung lượng 92,07 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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

Trang 1

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

Trang 2

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ậ ệ ổ ệ ố ươ 24ng 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

Trang 3

Create database cuahangthoitrang

Trang 4

create table sanpham

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

Trang 5

create table hoadonxuat

SoLuong int,)

Trang 6

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)

Trang 7

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

Trang 8

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)

Trang 9

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

Trang 10

add constraint unique_mahdn unique (MaHDN)

alter table hoadonxuat

add constraint unique_mahdx unique (MaHDX)

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)

insert into khachhang values ('KH05','Tr n Gia B','03-02-2000','Hà Nam','N ')ầ ữ

Trang 11

insert into khachhang values ('KH06','Đào Hương Hoa','05-11-1980','Cà

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

Trang 12

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

Trang 13

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

Trang 14

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

Trang 15

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

Trang 16

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

Trang 17

add SDT varchar (11)

alter table kh_sdt

modify column SDT varchar(11) not nullalter table hoadonxuat_chitiet

add MaSP varchar (10)

alter table hoadonnhap_chitiet

add MaSP varchar (10)

alter table kho_chitiet

add MaSP varchar (10)

Trang 18

-Chuy n s nhân viên có công vi c a thành cể ố ệ

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

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

Trang 19

and hoadonxuat_chitiet.MaSP=sanpham.MaSpand 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 SoNVfrom nhanvien

Trang 20

order by TongBanDuoc desc

create view mausacmuanhieunhat

order by SoLanMua desc

create view sokhachhangmuanhieunhat

as

select top 1 HoTen,Sum(SoLuong)as SoHangBanDuoc,(SoLuong*DGX)as

DoanhThu

Trang 21

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

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

Trang 22

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

exec SP_themsp

@MaSP='',@TenSP='',@MauSac='',@MoTa='',@MaHSX='',@MaKho=''

Trang 23

-T o các Stored Procedure xóa d li uạ ữ ệ

create procedure P_nhanvien

Trang 24

Tìm nhân viên theo mã nhân viên

create procedure SP_search1

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

Trang 25

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

T o View màu s c mua nhi u nh tạ ắ ề ấ

create view mausacduocmuanhieunhat

as

Trang 26

select MauSac, (hoadonxuat_chitiet.MaSP)as SoLanDuocMua

where sanpham.MaSP=hoadonnhap_chitiet.MaSP

group by MauSac

SELECT MauSac,SUM(SoLuong) AS solanmua

FROM sanpham , hoadonxuat_chitiet

WHERE sanpham.masp=hoadonxuat_chitiet.masp

GROUP BY mausac

HAVING SUM(SoLuong) = (

SELECT TOP 1 SUM(SoLuong) AS solanmua

FROM sanpham , hoadonxuat_chitiet

WHERE sanpham.masp=hoadonxuat_chitiet.masp

GROUP BY MauSac

ORDER BY solanmua DESC )

select MauSac,(COUNT(hoadonxuat_chitiet.MaSP)*SoLuong) as SoLanMuafrom sanpham,hoadonxuat_chitiet

Trang 27

where nhanvien.MaNV=hoadonxuat.MaNV

and HoTen='T H u Hoàng'ạ ữ

Tao proc tinh tong SP trong mot kho

create procedure TongSPMotKho

Ngày đăng: 10/04/2017, 11:12

TỪ KHÓA LIÊN QUAN

w