-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.sql
More file actions
313 lines (240 loc) · 19.9 KB
/
Database.sql
File metadata and controls
313 lines (240 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
create database coffeemanament;
use coffeemanament;
-- food
-- table
-- foodCategory
-- account
-- bill
-- billInfo
create table tablefood (
id int identity primary key,
name nvarchar(100) not null default N'Chưa có tên',
statuss nvarchar(100) not null default N'Trống',
)
create table account(
username nvarchar(100) primary key,
displayname nvarchar(100)not null,
pass nvarchar(100) not null default 0,
typeaccount int not null default 0 --1: admin, 0:staff
)
create table foodcategory(
id int identity primary key,
name nvarchar(100) not null default N'Chưa đặt tên',
)
create table food(
id int identity primary key,
name nvarchar(100) not null default N'Chưa đặt tên' ,
idcategory int not null,
price float not null,
foreign key (idcategory) references foodcategory(id)
)
create table bill (
id int identity primary key,
datecheckin date not null default getdate(),
datecheckout date,
idtable int not null,
statuss int not null default 0, --1: thanh toan, 0: chua thanh toan
foreign key (idtable) references tablefood(id)
)
create table billinfo (
id int identity primary key,
idbill int not null,
idfood int not null,
countt int not null default 0 ,
foreign key (idbill) references bill(id),
foreign key (idfood) references food(id)
)
insert into account (username, displayname, pass,typeaccount) values('doan','congdoan','123456',1)
insert into account (username, displayname, pass,typeaccount) values('staff','staff','1',0)
insert into account (username, displayname, pass,typeaccount) values('nhanvien','Tony','1',2)
select * from account
create PROC USP_GetAccountByUserName
@username nvarchar(100)
as
begin
select * from Account where username = @username
end
exec USP_GetAccountByUserName @username = N'doan'
create proc USP_Login
@userName nvarchar(100), @passWord nvarchar(100)
as
begin
select * from account where username = @userName and pass = @passWord
end
-- thêm bàn
declare @i int = 1
while @i < =10
begin
insert into tablefood (name) values (N'Bàn ' + CAST(@i as nvarchar(100)))
set @i = @i +1
end
select * from tablefood
-- thêm category
insert into foodcategory(name) values (N'Hải sản')
insert into foodcategory(name) values (N'Nông sản')
insert into foodcategory(name) values (N'Lâm sản')
insert into foodcategory(name) values (N'Sản sản')
insert into foodcategory(name) values (N'Nước')
-- thêm món ăn
insert into food(name,idcategory,price) values (N'Mực nướng sa tê',1,12000)
insert into food(name,idcategory,price) values (N'Nghêu hấp xả',1,50000)
insert into food(name,idcategory,price) values (N'Vú heo nướng',2,60000)
insert into food(name,idcategory,price) values (N'Gà chiên nước mắm',2,45000)
insert into food(name,idcategory,price) values (N'Vịt nướng chao',2,56000)
insert into food(name,idcategory,price) values (N'Heo rừng nướng',3,80000)
insert into food(name,idcategory,price) values (N'Cơm chiên haha',4,50000)
insert into food(name,idcategory,price) values (N'7 Up',5,10000)
insert into food(name,idcategory,price) values (N'Cafe',5,12000)
--thêm bill
insert into bill(datecheckin,datecheckout,idtable,statuss) values (GETDATE(), null, 1, 0)
insert into bill(datecheckin,datecheckout,idtable,statuss) values (GETDATE(), null, 2, 0)
insert into bill(datecheckin,datecheckout,idtable,statuss) values (GETDATE(), null, 2, 1)
-- thêm info
insert into billinfo(idbill,idfood,countt) values (1,1,2)
insert into billinfo(idbill,idfood,countt) values (1,3,4)
insert into billinfo(idbill,idfood,countt) values (1,5,1)
insert into billinfo(idbill,idfood,countt) values (2,2,2)
insert into billinfo(idbill,idfood,countt) values (3,6,1)
update tablefood set statuss = N'Có người' where id = 5
create proc USP_InsertBill
@idTable int
as
begin
insert into bill(datecheckin, datecheckout, idtable, statuss, discount) values (GETDATE(), null, @idTable, 0,0)
end
create proc USP_GetTableList
as select * from tablefood
exec USP_GetTableList
---------------------------
create PROC USP_InsertBillInfo
@idBill int, @idFood int, @count int
as
begin
declare @isExitsBillInfo int;
declare @foodCount int = 1;
select @isExitsBillInfo = id, @foodCount = b.countt from billinfo as b where idbill = @idBill and idfood = @idFood
if(@isExitsBillInfo > 0)
begin
declare @newCount int = @foodCount + @count
if(@newCount > 0)
update billinfo set countt = @foodCount + @count where idfood = @idFood
else
delete billinfo where idbill = @idBill and idfood = @idFood
end
else
begin
insert into billinfo(idbill, idfood, countt) values (@idBill, @idFood,@count)
end
end
delete billinfo
delete bill
---------------------------
alter trigger UTG_UpdateBillInfo
on billinfo for insert, update
as
begin
declare @idBill int
select @idBill = idbill from inserted
declare @idTable int
select @idTable = idtable from bill where id = @idBill and statuss = 0
declare @count int
select @count = COUNT(*) from billinfo where idbill = @idBill
if(@count > 0)
update tablefood set statuss = N'Có người' where id = @idTable
else
update tablefood set statuss = N'Trống' where id = @idTable
end
----------------------------------
----------------------------------
create trigger UTG_UpdateBill
on bill for update
as
begin
declare @idBill int
select @idBill = id from inserted
declare @idTable int
select @idTable = idtable from bill where id = @idBill
declare @count int = 0
select @count = COUNT(*) from bill where idtable = @idTable and statuss = 0
if(@count = 0)
update tablefood set statuss = N'Trống' where id = @idTable
end
----------------------------
alter table bill add discount int
alter PROC USP_SwitchTable
@idTable1 int, @idTable2 int
as
begin
declare @idFirstBill int
declare @idSecondBill int
declare @isFirstTableEmty int = 1
declare @isSecondTableEmty int = 1
select @idFirstBill = id from bill where idtable= @idTable1 and statuss = 0
select @idSecondBill = id from bill where idtable= @idTable2 and statuss = 0
if(@idFirstBill is null)
begin
insert into bill (datecheckin,datecheckout,idtable,statuss)
values(GETDATE(), null, @idTable1,0)
select @idFirstBill = max(id) from bill where idtable = @idTable1 and statuss = 0
end
select @isFirstTableEmty = COUNT(*) from billinfo where idbill = @idFirstBill
if(@idSecondBill is null)
begin
insert into bill (datecheckin,datecheckout,idtable,statuss)
values(GETDATE(), null, @idTable2,0)
select @idSecondBill = max(id) from bill where idtable = @idTable2 and statuss = 0
end
select @isSecondTableEmty = COUNT(*) from billinfo where idbill = @idSecondBill
select id into IDBillInfoTable from billinfo where idbill = @idSecondBill
update billinfo set idbill = @idSecondBill where idbill = @idFirstBill
update billinfo set idbill = @idFirstBill where id in (select * from IDBillInfoTable)
if(@isFirstTableEmty = 0)
update tablefood set statuss = N'Trống' where id = @idTable2
if(@isSecondTableEmty = 0)
update tablefood set statuss = N'Trống' where id = @idTable1
drop table IDBillInfoTable
end
update tablefood set statuss = N'Trống'
alter table bill add totalPrice float
select * from bill
select * from billinfo
select * from tablefood
create PROC USP_GetListBillByDate
@checkin date, @checkout date
as
begin
select t.name as [Tên bàn], b.totalPrice as [Tổng tiền], b.datecheckin as[Ngày vào], b.datecheckout as [Ngày ra], b.discount as [Giảm giá]
from bill as b,tablefood as t
where datecheckin >= @checkin and datecheckout <= @checkout and b.statuss = 1 and b.idtable = t.id
end
create PROC USP_UpdateAccount
@userName nvarchar(100), @displayName nvarchar(100), @password nvarchar(100), @newPassword nvarchar(100)
as
begin
declare @isRightPass int = 0
select @isRightPass = COUNT(*) from account where username = @userName and pass = @password
if(@isRightPass = 1)
begin
if(@newPassword = null or @newPassword = '')
begin
update account set displayname = @displayName where username = @userName
end
else
update account set displayname = @displayName, pass = @newPassword where username = @userName
end
end
create trigger UTG_DeleteBillInfo
on billinfo for delete
as
begin
declare @idBillInfo int
declare @idBill int
select @idBillInfo = id, @idBill = deleted.idbill from deleted
declare @idTable int
select @idTable = idtable from bill where id = @idBill
declare @count int = 0
select @count = COUNT(*) from billinfo as bi, bill as b where b.id = bi.idbill and b.id = @idBill and b.statuss = 0
if(@count = 0)
update tablefood set statuss = N'Trống' where id = @idTable
end
CREATE FUNCTION [dbo].[fuConvertToUnsign1] ( @strInput NVARCHAR(4000) ) RETURNS NVARCHAR(4000) AS BEGIN IF @strInput IS NULL RETURN @strInput IF @strInput = '' RETURN @strInput DECLARE @RT NVARCHAR(4000) DECLARE @SIGN_CHARS NCHAR(136) DECLARE @UNSIGN_CHARS NCHAR (136) SET @SIGN_CHARS = N'ăâđêôơưàảãạáằẳẵặắầẩẫậấèẻẽẹéềểễệế ìỉĩịíòỏõọóồổỗộốờởỡợớùủũụúừửữựứỳỷỹỵý ĂÂĐÊÔƠƯÀẢÃẠÁẰẲẴẶẮẦẨẪẬẤÈẺẼẸÉỀỂỄỆẾÌỈĨỊÍ ÒỎÕỌÓỒỔỖỘỐỜỞỠỢỚÙỦŨỤÚỪỬỮỰỨỲỶỸỴÝ' +NCHAR(272)+ NCHAR(208) SET @UNSIGN_CHARS = N'aadeoouaaaaaaaaaaaaaaaeeeeeeeeee iiiiiooooooooooooooouuuuuuuuuuyyyyy AADEOOUAAAAAAAAAAAAAAAEEEEEEEEEEIIIII OOOOOOOOOOOOOOOUUUUUUUUUUYYYYYDD' DECLARE @COUNTER int DECLARE @COUNTER1 int SET @COUNTER = 1 WHILE (@COUNTER <=LEN(@strInput)) BEGIN SET @COUNTER1 = 1 WHILE (@COUNTER1 <=LEN(@SIGN_CHARS)+1) BEGIN IF UNICODE(SUBSTRING(@SIGN_CHARS, @COUNTER1,1)) = UNICODE(SUBSTRING(@strInput,@COUNTER ,1) ) BEGIN IF @COUNTER=1 SET @strInput = SUBSTRING(@UNSIGN_CHARS, @COUNTER1,1) + SUBSTRING(@strInput, @COUNTER+1,LEN(@strInput)-1) ELSE SET @strInput = SUBSTRING(@strInput, 1, @COUNTER-1) +SUBSTRING(@UNSIGN_CHARS, @COUNTER1,1) + SUBSTRING(@strInput, @COUNTER+1,LEN(@strInput)- @COUNTER) BREAK END SET @COUNTER1 = @COUNTER1 +1 END SET @COUNTER = @COUNTER +1 END SET @strInput = replace(@strInput,' ','-') RETURN @strInput END