From e7d163a5d17445aa30b43984b9d9c7b6de3a6a84 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Sun, 28 May 2023 12:21:53 +0000 Subject: [PATCH 1/7] Setting up GitHub Classroom Feedback From 9d3b23e998820229de77742b8d281df6d1f367c0 Mon Sep 17 00:00:00 2001 From: Abdulla Rahman <121899502+abdulla30r@users.noreply.github.com> Date: Wed, 31 May 2023 09:30:27 +0600 Subject: [PATCH 2/7] Added DDL,DML and PL/SQl file --- bookShopPLSQL.sql | 138 ++++++++++++++++++++++++++++++++++++++++++++ bookshopDDL.sql | 88 ++++++++++++++++++++++++++++ bookshopDML.sql | 144 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 370 insertions(+) create mode 100644 bookShopPLSQL.sql create mode 100644 bookshopDDL.sql create mode 100644 bookshopDML.sql diff --git a/bookShopPLSQL.sql b/bookShopPLSQL.sql new file mode 100644 index 0000000..b80fc3f --- /dev/null +++ b/bookShopPLSQL.sql @@ -0,0 +1,138 @@ +--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; +/ \ No newline at end of file diff --git a/bookshopDDL.sql b/bookshopDDL.sql new file mode 100644 index 0000000..5a5c554 --- /dev/null +++ b/bookshopDDL.sql @@ -0,0 +1,88 @@ +drop table orders; +drop table products; +drop table books; +drop table sellers; +drop table customers; +drop view allProducts; +drop view orderList; + +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..95fba40 --- /dev/null +++ b/bookshopDML.sql @@ -0,0 +1,144 @@ +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'); + + + + +--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; \ No newline at end of file From 2700de3ad37444cbf5efc0559bb410cfd6a6a5cd Mon Sep 17 00:00:00 2001 From: Abdulla Rahman <121899502+abdulla30r@users.noreply.github.com> Date: Wed, 31 May 2023 09:51:28 +0600 Subject: [PATCH 3/7] Update bookShopPLSQL.sql with function and procedure --- bookShopPLSQL.sql | 59 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/bookShopPLSQL.sql b/bookShopPLSQL.sql index b80fc3f..4380575 100644 --- a/bookShopPLSQL.sql +++ b/bookShopPLSQL.sql @@ -135,4 +135,61 @@ begin end if; end loop; end; -/ \ No newline at end of file +/ + +--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; +/ From df710e68a52cf084bd871265fadbe4fb931789ec Mon Sep 17 00:00:00 2001 From: Abdulla Rahman <121899502+abdulla30r@users.noreply.github.com> Date: Wed, 31 May 2023 10:01:27 +0600 Subject: [PATCH 4/7] Update bookshopDDL.sql --- bookshopDDL.sql | 2 -- 1 file changed, 2 deletions(-) diff --git a/bookshopDDL.sql b/bookshopDDL.sql index 5a5c554..17c60cd 100644 --- a/bookshopDDL.sql +++ b/bookshopDDL.sql @@ -3,8 +3,6 @@ drop table products; drop table books; drop table sellers; drop table customers; -drop view allProducts; -drop view orderList; create table books( id NUMBER PRIMARY KEY, From 2ab3a56f3ce08c3a8b91f5222ee869035f6ea3d9 Mon Sep 17 00:00:00 2001 From: Abdulla Rahman <121899502+abdulla30r@users.noreply.github.com> Date: Wed, 31 May 2023 10:01:40 +0600 Subject: [PATCH 5/7] Update bookshopDML.sql --- bookshopDML.sql | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bookshopDML.sql b/bookshopDML.sql index 95fba40..831d93b 100644 --- a/bookshopDML.sql +++ b/bookshopDML.sql @@ -88,7 +88,8 @@ INSERT INTO orders (order_id, product_id, customer_id, quantity,order_date) VALU 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 @@ -141,4 +142,4 @@ 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; \ No newline at end of file + join customers on customers.customer_id = orders.customer_id; From 538fad5ca9a48ef20afda01781d0eac8403e2a55 Mon Sep 17 00:00:00 2001 From: Abdulla Rahman <121899502+abdulla30r@users.noreply.github.com> Date: Fri, 2 Jun 2023 14:12:05 +0600 Subject: [PATCH 6/7] Create README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README.md 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 From 5c51bd08e7aa5505415757fdc9792ce4740110f4 Mon Sep 17 00:00:00 2001 From: Abdulla Rahman <121899502+abdulla30r@users.noreply.github.com> Date: Wed, 28 Jun 2023 14:01:55 +0600 Subject: [PATCH 7/7] Update bookShopPLSQL.sql --- bookShopPLSQL.sql | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/bookShopPLSQL.sql b/bookShopPLSQL.sql index 4380575..5378a5c 100644 --- a/bookShopPLSQL.sql +++ b/bookShopPLSQL.sql @@ -193,3 +193,40 @@ 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; +/