diff --git a/1907105DDL.sql b/1907105DDL.sql new file mode 100644 index 0000000..14d943a --- /dev/null +++ b/1907105DDL.sql @@ -0,0 +1,80 @@ +drop table managements; +drop table payment; +drop table product_availability; +drop table orders; +drop table product; +drop table buyerinfo; + + + +--Buyer_info table created +create table buyerinfo( +Buyer_ID number(30) NOT NULL CHECK (Buyer_ID >0) PRIMARY KEY, +Buyer_Name varchar(40) NOT NULL, +Buyer_Address varchar(40) NOT NULL, +Buyer_email varchar(40) NOT NULL, +Buyer_phone varchar(11) NOT NULL CHECK(Length(Buyer_phone)=11) +); +--Product table created +create table Product( +Product_ID varchar(40) NOT NULL Primary key, +Product_type varchar(40), +Product_Name varchar(40), +Price DECIMAL (10,2) NOT NULL CHECK(Price>=0), +Product_Brand varchar(40) +); +--Order table created +create table orders( +Buyer_ID number(30) NOT NULL CHECK (Buyer_ID >0), +Order_ID number(30) NOT NULL CHECK (Order_ID >0), +Product_ID varchar(40) NOT NULL , +Quantity INT DEFAULT 1 NOT NULL Check (Quantity>=1) , +Product_status varchar(40), +order_date date NOT NULL, +Primary key (order_id), +foreign key (Buyer_ID) references buyerinfo(Buyer_ID) on delete cascade, +foreign key (Product_ID) references product(Product_ID) on delete cascade +); +--Product Availability table created +create table product_availability( +Product_ID varchar(40) NOT NULL, +Product_availabilityID varchar(40) NOT NULL Check(Product_availabilityID in ('Available','Unavailable')) , +Product_details varchar (120), +Primary Key (Product_ID), +foreign key (Product_ID) references product(Product_ID) on delete cascade +); +--Payment table created +create table payment( +order_id number(30) NOT NULL CHECK (Order_ID >0), +Payment_ID varchar(40) NOT NULL , +Payment_Type varchar(40) NOT NULL Check(Payment_Type in ('Credit Card','Cash','Mastercard','Visa Card','Mobile Banking','Debit Card')), +Payment_amount DECIMAL (10,2) DEFAULT 0 CHECK(Payment_amount>=0) , +Primary key(ORDER_ID,Payment_ID), +foreign key (order_id) references orders(order_id) on delete cascade +); +--Managements table created +create table managements( +Salesman_name varchar(40), +Buyer_ID number(30) NOT NULL CHECK (Buyer_ID >0), +order_id number(30) NOT NULL CHECK (Order_ID >0), +Payment_ID varchar(40) NOT NULL , +Payment_Date date , +foreign key (buyer_ID) references buyerinfo(buyer_id) on delete cascade, +foreign key (order_ID,Payment_ID) references Payment(order_ID,Payment_ID) on delete cascade +); + +--Altering-adding managers column to existing managements table +alter table managements add managers char(20); +describe managements; + +--Altering-modifying managers column from char to varchar on existing managements table +alter table managements modify managers varchar(40); +describe managements; + +--Altering-renaming managers column to managers_name column on existing managements table +alter table managements rename column managers to managers_name; +describe managements; + +--Altering-dropping managers column from existing managements table +alter table managements drop column managers_name; +describe managements; diff --git a/1907105DML.sql b/1907105DML.sql new file mode 100644 index 0000000..4e898eb --- /dev/null +++ b/1907105DML.sql @@ -0,0 +1,216 @@ +--inserting value into buyerinfo table +insert into buyerinfo (buyer_id,buyer_name,buyer_address,buyer_email,buyer_phone) values (1,'Soummo','Mymensingh','soummo@gmail.com','01999934999'); +insert into buyerinfo (buyer_id,buyer_name,buyer_address,buyer_email,buyer_phone) values (2,'Joy','Dhaka','joy@gmail.com','01999999119'); +insert into buyerinfo (buyer_id,buyer_name,buyer_address,buyer_email,buyer_phone) values (3,'Nihal','Rajshahi','nihal@gmail.com','01923999999'); +insert into buyerinfo (buyer_id,buyer_name,buyer_address,buyer_email,buyer_phone) values (4,'Asfaq','Rajshahi','ashfaq@gmail.com','01995699999'); +insert into buyerinfo (buyer_id,buyer_name,buyer_address,buyer_email,buyer_phone) values (5,'Tomal','Dhaka','tomal@gmail.com','01999999969'); +insert into buyerinfo (buyer_id,buyer_name,buyer_address,buyer_email,buyer_phone) values (6,'Kazi','Dhaka','kazi@gmail.com','01999999910'); +insert into buyerinfo (buyer_id,buyer_name,buyer_address,buyer_email,buyer_phone) values (7,'Mahdi','Narshindi','mahdi@gmail.com','01799999999'); +insert into buyerinfo (buyer_id,buyer_name,buyer_address,buyer_email,buyer_phone) values (8,'Irham','Dhaka','irham@gmail.com','01899998999'); +insert into buyerinfo (buyer_id,buyer_name,buyer_address,buyer_email,buyer_phone) values (9,'Hrithik','Tangail','hrithik@gmail.com','01945699999'); +insert into buyerinfo (buyer_id,buyer_name,buyer_address,buyer_email,buyer_phone) values (10,'Rahim','Naraynganj','rahim@gmail.com','01999966999'); +insert into buyerinfo (buyer_id,buyer_name,buyer_address,buyer_email,buyer_phone) values (11,'Sondhi','Naraynganj','sondhi@gmail.com','01999699899'); + + +select * from buyerinfo; + +--inserting value into product table +insert into Product (product_id,product_type,product_name,price,product_brand) values ('S101','Monitor', 'Samsung Monitor',599.20,'Samsung'); +insert into Product (product_id,product_type,product_name,price,product_brand) values ('S102','Keyboard', 'Samsung Keyboard',100.99,'Samsung'); +insert into Product (product_id,product_type,product_name,price,product_brand) values ('S103','GPU', 'MSI GPU',2000.2,'MSI'); +insert into Product (product_id,product_type,product_name,price,product_brand) values ('S104','RAM', 'Keycheron RAM',111.11,'MSI'); +insert into Product (product_id,product_type,product_name,price,product_brand) values ('S105','HDD', 'Gigabyte HDD',222.21,'Gigabyte'); +insert into Product (product_id,product_type,product_name,price,product_brand) values ('S106','SSD', 'Intel SSD',1000.5,'Intel'); +insert into Product (product_id,product_type,product_name,price,product_brand) values ('S107','Headphone', 'Samsung Headphone',800,'Samsung'); +insert into Product (product_id,product_type,product_name,price,product_brand) values ('S108','Mouse', 'Samsung Mouse',121.2,'Samsung'); +insert into Product (product_id,product_type,product_name,price,product_brand) values ('S109','Casing', 'Samsung Casing',799.99,'Samsung'); +insert into Product (product_id,product_type,product_name,price,product_brand) values ('S110','Motherboard', 'Samsung Motherboard',3000,'Samsung'); + + +select * from product; + +--inserting value into orders table +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(1,100,'S101',2,'New',date '2006-02-1'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(1,101,'S102',4,'New',date '2009-03-2'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(1,102,'S103',5,'New',date '2007-04-1'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(2,200,'S104',1,'New',date '2004-05-23'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(2,201,'S105',1,'New',date '2000-06-26'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(3,301,'S103',1,'New',date '2011-07-12'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(4,401,'S104',1,'New',date '2023-08-15'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(5,501,'S105',1,'New',date '2021-09-27'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(6,601,'S106',7,'New',date '2014-02-28'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(7,701,'S107',2,'New',date '2012-03-10'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(8,801,'S108',1,'New',date '2010-05-20'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(9,901,'S109',3,'New',date '2001-10-11'); +insert into orders (buyer_id,order_id,product_id,quantity,product_status,order_date) values(10,1001,'S110',1,'New',date '2001-08-06'); + + +select * from orders; + + +--inserting value into product_availability table +insert into product_availability (product_id,product_availabilityID,product_details) values ('S101','Available','Its a new, top of the line product'); +insert into product_availability (product_id,product_availabilityID,product_details) values ('S102','Unavailable','Its a refurbished product'); +insert into product_availability (product_id,product_availabilityID,product_details) values ('S103','Available','Its a well review product'); +insert into product_availability (product_id,product_availabilityID,product_details) values ('S104','Available','Its a cheap but well received product'); +insert into product_availability (product_id,product_availabilityID,product_details) values ('S105','Available','Its a useful product'); +insert into product_availability (product_id,product_availabilityID,product_details) values ('S106','Available','Its a powerful product'); +insert into product_availability (product_id,product_availabilityID,product_details) values ('S107','Available','Its a brand new product'); +insert into product_availability (product_id,product_availabilityID,product_details) values ('S108','Available','Its a pricey product'); +insert into product_availability (product_id,product_availabilityID,product_details) values ('S109','Available','Its a day to day product'); +insert into product_availability (product_id,product_availabilityID,product_details) values ('S110','Available','Its a good product'); + + +select * from product_availability; + +--inserting value into payment table and updating payment_amount option based on price +insert into payment (order_id,payment_id,payment_type,payment_amount) values (100,'Soummo1907101','Cash',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=100; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (101,'Soummo1907101','Cash',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=101; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (102,'Soummo1907101','Cash',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=102; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (200,'Soummo1907102','Cash',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=103; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (201,'Soummo1907102','Cash',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=201; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (301,'Soummo1907103','Cash',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=301; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (401,'Soummo1907104','Cash',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=401; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (501,'Soummo1907105','Cash',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=501; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (601,'Soummo1907106','Cash',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=601; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (701,'Soummo1907107','Visa Card',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=701; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (801,'Soummo1907108','Mastercard',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=801; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (901,'Soummo1907109','Credit Card',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=901; +insert into payment (order_id,payment_id,payment_type,payment_amount) values (1001,'Soummo1907110','Mobile Banking',0); +update payment set payment_amount=(select (select price from product where product.product_id=ORDERS.PRODUCT_ID)*quantity from orders where orders.order_id=payment.order_id) where order_id=1001; + +select * from payment; + + + + +--inserting value into managements table +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Rahim',1,100,'Soummo1907101',date '2023-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Rahim',1,101,'Soummo1907101',date '2022-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Karim',1,102,'Soummo1907101',date '2020-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Karim',2,200,'Soummo1907102',date '2021-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Karim',2,201,'Soummo1907102',date '2014-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Karim',3,301,'Soummo1907103',date '2019-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Karim',4,401,'Soummo1907104',date '2011-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Karim',5,501,'Soummo1907105',date '2003-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Karim',6,601,'Soummo1907106',date '2009-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Karim',7,701,'Soummo1907107',date '2006-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Karim',8,801,'Soummo1907108',date '2004-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Karim',9,901,'Soummo1907109',date '2002-10-11'); +insert into managements (salesman_name,buyer_id,order_id,payment_id,payment_date) values ('Karim',10,1001,'Soummo1907110',date '2002-10-11'); +select * from managements; + +--displaying all info from buyerinfo table using ID +select * from buyerinfo where buyer_id=6; + + +--displaying all info from orders table using ID which was derived fron buyer_name from buyerinfo table +select * from orders where buyer_id=(select buyer_id from buyerinfo where buyer_name='Joy'); + +--updating and then viewing payement type by using buyer_name +update payment set payment_type='Debit Card' where order_id=(select order_id from orders where buyer_id=(select buyer_id from buyerinfo where buyer_name='Kazi') ); +select * from payment where order_id=(select order_id from orders where buyer_id=(select buyer_id from buyerinfo where buyer_name='Kazi') ); + +--inserting then deleting a value +insert into buyerinfo values (12,'Tuli','Naraynganj','l@gmail.com','01999999999'); +delete from buyerinfo where buyer_name='Tuli'; +select * from buyerinfo; + +--Various types of string comparisons +select * from PRODUCT where product_brand like 'S%' union select * from product where product_brand like '%e'; + +select * from buyerinfo where buyer_name like 'K%' union select * from buyerinfo where buyer_name like '%i'; +select * from buyerinfo where buyer_name like 'K%' or buyer_name like 'M%' or buyer_name like 'J%' ; + +select * from buyerinfo where buyer_name like 'K%' and buyer_name like '%i'; +select * from buyerinfo where buyer_name like 'K%' intersect select * from buyerinfo where buyer_name like '%i'; + +select * from buyerinfo where buyer_name like '%i' AND not buyer_name LIKE'M%'; +select * from buyerinfo where buyer_name like '%i' minus select * from buyerinfo where buyer_name like 'M%'; + +select * from product where product_brand like 'Gigabyte'; + +select * from buyerinfo where buyer_name like '_____'; --5 char len + +--distinct buyer_id getting from orders table +select Distinct (buyer_id)from orders; + +--comaparison in where clause +select * from payment where payment_amount>1000; + +select * from payment where payment_amount between 1000 and 2000.5; + +select * from payment where payment_amount in (3000,7003.5); + +--product in descending order of price +select * from product order by price desc; + +--counting +select count(buyer_id) as total_buyer_with_duplication from orders; +select count (Distinct ( buyer_id ) ) as total_buyer_without_duplication from orders; +--counting and group by +select salesman_name,count(buyer_id) as total_customer_per_salesman_with_duplicate from managements group by salesman_name; +select salesman_name,count(Distinct(buyer_id)) as total_customer_per_salesman_without_duplicate from managements group by salesman_name; + +--Total money spent grouped by Buyer +select buyer_id,(select buyer_name from buyerinfo where buyerinfo.buyer_id=orders.buyer_id),sum ((select payment_amount from payment where payment.order_id=orders.Order_id)) as Total_Money_Spent from orders group by buyer_id; +--Average money spent per order and grouped by Buyer +select buyer_id,(select buyer_name from buyerinfo where buyerinfo.buyer_id=orders.buyer_id),avg ((select payment_amount from payment where payment.order_id=orders.Order_id)) as Total_Money_Spent from orders group by buyer_id; +--product brands which has less then 6 products +select product_brand,count(product_brand) from product group by product_brand having count(product_brand)<6; + +--join +select * from buyerinfo natural join orders ; +select * from buyerinfo natural join orders where buyer_id=1; +select * from buyerinfo join orders on buyerinfo.buyer_id= orders.BUYER_ID ; +select * from buyerinfo join orders using(buyer_id) ; +select * from buyerinfo join orders on buyerinfo.buyer_id= orders.BUYER_ID and orders.buyer_id=1 ; +select * from buyerinfo left outer join orders using(buyer_id) ; +select * from buyerinfo right outer join orders using(buyer_id) ; +select * from buyerinfo inner join orders using(buyer_id) ; +select * from buyerinfo full outer join orders using(buyer_id) ; +select * from buyerinfo full outer join orders on BUYERINFO.BUYER_ID=orders.buyer_id; + +--sum of price of every products grouped by product brands +select product_brand,sum(price) from product group by product_brand; +--some +select * from buyerinfo where buyer_id> some(select buyer_id from buyerinfo where buyer_id>=5); +--all +select * from buyerinfo where buyer_id> all(select buyer_id from buyerinfo where buyer_id<=5); + + +insert into Product (product_id,product_type,product_name,price,product_brand) values ('S111','Monitor', 'MSI Monitor',999.20,'MSI'); + +--exist (all samsung brands ) +select * from product p where exists(select product_name from product s where p.product_id=s.product_id and product_brand='Samsung' ); +--not exist(non samsung brands) +select * from product p where not exists(select product_name from product s where p.product_id=s.product_id and product_brand='Samsung' ); + +--max price of a product grouped by brand +select product_brand,max(price) from product group by product_brand; + +--creating view as samsung products to store all samsung brand products and then viewing and dropping the view +create view Samsung_Products as select * from product where product_brand='Samsung'; +select * from samsung_products; +drop view samsung_products; + +--distinct name of three buyers with highest price order +select distinct b.buyer_name +from payment m +join managements p ON m.payment_id = p.payment_id +join buyerinfo b ON b.buyer_id = p.buyer_id +order by m.payment_amount DESC +fetch first 3 rows only; + diff --git a/1907105TRIGGER.sql b/1907105TRIGGER.sql new file mode 100644 index 0000000..ae284b3 --- /dev/null +++ b/1907105TRIGGER.sql @@ -0,0 +1,32 @@ + + +CREATE OR REPLACE TRIGGER trig +BEFORE INSERT ON orders +FOR EACH ROW +BEGIN + + DBMS_OUTPUT.PUT_LINE('New order is being inserted'); + + UPDATE product SET price = price * 1.1 WHERE product_id = :NEW.product_id; +END; +/ +INSERT INTO orders (Buyer_ID, Order_ID, Product_ID, Quantity, Product_status, order_date) +VALUES (1, 1, 'S101', 5, 'Pending', SYSDATE); + +CREATE OR REPLACE TRIGGER update_order_date +BEFORE INSERT ON orders +FOR EACH ROW +BEGIN + :NEW.order_date := SYSDATE; +END; +/ +CREATE OR REPLACE TRIGGER update_payments_date +BEFORE INSERT ON payment +FOR EACH ROW +BEGIN + :NEW.payment_date := SYSDATE; +END; +/ + + + diff --git a/1907105plsql.sql b/1907105plsql.sql new file mode 100644 index 0000000..e125a7e --- /dev/null +++ b/1907105plsql.sql @@ -0,0 +1,349 @@ +--diplay +SET SERVEROUTPUT ON + +DECLARE + buyer_id buyerinfo.buyer_id%TYPE; + buyer_name buyerinfo.buyer_NAME%TYPE; +BEGIN + SELECT buyer_id, buyer_name + INTO buyer_id, buyer_name + FROM buyerinfo + WHERE buyer_id = 7; + + DBMS_OUTPUT.put_line ( + 'BUYER_id: ' || buyer_id || ' BUYER_name: ' || BUYER_name); +END; +/ +--insert +SET SERVEROUTPUT ON + +DECLARE + buyer_id buyerinfo.buyer_id%TYPE := 14; + buyer_name buyerinfo.buyer_NAME%TYPE := 'Shreya'; + Buyer_Address buyerinfo.buyer_address%TYPE := 'Khulna'; + Buyer_email buyerinfo.buyer_email%TYPE := 'shreya@gmail.com'; + Buyer_Phone buyerinfo.buyer_phone%TYPE := '01999999999'; +BEGIN + INSERT INTO buyerinfo + VALUES (buyer_id, + buyer_name, + Buyer_Address, + Buyer_email, + Buyer_Phone); + + DBMS_OUTPUT.put_line ( + 'BUYER_id: ' + || buyer_id + || ' BUYER_name: ' + || BUYER_name + || ' Buyer_Address' + || Buyer_Address + || ' Buyer_email ' + || Buyer_email + || ' Buyer_Phone ' + || Buyer_Phone); +END; +/ + +--Row type taken values from table and stored using rowtype +SET SERVEROUTPUT ON + +DECLARE + product_row product%ROWTYPE; +BEGIN + SELECT product_id, + product_type, + product_name, + price, + product_brand + INTO product_row.product_id, + product_row.product_type, + product_row.product_name, + product_row.price, + product_row.product_brand + FROM product + WHERE product_id = 'S102'; + + DBMS_OUTPUT.put_line ( + 'Product ID: ' + || product_row.product_id + || ' Product Name: ' + || product_row.product_name + || ' TYPE: ' + || product_row.product_type + || ' Price: ' + || product_row.price + || ' Brand: ' + || product_row.product_brand); +END; +/ + +--viewing all info from buyerinfo using cursor and rowcount will give us total buyer number + +SET SERVEROUTPUT ON + +DECLARE + CURSOR info_cursor IS SELECT * FROM buyerinfo; + + buyer_row buyerinfo%ROWTYPE; +BEGIN + OPEN info_cursor; + + FETCH info_cursor + INTO buyer_row.buyer_id, + buyer_row.buyer_name, + buyer_row.buyer_address, + buyer_row.buyer_email, + buyer_row.buyer_phone; + + WHILE info_cursor%FOUND + LOOP + DBMS_OUTPUT.put_line ( + 'BUYER_id: ' + || buyer_row.buyer_id + || ' BUYER_name: ' + || buyer_row.buyer_name + || ' ADDRESS: ' + || buyer_row.buyer_address + || ' EMAIL: ' + || buyer_row.buyer_email + || ' EMAIL: ' + || buyer_row.buyer_phone); + DBMS_OUTPUT.put_line ('Total Student: ' || info_cursor%ROWCOUNT); + + FETCH info_cursor + INTO buyer_row.buyer_id, + buyer_row.buyer_name, + buyer_row.buyer_address, + buyer_row.buyer_email, + buyer_row.buyer_phone; + END LOOP; + + CLOSE info_cursor; +END; +/ + +--for loop to find sum of payment amount of same payment id +DECLARE + total_payment_amount payment.payment_amount%TYPE; +BEGIN + + FOR p IN (SELECT DISTINCT payment_id + FROM payment) + LOOP + + total_payment_amount := 0; + + + FOR a IN (SELECT payment_amount + FROM payment + WHERE payment_id = p.payment_id) + LOOP + total_payment_amount := total_payment_amount + a.payment_amount; + END LOOP; + + -- Display + DBMS_OUTPUT.put_line ( + 'Payment ID: ' + || p.payment_id + || ', Total Payment Amount: ' + || total_payment_amount); + END LOOP; +END; +/ + +--arrayextend for loop and while loop displaying all buyer name + +DECLARE + counter NUMBER; + names buyerinfo.buyer_name%TYPE; + + TYPE namesARRAY IS VARRAY (11) OF buyerinfo.buyer_name%TYPE; + + A_NAME namesARRAY := namesARRAY (); +BEGIN + counter := 1; + + FOR x IN 1 .. 11 + LOOP + SELECT buyer_name + INTO names + FROM buyerinfo + WHERE buyer_id = x; + + A_NAME.EXTEND (); + A_NAME (counter) := names; + counter := counter + 1; + END LOOP; + + counter := 1; + + WHILE counter <= A_NAME.COUNT + LOOP + DBMS_OUTPUT.PUT_LINE (A_NAME (counter)); + counter := counter + 1; + END LOOP; +END; +/ + +--same as before without extend with for loop and while loop + +DECLARE + counter NUMBER := 1; + names buyerinfo.buyer_name%TYPE; + + TYPE namesARRAY IS VARRAY (11) OF buyerinfo.buyer_name%TYPE; + + A_NAME namesARRAY + := namesARRAY ('a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i', + 'j', + 'k'); +BEGIN + counter := 1; + + FOR x IN 1 .. 11 + LOOP + SELECT buyer_name + INTO names + FROM buyerinfo + WHERE buyer_id = x; + + A_NAME (counter) := names; + counter := counter + 1; + END LOOP; + + counter := 1; + + WHILE counter <= A_NAME.COUNT + LOOP + DBMS_OUTPUT.PUT_LINE (A_NAME (counter)); + counter := counter + 1; + END LOOP; +END; +/ + +--labeling products based on price using if else +DECLARE + pname product.product_name%TYPE; + price product.price%TYPE; + plabel VARCHAR2 (20); + + CURSOR products IS SELECT product_name, price FROM product; +BEGIN + OPEN products; + + + FETCH products INTO pname, price; + + + WHILE products%FOUND + LOOP + IF price <= 1000 + THEN + plabel := 'Low Price'; + ELSIF price > 1000 AND price <= 3000 + THEN + plabel := 'Medium Price'; + ELSE + plabel := 'High Price'; + END IF; + + + DBMS_OUTPUT.put_line ( + 'Product Name: ' || pname || ', Label: ' || plabel); + + + FETCH products INTO pname, price; + END LOOP; + + + CLOSE products; +END; +/ + +--procedure to see if buyer exists +CREATE OR REPLACE PROCEDURE check_buyer_existence ( + nam IN buyerinfo.buyer_name%TYPE, + existss OUT BOOLEAN) +AS + countss INTEGER; +BEGIN + SELECT COUNT (*) + INTO countss + FROM buyerinfo + WHERE buyer_name = nam; + + IF countss > 0 + THEN + existss := TRUE; + ELSE + existss := FALSE; + END IF; +END; +/ + +DECLARE + existss BOOLEAN; +BEGIN + check_buyer_existence ('Joy', existss); + + IF existss + THEN + DBMS_OUTPUT.PUT_LINE ('Buyer exists.'); + ELSE + DBMS_OUTPUT.PUT_LINE ('Buyer does not exist.'); + END IF; +END; +/ + + +--function to find top 3 buyer based on price +CREATE OR REPLACE FUNCTION top_buyers + RETURN SYS_REFCURSOR +IS + rcursor SYS_REFCURSOR; +BEGIN + OPEN rcursor FOR + SELECT b.buyer_name + FROM ( SELECT m.buyer_id, MAX (p.payment_amount) AS max_payment + FROM managements m + JOIN payment p ON m.payment_id = p.payment_id + GROUP BY m.buyer_id + ORDER BY max_payment DESC + FETCH FIRST 3 ROWS ONLY) top_buyers + JOIN buyerinfo b ON top_buyers.buyer_id = b.buyer_id; + + RETURN rcursor; +END; +/ + +DECLARE + r SYS_REFCURSOR; + buyer_name buyerinfo.buyer_name%TYPE; +BEGIN + r := top_buyers (); + + + FETCH r INTO buyer_name; + + + WHILE r%FOUND + LOOP + DBMS_OUTPUT.PUT_LINE ('Buyer Name: ' || buyer_name); + + + FETCH r INTO buyer_name; + END LOOP; + + + CLOSE r; +END; +/