-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazon_Procedures.sql
More file actions
413 lines (348 loc) · 10.1 KB
/
Copy pathAmazon_Procedures.sql
File metadata and controls
413 lines (348 loc) · 10.1 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
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
408
409
410
411
412
413
-- Stored Procedure for registering BUYER
set serveroutput on;
CREATE OR REPLACE PROCEDURE REGISTERBUYER (
u_userid user1.userid%TYPE,
u_userType user1.user_type%TYPE,
u_dateCreated user1.datecreated%TYPE,
b_id BUYER.BUYERID%TYPE,
b_membership buyer.membership%TYPE,
b_buyerFirstName buyer.buyerfirstname%TYPE,
b_buyerLastName buyer.buyerlastname%TYPE,
b_phoneNumber buyer.phonenumber%TYPE,
b_email buyer.email%TYPE)
IS
BEGIN
INSERT INTO USER1 (USERID, USER_TYPE, DATECREATED)
VALUES (u_userid, u_userType, u_dateCreated);
INSERT INTO BUYER (BUYERID, USERID, MEMBERSHIP, BUYERFIRSTNAME, BUYERLASTNAME, PHONENUMBER, EMAIL)
VALUES (b_id, u_userid, b_membership, b_buyerFirstName, b_buyerLastName, b_phoneNumber, b_email);
COMMIT;
END;
/
CREATE OR REPLACE PROCEDURE REGISTERSELLER (
u_userid user1.userid%TYPE,
u_userType user1.user_type%TYPE,
u_dateCreated user1.datecreated%TYPE,
s_id seller.sellerid%TYPE,
s_companyName seller.companyname%TYPE,
s_sellerFirstName seller.sellerfirstname%TYPE,
s_sellerLastName seller.sellerlastname%TYPE,
s_phoneNumber seller.phonenumber%TYPE,
s_email seller.email%TYPE,
s_logo seller.logo%TYPE)
IS
BEGIN
INSERT INTO USER1 (USERID, USER_TYPE, DATECREATED)
VALUES (u_userid, u_userType, u_dateCreated);
INSERT INTO SELLER (SELLERID, USERID, companyname, sellerfirstname, sellerlastname, phonenumber, email, logo)
VALUES (s_id, u_userid, s_companyName, s_sellerFirstName, s_sellerLastName, s_phoneNumber, s_email, s_logo);
COMMIT;
END;
/
BEGIN
REGISTERBUYER(1, 'b', SYSDATE, 12345678, 'Y', 'JANE', 'DOE', '123-456-7890', 'jdoe@gmail.com');
END;
BEGIN
REGISTERSELLER(456, 'S', SYSDATE-1, 2, 'AMAZON', 'ANANT', 'PRAKASH', '469-350-1307', 'ruchi@gmail.com', NULL);
END;
select * from user1;
SELECT * FROM BUYER;
select * from SELLER;
-- Stored Procedure for registering SELLER
-- Stored Procedure for storing an order
CREATE OR REPLACE PROCEDURE StoreProduct (
p_productID product.productid%TYPE,
p_department product.department%TYPE,
p_productName product.productname%TYPE,
p_unitPrice product.unitprice%TYPE,
p_productDesc product.productdescription%TYPE,
p_unitsInStock product.unitsinstock%TYPE,
p_unitsInOrder product.unitsinorder%TYPE,
p_itemPicture product.itempicture%TYPE)
IS
BEGIN
INSERT INTO PRODUCT(PRODUCTID, DEPARTMENT, PRODUCTNAME, UNITPRICE, PRODUCTDESCRIPTION, UNITSINSTOCK, UNITSINORDER, ITEMPICTURE)
VALUES (p_productID, p_department, p_productName, p_unitPrice, p_productDesc, p_unitsInStock, p_unitsInOrder, p_itemPicture);
COMMIT;
END;
/
BEGIN
StoreProduct(1, 'Electronics', 'Tablet', 5000, 'tablet', 100, 0, NULL);
StoreProduct(2, 'Sports', 'Football', 100, 'Signed by Messi', 100, 0, NULL);
StoreProduct(3, 'Kitchen', 'SilverWare', 50, 'kitchen ware', 100, 0, NULL);
StoreProduct(4, 'Education', 'DB Design Book', 100, 'Study Well', 100, 0, NULL);
END;
/
CREATE OR REPLACE PROCEDURE ADD_ADDRESS (
u_AddressID Address.AddressID%TYPE,
u_UserID Address.UserID%TYPE,
u_Address_Type Address.Address_Type%TYPE,
u_AddressLine1 Address.AddressLine1%Type,
u_City Address.City%TYPE,
u_Province Address.Province%TYPE,
u_Country Address.Country%TYPE,
u_PostalCode Address.PostalCode%TYPE
)
IS
BEGIN
INSERT INTO Address (AddressID ,UserID ,Address_Type ,AddressLine1 ,City ,Province ,Country ,PostalCode)
VALUES (u_AddressID, u_UserID, u_Address_Type, u_AddressLine1, u_City, u_Province, u_Country, u_PostalCode);
COMMIT;
END;
/
BEGIN
ADD_ADDRESS('H1', 1, 'Home', '7815 McCallum Blvd.', 'Dallas', 'Texas', 'US', 75252);
ADD_ADDRESS('W1', 1, 'Work', '2.104, ECSS, UT Dallas', 'Dallas', 'Texas', 'US', 75252);
END;
CREATE OR REPLACE PROCEDURE Insert_into_Cart (
s_ShoppingCartID Cart_Product.ShoppingCartID%TYPE ,
s_ProductID Cart_Product.ProductID%TYPE,
s_USerID User1.UserID%TYPE
)
IS
BEGIN
DECLARE
counter INT;
pcount VARCHAR(8);
BEGIN
Select count(*) into pcount from Product where ProductID=s_ProductID;
if pcount>0
then
Select count(*) into counter from Cart_Product where ShoppingCartID = s_ShoppingCartID;
INSERT INTO Cart_Product(ShoppingCartID,ProductID)
VALUES (s_ShoppingCartID, s_ProductID);
if counter = 0
THEN
INSERT into Shopping_Cart(ShoppingCartID ,UserID ,Quantity)
VALUES (s_ShoppingCartID,s_USerID,counter+1);
ELSE
UPDATE Shopping_Cart SET Quantity = counter+1 where ShoppingCartID = s_ShoppingCartID;
END IF;
COMMIT;
else
Raise_Application_Error(-20005, 'No such Product available');
END IF;
END;
END;
/
BEGIN
Insert_into_Cart('SH1', 1, 1);
Insert_into_Cart('SH1', 2, 1);
END;
CREATE OR REPLACE PROCEDURE Quantity_IN_ShoppingCart (
s_ShoppingCartID Cart_Product.ShoppingCartID%TYPE ,
qty out INT
)
IS
BEGIN
Declare counter INT;
BEGIN
Select count(*) into counter from Cart_Product where ShoppingCartID = s_ShoppingCartID;
if counter = 0
THEN
Raise_Application_Error(-20000, 'No item in Shopping Cart');
ELSE
qty:=counter;
END IF;
END;
END;
/
set serveroutput on;
DECLARE
qty INT;
BEGIN
Quantity_IN_ShoppingCart('SH1',qty);
dbms_output.put_line('Result is '||qty);
END;
CREATE OR REPLACE PROCEDURE Products_IN_ShoppingCart (
s_ShoppingCartID IN Cart_Product.ShoppingCartID%TYPE ,
productList out SYS_REFCURSOR
)
IS
BEGIN
OPEN productList FOR
Select ProductID from Cart_Product where ShoppingCartID = s_ShoppingCartID;
END;
/
set serveroutput on;
DECLARE
cur SYS_REFCURSOR;
product VARCHAR(8);
BEGIN
Products_IN_ShoppingCart('SH1',cur);
LOOP
FETCH cur into product;
EXIT WHEN cur%NOTFOUND;
dbms_output.put_line('Result is '||product);
END LOOP;
CLOSE cur;
END;
CREATE OR REPLACE PROCEDURE PriceEngine (
s_ShoppingCartID Cart_Product.ShoppingCartID%TYPE ,
price out DECIMAL
)
IS
BEGIN
DECLARE
prodList SYS_REFCURSOR;
pr DECIMAL;
product VARCHAR(8);
p DECIMAL;
BEGIN
Products_IN_ShoppingCart(s_ShoppingCartID, prodList);
pr:=0;
LOOP
FETCH prodList into product;
EXIT WHEN prodList%NOTFOUND;
p:=0;
Select UnitPrice into p from Product where ProductID=product;
pr:= pr+p;
dbms_output.put_line(pr || ' ' || p);
END LOOP;
CLOSE prodList;
price:=pr;
END;
END;
/
set serveroutput on;
DECLARE
price DECIMAL;
BEGIN
PriceEngine('SH1',price);
dbms_output.put_line('Order price is '|| price);
END;
CREATE OR REPLACE Procedure Insert_Order_Product(
OrID Orders.OrderID%TYPE,
ListOfProducts SYS_REFCURSOR)
IS
BEGIN
DECLARE
product VARCHAR(8);
BEGIN
LOOP
FETCH ListOfProducts into product;
EXIT WHEN ListOfProducts%NOTFOUND;
Insert into order_product(OrderID,ProductID)
VALUES(OrID,product);
END LOOP;
END;
END;
--Orders
CREATE OR REPLACE PROCEDURE Place_Order (
OrderID Orders.OrderID%TYPE,
UserID Orders.UserID%TYPE,
ShipperID Orders.ShipperID%TYPE
)
IS
BEGIN
DECLARE
s_cart_id VARCHAR(8);
qty INT;
price DECIMAL;
Tax DECIMAL;
dt DATE;
productList SYS_REFCURSOR;
BEGIN
s_cart_id:='';
Select Distinct ShoppingCartID into s_cart_id from Shopping_Cart s where s.UserID = UserID;
Products_IN_ShoppingCart ( s_cart_id,productList );
Insert_Order_Product(OrderID , productList);
Quantity_IN_ShoppingCart(s_cart_id,qty);
PriceEngine(s_cart_id,price);
Tax:=0.1*price;
dt := '';
Select SYSDATE into dt from dual;
INSERT into Orders(OrderID ,
UserID ,
ShipperID ,
OrderDate ,
RequiredDate ,
Tax,
TransactionStatus ,
PaymentDate ,
ItemQuantity ,
Price)
VALUES (OrderID ,
UserID ,
ShipperID ,
dt ,
dt+7,
Tax,
'PENDING' ,
dt,
qty ,
price);
COMMIT;
END;
END;
/
BEGIN
Place_Order(999,1,'BlueDart');
END;
CREATE OR REPLACE PROCEDURE CREATE_INVOICE(
p_orderID payment.orderid%TYPE,
invoice_id Invoice.InvoiceID%TYPE,
invoice_type invoice.invoice_type%TYPE
)
IS
BEGIN
DECLARE
pr DECIMAL;
BEGIN
Select Price into pr from ORDERS where orderID=p_orderID;
INSERT INTO INVOICE(ORDERID, INVOICEID, INVOICE_TYPE,INVOICEAMOUNT)
VALUES(p_orderID,invoice_id,invoice_type,pr);
COMMIT;
END;
END;
CREATE OR REPLACE PROCEDURE Insert_Payment_CARD (
p_paymentID payment.paymentid%TYPE,
p_orderID payment.orderid%TYPE,
p_paymentType payment.payment_type%TYPE,
p_cardid payment_card.cardid%TYPE,
p_cardnumber payment_card.cardnumber%TYPE,
p_cardexpmonth payment_card.cardexpmonth%TYPE,
p_cardexpyear payment_card.cardexpyear%TYPE,
invoice_id Invoice.InvoiceID%TYPE,
invoice_type invoice.invoice_type%TYPE
)
IS
BEGIN
INSERT INTO Payment(paymentid, orderid, payment_type)
VALUES (p_paymentID, p_orderID, p_paymentType);
INSERT INTO PAYMENT_CARD(cardid, paymentid, cardnumber, cardexpmonth, cardexpyear)
values (p_cardid, p_paymentID, p_cardnumber, p_cardexpmonth, p_cardexpyear);
CREATE_INVOICE(p_orderID, invoice_id,invoice_type);
COMMIT;
END;
BEGIN
Insert_Payment_CARD('XYZ', 999, 'C', '343252', '34456678', '12', '2023','I123','D');
END;
-- Insert payment in payment gift card
CREATE OR REPLACE PROCEDURE Insert_Payment_GIFT_CARD (
p_paymentID payment.paymentid%TYPE,
p_orderID payment.orderid%TYPE,
p_paymentType payment.payment_type%TYPE,
p_giftcardid payment_giftcard.giftcardid%TYPE,
p_giftcardamount payment_giftcard.giftcardamount%TYPE,
p_giftcardnumber payment_giftcard.giftcardnumber%TYPE,
p_giftcardexpmonth payment_giftcard.giftcardexpmonth%TYPE,
p_giftcardexpyear payment_giftcard.giftcardexpyear%TYPE,
invoice_id Invoice.InvoiceID%TYPE,
invoice_type invoice.invoice_type%TYPE
)
IS
BEGIN
INSERT INTO Payment(paymentid, orderid, payment_type)
VALUES (p_paymentID, p_orderID, p_paymentType);
INSERT INTO PAYMENT_GIFTCARD(giftcardid, paymentid, giftcardamount, giftcardnumber, giftcardexpmonth, giftcardexpyear)
values (p_giftcardid, p_paymentID, p_giftcardamount, p_giftcardnumber, p_giftcardexpmonth, p_giftcardexpyear);
CREATE_INVOICE(p_orderID, invoice_id,invoice_type);
COMMIT;
END;
BEGIN
Insert_Payment_GIFT_CARD('456', '999', 'G', '76756465', '100', '332334', '12', '2020','I123','D');
END;
select * from payment;
select * from payment_card;
select * from payment_giftcard;