1
+ CREATE TABLE KHACHHANG
2
+ (
3
+ MAKH CHAR (10 ) NOT NULL Primary Key ,
4
+ TENKH NVARCHAR2(50 ),
5
+ DIENTHOAI CHAR (10 ) unique,
6
+ DIACHI NVARCHAR2(250 )
7
+ );
8
+ CREATE TABLE NHANVIEN
9
+ (
10
+ MANV CHAR (10 ) NOT NULL PRIMARY KEY ,
11
+ TENVN NVARCHAR2(50 ) not null ,
12
+ DIENTHOAI CHAR (10 ),
13
+ DIACHI NVARCHAR2(250 ),
14
+ CHUCVU NVARCHAR2(10 )
15
+ );
16
+ create table sanpham
17
+ (
18
+ MaSP NUMBER PRIMARY KEY ,
19
+ TenSP NVARCHAR2(50 ) NOT NULL ,
20
+ DonGia NUMBER (10 ,2 ) NOT NULL ,
21
+ SoLuong NUMBER (10 ,2 ) NOT NULL ,
22
+ TrangThai NUMBER (1 ) NOT NULL ,
23
+ HinhAnh BLOB
24
+ );
25
+ CREATE TABLE BAN
26
+ (
27
+ MABAN NUMBER PRIMARY KEY ,
28
+ TenBAN VARCHAR2 (50 ) NOT NULL ,
29
+ TrangThai NUMBER (1 ) NOT NULL
30
+ );
31
+ create table HoaDon
32
+ (
33
+ MaHD CHAR (10 ) NOT NULL PRIMARY KEY ,
34
+ MaNV CHAR (10 ) NOT NULL ,
35
+ MaKH CHAR (10 ) NOT NULL ,
36
+ Maban NUMBER NOT NULL ,
37
+ NgayLap DATE NOT NULL ,
38
+ TongTien NUMBER (10 ,2 ) NOT NULL ,
39
+ foreign key (MaNV) references NhanVien(MaNV),
40
+ foreign key (MaKH) references KhachHang(MaKH),
41
+ foreign key (MaBan) references Ban(MaBan)
42
+ );
43
+ CREATE table ChiTietHD
44
+ (
45
+ MaCTHD CHAR (10 ) NOT NULL PRIMARY KEY ,
46
+ MaHD CHAR (10 ) NOT NULL ,
47
+ MaSP NUMBER NOT NULL ,
48
+ SoLuong NUMBER (10 ,2 ) NOT NULL ,
49
+ DonGia NUMBER (10 ,2 ) NOT NULL ,
50
+ ThanhTien NUMBER (10 ,2 ) NOT NULL ,
51
+ foreign key (MaHD) references HoaDon(MaHD),
52
+ foreign key (MaSP) references SanPham(MaSP)
53
+ );
54
+
55
+ -- TH M KH A NGO?I
56
+ ALTER TABLE HOADON
57
+ ADD CONSTRAINT FK_KHACHHANG_HD FOREIGN KEY (MAKH) REFERENCES KHACHHANG(MAKH);
58
+ ALTER TABLE HOADON
59
+ ADD CONSTRAINT FK_NHANVIEN_HD FOREIGN KEY (MANV) REFERENCES NHANVIEN(MANV);
60
+ ALTER TABLE HOADON
61
+ ADD CONSTRAINT FK_MATHHANG_HD FOREIGN KEY (MAHANG) REFERENCES MATHANG(MAHANG);
62
+
63
+ ALTER TABLE CHITIETHD
64
+ ADD CONSTRAINT FK_CHITIETHD_HD FOREIGN KEY (MAHD) REFERENCES HOADON(MAHD);
65
+ ALTER TABLE CHITIETHD
66
+ ADD CONSTRAINT FK_CHITIETHD_MH FOREIGN KEY (MAHANG) REFERENCES MATHANG(MAHANG);
67
+
68
+ -- INSERT D? LI?U
69
+ INSERT INTO KHACHHANG
70
+ VALUES (' KH001' ,N' B i Ti?n Qu?c' ,' 0558392618' ,N' ??ng Nai' );
71
+ INSERT INTO KHACHHANG
72
+ VALUES (' KH002' ,N' Ph?m Ho ng Ph c' ,' 0978276354' ,N' Long An' );
73
+ INSERT INTO KHACHHANG
74
+ VALUES (' KH003' ,N' L m D??ng Di?m' ,' 0887263765' ,N' Ti?n Giang' );
75
+ INSERT INTO KHACHHANG
76
+ VALUES (' KH004' ,N' L m H n' ,' 0987265426' ,N' Th i B nh' );
77
+
78
+ INSERT INTO NHANVIEN
79
+ VALUES (' NV001' ,N' Ng Minh T i' ,' 0976837625' ,N' ? N?ng' ,N' Nh n vi n' );
80
+ INSERT INTO NHANVIEN
81
+ VALUES (' NV002' ,N' Tr?n B Nh n' ,' 0886309827' ,N' Phan Rang' ,N' Nh n vi n' );
82
+ INSERT INTO NHANVIEN
83
+ VALUES (' NV003' ,N' Nguy?n Vi?t H n' ,' 0968721098' ,N' An Giang' ,N' Qu?n l ' );
84
+ INSERT INTO NHANVIEN
85
+ VALUES (' NV004' ,N' Ph?m Nh?t Linh' ,' 0565427865' ,N' H N?i' ,N' Nh n vi n' );
86
+
87
+ INSERT INTO MATHANG
88
+ VALUES (' MH001' ,N' M n h nh m y t nh 20"' ,' Samsung' ,2500000 ,20 );
89
+ INSERT INTO MATHANG
90
+ VALUES (' MH002' ,' CPU Intel Core i7' ,' Intel' ,300000 ,25 );
91
+ INSERT INTO MATHANG
92
+ VALUES (' MH003' ,N' Chu?t m y t nh kh ng d y' ,' Logitech' ,360000 ,30 );
93
+ INSERT INTO MATHANG
94
+ VALUES (' MH004' ,N' ? c?ng SSD SATA 250GB' ,' WD' ,930000 ,40 );
95
+
96
+ ALTER session SET NLS_DATE_FORMAT = ' DD-MM-YYYY' ;
97
+ INSERT INTO HOADON
98
+ VALUES (' HD001' ,' NV004' ,' KH001' ,' 12-04-2021' ,' MH004' ,930000 );
99
+ INSERT INTO HOADON
100
+ VALUES (' HD002' ,' NV003' ,' KH002' ,' 2021-10-05' ,' MH003' ,360000 );
101
+ INSERT INTO HOADON
102
+ VALUES (' HD003' ,' NV002' ,' KH003' ,' 2021-06-23' ,' MH002' ,300000 );
103
+ INSERT INTO HOADON
104
+ VALUES (' HD004' ,' NV001' ,' KH004' ,' 2021-07-30' ,' MH001' ,2500000 );
105
+
106
+ INSERT INTO CHITIETHD
107
+ VALUES (' HD001' ,' MH004' ,N' ? c?ng SSD SATA 250GB' ,1 ,930000 ,930000 );
108
+ INSERT INTO CHITIETHD
109
+ VALUES (' HD002' ,' MH003' ,N' Chu?t m y t nh kh ng d y' ,1 ,360000 ,360000 );
110
+ INSERT INTO CHITIETHD
111
+ VALUES (' HD003' ,' MH002' ,' CPU Intel Core i7' ,1 ,300000 ,300000 );
112
+ INSERT INTO CHITIETHD
113
+ VALUES (' HD004' ,' MH001' ,N' M n h nh m y t nh 20"' ,1 ,2500000 ,2500000 );
114
+
115
+ INSERT INTO TAIKHOAN
116
+ VALUES (' nv001' ,' nv001' );
117
+ INSERT INTO TAIKHOAN
118
+ VALUES (' nv002' ,' nv002' );
119
+ INSERT INTO TAIKHOAN
120
+ VALUES (' nv003' ,' nv003' );
121
+ INSERT INTO TAIKHOAN
122
+ VALUES (' nv004' ,' nv004' );
123
+
124
+ -- ------------------------------------------------------------------------------
125
+ -- XEM SID
126
+ SELECT INSTANCE FROM V$THREAD;
127
+ -- XEM DATABASE
128
+ SELECT NAME FROM V$DATABASE;
129
+ -- XEM TH NG TIN C C USER
130
+ SELECT * FROM DBA_USERS;
131
+
132
+
133
+ SELECT INSTANCE_NUMBER, INSTANCE_NAME, STATUS, DATABASE_STATUS FROM V$INSTANCE;
134
+ SELECT DBID, NAME, CREATED, DATABASE_ROLE, DB_UNIQUE_NAME, OPEN_MODE FROM V$DATABASE;
135
+ select FILE# , CREATION_TIME, STATUS, ENABLED, CHECKPOINT_TIME from V$DATAFILE;
136
+ SELECT SID, SERIAL # , USERNAME, SCHEMANAME, PROGRAM FROM V$SESSION;
137
+ SELECT * FROM DBA_TABLESPACES;
138
+ SELECT PID, PNAME, USERNAME, PROGRAM, BACKGROUND FROM V$PROCESS;
139
+ SELECT NUM, NAME, DISPLAY_VALUE, ISDEFAULT FROM V$PARAMETER;
140
+ SELECT USERNAME, ACCOUNT_STATUS, CREATED, EXPIRY_DATE, PROFILE, LAST_LOGIN FROM DBA_USERS;
141
+ SELECT TABLESPACE_NAME, CON_ID FROM CDB_TABLESPACES;
142
+ CREATE USER giamdoc IDENTIFIED BY giamdoc;
143
+ /
144
+ -- XEM D? LI?U
145
+ SELECT * FROM KHACHHANG;
146
+ SELECT * FROM NHANVIEN;
147
+ SELECT * FROM quanly .MATHANG ORDER BY MAHANG;
148
+ SELECT * FROM HOADON;
149
+ SELECT * FROM CHITIETHD;
150
+ SELECT * FROM TAIKHOAN;
151
+ /
152
+ create or replace procedure xem_MatHang (v_cur_Mathang out sys_refcursor)
153
+ as
154
+ begin
155
+ open v_cur_Mathang for
156
+ select * from mathang;
157
+ end xem_MatHang;
158
+ /
159
+ variable cur_test refcursor
160
+ exec xem_MatHang (:cur_test)
161
+ print cur_test;
162
+ /
163
+
164
+ SET SERVEROUTPUT ON
165
+ -- THỦ TỤC XEM MẶT HÀNG
166
+ CREATE OR REPLACE PROCEDURE Xem_MatHang (MA OUT CHAR , TEN OUT NVARCHAR2, X_NSX OUT NVARCHAR2, GIA OUT FLOAT, SL OUT INT )
167
+ IS
168
+ BEGIN
169
+ SELECT * INTO MA, TEN, X_NSX, GIA, SL FROM QUANLY .MATHANG ;
170
+ END;
171
+ / -- THỰC THI THỦ TỤC
172
+ DECLARE
173
+ V_MA MATHANG .MAHANG %TYPE;
174
+ V_TEN MATHANG .TENHANG %TYPE;
175
+ V_NSX MATHANG .NSX %TYPE;
176
+ V_GIA MATHANG .GIABAN %TYPE;
177
+ V_SL MATHANG .SOLUONGTON %TYPE;
178
+ BEGIN
179
+ Xem_MatHang(V_MA, V_TEN, V_NSX, V_GIA, V_SL);
180
+ DBMS_OUTPUT .PUT_LINE (' record inserted successfully' );
181
+ END;
182
+ /
183
+
184
+ SET SERVEROUTPUT ON
185
+ -- THỦ TỤC TH�?M MẶT HÀNG
186
+ CREATE OR REPLACE PROCEDURE Them_MatHang (MA IN CHAR , TEN IN NVARCHAR2, NSX IN NVARCHAR2, GIA IN FLOAT, SL IN INT )
187
+ IS
188
+ BEGIN
189
+ INSERT INTO MATHANG VALUES (MA, TEN, NSX, GIA, SL);
190
+ END;
191
+ / -- THỰC THI THỦ TỤC
192
+ EXEC Them_MatHang(' axc' , N' CPU AMD R7' , ' AMD' , 2000000 , 40 );
193
+
194
+ -- BEGIN
195
+ -- Them_MatHang('MH007', N'XXX', 'XXX', 30000, 20);
196
+ -- DBMS_OUTPUT.PUT_LINE('record inserted successfully');
197
+ -- END;
198
+ /
199
+
200
+ SET SERVEROUTPUT ON
201
+ -- THỦ TỤC SỬA MẶT HÀNG
202
+ CREATE OR REPLACE PROCEDURE Sua_MatHang (MA IN CHAR , TEN IN NVARCHAR2, S_NSX IN NVARCHAR2, GIA IN FLOAT, SL IN INT )
203
+ IS
204
+ BEGIN
205
+ UPDATE MATHANG SET TENHANG = TEN, NSX = S_NSX, GIABAN = GIA, SOLUONGTON = SL WHERE MAHANG = MA;
206
+ END;
207
+ / -- THỰC THI THỦ TỤC
208
+ EXEC Sua_MatHang(' MH002' , N' CPU Intel Core i7' , ' Intel' , 3000000 , 50 );
209
+
210
+ -- BEGIN
211
+ -- Sua_MatHang('MH007', N'C', 'B', 20000, 20);
212
+ -- DBMS_OUTPUT.PUT_LINE('record updated successfully');
213
+ -- END;
214
+ /
215
+
216
+ SET SERVEROUTPUT ON
217
+ -- THỦ TỤC XÓA MẶT HÀNG
218
+ CREATE OR REPLACE PROCEDURE Xoa_MatHang (MA IN CHAR )
219
+ IS
220
+ BEGIN
221
+ DELETE FROM MATHANG WHERE MAHANG = MA;
222
+ END;
223
+ / -- THỰC THI THỦ TỤC
224
+ EXEC Xoa_MatHang(' axc' );
225
+
226
+ -- BEGIN
227
+ -- Xoa_MatHang('MH007');
228
+ -- DBMS_OUTPUT.PUT_LINE('record deleted successfully');
229
+ -- END;
230
+ /
231
+ -- LAY NHUNG CO TRO CO MAT HANG LA MA 1
232
+ DECLARE
233
+ CURSOR C_TRO IS
234
+ SELECT TENHANG , NSX ,GIABAN, SOLUONGTON
235
+ WHERE MAHANG= 1 ;
236
+ v_ten MATHANG .TENHANG %TYPE;
237
+ v_nxb MATHANG .NXB %TYPE;
238
+ v_giaban MATHANG .GIABAN %TYPE;
239
+ v_slton MATHANG .SOLUONGTON %TYPE;
240
+ BEGIN
241
+ OPEN C_TRO;
242
+ FETCH C_TRO
243
+ INTO v_ten,v_nxb,v_giaban,v_slton
244
+ DBMS_OUTPUT .PUT_LINE (v_ten|| ' ' || v_nxb|| ' ' || v_giaban|| ' ' || vslton)
245
+ END;
246
+ -- -in ra nhung don hnag
247
+ DECLARE
248
+ CURSOR C_TRO IS
249
+ SELECT MAHD,MANV , MAKH , MAHANG, TONGTIEN
250
+ WHERE MAHD= ' HD001' ;
251
+ v_mahd HOADON .MAHD %TYPE;
252
+ v_manv HOADON .MANV %TYPE;
253
+ v_makh HOADON .MAKH %TYPE;
254
+ v_mahang HOADON .MAHANG %TYPE;
255
+ v_tongtien HOADON .MATONGTIEN %TYPE;
256
+ BEGIN
257
+ OPEN C_TRO;
258
+ FETCH C_TRO
259
+ INTO v_mahd,v_manv,v_makh,v_mahang,v_tongtien
260
+ DBMS_OUTPUT .PUT_LINE (v_mahd|| ' ' || v_manv|| ' ' || v_makh|| ' ' || v_mahang|| ' ' || v_tongtien)
261
+ END
262
+
263
+ -- TAO TRIGGER
264
+ CREATE OR REPLACE TRIGGER KIEMTRA_GIABAN
265
+ BEFORE INSERT OR UPDATE OF GIABAN ON MATHANG
266
+ FOR EACH ROW
267
+ BEGIN
268
+ IF(:NEW .MAHANG IN (' MH001' ,' MH004' ))
269
+ AND :NEW .GIABAN < 300000 THEN
270
+ RAISE_APPLICATION_ERROR(- 20202 ,' GIA MAT HANG PHAI LON HON 300000' );
271
+ END IF;
272
+ END;
273
+ -- --TEST DL KHONG THOA
274
+ UPDATE MATHANG SET GIABAN= 2000 WHERE MAHANG= ' MH004'
0 commit comments