22
[email protected] JOURNAL FOR DATABASE MANAGEMENT SYSTEM – ORACLE ANKIT GUPTA

JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

JOURNAL FOR DATABASE MANAGEMENT SYSTEM – ORACLE

ANKIT GUPTA

Page 2: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 1

1. create client_master, product_master and salesman_master tables 2. insert 5 records in each table 3. find out the name of all clients 4. receive the entire content of client_master 5. list the name and cities of all the clients 6. list all the various products available from the product_master table 7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005 to mumbai 10. change the balance_due od client_no c001 to 4000 11. change the cost price of 1.44 floppies to 1000 12. change the salaries of salesman manish to 5000 13. add a column telephone having datatype number and size 10 to the client_master 14. delete all salesman from the salesman_master whose salary is 10000 15. delete from client_master where column state hold the value maharashtra 16. destroy the table client_master along with the data 17. change the size of sale_price in product_master table to 10,2 18. retrieve the content of the column description and computer 5% the value contained in the

column sale_price and 105% of the values contained under sale_price from sales_master 19. retrieve sale_price*0.05 as increased and sale_price*1.05 as new prices.

Solution

1. create table client_master (client_no varchar2(6),client_name varchar2(10), city varchar2(15), pincode number(8), state varchar2(15), bal_due number(10,2));

create table product_master (product_no varchar2(6), description varchar2(15), profit_per number(4,2), unit_measure varchar2(10), qtl_on_hand number(8), record_hand number(8), sale_price number(8,2), cost_price number(8,2));

create table salesman_master (salesman_no varchar2(6), salesman_name varchar2(20), address1 varchar2(30), address2 varchar2(20), city varchar2(20), pincode number(8), state varchar2(20), salary_amt number(8,2), target_get number(6,2), remarks varchar2(30));

2. insert into client_master values ('c001', 'ankit', 'kanpur', '549673', 'UP', '2300');

insert into client_master values ('c002', 'rashid', 'delhi', '573687', 'delhi', '4500'); insert into client_master values ('c003', 'mayank', mumbai', '357346', 'maharashtra', '5200'); insert into client_master values ('c004', 'sachin', 'lucknow', '498956', 'UP', '3400'); insert into client_master values ('c005', 'praveen', 'pune', '203893', 'maharashtra', '3000');

insert into product_master values('p001','1.44 floppies','1.2','pc','100','500','900','980.00'); insert into product_master values('p002','700 mb cds','1.5','pc','120','300','','340.50'); insert into product_master values('p003','4.7 gb dvds','4.0','pc','100','450','980','1050'); insert into product_master values('p004','4 gb pendrive','10.0','pc','30','400','9000','10500'); insert into product_master values('p005','8 gb pendrive','1.0','pc','10','200','8000','10000');

insert into salesman_master values('s0001','sudhanshu','231 abc building','kc road','pune','435643','maharashtra','30000','4','good');

Page 3: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

insert into salesman_master values('s0002','shekhar','44 rose building','ym road','delhi','546743','delhi','10000','6','good'); insert into salesman_master values('s0003','alok','44 o block','mall road','kanpur','208047','UP','31000','3','good'); insert into salesman_master values('s0004','manish','23 paud roadbuilding','fc road','pune','443356','maharashtra','49000','4','good'); insert into salesman_master values('s0005','bhawesh','55 k block','mg road','lucknow','208023','UP','33000','2','good');

3. select client_name from client_master; 4. select * from client_master; 5. select client_name, city from client_master; 6. select * from product_master; 7. select * from client_master where city='mumbai'; 8. select salesman_name from salesman_master where salary_amt=30000; 9. update client_master set city='mumbai' where client_no='c005'; 10. update client_master set bal_due='4000' where client_no='c001'; 11. update product_master set cost_price='1000' where description='1.44 floppies'; 12. update salesman_master set salary_amt='5000' where salesman_name='manish'; 13. alter table client_master add telephone number(10); 14. delete from salesman_master where salary_amt='10000'; 15. delete from client_master where state='maharashtra'; 16. drop table client_master; 17. alter table product_master modify(sale_price number(10,2)); 18. select product_no, description,(5*sale_price)/100,(105*sale_price)/100 from product_master; 19. select product_no, 0.05*sale_price as increased_price, 1.05*sale_price as new_price from

product_master;

Page 4: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 2 1. create sales_person, customer and orders table 2. add five records in each table 3. list all columns and rows from sales_person 4. list all columns and rows from customer 5. list all columns and rows from orders 6. retrieve only s_name and s_no from sales_person 7. retrieve o_no and o_date from orders table 8. retrieve c_name and rating from customer table 9. retrieve o_no, o_amt, c_no and s_no from orders 10. find out which sales person have currently ordered 11. retrieve exactly one occurence of c_name from customer 12. retrieve all customers living in pune city 13. retrieve all sales person having commission more than 1 14. list customer with rating 100 15. list all order with amount less than 50

Solution

1. create table sales_person (s_no number(4) not null, s_name varchar2(20) not null, city char(15) default 'pune', commission number(2), primary key(s_no)); create table customer (c_no number(5) not null, c_name varchar2(7) not null, city varchar2(15), rating number(5), s_no number(4), primary key(c_no), foreign key(s_no) references sales_person(s_no)); create table orders(o_no number not null, o_amt number(7) not null, o_date date not null, c_no number(5), s_no number(5), foreign key(c_no) references customer(c_no), foreign key(s_no) references sales_person(s_no));

2. insert into sales_person values (); insert into customer values (); insert into orders values (3001,2000,'12-jan-2011',);

3. select * from sales_person; 4. select * from customer; 5. select * from orders; 6. select s_no, s_name from sales_person; 7. select o_no, o_date from orders; 8. select o_no, o_name, c_no, s_no from orders; 9. select * from orders where o_date=(select max(o_date) from orders); 10. select distinct s_name from sales_person; 11. select * from customer where city='pune'; 12. select * from sales_person where commission>1; 13. select * from customer where rating=100; 14. select * from orders where o_amt<50;

Page 5: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 3 Consider the sales_person, customer and orders table from the Assignment 2 and solve the following queries.

1. retrieve name & rating of customer from ‘pune’ city 2. retrieve all order with on 10-10-1997 3. retrieve all order before 10.10.1997 4. retrieve all order after 10-10-1997 5. retrieve all customer with rating order than 100 6. retrieve all customer with rating at least 300 7. retrieve all customer with rating at least 100 8. list order number and date with date display in DD/MM/YY format 9. modify 8 to display date in “DD-MON” format 10. modify 8 to display date in “DD-MON-YYYY” format 11. list customer located in ‘pune’ city with rating more than 200 12. list customer located in ‘pune’ as well as of those with rating 200 or less 13. retrieve all customer who live in ‘mumbai’ as well as those with rating 200 or less 14. retrieve all customers who live in ‘pune’ with rating about 300 15. retrieve all order with amount more than 1000 16. list name and cities of all sales_person with commission above 10 17. list all customers with rating above 1000 unless located in ‘mumbai’ 18. list all customers living in ‘pune’ of ‘mumbai’

Solution

1. select c_name, rating from customer where city=’punr’; 2. select * from orders where o_date=to_date(’10-10-1997’, ‘DD-MM-YYYY’); 3. select * from orders where o_date<to_date(’10-10-1998’, ‘DD-MM-YYYY’); 4. select * from orders where o_date>to_date(’10-10-1998’, ‘DD-MM-YYYY’); 5. select * from customer where rating=100; 6. select * from customer where rating>=300; 7. select * from customer where rating<=100; 8. select o_num, to_char(o_date, ‘DD-MM-YYYY’) “o_date” from orders; 9. select o_num, to_char(o_date, ‘DD-MON’) “o_date” from orders; 10. select o_num to_char(o_date, ‘DD-MM-YYYY’) “o_date” from orders; 11. select * from customer where ciy=’pune’ and rating>200; 12. select * from customer where city=’pune’ and rating<200; 13. select * from customer where city=’mumbai’ and rating<=200; 14. select * from customer where city=’pune’ and rating>300; 15. select * from orders where o_amt>1000; 16. select s_name from sales_person where commission>10; 17. select * from customer where rating>1000 and city=’mumbai’; 18. select * from customer where city in(‘pune’, ‘mumbai’);

Page 6: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 4 Consider the salesperson, customer and orders table from the Assignment 2 and solve the following queries.

1. list all order for customers with customers number 1001, 1002, 1003 2. list all customers services by salesperson having number 1001, 1004, 1006 3. list all order on date “10-10-96”, ”12-10-97 4. list all salesperson with commissions in the range 10 & 12 inclusive 5. list all salesperson with commissions in the range 10 & 12 exclusive 6. retrieve all customers whose name following in the range “a” to “g” 7. list all order in the range march 10 to march 20 1997 8. list all order on dates other than the dates in 240 9. list all customers whose name begin with “p” 10. list all customers whose names contain the letter “a” occurring 2 or more times 11. list all customers whose name end with “p” 12. list all salesperson whose name end with “p” or “t” 13. list all customers who have “a” anywhere in the name 14. retrieve all customers having “%” in their name 15. retrieve all order for march irrespective of years 16. list all order for April 1997 17. list all four character name ending with “t” 18. list all four character name beginning with “g” of customers 19. list all customers having underscore(-) in their name 20. list all customers having apostrophe in name

Solution

1. select * from customer where c_num in(1001,1002,1003); 2. select * from customer where s_num in(1001,1004,1006); 3. select * from orders where o_date in(to_date(’10-10-96’, ‘DD-MM-YY’) to_date(’10-10-97’, ‘DD-

MM-YY); 4. select * from sales_person where commission between 10 and 12; 5. select * from sales_person where commission not between 10 and 12; 6. select * from customer where c_name between ‘a%’ and ‘g%’; 7. select * from orders where o_date between to_date(’10-march-1997’,’DD-month-YYYY’) and

to_date(’20-march-1997’,’DD-month-YYYY’); 8. select * from orders where o_date=sysdate or o_date in(to_date(‘24’,’DD’); 9. select * from customer where c_name like ‘p%’; 10. select * from customer where c_name like ‘%a%’; 11. select * from customer where c_name like ‘%p”; 12. select * from sales_person where s_name like ‘%p’ or s_name like ‘%t’; 13. select * from customer where c_name like ‘%a%’; 14. select * from customer where c_name like ‘%%%’; 15. select * from orders where o_date like ‘%mar%’; 16. select * from orders where o_date like ‘%arp-97%’; 17. select * from customer where c_name like ‘%t’; 18. select * from customer where c_name like ‘g%’; 19. select * from customer where c_name like ‘%_%’; 20. select * from customer where c_name like ‘%.%’;

Page 7: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 5 Consider the salesperson, customer and orders table from the Assignment 2 and solve the following queries.

1. list all customers records with city value null 2. list all customers records with non-null cities 3. list all customers living in city other than “pune” or “mumbai” 4. list all order except then for march 5. list all order taken on october 3 or 4-1997 6. list all customers serviced by “suresh” & “ravi” 7. list all customers whose name begin with a letter from “a” to “g” 8. list all order with non-zero & non-null amount 9. list total order amount 10. find the average order amount 11. find the order having maximum amount 12. find the order having minimum amount 13. find the number of order having in the order table 14. list how many salesperson have booked order in the order table 15. list all salesperson numbers in the order table 16. list total amount of order booked by ‘rach’ salesperson 17. list total amount of order for each salesperson with total amount exceeding 5000 18. list total amount of order for each salesperson for 10-march-1997 19. list the maximum amount of order taken by salesperson 1002 & 1004 20. find the number of order for October 3, 1997

Solution

1. select * from customer where city=’NULL’; 2. select * from customer where city<>’NULL’; 3. select * from customer where city=’pune’ or city=’mumbai’; 4. select * from orders where o_date like ‘%mar%’; 5. select * from orders where o_date in(to_date(‘3-OCT-1997’,’DD-MM-YYYY’), to_date(‘4-OCT-

1997’,’DD-MM-YYYY’)); 6. select * from customer where s_name in(‘suresh’,’ravi’); 7. select * from customer where c_name between ‘a%’ and ‘g%’; 8. select * from orders where o_amt<>0 and o_amt NOT NULL; 9. select sum(o_amt) from orders; 10. select avg(o_amt) from orders; 11. select max(o_amt) from orders; 12. select min(o_amt) from orders; 13. select count(*) from orders; 14. select distinct(s_num) from orders; 15. select count(s_num) from orders; 16. select sum(o_amt) from orders where s_no=(select s_no from sales_person where

s_name=’prakash’); 17. select o_amt from orders where o_amt>5000 group by s_no; 18. select o_amt from orders where o_date=to_date(’10-march-1997’,’DD-month-YYYY’) group by

s_no; 19. select max(o_amt) from orders where s_no=1002 and s_no=1004; 20. select count(o_num) from orders where o_date=to_date(‘3-oct-1997’,’DD-month-YYYY’);

Page 8: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 6 Consider the salesperson, customer and orders table from the Assignment 2 and solve the following queries.

1. list number of distinct not-null city from customer 2. list each customers smallest order 3. list 1st customer in alphabetical order whose name begins with “g” 4. list commission as percentage rather than a decimal value 5. modify above query to paid % to the percentage commission 6. list number of order on each date with the following format:

For 0 data} there are 5} order 7. list all order, in ascending order for customer number 8. list all order in descending order of order date 9. list all order sorted customer-wise, amount-wise 10. list all order sorted in ascending order of salesperson but in descending order of order amount 11. list salesperson wise total amount of order 12. list salesperson wise date wise total amount 13. list all customer name having length greater than 10 14. list only the last two characters of name of salesperson 15. list all customer name in lower case 16. list all customer name in upper case 17. list all customers name with first letter of every word capitalized 18. list 3 characters of customer city starting form second character 19. list city form customer for those with null value display unknown 20. list salesperson name right padded with asterisk and other commission

Solution

1. select distinct city from customer where city is NOT NULL; 2. select distinct * from customer where s_num=(select s_num from orders where O-amt=(select

min(o_amt from orders))); 3. select * from customer where c_name like ‘g%’ order bu c_name; 4. select * from sales_person where commission like ‘%%’; 5. update sales_person set commission=commission*100 where commission like ‘%%’; 6. select count(*) from orders; 7. select * from orders order by c_no asc; 8. select * from orders order by o_date; 9. select * from orders order by c_no,o_amt; 10. select * from orders order by s_no asc, o_amt; 11. select s_no from orders order by o_amt; 12. select s_no from orders order by o_date; 13. select c_name from customer where length(c_name)>10; 14. select s_name from sales_person where s_name like ‘%__%’; 15. select lower(c_name) from customer; 16. select upper(c_name) from customer; 17. select initcap(c_name) from customer; 18. select ltrim(city) from customer; 19. select NULL(city,0) from customer; 20. select rpad(s_name,commission,*) from sales_person;

Page 9: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 7 Create the following tables with proper constraints (use PK, FK, NULL, NOT NULL, default, check): category_header (cat_code number(5), cat_desc varchar2(20))

route_header (route_it number(5), route_no number(5), cat_code number(5), origin varchar2(20), destination varchar2(20), fare number(7,2), distance number(3), capacity number(3))

place_header (place_id number(5), place_name varchar2(20), place_address varchar2(20), bus_station varchar2(20)

fleet_header (fleet_id number(5), day date, route_id number(5), cat_code number(5)) ticket_header (fleet_id number(5), ticket_no number(5), doi date, dot date, time_travel char(8),

board_place varchar2(20), origin varchar2(20), destination varchar2(20), adults number(3), children number(3), total_fare number(7,2), route_id number(3))

ticket_detail (ticket_no number(5), name varchar2(20), sex char(1), age number(3), fare number(5,2)) Solve the following queries:

1. create the above tables 2. alter the table route_header to add a column comments with data type as long 3. describe route_header 4. display only distinct category code from the table route_header order in the descending manner 5. alter the table to modify the length of the column distance in table route_header to 5 6. grant the alter, update insert privileges to your friend on the table route_header 7. revoke the above privileges

Solution

1. create table category_header (cat_code number(5), cat_desc varchar2(20)); create table route_header (route_it number(5), route_no number(5), cat_code number(5), origin varchar2(20), destination varchar2(20), fare number(7,2), distance number(3), capacity number(3)); create table place_header (place_id number(5), place_name varchar2(20), place_address varchar2(20), bus_station varchar2(20); create table fleet_header (fleet_id number(5), day date, route_id number(5), cat_code number(5)); create table ticket_header (fleet_id number(5), ticket_no number(5), doi date, dot date, time_travel char(8), board_place varchar2(20), origin varchar2(20), destination varchar2(20), adults number(3), children number(3), total_fare number(7,2), route_id number(3)); create table ticket_detail (ticket_no number(5), name varchar2(20), sex char(1), age number(3), fare number(5,2));

2. alter table route_header add comments number(100); 3. desc route_header; 4. select distinct cat_code from route_header order by cat_code desc; 5. alter table route_header modify distance number(5); 6. grant alter,update,insert on route_header to U1; 7. revoke alter,update,insert on route_header to U1;

Page 10: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 8 1. create a table route_deatil 2. insert 5 records into the table 3. create a savepoint s1 4. insert into above table the following values: 106,02,n 5. rollback to savepoint s1 6. commit the changes 7. change the nonstop “n” of the route_detail where the route_id is 102 8. create the savepoint s2 9. delete the row whose route_id is 102 10. rollback to savepoint s2 11. commit the changes

Solution

1. create table route_detail (route_id varchar2(20) primary key, place_id varchar2(10), nonstop varchar2(5)); 2. insert into route_detail values(‘105’,’01’,’n’);

insert into route_detail values(‘012’,’02’,’s’); insert into route_detail values(‘106’,’01’,’s’); insert into route_detail values(‘108’,’05’,’n’);

3. save point s1’ 4. insert into route_detail values(‘106’,’22’,’n’); 5. rollback to s1; 6. commit 7. update route_detail set nonstop=’n’ where route_id=’102’; 8. save point s2; 9. delete from route_detail where route_id=’102’; 10. rollback s2; 11. commit

Page 11: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 9 Consider following entities and their relationships student (rollno, name, class) teacher (tno, tname) student and teacher are relation with many to many relationship with the subject as a attribute of relationship Create a relational database in 3NF and construct queries in oracle

1. list the students to whom ‘mr. pawar’ is teaching more than two subjects 2. list all students to whom ‘mr. patil’ is teaching ‘dbms’ subject 3. list all teachers teaching ‘c++’ 4. print subject wise list of students 5. print total number of students for each subject

Solution create table student (rollno number(10) primary key, name varchar2(20), class varchar2(10)); create table teacher (tno number(10) primary key, tname varchar2(20)); create table student_teacher (student number(10) references student(rollno), teacher number(10) references teacher(tno), subject varchar2(20));

1. select student.rollno from student,teacher,student_teacher where student.rollno=student_teacher.student and count(student_teacher.subject)>2 and student_teacher.teacher=teacher.tno and teacher.tname=’mr. pawar’;

2. select student.rollno from student,teacher,student_teacher where student.roll=student_teacher.student and student_teacher.subject=’dbms’ and student_teacher.teacher=teacher.tno and teacher.tno=’mr. patil’;

3. select teacher.tno from teacher,student_teacher where teacher.tno=student_teacher.teacher and student_teacher.subject=’c++’;

4. select subject from student_teacher order by student; 5. select count(student.rollno) from student, student_teacher where

student.rollno=student_teacher.student;

Page 12: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 10 Consider following entities and their relationships book (bookno, name, price) dealer (dealerno, dealername, address) department (deptno, deptname, head) The relationship between book and dealer is many-to-one Relationship between book and department is many-to-one Create a relational database in 3NF and construct queries in oracle

1. give the name of department spending maximum amount of books 2. list name of department to whom 'tata mg-hill' has supplies books 3. list the names and prices of books bought for 'computer' department 4. insert a record in department relation. print department wise list of books with total amount

spent on books by every department Solution

1. create table book (bookno varchar2(10) primary key, name varchar2(20), price number(5), dealernum varchar2(10) reference dealer(dealerno), deptnum varchar(10) refernces department(deptno)); create table dealer (dealerno varchar2(10) primary key, dealername varchar2(20), address varchar2(20)); create table department (deptno varchar2(10) primary key, deptname varchar2(20), head varchar2(20));

2. select department.deptname from book, department where department.deptno=book.deptnum

and count(bookno) in (select max(count(bookno)) from book group by deptnum having

3. select department.deptname from department, book, dealer where department.deptno=book.deptnum and book.dealernum=dealer.dealerno and dealer.dealername='tata-mg-hill';

4. select book.bookbname, book.price from book, department where

book.deptnum=department.deptno and department.deptname='computer';

Page 13: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 11 Consider following entities and their relationships employee (empno, empname, salary, commission, designation) department (deptno, deptname, location) Employee and department are related with many-to-one relationship.

1. create a relational database in 3NF and construct queires in oracle 2. find the maximum, minimum and average salary for every designation 3. find out employee who is working at 'pune' location 4. update commission for every employee by 5% who belong to 'computer' department 5. print department wise list of employees 6. also print the total no of employees in each department

Solution

1. create table employee (empno varchar2(10), empname varchar2(20), salary number(10), commission number(10), designation varchar2(10), deptnum number(10) references department(deptno)); create table department (deptno number(10) primary key, deptname varchar2(20), location varchar2(20));

2. select max(salary), min(salary), avg(salary) from employee group by designation;

3. select employee.empname from employee, department where

employee.deptnum=department.deptno and department.location='pune';

4. update employee set commission=commission*0.5 where empno in (select employee.empno from employee, department where employee.deptnum=department.deptno and department.deptname='computer');

5. select empname from employee order by deptnum;

6. select count(empname) from employee group by deptnum;

Page 14: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 12 Consider following entities and their relationships movie (mvno, mvname, releaseyear) actor (actno, actname) The relation between movie and actor is many-to-many

1. create a relational database in 3NF and construct queries in oracle 2. count the number of movies in which 'shahrukh' has acted 3. find all actors of movie 'silsila' 4. find all movies of 'amitabh' which are released in year 1975 5. print actor wise list of pictures along with release year and movie name

Solution

1. create table movie (mvno number(10) primary key, mvname varchar2(20), releaseyear number(4)); create table actor (actno number(10) primary key, actname varchar2(20)); create table movie_actor (movie number(10) references movie(mvno), actor number(10) references actor(actno));

2. select count(movie.mvno) from movie, actor, movie_actor where

movie.mvno=movie_actor.movie and movie_actor.actor=actor.actno and actor.actname='shahrukh';

3. select actname from actor, movie, movie_actor where actor.actno=movie_actor.actor and

movie_actor.movie=movie.mvno and movie.mvname='silsila'; 4. select movie.mvname from actor, movie, movie_actor where movie.releaseyear=1975 and

movie.mvno=movie_actor.movie and movie_actor.actor=actor.actno and actor.actname='amitabh';

5. select movie.mvname, movie.releaseyear from movie, movie_actor order by movie_actor.actor;

Page 15: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 13 Consider the following entities and their relationships employee (empno, name, address, city, deptname) project (pno, pname, status) employee and project are related with m-m relationship with attribute noofdays employee worked on that project Create a relational database in 3NF and construct queries in oracle

1. list the name of employee worked in any project more than 25 days 2. list the name of employee working on project having status 'progressive' 3. list the project name and total no of employees worked in a project which are 'incomplete' 4. print project wise list of employees working on that project containing status of project and emp

name Solution create table employee (empno varchar2(10) primary key, name varchar2(20), address varchar2(20), city varchar2(10), deptname varchar2(20)); create table project (pno varchar2(10) primary key, pname varchar2(20), status varchar2(10)); create table emp_project (employee varchar2(10) references employee(empno), project varchar2(10) references project(pno), noofdays number(10));

1. select employee.name from employee, emp_project where employee.empno=emp_project.employee and emp_project.noofdays>25;

2. select employee.name from employee, project, emp_project where employee.empno=emp_project.employee and emp_project.project=project.pno and project.status='progressive';

3. select project.pname, count(employee.empno) from employee, emp_project, project where employee.empno=emp_project.employee and emp_project.project=project.pno and project.status='imcomplete';

4. select emplyee.name, project.status from employee, project, emp_project group by project.pno;

Page 16: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 14 Write PL/SQL block of code for inverting a number 5639 to 9365 Solution DECLARE given_number VARCHAR(5):='5639'; str_length NUMBER(2); inverted_number VARCHAR(5); BEGIN str_length:=length(given_number); FOR counter IN REVERSE 1 .. str_length LOOP inverted number:=inverted_number||substr(given_number,counter,1); END LOOP; dbms_output.put_line(‘The given number is’ || given_number); dbms_output.put_line(‘The inverted number is’ || inverted_number); END;

Page 17: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 15 Write a PL/SQL block of code which accepts 3 numbers from user and print the second maximum number Solution DECLARE num1 NUMBER(2); num2 NUMBER(2); num3 NUMBER(2); BEGIN num1:=&num1; num2:=&num2; num3:=&num3; IF num1<num2 THEN IF num1>num3 THEN dbms_output.put_line(‘The second maximum number is’ || num1); END IF; END IF; IF num2<num1 THEN IF num2>num3 THEN dbms_output.put_line(‘The second maximum number is’ || num2); END IF; END IF; IF num3<num2 THEN IF num3>num1 THEN dbms_output.put_line(‘The second maximum number is’ || num3); END IF; END IF; END;

Page 18: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 16 Write PL/SQL code that will accept an account number from the user and debit amount of Rs. 2000 from account table if the balance of account remains minimum Rs. 500, the process is to be fired on Account table. Account (ac_Id, Name, bal) Solution DECLARE ac_bal NUMBER(10, 2); ac_no VARCHAR2(6); debit_amt NUMBER(5):=2000; min_bal CONSTANT NUMBER(5,2):=500; BEGIN ac_no:=&ac_no; SELECT bal INTO ac_bal FROM account WHERE ac_id=ac_no; ac_bal:=ac_bal - debit_amt; IF ac_bal >= min_bal THEN UPDATE accounts SET bal=bal – debit_amt WHERE ac_Id=ac_no; END IF; END;

Page 19: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 17 Write a PL/SQL block of code to accept 10 numbers in loop and print their sum and average Solution DECLARE num NUMBER(2); len NUMBER(2):=10; sum NUMBER(5):=0; avg NUMBER(5):=0; i NUMBER(2); BEGIN FOR i IN 1..len LOOP num:=&num; sum:=sum+num; END LOOP; avg:=sum/10; dbms_output.put_line(‘Sum of the numbers: ’ || sum); dbms_output.put_line(‘Average of the numbers: ’ || avg); END;

Page 20: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 18 Write a PL/SQL block of code for following pattern 1 2 3 4 1 2 3 1 2 1 Solution DECLARE i NUMBER(1):=5; j NUMBER(1):=5; BEGIN FOR i IN REVERSE 1..5 LOOP FOR j in reverse 1..5 LOOP dbms_output.put_line(j); END LOOP; dbms_output.put_line(\n); END LOOP; END;

Page 21: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 19 Write a procedure to swap digits of a two digit number Solution DECLARE original_number NUMBER(2); new_number NUMBER(2); BEGIN original_number:=&original_number; new_number:=(original_number%10)*10+(original_number/10); dbms_output.putline('Original number: '||original_number); dbms_output.putline('Reversed number: '||new_number); END;

Page 22: JOURNAL FOR DATABASE MANAGEMENT SYSTEM ORACLE7. list all the clients who are located in mumbai 8. find the name of salesman who have salary=30000 9. change the city of client_no c005

[email protected]

Assignment 20 The HRD manager has decided to raise the salary for all employees in department number 20 by 0.5. Whenever any such raise is given to employee, a record for the same is maintained in emp_raise table. It includes (emp_no, date, when raise was given and actual raise). Write PL/SQL block using cursor to update salary of each employee and insert a record in emp_raise table. Solution DECLARE salary NUMBER(10,2); employee NUMBER(5); CURSOR salary_cursor IS SELECT empno,sal FROM employee_master WHERE dept_no=20; BEGIN OPEN salary_cursor; LOOP FETCH salary_cursor INTO salary,employee; EXIT WHEN salary_cursor%NOTFOUND; END LOOP; dbms_output.putline('Emp. No. :'||employee||'Date:'||sysdate||'Old Salary:'||salary); UPDATE employee_master SET salary=salary+salary*0.5 WHERE empno=employee; INSERT INTO emp_raise(emp_no,date,sal) VALUES(employee,sysdate,salary); COMMIT; dbms_output.putline('Emp. No. :'||employee||'Date:'||sysdate||'New Salary:'||salary); END;

- Ankit Gupta