-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnlineShoppingTables.sql
407 lines (328 loc) · 9.17 KB
/
OnlineShoppingTables.sql
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
Group 8
Database: OnlineShoppingMall
*/
create database OnlineShoppingMall
USE OnlineShoppingMall;
GO
CREATE TABLE User_Account(
User_id INT NOT NULL PRIMARY KEY,
user_name VARCHAR(45) NOT NULL,
password VARCHAR(45) NOT NULL,
time_stamp DATETIME,
trans_number INT);
CREATE TABLE User_Session(
Session_id INT NOT NULL PRIMARY KEY,
IP_address VARCHAR(225) NOT NULL,
clicks INT,
time_stamp DATETIME,
User_id INT NOT NULL
REFERENCES dbo.User_Account(User_id)
);
CREATE TABLE Inventory_Item(
item_id INT IDENTITY NOT NULL PRIMARY KEY,
price MONEY NOT NULL,
item_name VARCHAR(45) NOT NULL,
qty INT,
catalog_id INT,
vendor_id INT
);
CREATE TABLE Shopping_Cart(
cart_id INT NOT NULL PRIMARY KEY,
active BIT,
total_price MONEY,
User_id INT NOT NULL
REFERENCES User_Account(User_id)
);
CREATE TABLE Cart_Detail(
cart_detial_id INT NOT NULL PRIMARY KEY,
item_id INT NOT NULL
REFERENCES Inventory_Item(item_id),
qty INT,
time_stamp DATETIME,
card_id INT NOT NULL
REFERENCES Shopping_Cart(cart_id));
CREATE TABLE Vendor(
vendor_id INT NOT NULL PRIMARY KEY,
VendorName VARCHAR(45) NOT NULL,
VendorAddress VARCHAR(45) NOT NULL);
CREATE TABLE InventoryCatalog(
catalog_id INT NOT NULL PRIMARY KEY,
CatalogName VARCHAR(45) NOT NULL);
ALTER TABLE Inventory_Item
ADD CONSTRAINT PK1_Catalog FOREIGN KEY (catalog_id)
REFERENCES InventoryCatalog(catalog_id);
ALTER TABLE Inventory_Item
ADD CONSTRAINT PK2_Vendor FOREIGN KEY (vendor_id)
REFERENCES Vendor(vendor_id);
DROP TABLE OrderHeader
CREATE TABLE OrderHeader
(
OrderID INT IDENTITY,
OrderDate DATETIME,
TotalAmount decimal(10,2) NOT NULL,
Status VARCHAR(255) NOT NULL,
UserID INT NOT NULL,
Discount decimal(10,2),
Tax decimal(10,2),
PRIMARY KEY (OrderID),
FOREIGN KEY (UserID) REFERENCES User_Account(User_id)
)
ALTER TABLE OrderHeader ALTER COLUMN Discount decimal NOT NULL
ALTER TABLE OrderHeader ALTER COLUMN Tax decimal NOT NULL
CREATE FUNCTION FU_TotalDue(@OrderID INT)
RETURNS DECIMAL
AS
BEGIN
DECLARE @TotalDue DECIMAL;
SELECT @TotalDue = (TotalAmount + Tax - Discount)
FROM OrderHeader
WHERE OrderID = @OrderID
RETURN @TotalDue
END
ALTER TABLE OrderHeader
DROP COLUMN TotalDue
ALTER TABLE OrderHeader
ADD TotalDue AS (dbo.FU_TotalDue(OrderID));
DROP TABLE CreditCard
CREATE TABLE CreditCard
(
CardID BIGINT,
HolderName VARCHAR(255) NOT NULL,
ExpireDate DATE,
UserID INT NOT NULL,
PRIMARY KEY (CardID),
FOREIGN KEY (UserID) REFERENCES User_Account(User_id)
)
DROP TABLE Payment
CREATE TABLE Payment
(
PaymentID INT IDENTITY,
TotalAmount decimal(10,2) NOT NULL,
Status VARCHAR(255) NOT NULL,
PayTime TIMESTAMP,
CardID BIGINT,
OrderID INT,
PRIMARY KEY (PaymentID),
FOREIGN KEY (OrderID) REFERENCES OrderHeader(OrderID),
FOREIGN KEY (CardID) REFERENCES CreditCard(CardID)
)
ALTER TABLE Payment ALTER COLUMN OrderID INT NOT NULL
DROP FUNCTION FU_CheckTotalAmount
CREATE FUNCTION FU_CheckTotalAmount (@OrderID INT)
RETURNS BIT
AS
BEGIN
DECLARE @flag BIT = 1;
IF (
SELECT ISNULL(SUM(TotalAmount),0) FROM Payment WHERE @OrderID = OrderID
) <= (SELECT TotalDue FROM OrderHeader WHERE @OrderID = OrderID)
BEGIN
SELECT @flag = 0
END
RETURN @flag
END
ALTER TABLE Payment DROP CONSTRAINT MaxTotalAmount
ALTER TABLE Payment ADD CONSTRAINT MaxTotalAmount CHECK (dbo.FU_CheckTotalAmount(OrderID) = 0);
CREATE TABLE OrderItem
(
OrderItemID INT IDENTITY,
ItemID INT NOT NULL,
qty INT NOT NULL,
OrderID INT NOT NULL,
PRIMARY KEY (OrderItemID),
FOREIGN KEY (OrderID) REFERENCES OrderHeader(OrderID),
FOREIGN KEY (ItemID) REFERENCES Inventory_Item(item_id)
)
CREATE TABLE OrderItemHistory
(
OrderHistoryID INT IDENTITY,
OrderItemID INT NOT NULL,
qty INT NOT NULL,
Status VARCHAR(255) NOT NULL,
Note VARCHAR(255),
StatusTime TIMESTAMP,
PRIMARY KEY (OrderHistoryID),
FOREIGN KEY (OrderItemID) REFERENCES OrderItem(OrderItemID)
)
CREATE TRIGGER TR_OrderHis
ON OrderItem
AFTER INSERT
AS
BEGIN
DECLARE @OrderItemID INT
DECLARE @qty INT
SELECT @OrderItemID = OrderItemID FROM Inserted
SELECT @qty = qty FROM Inserted
INSERT INTO OrderItemHistory
(OrderItemID,qty,Status,Note)
VALUES
(@OrderItemID,@qty,'purchased','')
END
CREATE TABLE Customer_Service
(
Cus_Ser_ID int PRIMARY KEY,
User_ID int FOREIGN KEY REFERENCES User_Account(User_ID) NOT NULL,
Item_ID int FOREIGN KEY REFERENCES Inventory_Item(Item_ID) NOT NULL,
Staff_ID int FOREIGN KEY REFERENCES Staff(Staff_ID) NOT NULL,
Session_ID int FOREIGN KEY REFERENCES User_Session(Session_ID) NOT NULL,
Notes varchar(225)
)
CREATE TABLE Shipping
(
Shipping_ID int PRIMARY KEY,
OrderID int FOREIGN KEY REFERENCES OrderHeader(OrderID) NOT NULL,
Address_ID int FOREIGN KEY REFERENCES Address(Address_ID) NOT NULL,
Shipping_Method varchar NOT NULL,
Shipping_Charge decimal(5,2) NOT NULL,
Status varchar NOT NULL,
Shipping_Date date
)
CREATE TABLE Invoice
(
Invoice_ID int PRIMARY KEY,
OrderID int FOREIGN KEY REFERENCES OrderHeader(OrderID) NOT NULL,
User_ID int FOREIGN KEY REFERENCES User_Account(User_ID) NOT NULL,
Address_ID int FOREIGN KEY REFERENCES Address(Address_ID) NOT NULL,
Creation_Date date
)
CREATE TABLE Invoice_History
(
Invoice_History_ID int PRIMARY KEY,
Invoice_ID int FOREIGN KEY REFERENCES Invoice(Invoice_ID) NOT NULL,
User_ID int FOREIGN KEY REFERENCES User_Account(User_ID) NOT NULL,
Notes varchar(225),
Invoice_History_Time timestamp NOT NULL
)
CREATE TABLE Address
(
Address_ID int PRIMARY KEY,
Name varchar(45) NOT NULL,
Add1 varchar(45) NOT NULL,
Add2 varchar(45),
City varchar(45) NOT NULL,
State varchar(45) NOT NULL,
Zip int NOT NULL
);
CREATE TABLE Staff
(
Staff_ID int PRIMARY KEY,
Staff_name varchar(45) not NULL,
Department varchar(45) not NULL,
Staff_Position varchar(45) not NULL
);
CREATE TABLE Customer_Feedback
(
Cus_Fb_ID int PRIMARY KEY,
Comment VARCHAR(255),
User_ID int FOREIGN KEY REFERENCES User_Account(User_ID) NOT NULL,
Item_ID int FOREIGN KEY REFERENCES Inventory_Item(Item_ID) NOT NULL,
Response VARCHAR(45),
Session_ID int FOREIGN KEY REFERENCES User_Session(Session_ID) NOT NULL,
Staff_ID int FOREIGN KEY REFERENCES Staff(Staff_ID) NOT NULL
);
CREATE TABLE Advertising
(
Ad_ID int PRIMARY KEY,
Title VARCHAR(45) NOT NULL,
Item_ID int FOREIGN KEY REFERENCES Inventory_Item(Item_ID) NOT NULL,
Type VARCHAR(45),
Staff_ID int FOREIGN KEY REFERENCES Staff(Staff_ID) NOT NULL
);
-- Create views
DROP VIEW CustomerPurchase_vw
CREATE VIEW CustomerPurchase_vw
AS
SELECT ua.User_id UserID, user_name,ISNULL(YEAR(OrderDate),0) PurchaseYear,ISNULL(SUM(TotalDue),0) TotalDue
FROM OrderHeader o
FULL JOIN User_Account ua
ON ua.User_id = o.UserID
GROUP BY CUBE(ua.User_id),user_name,YEAR(OrderDate)
DROP VIEW ItemPurchase_vw
CREATE VIEW ItemPurchase_vw
AS
SELECT ii.item_id ItemID, item_name,price,ISNULL(YEAR(OrderDate),0) PurchaseYear,ISNULL(SUM(TotalDue),0) TotalDue
FROM OrderItem oi
FULL JOIN Inventory_Item ii
ON ii.item_id = oi.ItemID
FULL JOIN OrderHeader oh
ON oi.OrderID = oh.OrderID
GROUP BY CUBE(ii.item_id),item_name,price,YEAR(OrderDate)
--Encryption column
CREATE TABLE TempUserAccount
(
UseID int PRIMARY KEY,
UserName nvarchar(50),
UserPassword nvarchar(50),
EncUserName varbinary(200),
EncPassword varbinary(200)
);
-- Create DMK
CREATE MASTER KEY
ENCRYPTION BY PASSWORD = 'Test_Password';
-- Create certificate to protect symmetric key
CREATE CERTIFICATE Certificate01
WITH SUBJECT = 'OnlineShopping Test Certificate',
EXPIRY_DATE = '2026-10-31';
-- Create symmetric key to encrypt data
CREATE SYMMETRIC KEY SymmetricKey01
WITH ALGORITHM = AES_128
ENCRYPTION BY CERTIFICATE Certificate01;
-- Open symmetric key
OPEN SYMMETRIC KEY SymmetricKey01
DECRYPTION BY CERTIFICATE Certificate01;
/* Populate temp table with 100 encrypted names from the Person.Person table */
INSERT
INTO TempUserAccount
(
UseID,
UserName,
UserPassword,
EncUserName,
EncPassword
)
SELECT User_id, user_name, password,
EncryptByKey(Key_GUID(N'SymmetricKey01'), user_name),
EncryptByKey(Key_GUID(N'SymmetricKey01'), password)
FROM OnlineShoppingMall.dbo.User_Account
ORDER BY User_id;
-- Update the temp table with decrypted names
UPDATE TempUserAccount
SET UserName = DecryptByKey(EncUserName),
UserPassword = DecryptByKey(EncPassword);
-- Show the results
SELECT UseID,
UserName,
UserPassword,
EncUserName,
EncPassword
FROM TempUserAccount;
--Drop table
DROP TABLE TempUserAccount;
-- Close the symmetric key
CLOSE SYMMETRIC KEY SymmetricKey01;
-- Drop the symmetric key
DROP SYMMETRIC KEY SymmetricKey01;
-- Drop the certificate
DROP CERTIFICATE Certificate01;
--Drop the DMK
DROP MASTER KEY;
--Trigger on Cart_Detail for updating inventory amount
CREATE TRIGGER UpdateInventoryAmount
ON Cart_Detail
FOR INSERT, UPDATE, DELETE
AS
BEGIN
UPDATE Inventory_Item
SET qty -= (ISNULL(i.qty,0))
FROM Inventory_Item v, inserted i
WHERE v.item_id = i.item_id;
UPDATE Inventory_Item
SET qty += (ISNULL(d.qty,0))
FROM Inventory_Item v, deleted d
WHERE v.item_id = d.item_id;
END;
DROP TRIGGER UpdateInventoryAmount
select * from Shopping_Cart;
select * from Inventory_Item;
select * from Cart_Detail;