-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookShopPLSQL.sql
232 lines (203 loc) · 5.58 KB
/
bookShopPLSQL.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
--printing name and email from customers (Basic pl/sql structure)
set serveroutput on
declare cus_name customers.customer_name%type;
email varchar(50);
begin
select customer_name, email into cus_name,email from customers where customer_id = 5001;
dbms_output.put_line('Name:' || cus_name || ' Email:'||email);
end;
/
--use of rowtype
set serveroutput on
declare orders_row ORDERS%rowtype;
begin
select order_id into orders_row.order_id from orders where order_id = 7001;
dbms_output.put_line('id:' || orders_row.order_id );
end;
/
--insert or set default value
set serveroutput on
declare
order_id ORDERS.ORDER_ID%type:=7010;
product_id ORDERS.PRODUCT_ID%type := 3001;
customer_id Orders.Customer_id%type := 5001;
quantity ORDERS.QUANTITY%type := 2;
begin
INSERT INTO orders (order_id, product_id, customer_id, quantity) VALUES (order_id, product_id, customer_id, quantity);
end;
/
--cursor
set serveroutput on
declare
cursor c is select * from customers;
c_row customers%rowtype;
begin
open c;
fetch c into c_row.customer_id,c_row.customer_name,c_row.address,c_row.phone,c_row.email;
dbms_output.put_line('id:'|| c_row.customer_id || ' name:'||c_row.customer_name);
close c;
end;
/
--cursor with loop
set serveroutput on
declare
cursor c is select * from customers;
c_row customers%rowtype;
begin
open c;
fetch c into c_row.customer_id,c_row.customer_name,c_row.address,c_row.phone,c_row.email;
while c%found loop
dbms_output.put_line('id:'|| c_row.customer_id || ' name:'||c_row.customer_name);
dbms_output.put_line('row_count: ' || c%rowcount);
fetch c into c_row.customer_id,c_row.customer_name,c_row.address,c_row.phone,c_row.email;
end loop;
close c;
end;
/
--array
set serveroutput on
declare
i number;
c_name customers.customer_name%type;
TYPE NAMEARRAY is VARRAY(5) of CUSTOMERS.CUSTOMER_NAME%type;
myarray NAMEARRAY:= NAMEARRAY();
begin
i:= 1;
for x in 5001..5005
loop
select customer_name into c_name from customers where customer_id = x;
myarray.EXTEND();
myarray(i):= c_name;
i:=i+1;
end loop;
i:=1;
while i<=myarray.count
loop
dbms_output.put_line(myarray(i));
i:=i+1;
end loop;
end;
/
--array with extend function
set serveroutput on
declare
i number;
c_name customers.customer_name%type;
TYPE NAMEARRAY is VARRAY(5) of CUSTOMERS.CUSTOMER_NAME%type;
myarray NAMEARRAY:= NAMEARRAY('Customer 1','Customer 2','Customer 3','Customer 4','Customer 5');
begin
i:= 1;
for x in 5001..5005
loop
select customer_name into c_name from customers where customer_id = x;
myarray(i):= c_name;
i:=i+1;
end loop;
i:=1;
while i<=myarray.count
loop
dbms_output.put_line(myarray(i));
i:=i+1;
end loop;
end;
/
--if/else
set serveroutput on;
declare
i number;
product_id PRODUCTS.PRODUCT_ID%type;
seller_id PRODUCTS.SELLER_ID%type;
begin
i:=1;
for x in 3001..3015
loop
select product_id,seller_id into product_id,seller_id from products where product_id = x;
IF seller_id = 1001 then
dbms_output.put_line(product_id || ' sold by ABC Books');
ELSIF seller_id = 1002 then
dbms_output.put_line(product_id || ' sold by XYZ Bookstore');
else
dbms_output.put_line(product_id || ' sold by other sellers');
end if;
end loop;
end;
/
--simple procedure for printing customer name according to customer_id (IN)
create or replace procedure proc(var1 in customers.customer_id%type) is
var2 varchar2(30);
begin
select customer_name into var2 from customers where customer_id = var1;
dbms_output.put_line('customer name:'|| var2);
end;
/
set serveroutput on
declare
begin
proc(5005);
end;
/
--in out
create or replace procedure proc2(varId in customers.customer_id%type, varName out customers.customer_name%type,varEmail out customers.email%type) is
begin
select customer_name,email into varName,varEmail from customers where customer_id = varId;
end;
/
set serveroutput on
declare
cName Customers.customer_name%type;
cEmail CUSTOMERS.EMAIL%type;
begin
proc2(5005,CName,CEmail);
dbms_output.put_line('Name:'||cName || ' Email: '||cEmail);
end;
/
--function (take input of a customer id and print his email)
create or replace function func(varId in Customers.customer_id%type) return varchar as
cName customers.customer_NAME%type;
begin
select customer_NAME into cName from customers where customer_id = varId;
return cName;
end;
/
set serveroutput on
declare
cName customers.customer_name%type;
cEmail customers.Email%type;
begin
cName:=func(5001);
select email into cEmail from customers where customer_name = cName;
dbms_output.put_line('Email: '||cEmail);
end;
/
--Trigger
-- Creating trigger to update total_price
CREATE OR REPLACE TRIGGER update_total_price
BEFORE INSERT OR UPDATE ON orders
FOR EACH ROW
BEGIN
SELECT price * :NEW.quantity
INTO :NEW.total_price
FROM products
WHERE product_id = :NEW.product_id;
END;
/
-- Checking Availabity when placing order
CREATE OR REPLACE TRIGGER place_order
BEFORE INSERT ON orders
FOR EACH ROW
DECLARE
product_quantity NUMBER;
BEGIN
SELECT count
INTO product_quantity
FROM products
WHERE product_id = :NEW.product_id;
IF product_quantity < :NEW.quantity THEN
RAISE_APPLICATION_ERROR(-20001, 'Cannot place order. Stock out.');
ELSE
UPDATE products
SET count = count - :NEW.quantity
WHERE product_id = :NEW.product_id;
END IF;
END;
/