diff --git a/DDL.sql b/DDL.sql new file mode 100644 index 0000000..f6338fc --- /dev/null +++ b/DDL.sql @@ -0,0 +1,60 @@ +drop TABLE payments; +drop TABLE orders; +drop TABLE customers; +drop TABLE products; + + +CREATE TABLE customers ( + customer_id NUMBER(10) PRIMARY KEY, + name VARCHAR(100) NOT NULL, + email VARCHAR(100) NOT NULL, + phone VARCHAR(20), + address VARCHAR(200), + date_of_birth DATE +); + +CREATE TABLE products ( + product_id NUMBER(10) PRIMARY KEY, + name VARCHAR(100) NOT NULL, + description VARCHAR(1000), + price NUMBER(10,2) NOT NULL, + category VARCHAR(100) +); + +CREATE TABLE orders ( + order_id NUMBER(10) PRIMARY KEY, + customer_id NUMBER(10) REFERENCES customers(customer_id) ON DELETE CASCADE, + product_id NUMBER(10) REFERENCES products(product_id) ON DELETE CASCADE, + order_date DATE NOT NULL, + quantity NUMBER(10) NOT NULL, + status VARCHAR(20) NOT NULL +); + +CREATE TABLE payments ( + payment_id NUMBER(10) PRIMARY KEY, + order_id NUMBER(10) REFERENCES orders(order_id) ON DELETE CASCADE, + amount NUMBER(10,2) NOT NULL, + payment_date DATE NOT NULL +); + + + + + +---Add column in the table +alter table payments add Payment_type char(20); + + + +---Modify column definition in the table +alter table payments modify Payment_type varchar(10); + +---Rename the column name +alter table payments rename column Payment_type to PaymentType; + +---Drop the column from table +alter table payments drop column PaymentType; + + + + diff --git a/DML.sql b/DML.sql new file mode 100644 index 0000000..21563c4 --- /dev/null +++ b/DML.sql @@ -0,0 +1,270 @@ +INSERT INTO customers VALUES(1, 'Akif','akif@gmail.com', '1234567890', 'Khulna', TO_DATE('1990-01-01', 'YYYY-MM-DD')); +INSERT INTO customers VALUES(2, 'tahim ','tahim@gmail.com', '9876543210', 'Dhaka', TO_DATE('1985-05-10', 'YYYY-MM-DD')); +INSERT INTO customers VALUES(3, 'Rana', 'rana@gmail.com', '5558755555', 'Rangpur', TO_DATE('1992-09-15', 'YYYY-MM-DD')); +INSERT INTO customers VALUES(4, 'Swaraj Biswas', 'biswas@gmail.com', '4321111111', 'Dinajpur', TO_DATE('1990-01-01', 'YYYY-MM-DD')); +INSERT INTO customers VALUES(5, 'Rafi', 'rafi@gmail.com', '9876565656', 'Dinajpur', TO_DATE('1990-10-01', 'YYYY-MM-DD')); +INSERT INTO customers VALUES(6, 'Atik', 'atik@gmail.com', '9898787887', 'Dhaka', TO_DATE('2002-10-01', 'YYYY-MM-DD')); +INSERT INTO customers VALUES(7, 'Robied', 'robied@gmail.com', '1233214567', 'Rajshahi', TO_DATE('1994-12-01', 'YYYY-MM-DD')); +INSERT INTO customers VALUES(8, 'Arghya', 'arghya@gmail.com', '7664777832', 'Sylhet', TO_DATE('2001-01-01', 'YYYY-MM-DD')); +INSERT INTO customers VALUES(9, 'Pantha', 'pantha@gmail.com', '9080798734', 'Dhaka', TO_DATE('1998-11-01', 'YYYY-MM-DD')); +INSERT INTO customers VALUES(10, 'Arif', 'arif@gmail.com', '7654567894', 'Rajshahi', TO_DATE('2000-01-11', 'YYYY-MM-DD')); + + +INSERT INTO products VALUES(1, 'Shirt', 'cotton shirt', 19, 'Apparel'); +INSERT INTO products VALUES(2, 'Jeans', 'Stylish jeans', 49, 'Apparel'); +INSERT INTO products VALUES(3, 'Sneakers', 'Sporty sneakers', 59, 'Footwear'); +INSERT INTO products VALUES(4, 'Watch', 'Elegant wristwatch', 99.99, 'Accessories'); +INSERT INTO products VALUES(5, 'Dress', 'Fashionable dress', 79.99, 'Apparel'); +INSERT INTO products VALUES(6, 'Backpack', 'Durable backpack', 39, 'Accessories'); +INSERT INTO products VALUES(7, 'Headphones', 'Wireless headphones', 129.99, 'Electronics'); +INSERT INTO products VALUES(8, 'Skirt', 'Mini skirt', 29, 'Apparel'); +INSERT INTO products VALUES(9, 'Sunglasses', 'Stylish sunglasses', 49, 'Accessories'); +INSERT INTO products VALUES(10, 'Tennis Shoes', 'High-performance tennis shoes', 89.99, 'Footwear'); + + + + + +INSERT INTO orders VALUES(1, 1, 1, TO_DATE('2023-01-10', 'YYYY-MM-DD'), 2, 'Pending'); +INSERT INTO orders VALUES(2, 2, 3, TO_DATE('2023-02-15', 'YYYY-MM-DD'), 1, 'Shipped'); +INSERT INTO orders VALUES(3, 1, 2, TO_DATE('2023-03-02', 'YYYY-MM-DD'), 3, 'Pending'); +INSERT INTO orders VALUES(4, 3, 4, TO_DATE('2023-01-25', 'YYYY-MM-DD'), 1, 'Pending'); +INSERT INTO orders VALUES(5, 2, 1, TO_DATE('2023-02-18', 'YYYY-MM-DD'), 2, 'Shipped'); +INSERT INTO orders VALUES(6, 1, 5, TO_DATE('2023-03-10', 'YYYY-MM-DD'), 1, 'Pending'); +INSERT INTO orders VALUES(7, 3, 2, TO_DATE('2023-01-05', 'YYYY-MM-DD'), 2, 'Shipped'); +INSERT INTO orders VALUES(8, 1, 4, TO_DATE('2023-02-28', 'YYYY-MM-DD'), 1, 'Pending'); +INSERT INTO orders VALUES(9, 2, 6, TO_DATE('2023-03-05', 'YYYY-MM-DD'), 2, 'Shipped'); +INSERT INTO orders VALUES(10, 3, 3, TO_DATE('2023-01-15','YYYY-MM-DD'), 1, 'Pending'); + + + + +INSERT INTO payments VALUES(1, 1, 38, TO_DATE('2023-01-12', 'YYYY-MM-DD')); +INSERT INTO payments VALUES(2, 2,59, TO_DATE('2023-02-20', 'YYYY-MM-DD')); +INSERT INTO payments VALUES(3, 3,147, TO_DATE('2023-03-03', 'YYYY-MM-DD')); +INSERT INTO payments VALUES(4, 4,99.99, TO_DATE('2023-01-28', 'YYYY-MM-DD')); +INSERT INTO payments VALUES(5, 5,38, TO_DATE('2023-02-20', 'YYYY-MM-DD')); +INSERT INTO payments VALUES(6, 6,79.99, TO_DATE('2023-03-12', 'YYYY-MM-DD')); +INSERT INTO payments VALUES(7, 7,98, TO_DATE('2023-01-07', 'YYYY-MM-DD')); +INSERT INTO payments VALUES(8, 8, 99.99, TO_DATE('2023-03-01', 'YYYY-MM-DD')); +INSERT INTO payments VALUES(9, 9,78, TO_DATE('2023-03-08', 'YYYY-MM-DD')); +INSERT INTO payments VALUES(10, 10, 59, TO_DATE('2023-01-17', 'YYYY-MM-DD')); + + + + +---Displaying table data using SELECT command +select * from payments +WHERE amount>50; + + +alter table orders add total_revenue NUMBER(10,2); + +---Updating the data in a table +update payments set amount=100 where amount=99.99; + +---Deleting row from a table +INSERT INTO orders VALUES(30, 3, 3, TO_DATE('2023-01-15','YYYY-MM-DD'), 10, 'Pending'); +INSERT INTO payments VALUES(15, 30,380, TO_DATE('2023-12-20', 'YYYY-MM-DD')); +delete from payments where payment_id=15; + + +---Save the SQL command output in csv file +SET MARKUP XML ON +SET HEADING OFF +SET PAGESIZE 0 +SET FEEDBACK OFF +SPOOL C:\Users\DELL\Desktop\DB Lab Project\database_trial\file1.csv +SELECT * +FROM payments; +SPOOL OFF + + + + +---Save the SQL command output in xml file +SET COLSEP "," +SET HEADING OFF +SET PAGESIZE 0 +SET FEEDBACK OFF +SPOOL C:\Users\DELL\Desktop\DB Lab Project\database_trial\file2.csv +SELECT * +FROM payments; +SPOOL OFF + +---Save the SQL command output in txt file +SPOOL C:\Users\DELL\Desktop\DB Lab Project\database_trial\file3.txt +SELECT * +FROM payments; +SPOOL OFF + + +-----Aggregate function + +---count how many row exist in products table +select count(*) +from customers; + + +select count(customer_id) as number_of_customers +from customers; + +select avg(amount) from payments; +select sum(amount) from payments; +select max(amount) from payments; +select min(amount) from payments; + + + +---Group by and Having + +select customer_id,sum(quantity) +from orders +group by customer_id; + + ---prompt *=*=*=*=*=*=*=*= Chaecking *=*=*=*=*=*=*=*=*=*=*=*= + +select customer_id,sum(quantity) +from orders +group by customer_id +having sum(quantity)>5; + + +---Nested subquery + +---A order of which customer + + select name + from customers + where customer_id=(select customer_id + from orders + where order_id=5); + + commit; + + + + + + +---Retrieve all customers and their associated orders: +select c.customer_id, c.name, o.order_id, o.order_date, o.status +from customers c +JOIN orders o ON c.customer_id = o.customer_id; + + + +---Get the total number of orders placed by each customer: +select c.customer_id, c.name, COUNT(o.order_id) AS order_count +from customers c +LEFT JOIN orders o ON c.customer_id = o.customer_id +GROUP BY c.customer_id, c.name; + + + +---List all products along with their categories: +select p.product_id, p.name, p.category +from products p; + + + +---Retrieve the details of a specific order and its associated customer information: +select o.order_id, o.order_date, o.status, c.name AS customer_name, c.email, c.phone +from orders o +JOIN customers c ON o.customer_id = c.customer_id +WHERE o.order_id = 1; + + + +---Calculate the total amount paid for each order: +select o.order_id, SUM(p.amount) AS total_amount_paid +from orders o +JOIN payments p ON o.order_id = p.order_id +GROUP BY o.order_id; + + + +---Retrieve all customers who have placed orders: +select c.customer_id, c.name, c.email +from customers c +WHERE EXISTS ( + select 1 + from orders o + WHERE o.customer_id = c.customer_id +); + + + +---List all orders along with the corresponding customer and product details: +select o.order_id, c.name AS customer_name, p.name AS product_name, o.quantity, o.order_date +from orders o +JOIN customers c ON o.customer_id = c.customer_id +JOIN products p ON o.product_id = p.product_id; + + + + +---Get the total revenue generated by each product category: +select p.category, SUM(o.quantity * p.price) AS revenue +from orders o +JOIN products p ON o.product_id = p.product_id +GROUP BY p.category; + + +---Retrieve the latest order placed by each customer: +select c.customer_id, c.name, o.order_id, o.order_date +from customers c +JOIN orders o ON c.customer_id = o.customer_id +WHERE o.order_date = ( + select MAX(order_date) + from orders + WHERE customer_id = c.customer_id +); + + + + +---creating Myview +drop view Myview11; + +CREATE VIEW Myview11 AS +select c.customer_id,c.name AS customer_name,c.email,c.phone,c.address,c.date_of_birth, + p.product_id,p.name AS product_name,p.description,p.price,p.category, + o.order_id,o.order_date,o.quantity,o.status, + pm.payment_id,pm.amount,pm.payment_date +from customers c + JOIN orders o ON c.customer_id = o.customer_id + JOIN products p ON o.product_id = p.product_id + LEFT JOIN payments pm ON o.order_id = pm.order_id; + + +---select * from Myview11; + +---payments done between dates + +SELECT customer_name, product_name, amount, payment_date +FROM Myview11 +WHERE payment_date >= TO_DATE('2023-01-01', 'YYYY-MM-DD') +AND payment_date <= TO_DATE('2023-12-31', 'YYYY-MM-DD'); + +---show shiped items +SELECT order_id, status +FROM orders +WHERE status LIKE '%shipped%'; + + +---show products started with S +select name,price +from products +WHERE name LIKE 'S%'; + +---second highest priced product +select name,price from +( select + name,price,ROW_NUMBER() OVER(ORDER BY price DESC) AS rrow + from products) +WHERE rrow=2; + +commit + + diff --git a/PL_SQL.sql b/PL_SQL.sql new file mode 100644 index 0000000..242fecc --- /dev/null +++ b/PL_SQL.sql @@ -0,0 +1,254 @@ +set pagesize 100 +set linesize 200 + + +---PL SQL + +---PL/SQL block to calculate and update the total amount paid for each order: +DECLARE + v_order_id orders.order_id%TYPE; + v_total_amount NUMBER(10, 2); +BEGIN + FOR rec IN (select order_id from orders) + LOOP + v_order_id := rec.order_id; + + select SUM(amount) INTO v_total_amount + from payments + WHERE order_id = v_order_id; + + UPDATE payments + SET amount= v_total_amount + WHERE order_id = v_order_id; + + COMMIT; + END LOOP; +END; +/ + + + prompt *=*=*=*=*=*=*=*= Chaecking *=*=*=*=*=*=*=*=*=*=*=*= + +--- PL/SQL block to retrieve customer details and the total number of orders placed: +set serveroutput on +DECLARE + v_customer_id customers.customer_id%TYPE; + v_customer_name customers.name%TYPE; + v_order_count NUMBER; +BEGIN + FOR rec IN (select customer_id, name from customers) + LOOP + v_customer_id := rec.customer_id; + v_customer_name := rec.name; + + select COUNT(*) INTO v_order_count + from orders + WHERE customer_id = v_customer_id; + + DBMS_OUTPUT.PUT_LINE('Customer Name: ' || v_customer_name); + DBMS_OUTPUT.PUT_LINE('Total Orders: ' || v_order_count); + DBMS_OUTPUT.PUT_LINE('-------------------------'); + END LOOP; +END; +/ + + + +---PL/SQL block to delete all orders placed by a specific customer: +set serveroutput on +DECLARE + v_customer_id customers.customer_id%TYPE:= 1; -- orders of customerid to be deleted +BEGIN + DELETE from orders + WHERE customer_id = v_customer_id; + + COMMIT; + + DBMS_OUTPUT.PUT_LINE('All orders for Customer ID ' || v_customer_id || ' have been deleted.'); +END; +/ + + + + + +--- PL/SQL block to retrieve all customers who have pending orders: +set serveroutput on +DECLARE + v_customer_id customers.customer_id%TYPE; + v_customer_name customers.name%TYPE; +BEGIN + FOR rec IN (select c.customer_id, c.name + from customers c + WHERE EXISTS ( + select 1 + from orders o + WHERE o.customer_id = c.customer_id + AND o.status = 'Pending' + )) + LOOP + v_customer_id := rec.customer_id; + v_customer_name := rec.name; + + DBMS_OUTPUT.PUT_LINE('Customer ID: ' || v_customer_id); + DBMS_OUTPUT.PUT_LINE('Customer Name: ' || v_customer_name); + DBMS_OUTPUT.PUT_LINE('-------------------------'); + END LOOP; +END; +/ + + +---PL/SQL block to calculate the total revenue generated by each customer: +SET SERVEROUTPUT ON + +DECLARE + v_customer_id customers.customer_id%TYPE; + v_customer_name customers.name%TYPE; + v_total_revenue NUMBER(10, 2); +BEGIN + FOR rec IN (SELECT DISTINCT c.customer_id, c.name + FROM customers c + JOIN orders o ON c.customer_id = o.customer_id + JOIN products p ON o.product_id = p.product_id) + LOOP + v_customer_id := rec.customer_id; + v_customer_name := rec.name; + + SELECT SUM(o.quantity * p.price) INTO v_total_revenue + FROM orders o + JOIN products p ON o.product_id = p.product_id + WHERE o.customer_id = v_customer_id; + + DBMS_OUTPUT.PUT_LINE('Customer ID: ' || v_customer_id); + DBMS_OUTPUT.PUT_LINE('Customer Name: ' || v_customer_name); + DBMS_OUTPUT.PUT_LINE('Total Revenue: $' || v_total_revenue); + DBMS_OUTPUT.PUT_LINE('-------------------------'); + END LOOP; +END; +/ + + +---output customer id and name + +set serveroutput on +declare +cursor customer_cursor is select * from customers; +CR customers%rowtype; + + +begin +open customer_cursor; + ---fetch customer_cursor into CR.customers_id,CR.customers_name,CR.faculty,CR.no_of_student; +fetch customer_cursor into CR; +while customer_cursor%found loop +dbms_output.put_line('customers_id: '||CR.customer_id|| ' customers_name: '||CR.name ); +dbms_output.put_line('Row count: '|| customer_cursor%rowcount); + + ---fetch customer_cursor into CR.customers_id,CR.customers_name,CR.faculty,CR.no_of_student; +fetch customer_cursor into CR; +end loop; +close customer_cursor; +end; +/ + + + +drop table test; +CREATE table test( +customer_id NUMBER(10) PRIMARY KEY, +name VARCHAR(100) NOT NULL, +quantity NUMBER(10) NOT NULL); + + +--- PL/SQL block to retrieve customer details and the total number of orders placed: +set serveroutput on +DECLARE + v_customer_id customers.customer_id%TYPE; + v_customer_name customers.name%TYPE; + v_order_count NUMBER; +BEGIN + FOR rec IN (select customer_id, name from customers) + LOOP + v_customer_id := rec.customer_id; + v_customer_name := rec.name; + + select COUNT(*) INTO v_order_count + from orders + WHERE customer_id = v_customer_id; + + insert into test VALUES(v_customer_id,v_customer_name,v_order_count); + + DBMS_OUTPUT.PUT_LINE('Customer Name: ' || v_customer_name); + DBMS_OUTPUT.PUT_LINE('Total Orders: ' || v_order_count); + DBMS_OUTPUT.PUT_LINE('-------------------------'); + + END LOOP; +END; +/ + + +select name,quantity from +( select + customer_id,name,quantity,ROW_NUMBER() OVER(ORDER BY quantity DESC) AS rrow + from test) +WHERE rrow>=0 +And rrow<=3; + + + + + + +---procedure that retrieves the total revenue for each customer : + +set serveroutput on +CREATE OR REPLACE PROCEDURE calculate_total_revenue IS +BEGIN + FOR rec IN (SELECT c.customer_id, c.name + FROM customers c + JOIN orders o ON c.customer_id = o.customer_id + JOIN products p ON o.product_id = p.product_id) + LOOP + DECLARE + v_customer_id customers.customer_id%TYPE := rec.customer_id; + v_customer_name customers.name%TYPE := rec.name; + v_total_revenue NUMBER(10, 2); + BEGIN + SELECT SUM(o.quantity * p.price) INTO v_total_revenue + FROM orders o + JOIN products p ON o.product_id = p.product_id + WHERE o.customer_id = v_customer_id; + + DBMS_OUTPUT.PUT_LINE('Customer ID: ' || v_customer_id); + DBMS_OUTPUT.PUT_LINE('Customer Name: ' || v_customer_name); + DBMS_OUTPUT.PUT_LINE('Total Revenue: $' || v_total_revenue); + DBMS_OUTPUT.PUT_LINE('-------------------------'); + END; + END LOOP; +END; +/ + + + + +---automatically update the total revenue +CREATE OR REPLACE TRIGGER update_total_revenue +AFTER INSERT OR UPDATE ON orders +FOR EACH ROW +DECLARE + v_customer_id customers.customer_id%TYPE; +BEGIN + + v_customer_id := :NEW.customer_id; + + + UPDATE orders o + SET total_revenue = ( + SELECT (o.quantity * p.price) + FROM orders o + JOIN products p ON o.product_id = p.product_id + WHERE o.customer_id = v_customer_id + ) + WHERE o.customer_id = v_customer_id; +END; +/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..cd2e17f --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# database-project-Swaraj44 +database-project-Swaraj44 created by GitHub Classroom +ID:1907069