diff --git a/README.md b/README.md new file mode 100644 index 0000000..162af20 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Project Name: BookShop +Created by : Abdulla Rahman +Roll: 1907030 diff --git a/bookShopPLSQL.sql b/bookShopPLSQL.sql new file mode 100644 index 0000000..5378a5c --- /dev/null +++ b/bookShopPLSQL.sql @@ -0,0 +1,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; +/ diff --git a/bookshopDDL.sql b/bookshopDDL.sql new file mode 100644 index 0000000..17c60cd --- /dev/null +++ b/bookshopDDL.sql @@ -0,0 +1,86 @@ +drop table orders; +drop table products; +drop table books; +drop table sellers; +drop table customers; + +create table books( + id NUMBER PRIMARY KEY, + title varchar(50) not null, + author varchar(20) not null, + category varchar(20) +); + +create table sellers( + Seller_id number primary key, + seller_name varchar(30) not null, + phone NUMBER not null, + email varchar(30) +); + +create table customers( + customer_id number PRIMARY key, + customer_name varchar(30), + address VARCHAR(50), + phone number, + email varchar(30) +); + +create table products ( + product_id number primary key, + book_id number not null, + seller_id number not null, + price number not null check (price>0), + count number not null check (count>=0), + FOREIGN key (book_id) references books(id), + FOREIGN key (seller_id) REFERENCEs sellers(seller_id) +); + +CREATE TABLE orders ( + order_id NUMBER PRIMARY KEY, + order_date date DEFAULT SYSDATE, + product_id NUMBER NOT NULL, + customer_id NUMBER NOT NULL, + quantity NUMBER CHECK (quantity > 0), + total_price NUMERIC(6,2), + status VARCHAR(20) DEFAULT 'In progress' CHECK (status IN ('In progress', 'Shipped', 'Delivered')), + FOREIGN KEY (product_id) REFERENCES products(product_id), + FOREIGN KEY (customer_id) REFERENCES customers(customer_id) +); + +-- 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; +/ + + + diff --git a/bookshopDML.sql b/bookshopDML.sql new file mode 100644 index 0000000..831d93b --- /dev/null +++ b/bookshopDML.sql @@ -0,0 +1,145 @@ +INSERT INTO books (id, title, author, category) VALUES +(1, 'To Kill a Mockingbird', 'Harper Lee', 'Fiction'); +INSERT INTO books (id, title, author, category) VALUES +(2, '1984', 'George Orwell', 'Science Fiction'); +INSERT INTO books (id, title, author, category) VALUES +(3, 'Pride and Prejudice', 'Jane Austen', 'Romance'); +INSERT INTO books (id, title, author, category) VALUES +(4, 'The Great Gatsby', 'F. Scott Fitzgerald', 'Classic'); +INSERT INTO books (id, title, author, category) VALUES +(5, 'Harry Potter and the Sorcerer''s Stone', 'J.K. Rowling', 'Fantasy'); +INSERT INTO books (id, title, author, category) VALUES +(6, 'The Catcher in the Rye', 'J.D. Salinger', 'Fiction'); +INSERT INTO books (id, title, author, category) VALUES +(7, 'To the Lighthouse', 'Virginia Woolf', 'Classic'); +INSERT INTO books (id, title, author, category) VALUES +(8, 'The Hobbit', 'J.R.R. Tolkien', 'Fantasy'); +INSERT INTO books (id, title, author, category) VALUES +(9, 'The Alchemist', 'Paulo Coelho', 'Self-Help'); +INSERT INTO books (id, title, author, category) VALUES +(10, 'The Odyssey', 'Homer', 'Classic'); + + +INSERT INTO sellers (Seller_id, seller_name, phone, email) VALUES +(1001, 'ABC Books', '1234567890', 'abcbooks@example.com'); +INSERT INTO sellers (Seller_id, seller_name, phone, email) VALUES +(1002, 'XYZ Bookstore', '9876543210', 'xyzbookstore@example.com'); +INSERT INTO sellers (Seller_id, seller_name, phone, email) VALUES +(1003, 'Book Haven', '5555555555', 'bookhaven@example.com'); +INSERT INTO sellers (Seller_id, seller_name, phone, email) VALUES +(1004, 'Readers Paradise', '9999999999', 'readersparadise@example.com'); +INSERT INTO sellers (Seller_id, seller_name, phone, email) VALUES +(1005, 'Bookworm Emporium', '1111111111', 'bookwormemporium@example.com'); + + +INSERT INTO customers (customer_id, customer_name, address, phone, email) VALUES +(5001, 'John Doe', '123 Main St, City', '9832103210', 'johndoe@example.com'); +INSERT INTO customers (customer_id, customer_name, address, phone, email) VALUES +(5002, 'Jane Smith', '456 Oak St, Town', '5598754378', 'janesmith@example.com'); +INSERT INTO customers (customer_id, customer_name, address, phone, email) VALUES +(5003, 'Mike Johnson', '789 Elm St, Village', '4321567890', 'mikejohnson@example.com'); +INSERT INTO customers (customer_id, customer_name, address, phone, email) VALUES +(5004, 'Emily Davis', '789 Walnut St, Town', '5673349999', 'emilydavis@example.com'); +INSERT INTO customers (customer_id, customer_name, address, phone, email) VALUES +(5005, 'David Wilson', '321 Pine St, City', '1156731111', 'davidwilson@example.com'); +INSERT INTO customers (customer_id, customer_name, address, phone, email) VALUES +(5006, 'Sherlock Holmes', '221B Becker St, City', '198456721', 'sherlockholmes@detective.com'); + + +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3001, 1, 1001, 20.99, 50); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3002, 2, 1002, 15.99, 30); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3003, 3, 1003, 12.50, 20); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3004, 4, 1004, 18.75, 15); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3005, 5, 1005, 9.99, 40); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3006, 6, 1001, 9.99, 25); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3007, 7, 1002, 14.50, 15); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3008, 8, 1003, 19.99, 35); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3009, 9, 1004, 10.99, 50); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3010, 10, 1005, 8.99, 10); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3011, 1, 1005, 25.50, 20); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3012, 2, 1003, 12.75, 30); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3013, 3, 1004, 7.99, 45); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3014, 4, 1002, 16.99, 5); +INSERT INTO products (product_id, book_id, seller_id, price, count) VALUES +(3015, 5, 1001, 11.50, 25); + + +INSERT INTO orders (order_id, product_id, customer_id, quantity,order_date) VALUES (7001, 3001, 5001, 2,date '2023-05-25'); +INSERT INTO orders (order_id, product_id, customer_id, quantity,order_date) VALUES (7002, 3002, 5002, 1,date '2023-03-21'); +INSERT INTO orders (order_id, product_id, customer_id, quantity,order_date) VALUES (7003, 3003, 5003, 3,date '2023-05-15'); +INSERT INTO orders (order_id, product_id, customer_id, quantity,order_date) VALUES (7004, 3004, 5004, 1,date '2023-04-20'); +INSERT INTO orders (order_id, product_id, customer_id, quantity,order_date) VALUES (7008, 3014, 5004, 2,date '2023-05-18'); +INSERT INTO orders (order_id, product_id, customer_id, quantity) VALUES (7005, 3005, 5002, 4); +INSERT INTO orders (order_id, product_id, customer_id, quantity,order_date) VALUES (7006, 3003, 5005, 4,date '2023-05-15'); +INSERT INTO orders (order_id, product_id, customer_id, quantity,order_date) VALUES (7007, 3013, 5004, 5,date '2023-04-28'); +INSERT INTO orders (order_id, product_id, customer_id, quantity,order_date) VALUES (7009, 3005, 5005, 2,date '2023-05-10'); + +drop view allProducts; +drop view orderList; + + +--Show table name +select table_name from user_tables; + +--Basic Update Query +update orders set status = 'Shipped' where order_date< date '2023-05-24'; +UPDATE orders SET status = 'Delivered' WHERE EXTRACT(MONTH FROM order_date) < 5; + +--Basic Select Query with where condition +select * from customers where customer_id=5001; + +--Select query (Nested) +Select * from books where id = (select book_id from products where product_id = (select product_id from orders where order_id = 7001)); + +--Aggregate function +--total_sell of this shop +select SUM(total_price) from orders; + +--the product which has earns highest money +select product_id,tot from(select product_id,sum(total_price) as tot from orders group by product_id) +where tot = +(select max(sum(total_price)) from orders group by product_id); + +--using having +select product_id,sum(total_price) as total_sell from orders group by product_id having sum(total_price) = (select max(sum(total_price)) from orders group by product_id); + +--the customer who hasn't ordered any product yet (using in) +select * from customers where customer_id not in (select customer_id from orders); + +--string operation +select * from customers where email like '%@example.com'; + +--exist (those customer who has ordered 2 or quantity. +select * from customers where exists (select * from orders where quantity >2 and orders.customer_id=customers.customer_id); + +--natural join +select * from orders natural join customers; + +--join +select * from orders join customers on orders.customer_id=customers.customer_id; + +--Creating View of AllProducts +create view allProducts as + select products.product_id,books.title,products.price,sellers.seller_name,products.count as stock from + books join products on books.id = products.book_id join sellers on sellers.seller_id= products.seller_id; + +--Creating view of orderList +create view OrderList as + select orders.order_id, allProducts.title,allproducts.price,orders.quantity,orders.total_price, + customers.customer_name,allproducts.seller_name, orders.status, orders.order_date from + allProducts join orders on allProducts.product_id = orders.product_id + join customers on customers.customer_id = orders.customer_id;