Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
DROP TABLE orders;
DROP TABLE customer;
DROP TABLE product;
DROP TABLE employee;
DROP TABLE branch;


create table branch(
branch_id int not null,
street varchar(40),
city varchar(40),
contact_no varchar(20),
primary key (branch_id)
);

create table employee(
employee_id int not null,
branch_id int not null,
name varchar(40),
employee_type varchar(40),
salary int,
address varchar(40),
phone varchar(20),
primary key (employee_id),
foreign key (branch_id) references branch
);

create table customer(
customer_id int not null,
name varchar(40),
address varchar(40),
phone varchar(20),
primary key (customer_id)
);

create table product(
product_id int not null,
branch_id int not null,
product_type varchar(40),
product_size varchar(20),
price decimal(10,2),
primary key (product_id),
foreign key (branch_id) references branch
);

create table orders(
order_id int not null,
customer_id int not null,
product_id int not null,
order_date varchar(40),
delivary_location varchar(40),
primary key(order_id),
foreign key(customer_id) references customer,
foreign key(product_id) references product
);

set pagesize 100
set linesize 200

--show all created table
select table_name from user_tables;

--delete a column from table
alter table orders drop column order_date;

--add a column in the table
alter table orders add order_date varchar(40);

--rename column
alter table product rename column price to product_price;
alter table product rename column product_price to price;

--change column data type
alter table product modify product_size varchar(40);

alter table employee modify employee_type varchar(20);
alter table employee modify name varchar(20);
alter table employee modify address varchar(20);
111 changes: 111 additions & 0 deletions dml.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
insert into branch values(1,'shib bari','khulna','01*********');
insert into branch values(2,'shonadanga','khulna','01*********');
insert into branch values(3,'new market','dhaka','01*********');
insert into branch values(4,'new market','khulna','01*********');
insert into branch values(5,'daulotpur','khulna','01*********');
insert into branch values(6,'bhanga','faridpur','01*********');
insert into branch values(7,'mirpur-10','dhaka','01*********');
insert into branch values(8,'gulshan-12','dhaka','01*********');
insert into branch values(9,'shahabag','dhaka','01*********');
insert into branch values(10,'fulbariget','khulna','01*********');


insert into employee values(1,1,'sadi','manager',30000,'khulna','01*********');
insert into employee values(2,1,'asif','salesman',10000,'khulna','01*********');
insert into employee values(3,2,'atik','salesman',10000,'khulna','01*********');
insert into employee values(4,3,'salman','cashier',20000,'dhaka','01*********');
insert into employee values(5,2,'rahim','manager',30000,'khulna','01*********');
insert into employee values(6,3,'sabbir','salesman',10000,'dhaka','01*********');
insert into employee values(7,4,'abrar','manager',30000,'khulna','01*********');
insert into employee values(8,10,'safwan','cashier',20000,'khulna','01*********');
insert into employee values(9,4,'shuvro','salesman',10000,'khulna','01*********');
insert into employee values(10,4,'naieem','salesman',10000,'khulna','01*********');


insert into customer values(1,'sadi','daulotpur,khulna','01*********');
insert into customer values(2,'asif','fulbariget,khulna','01*********');
insert into customer values(3,'atik','banani,dhaka','01*********');
insert into customer values(4,'shafiqul','boyra,khulna','01*********');
insert into customer values(5,'hadi','bhanga,faridpur','01*********');
insert into customer values(6,'zunnun','fultala,khulna','01*********');
insert into customer values(7,'shonjoy','shibbari,khulna','01*********');
insert into customer values(8,'ashfaq','cumilla','01*********');
insert into customer values(9,'hasib','varamara,kushtia','01*********');
insert into customer values(10,'naieem','daulotpur,khulna','01*********');


insert into product values(1,1,'shirt','L',1299.99);
insert into product values(2,1,'pant','34',1099.99);
insert into product values(3,1,'shirt','xL',1599.99);
insert into product values(4,1,'t-shirt','M',699.99);
insert into product values(5,4,'shirt','L',1299.99);
insert into product values(6,4,'t-shirt','S',599.99);
insert into product values(7,4,'jacket','xL',2299.99);
insert into product values(8,4,'shirt','xxL',1899.99);
insert into product values(9,3,'shirt','L',1299.99);
insert into product values(10,3,'pant','32',1599.99);


insert into orders values(1,1,1,'01-01-2023','daulotpur,khulna');
insert into orders values(2,1,2,'01-01-2023','daulotpur,khulna');
insert into orders values(3,2,1,'01-01-2023','daulotpur,khulna');
insert into orders values(4,2,4,'01-01-2023','daulotpur,khulna');
insert into orders values(5,2,3,'01-01-2023','daulotpur,khulna');
insert into orders values(6,3,9,'01-01-2023','daulotpur,khulna');
insert into orders values(7,3,10,'01-01-2023','daulotpur,khulna');
insert into orders values(8,3,1,'01-01-2023','daulotpur,khulna');
insert into orders values(9,4,7,'01-01-2023','daulotpur,khulna');
insert into orders values(10,4,6,'01-01-2023','daulotpur,khulna');


select * from branch;
select * from employee;
select * from customer;
select * from product;
select * from orders;

--all order made by sadi
select * from orders where customer_id=(select customer_id from customer where name='sadi');

--all order placed in shib bari branch
select * from orders where product_id IN(select product_id from product where branch_id=(select branch_id from branch where street='shib bari'));

--update salary of salesman
update employee set salary=15000 where employee_type='salesman';
select * from employee;

--all shirt and tshirt
select product_type,product_size,price from product where product_type like 's%' or product_type like 't%';

--all shirt
select product_type,product_size,price from product where product_type like 's%' and product_type like 't%';


--avarage price of shirt,t-shirt,pant,jacket
select product_type,avg(price) from product group by product_type;

--avarage price greater than 1000;
select product_type,avg(price) from product group by product_type having avg(price)>1000;

--product with maximum price
with max_price(price) as (select max(price) from product)
select * from product,max_price where product.price=max_price.price;

--product with second maximum price
with max_price(price) as (select max(price) from product where price<(select max(price) from product))
select * from product,max_price where product.price=max_price.price;

--number of ordered product
select count(distinct product_id) as no_of_product_ordered from orders;

--number of shirt ordered
select count(product_id) from orders where product_id IN(select product_id from product where product_type='shirt');

--join product and order table
select order_id,product_id,product_type from orders join product using (product_id)
order by order_id;

--total price of all product ordered
select sum(price) from orders join product using (product_id);


199 changes: 199 additions & 0 deletions pl-sql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
set serveroutput on
declare
product_id2 product.product_id%type;
product_type product.product_type%type;
price product.price%type;
begin
select product_id,product_type,price into product_id2,product_type,price from product where product_id=1;
dbms_output.put_line('product_id: '||product_id2|| ' product_type: '||product_type || ' price: '||price);
end;
/

--insert and set default value
set serveroutput on
declare
product_id product.product_id%type:=11;
branch_id product.branch_id%type:=8;
product_type product.product_type%type:='pant';
product_size product.product_size%type:='33';
price product.price%type:=1599.99;
begin
insert into product values(product_id,branch_id,product_type,product_size,price);
end;
/


--row type
set serveroutput on
declare
product_row product%rowtype;
begin
select product_id,product_type,price into product_row.product_id,product_row.product_type,product_row.price from product where product_id=1;
dbms_output.put_line('product_id: '||product_row.product_id|| ' product_type: '||product_row.product_type || ' price: '||product_row.price);
end;
/


--cursor and row count
set serveroutput on
declare
cursor product_cursor is select * from product;
product_row product%rowtype;
begin
open product_cursor;
fetch product_cursor into product_row.product_id,product_row.branch_id,product_row.product_type,product_row.product_size,product_row.price;
while product_cursor%found loop
dbms_output.put_line('product_id: '||product_row.product_id|| ' product_type: '||product_row.product_type || ' price: '||product_row.price);
dbms_output.put_line('Row count: '|| product_cursor%rowcount);
fetch product_cursor into product_row.product_id,product_row.branch_id,product_row.product_type,product_row.product_size,product_row.price;
end loop;
close product_cursor;
end;
/


--for while loop ,array
set serveroutput on
declare
counter number;
name customer.name%type;
TYPE NAMEARRAY IS VARRAY(8) OF customer.name%type;
A_NAME NAMEARRAY:=NAMEARRAY();
begin
counter:=2;
A_NAME.EXTEND();
for x in 2..8
loop
select name into name from customer where customer_id=x;
A_NAME.EXTEND();
A_NAME(counter):=name;
counter:=counter+1;
end loop;
counter:=2;
while counter<=A_NAME.COUNT
loop
dbms_output.put_line(A_NAME(counter));
counter:=counter+1;
end loop;
end;
/


--for while loop ,array without extend
set serveroutput on
declare
counter number;
name customer.name%type;
TYPE NAMEARRAY IS VARRAY(8) OF customer.name%type;
A_NAME NAMEARRAY:=NAMEARRAY('name1','name1','name1','name1','name1','name1','name1','name1');
begin
counter:=1;
for x in 2..8
loop
select name into name from customer where customer_id=x;
A_NAME(counter):=name;
counter:=counter+1;
end loop;
counter:=1;
while counter<=7
loop
dbms_output.put_line(A_NAME(counter));
counter:=counter+1;
end loop;
end;
/



--if else
set serveroutput on
declare
counter number;
name employee.name%type;
salary employee.salary%type;
begin
for x in 2..8
loop
select name,salary into name,salary from employee where employee_id=x;
if salary>=30000
then
dbms_output.put_line(name||' is a manager');
elsif salary>=20000
then
dbms_output.put_line(name||' is a cashier');
else
dbms_output.put_line(name||' is a salesman');
end if;
end loop;
end;
/



create or replace procedure proc2(
var1 in number,
var2 out VARCHAR2
)
AS
t_show CHAR(30);
begin
t_show := 'From procedure: ';
select name into var2 from customer where customer_id IN (select customer_id from orders where product_id = var1);
dbms_output.put_line(t_show || var2 || ' ordered product with id: ' || var1);
end;
/

set serveroutput on
declare
product_id product.product_id%type:=9;
name customer.name%type;
begin
proc2(product_id,name);
end;
/

drop procedure proc2;



set serveroutput on
create or replace function fun(var1 in varchar) return varchar AS
value customer.name%type;
begin
select name into value from customer where customer_id=var1;
return value;
end;
/

set serveroutput on
declare
value varchar(40);
begin
value:=fun(2);
dbms_output.put_line(value);
end;
/

drop function fun;

SET SERVEROUTPUT ON
CREATE OR REPLACE TRIGGER try
BEFORE delete ON product
REFERENCING OLD AS o NEW AS n
FOR EACH ROW
BEGIN
delete from orders where product_id=:o.product_id;
END;
/


SET SERVEROUTPUT ON
CREATE OR REPLACE TRIGGER try2
BEFORE delete ON branch
REFERENCING OLD AS o NEW AS n
FOR EACH ROW
BEGIN
delete from employee where branch_id=:o.branch_id;
END;
/