Submission to the Blackboard Is Due on March 21 and Demonstration Is Due on March 23 in Class

Submission to the Blackboard Is Due on March 21 and Demonstration Is Due on March 23 in Class

A Lab on PL/SQL

Submission to the Blackboard is due on March 21 and demonstration is due on March 23 in class.

Preparation (20 points):

  1. Execute create_mail.sql and insert_object.sql scripts.
  2. Execute p2.sql, p3.sql, p4.sql, p5.sql, p6.sql by typing: start p2
  3. p2.sql gets all the rows from the customers table and prints the names of the customers on the screen.
  4. p3.sql contains a procedure that accepts as input a customer number and returns the name, phone, and city values for that customers. If such a customer is not found, status is returned as false; otherwise, status is returned as true.
  5. p4.sql contains a function declaration. Given an employee number, the function computes and returns the total sales for that employee.
  6. P5.sql contains stored function that takes a customer number as input and returns the city in which the customer lives. A stored procedure can be invoked from various environments, including an SQL statement, another stored function or procedure, an anonymous block or a procedure or function defined within it, an embedded SQL program, a database trigger, and Oracle tools. The stored function get_city can be called by typing: select cno, cname, get_city(cno);
  7. p6.sql defines a package called process_orderscontaing three procedures. add_order takes as input an order number, customer number, employee number, and received date and tries to insert a new row in the orders table. add_order_details receives as input an order number, part number, and quantity and attempts to add a row corresponding to the input in the odetails table. ship_order takes as input an order number and a shipped date and tries to update the shipped value for the order.
  8. Execute trig2.sql, trig2test.sql, trig3.sql
  9. In trig2.sql, a trigger is executed when a row is inserted into the odetails table. the trigger checks to see if the quantity ordered is more than the quantity on hand. If it is, an error message is generated, and the row is not inserted. Otherwise, the trigger updates the quantity on hand for the part and checks to see if it has fallen below the reorder level. If it has, it sends a row to the restock table indicating that the part needs to be reordered. You may need create a restock table here, with the first column is TDATE, and the second column is PNO.
  10. trig2test.sql is executed to create a new order.
  11. trig3 is defined on the parts table and is triggered when the price column is updated. Each time someone updates the price of a particular part, the trigger makes an entry in a log file of this update along with the userid of the person performing the update and the date the update. The log file is created using

create table parts_log(

pnonumber(5),

usernamechar(8),

update_datedate,

old_pricenumber(6,2),

new_pricenumber(6,2));

  1. Test trig3.sql by typing:

update parts set price=55.00 where pno=10900;

select * from parts_log;

  1. Create Grade book database using scripts of create_grade.sql and insert_grades.sql

Problem one (40 points): Write a PL/SQL procedure that finds the student with the highest overall average in every course. In case there is more than one student tied for the highest average, the procedure should return all of the students. The results should be returned in a PL/SQL table of records, where each record has the field term, line number, course title, student ID, student name, and overall average. Also, write an anonymous PL/SQL block that makes a call to the procedure and prints the results to the standard output. Refer to p4.sql for some ideas.

Problem two (40 points): Write a trigger that fires when a row is deleted from the enrolls table. The trigger should record the dropped student’s scores in a temporary table, called deleted_scores, and cascade the deletes to ensure that the referential integrity constraints are maintained.

Additional SQL exercises:

Populate the mail-order database, using scripts of create_mail.sql and insert_object.sql. Also use SQL insert statements to create at least 30 customers, 10 employees, 5 zip codes, and 50 parts. Also insert around 100 orders (an average of about 3 per customer), with each order containing an average of 2 parts.

EMPLOYEES(ENO, ENAME, ZIP, HDATE)

PARTS(PNO, PNAME, QOH, PRICE, LEVEL)

CUSTOMERS(CNO, CNAME, STREET, ZIP, PHONE)

ORDERS(ONO, CNO, ENO, RECEIVED, SHIPPED)

ODETAILS(ONO, PNO, QTY)

ZIPCODES(ZIP,CITY)

Write SQL expressions that answer the following queries:

  1. Get the name of parts that cost less than $20.
  2. Get the names and cities of employees who have taken orders for parts costing more than $50.
  3. Get the pairs of customer number values of customers having the same zip code.
  4. Get the names of customers who have ordered parts from employees living in Wichita.
  5. Get the names of customers who have ordered parts only from employees living in Wichita.
  6. Get the names of customers who have ordered all parts costing less than $20.
  7. Get the names of employees along with their total sales for the year 1995.
  8. Get the numbers of names of employees who have never made a sale to a customer living in the same zip code as the employee.
  9. Get the names of customers who have placed the highest number of orders.
  10. Get the names of customers who have placed the most expensive orders.
  11. Get the names of parts that have been ordered the most (in terms of quantity ordered, not number of orders.)
  12. Get the names of parts along with the number of orders they appear in, sorted in decreasing order of the number of orders.
  13. Get the average waiting time for all orders in number of days. The waiting time for an order is defined as the difference between the shipped date and the received date. Note, the dates should be truncated to 12:00am so that the differences is always a whole number of days.
  14. Get the names of customers who had to wait the longest for their orders to be shipped.
  15. For all orders greater than $100, get the order number and the waiting time for the order.

Appendix A:

------

-- create_mail.sql

-- Mail Order Database; Create Tables Script

-- Chapter 2; Oracle 9i Programming -- A Primer

-- by R. Sunderraman

------

drop table zipcodes cascade constraints;

create table zipcodes (

zip number(5),

city varchar2(30),

primary key (zip));

drop table employees cascade constraints;

create table employees (

eno number(4) not null primary key,

ename varchar2(30),

zip number(5) references zipcodes,

hdate date);

drop table parts cascade constraints;

create table parts(

pno number(5) not null primary key,

pname varchar2(30),

qoh integer check(qoh >= 0),

price number(6,2) check(price >= 0.0),

olevel integer);

drop table customers cascade constraints;

create table customers (

cno number(5) not null primary key,

cname varchar2(30),

street varchar2(30),

zip number(5) references zipcodes,

phone char(12));

drop table orders cascade constraints;

create table orders (

ono number(5) not null primary key,

cno number(5) references customers,

eno number(4) references employees,

received date,

shipped date);

drop table odetails cascade constraints;

create table odetails (

ono number(5) not null references orders,

pno number(5) not null references parts,

qty integer check(qty > 0),

primary key (ono,pno));

------

-- insert_object.sql

-- Grade Book Database; Create Tables Script

-- Chapter 2; Oracle 9i Programming -- A Primer

-- by R. Sunderraman

------

insert into o_employees values

(1000,

person_type('Jones',

address_type('123 Main St','Wichita','KS',67226),

phones_varray_type('316-555-1212',null,null)),

'12-DEC-95');

insert into o_employees values

(1001,

person_type('Smith',

address_type('101 Elm St','Fort Dodge','KS',60606),

phones_varray_type('316-555-2121','316-555-2323',null)),

'01-JAN-92');

insert into o_employees values

(1002,

person_type('Brown',

address_type('100 Elm St','Kansas City','KS',50302),

phones_varray_type('780-555-1111',null,null)),

'01-SEP-94');

insert into o_parts values

(10506,'Land Before Time I',200,19.99,20);

insert into o_parts values

(10507,'Land Before Time II',156,19.99,20);

insert into o_parts values

(10508,'Land Before Time III',190,19.99,20);

insert into o_parts values

(10509,'Land Before Time IV',60,19.99,20);

insert into o_parts values

(10601,'Sleeping Beauty',300,24.99,20);

insert into o_parts values

(10701,'When Harry Met Sally',120,19.99,30);

insert into o_parts values

(10800,'Dirty Harry',140,14.99,30);

insert into o_parts values

(10900,'Dr. Zhivago',100,24.99,30);

insert into o_customers values

(1111,

person_type('Charles',

address_type('123 Main St','Wichita','KS',67226),

phones_varray_type('316-636-5555',null,null)));

insert into o_customers values

(2222,

person_type('Bertram',

address_type('237 Ash Avenue','Wichita','KS',67226),

phones_varray_type('316-689-5555','316-689-5556',null)));

insert into o_customers values

(3333,

person_type('Barbara',

address_type('111 InwoodSt','Fort Dodge','KS',60606),

phones_varray_type('316-111-1234','316-111-1235',null)));

insert into o_orders values

(1020,

odetails_ntable_type(odetails_type(10506,1),

odetails_type(10507,1),

odetails_type(10508,2),

odetails_type(10509,3)),

1111,1000,'10-DEC-94','12-DEC-94');

insert into o_orders values

(1021,

odetails_ntable_type(odetails_type(10601,4)),

1111,1000,'12-JAN-95','15-JAN-95');

insert into o_orders values

(1022,

odetails_ntable_type(odetails_type(10601,1),

odetails_type(10701,1)),

2222,1001,'13-FEB-95','20-FEB-95');

insert into o_orders values

(1023,

odetails_ntable_type(odetails_type(10800,1),

odetails_type(10900,1)),

3333,1000,'20-JUN-97',null);

Appendix B;

------

-- create_grade.sql

-- Grade Book Database; Create Tables Script

-- Chapter 2; Oracle 9i Programming -- A Primer

-- by R. Sunderraman

------

drop table catalog cascade constraints;

create table catalog (

cno varchar2(7) not null,

ctitle varchar2(50),

primary key (cno));

drop table students cascade constraints;

create table students (

sid varchar2(5) not null,

fname varchar2(20),

lname varchar2(20) not null,

minit char,

primary key (sid));

drop table courses cascade constraints;

create table courses (

term varchar2(10) not null,

lineno number(4) not null,

cno varchar2(7) not null,

a number(2) check(a > 0),

b number(2) check(b > 0),

c number(2) check(c > 0),

d number(2) check(d > 0),

primary key (term,lineno),

foreign key (cno) references catalog);

drop table components cascade constraints;

create table components (

term varchar2(10) not null,

lineno number(4) not null check(lineno >= 1000),

compname varchar2(15) not null,

maxpoints number(4) check(maxpoints >= 0),

weight number(2) check(weight>=0),

primary key (term,lineno,compname),

foreign key (term,lineno) references courses);

drop table enrolls cascade constraints;

create table enrolls (

sid varchar2(5) not null,

term varchar2(10) not null,

lineno number(4) not null,

primary key (sid,term,lineno),

foreign key (sid) references students,

foreign key (term,lineno) references courses);

drop table scores cascade constraints;

create table scores (

sid varchar2(5) not null,

term varchar2(10) not null,

lineno number(4) not null,

compname varchar2(15) not null,

points number(4) check(points >= 0),

primary key (sid,term,lineno,compname),

foreign key (sid,term,lineno) references enrolls,

foreign key (term,lineno,compname) references components);

------

-- insert_grades.sql

-- Grade Book Database: Insert Rows

-- Chapter 2; Oracle 9i Programming -- A Primer

-- by R. Sunderraman

------

insert into catalog values

('csc226','Introduction to Programming I');

insert into catalog values

('csc227','Introduction to Programming II');

insert into catalog values

('csc343','Assembly Programming');

insert into catalog values

('csc481','Automata and Formal Languages');

insert into catalog values

('csc498','Introduction to Database Systems');

insert into catalog values

('csc880','Deductive Databases and Logic Programming');

insert into students values

('1111','Nandita','Rajshekhar','K');

insert into students values

('2222','Sydney','Corn','A');

insert into students values

('3333','Susan','Williams','B');

insert into students values

('4444','Naveen','Rajshekhar','B');

insert into students values

('5555','Elad','Yam','G');

insert into students values

('6666','Lincoln','Herring','F');

insert into courses values

('f96',1031,'csc226',90,80,65,50);

insert into courses values

('f96',1032,'csc226',90,80,65,50);

insert into courses values

('sp97',1031,'csc227',90,80,65,50);

insert into components values

('f96',1031,'exam1',100,30);

insert into components values

('f96',1031,'quizzes',80,20);

insert into components values

('f96',1031,'final',100,50);

insert into components values

('f96',1032,'programs',400,40);

insert into components values

('f96',1032,'midterm',100,20);

insert into components values

('f96',1032,'final',100,40);

insert into components values

('sp97',1031,'paper',100,50);

insert into components values

('sp97',1031,'project',100,50);

insert into enrolls values

('1111','f96',1031);

insert into enrolls values

('2222','f96',1031);

insert into enrolls values

('4444','f96',1031);

insert into enrolls values

('1111','f96',1032);

insert into enrolls values

('2222','f96',1032);

insert into enrolls values

('3333','f96',1032);

insert into enrolls values

('5555','sp97',1031);

insert into enrolls values

('6666','sp97',1031);

insert into scores values

('1111','f96',1031,'exam1',90);

insert into scores values

('1111','f96',1031,'quizzes',75);

insert into scores values

('1111','f96',1031,'final',95);

insert into scores values

('2222','f96',1031,'exam1',70);

insert into scores values

('2222','f96',1031,'quizzes',40);

insert into scores values

('2222','f96',1031,'final',82);

insert into scores values

('4444','f96',1031,'exam1',83);

insert into scores values

('4444','f96',1031,'quizzes',71);

insert into scores values

('4444','f96',1031,'final',74);

insert into scores values

('1111','f96',1032,'programs',400);

insert into scores values

('1111','f96',1032,'midterm',95);

insert into scores values

('1111','f96',1032,'final',99);

insert into scores values

('2222','f96',1032,'programs',340);

insert into scores values

('2222','f96',1032,'midterm',65);

insert into scores values

('2222','f96',1032,'final',95);

insert into scores values

('3333','f96',1032,'programs',380);

insert into scores values

('3333','f96',1032,'midterm',75);

insert into scores values

('3333','f96',1032,'final',88);

insert into scores values

('5555','sp97',1031,'paper',80);

insert into scores values

('5555','sp97',1031,'project',90);

insert into scores values

('6666','sp97',1031,'paper',80);

insert into scores values

('6666','sp97',1031,'project',85);

commit;

1