Skip to content

Commit

Permalink
Finish Lab 2 with some changes from SQL command
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyads69 committed Jul 25, 2023
1 parent 7c0b6cf commit 4253fee
Show file tree
Hide file tree
Showing 2 changed files with 379 additions and 280 deletions.
252 changes: 54 additions & 198 deletions 22521543_BTTH1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
-- MSSV: 22521543

-- BAI THUC HANH: QLY_BANHANG


CREATE DATABASE QLY_BANHANG
GO

Expand All @@ -12,38 +10,38 @@ USE QLY_BANHANG
CREATE TABLE KHACHHANG
(
MAKH CHAR(4) PRIMARY KEY NOT NULL,
HOTEN VARCHAR(40) ,
DCHI VARCHAR(50) ,
SODT VARCHAR(20),
NGSINH SMALLDATETIME,
DOANHSO MONEY,
NGDK SMALLDATETIME,
HOTEN VARCHAR(40) NOT NULL,
DCHI VARCHAR(50) NOT NULL,
SODT VARCHAR(20) NOT NULL,
NGSINH SMALLDATETIME NOT NULL,
DOANHSO MONEY NOT NULL,
NGDK SMALLDATETIME NOT NULL
)

CREATE TABLE NHANVIEN
(
MANV CHAR(4) PRIMARY KEY NOT NULL,
HOTEN VARCHAR(40) ,
SODT VARCHAR(20),
NGVL SMALLDATETIME,
HOTEN VARCHAR(40) NOT NULL,
SODT VARCHAR(20) NOT NULL,
NGVL SMALLDATETIME NOT NULL,
)

CREATE TABLE SANPHAM
(
MASP CHAR(4) PRIMARY KEY NOT NULL,
TENSP VARCHAR(40),
DVT VARCHAR(20),
NUOCSX VARCHAR(40),
GIA MONEY,
TENSP VARCHAR(40) NOT NULL,
DVT VARCHAR(20) NOT NULL,
NUOCSX VARCHAR(40) NOT NULL,
GIA MONEY NOT NULL
)

CREATE TABLE HOADON
(
SOHD INT PRIMARY KEY NOT NULL,
NGHD SMALLDATETIME,
NGHD SMALLDATETIME NOT NULL,
MAKH CHAR(4) FOREIGN KEY REFERENCES KHACHHANG(MAKH),
MANV CHAR(4) FOREIGN KEY REFERENCES NHANVIEN(MANV),
TRIGIA MONEY,
TRIGIA MONEY NOT NULL,
)

CREATE TABLE CTHD
Expand All @@ -53,201 +51,59 @@ CREATE TABLE CTHD
SL INT CONSTRAINT PK_CTHD PRIMARY KEY(SOHD,MASP)
)

ALTER TABLE SANPHAM ADD GHICHU VARCHAR(20)

ALTER TABLE KHACHHANG ADD LOAIKH VARCHAR(20)

ALTER TABLE SANPHAM ALTER COLUMN GHICHU VARCHAR(100)
ALTER TABLE SANPHAM ADD GHICHU VARCHAR(20) NOT NULL

ALTER TABLE SANPHAM DROP COLUMN GHICHU
ALTER TABLE KHACHHANG ADD LOAIKH TINYINT NOT NULL

ALTER TABLE SANPHAM ADD CONSTRAINT GIA_SP CHECK (GIA>=500)
ALTER TABLE SANPHAM ALTER COLUMN GHICHU VARCHAR(100) NOT NULL

ALTER TABLE KHACHHANG ADD CONSTRAINT GT_LOAIKH CHECK (LOAIKH='Vang lai' OR LOAIKH='Thuong xuyen' OR LOAIKH='Vip')

ALTER TABLE SANPHAM ADD CONSTRAINT GT_DVT CHECK (DVT='cay' OR DVT='hop' OR DVT='cai' OR DVT='quyen' OR DVT='chuc')

ALTER TABLE KHACHHANG ADD CONSTRAINT NGDK_NGSINH CHECK (NGDK>NGSINH)



-- INSERT VALUE TO NHANVIEN

INSERT INTO NHANVIEN (MANV, HOTEN, SODT, NGVL)
VALUES
('NV01', 'Nguyen Nhu Nhut', '0927345678', '2006-04-13'),
('NV02', 'Le Thi Phi Yen', '0987567390', '2006-04-21'),
('NV03', 'Nguyen Van B', '0997047382', '2006-04-27'),
('NV04', 'Ngo Thanh Tuan', '0913758498', '2006-06-24'),
('NV05', 'Nguyen Thi Truc Thanh', '0918590387', '2006-07-20');

-- INSERT VALUE TO KHACHHANG

INSERT INTO KHACHHANG (MAKH, HOTEN, DCHI, SODT, NGSINH, DOANHSO, NGDK)
VALUES
('KH01', 'Nguyen Van A', '731 Tran Hung Dao, Q5, TpHCM', '8823451', '1960-10-22', '13060000', '2006-07-22'),
('KH02', 'Tran Ngoc Han', '23/5 Nguyen Trai, Q5, TpHCM', '908256478', '1974-04-03', '280000', '2006-07-30'),
('KH03', 'Tran Ngoc Linh', '45 Nguyen Canh Chan, Q1, TpHCM', '938776266', '1980-06-12', '3860000', '2006-08-05'),
('KH04', 'Tran Minh Long', '50/34 Le Dai Hanh, Q10, TpHCM', '917325476', '1965-03-09', '250000', '2006-10-02'),
('KH05', 'Le Nhat Minh', '34 Truong Dinh, Q3, TpHCM', '8246108', '1950-03-10', '21000', '2006-10-28'),
('KH06', 'Le Hoai Thuong', '227 Nguyen Van Cu, Q5, TpHCM', '8631738', '1981-12-31', '915000', '2006-11-24'),
('KH07', 'Nguyen Van Tam', '32/3 Tran Binh Trong, Q5, TpHCM', '916783565', '1971-04-06', '12500', '2006-12-01'),
('KH08', 'Phan Thi Thanh', '45/2 An Duong Vuong, Q5, TpHCM', '938435756', '1971-01-10', '365000', '2006-12-13'),
('KH09', 'Le Ha Vinh', '873 Le Hong Phong, Q5, TpHCM', '8654763', '1979-09-03', '70000', '2007-01-14'),
('KH10', 'Ha Duy Lap', '34/34B Nguyen Trai, Q1, TpHCM', '8768904', '1983-05-02', '67500', '2007-01-16');

-- INSERT VALUE TO SANPHAM

INSERT INTO SANPHAM (MASP, TENSP, DVT, NUOCSX, GIA)
VALUES
('BC01', 'But chi', 'cay', 'Singapore', 3000),
('BC02', 'But chi', 'cay', 'Singapore', 5000),
('BC03', 'But chi', 'cay', 'Viet Nam', 3500),
('BC04', 'But chi', 'hop', 'Viet Nam', 30000),
('BB01', 'But bi', 'cay', 'Viet Nam', 5000),
('BB02', 'But bi', 'cay', 'Trung Quoc', 7000),
('BB03', 'But bi', 'hop', 'Thai Lan', 100000),
('TV01', 'Tap 100 giay mong', 'quyen', 'Trung Quoc', 2500),
('TV02', 'Tap 200 giay mong', 'quyen', 'Trung Quoc', 4500),
('TV03', 'Tap 100 giay tot', 'quyen', 'Viet Nam', 3000),
('TV04', 'Tap 200 giay tot', 'quyen', 'Viet Nam', 5500),
('TV05', 'Tap 100 trang', 'chuc', 'Viet Nam', 23000),
('TV06', 'Tap 200 trang', 'chuc', 'Viet Nam', 53000),
('TV07', 'Tap 100 trang', 'chuc', 'Trung Quoc', 34000),
('ST01', 'So tay 500 trang', 'quyen', 'Trung Quoc', 40000),
('ST02', 'So tay loai 1', 'quyen', 'Viet Nam', 55000),
('ST03', 'So tay loai 2', 'quyen', 'Viet Nam', 51000),
('ST04', 'So tay', 'quyen', 'Thai Lan', 55000),
('ST05', 'So tay mong', 'quyen', 'Thai Lan', 20000),
('ST06', 'Phan viet bang', 'hop', 'Viet Nam', 5000),
('ST07', 'Phan khong bui', 'hop', 'Viet Nam', 7000),
('ST08', 'Bong bang', 'cai', 'Viet Nam', 1000),
('ST09', 'But long', 'cay', 'Viet Nam', 5000),
('ST10', 'But long', 'cay', 'Trung Quoc', 7000);

-- INSERT VALUE TO HOADON

INSERT INTO HOADON (SOHD, NGHD, MAKH, MANV, TRIGIA)
VALUES
(1001, '2006-07-23', 'KH01', 'NV01', 320000),
(1002, '2006-08-12', 'KH01', 'NV02', 840000),
(1003, '2006-08-23', 'KH02', 'NV01', 100000),
(1004, '2006-09-01', 'KH02', 'NV01', 180000),
(1005, '2006-10-20', 'KH01', 'NV02', 3800000),
(1006, '2006-10-16', 'KH01', 'NV03', 2430000),
(1007, '2006-10-28', 'KH03', 'NV03', 510000),
(1008, '2006-10-28', 'KH01', 'NV03', 440000),
(1009, '2006-10-28', 'KH03', 'NV04', 200000),
(1010, '2006-11-01', 'KH01', 'NV01', 5200000),
(1011, '2006-11-04', 'KH04', 'NV03', 250000),
(1012, '2006-11-30', 'KH05', 'NV03', 21000),
(1013, '2006-12-12', 'KH06', 'NV01', 5000),
(1014, '2006-12-31', 'KH03', 'NV02', 3150000),
(1015, '2007-01-01', 'KH06', 'NV01', 910000),
(1016, '2007-01-01', 'KH07', 'NV02', 12500),
(1017, '2007-01-02', 'KH08', 'NV03', 35000),
(1018, '2007-01-13', 'KH08', 'NV03', 330000),
(1019, '2007-01-13', 'KH01', 'NV03', 30000),
(1020, '2007-01-14', 'KH09', 'NV04', 70000),
(1021, '2007-01-16', 'KH10', 'NV03', 67500),
(1022, '2007-01-16', NULL, 'NV03', 7000),
(1023, '2007-01-17', NULL, 'NV01', 330000);

-- INSERT VALUE TO CTHD

INSERT INTO CTHD (SOHD, MASP, SL)
VALUES
(1001, 'TV02', 10),
(1001, 'ST01', 5),
(1001, 'BC01', 5),
(1001, 'BC02', 10),
(1001, 'ST08', 10),
(1002, 'BC04', 20),
(1002, 'BB01', 20),
(1002, 'BB02', 20),
(1003, 'BB03', 10),
(1004, 'TV01', 20),
(1004, 'TV02', 10),
(1004, 'TV03', 10),
(1004, 'TV04', 10),
(1005, 'TV05', 50),
(1005, 'TV06', 50),
(1006, 'TV07', 20),
(1006, 'ST01', 30),
(1006, 'ST02', 10),
(1007, 'ST03', 10),
(1008, 'ST04', 8),
(1009, 'ST05', 10),
(1010, 'TV07', 50),
(1010, 'ST07', 50),
(1010, 'ST08', 100),
(1010, 'ST04', 50),
(1010, 'TV03', 100),
(1011, 'ST06', 50),
(1012, 'ST07', 3),
(1013, 'ST08', 5),
(1014, 'BC02', 80),
(1014, 'BB02', 100),
(1014, 'BC04', 60),
(1014, 'BB01', 50),
(1015, 'BB02', 30),
(1015, 'BB03', 7),
(1016, 'TV01', 5),
(1017, 'TV02', 1),
(1017, 'TV03', 1),
(1017, 'TV04', 5),
(1018, 'ST04', 6),
(1019, 'ST05', 1),
(1019, 'ST06', 2),
(1020, 'ST07', 10),
(1021, 'ST08', 5),
(1021, 'TV01', 7),
(1021, 'TV02', 10),
(1022, 'ST07', 1),
(1023, 'ST04', 6);

-- BAI THUC HANH: QLY_HOCVU

/* CREATE DATABASE QLY_HOCVU
CREATE DATABASE QLY_HOCVU
GO
USE QLY_HOCVU

CREATE TABLE KHOA
(
MAKHOA VARCHAR(4) PRIMARY KEY NOT NULL,
TENKHOA VARCHAR(40),
NGTLAP SMALLDATETIME,
TENKHOA VARCHAR(40) NOT NULL,
NGTLAP SMALLDATETIME NOT NULL,
TRGKHOA CHAR(4)
)

CREATE TABLE MONHOC
(
MAMH VARCHAR(10) PRIMARY KEY NOT NULL,
TENMN VARCHAR(40),
TCLT TINYINT ,
TCTH TINYINT,
TENMN VARCHAR(40) NOT NULL,
TCLT TINYINT NOT NULL,
TCTH TINYINT NOT NULL,
MAKHOA VARCHAR(4) FOREIGN KEY REFERENCES KHOA(MAKHOA),
)

CREATE TABLE DIEUKIEN
(
MAMH VARCHAR(10) NOT NULL,
MAMH_TRUOC VARCHAR(10) NOT NULL,
MAMH_TRUOC VARCHAR(10) NOT NULL
)

ALTER TABLE DIEUKIEN ADD CONSTRAINT PK_DIEUKINE PRIMARY KEY(MAMH,MAMH_TRUOC)

CREATE TABLE GIAOVIEN
(
MAGV CHAR(4) PRIMARY KEY NOT NULL,
HOTEN VARCHAR(40) ,
HOCVI VARCHAR(10) ,
HOCHAM VARCHAR(10) ,
GIOITINH VARCHAR(3),
NGSINH SMALLDATETIME,
NGVL SMALLDATETIME,
HESO NUMERIC(4,2),
MUCLUONG MONEY,
HOTEN VARCHAR(40) NOT NULL,
HOCVI VARCHAR(10) NOT NULL,
HOCHAM VARCHAR(10) NOT NULL,
GIOITINH VARCHAR(3) NOT NULL,
NGSINH SMALLDATETIME NOT NULL,
NGVL SMALLDATETIME NOT NULL,
HESO NUMERIC(4,2) NOT NULL,
MUCLUONG MONEY NOT NULL,
MAKHOA VARCHAR(4) FOREIGN KEY REFERENCES KHOA(MAKHOA)
)

Expand All @@ -256,20 +112,20 @@ ALTER TABLE KHOA ADD CONSTRAINT GV_TRUONGKHOA FOREIGN KEY (TRGKHOA) REFERENCES G
CREATE TABLE LOP
(
MALOP CHAR(3) PRIMARY KEY NOT NULL,
TENLOP VARCHAR(40),
TRGLOP CHAR(5),
SISO TINYINT,
TENLOP VARCHAR(40) NOT NULL,
TRGLOP CHAR(5) NOT NULL,
SISO TINYINT NOT NULL,
MAGVCN CHAR(4) FOREIGN KEY REFERENCES GIAOVIEN(MAGV)
)

CREATE TABLE HOCVIEN
(
MAHV CHAR(5) PRIMARY KEY NOT NULL,
HO VARCHAR(40),
TEN VARCHAR(10),
NGSINH SMALLDATETIME,
GIOITINH VARCHAR(3),
NOISINH VARCHAR(40),
HO VARCHAR(40) NOT NULL,
TEN VARCHAR(10) NOT NULL,
NGSINH SMALLDATETIME NOT NULL,
GIOITINH VARCHAR(3) NOT NULL,
NOISINH VARCHAR(40) NOT NULL,
MALOP CHAR(3) FOREIGN KEY REFERENCES LOP(MALOP)
)

Expand All @@ -280,10 +136,10 @@ CREATE TABLE GIANGDAY
MALOP CHAR(3) FOREIGN KEY REFERENCES LOP(MALOP) NOT NULL,
MAMH VARCHAR(10) FOREIGN KEY REFERENCES MONHOC(MAMH) NOT NULL,
MAGV CHAR(4) FOREIGN KEY REFERENCES GIAOVIEN(MAGV),
HOCKY TINYINT,
NAM SMALLINT ,
TUNGAY SMALLDATETIME,
DENNGAY SMALLDATETIME,
HOCKY TINYINT NOT NULL,
NAM SMALLINT NOT NULL,
TUNGAY SMALLDATETIME NOT NULL,
DENNGAY SMALLDATETIME NOT NULL
)

ALTER TABLE GIANGDAY ADD CONSTRAINT PK_GIANGDAY PRIMARY KEY(MALOP, MAMH)
Expand All @@ -292,24 +148,24 @@ CREATE TABLE KETQUATHI
(
MAHV CHAR(5) FOREIGN KEY REFERENCES HOCVIEN(MAHV) NOT NULL,
MAMH VARCHAR(10) FOREIGN KEY REFERENCES MONHOC(MAMH) NOT NULL,
LANTHI TINYINT,
NGTHI SMALLDATETIME,
DIEM NUMERIC(4,2),
KQUA VARCHAR(10)
LANTHI TINYINT NOT NULL,
NGTHI SMALLDATETIME NOT NULL,
DIEM NUMERIC(4,2) NOT NULL,
KQUA VARCHAR(10) NOT NULL
)
ALTER TABLE KETQUATHI ADD CONSTRAINT PK_KETQUATHI PRIMARY KEY(MAHV, MAMH, LANTHI)



ALTER TABLE HOCVIEN ADD GHICHU VARCHAR(100)
ALTER TABLE HOCVIEN ADD DIEMTB NUMERIC(4,2)
ALTER TABLE HOCVIEN ADD XEPLOAI VARCHAR(10)
ALTER TABLE HOCVIEN ADD GHICHU VARCHAR(100) NOT NULL
ALTER TABLE HOCVIEN ADD DIEMTB NUMERIC(4,2) NOT NULL
ALTER TABLE HOCVIEN ADD XEPLOAI VARCHAR(10) NOT NULL

ALTER TABLE GIAOVIEN ADD CONSTRAINT GV_GIOITINH CHECK (GIOITINH='Nam' OR GIOITINH='Nu')
ALTER TABLE HOCVIEN ADD CONSTRAINT HV_GIOITINH CHECK (GIOITINH='Nam' OR GIOITINH='Nu')

ALTER TABLE KETQUATHI ALTER COLUMN DIEM DECIMAL(2,2)
ALTER TABLE KETQUATHI ALTER COLUMN DIEM DECIMAL(2,2)
ALTER TABLE KETQUATHI ADD CONSTRAINT DIEMSO_MLT CHECK (DIEM>=0 AND DIEM<=10)

ALTER TABLE HOCVIEN ALTER COLUMN DIEMTB DECIMAL(2,2)
ALTER TABLE HOCVIEN ADD CONSTRAINT GT_DIEMTB CHECK (DIEMTB>=0 AND DIEMTB<=10) */
ALTER TABLE HOCVIEN ADD CONSTRAINT GT_DIEMTB CHECK (DIEMTB>=0 AND DIEMTB<=10)
Loading

0 comments on commit 4253fee

Please sign in to comment.