Base Schema SQL Code
Base Schema SQL Code
Statement ideas:
- From oracle tutorials index-by tables example (explicit cursor, loop, while)
- 3. From oracle tutorials cursors: increase salary or scholarship using IMPLICIT cursor and using sql
%rowcount to see how many rows have been affected. Increase the bonus with 500 of those staff
members whose bonus is null. Show how many staff members were affected.
DECLARE
total_rows number(2);
BEGIN
UPDATE STAFF
SET bonus = 500
WHERE bonus IS NULL;
IF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' staff members selected ');
END IF;
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('All staff members already have bonuses!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
- Package from the package tut to display salary of a staff member by entering the id
========================Package specification==========================================
END stud_package;
/
===========Package body=========================================================
PROCEDURE listStudent IS
CURSOR c_students is
SELECT stud_name, stud_surname FROM students;
TYPE s_list is TABLE OF students.stud_name%type;
full_name_list s_list := s_list();
aux integer :=0;
BEGIN
FOR n IN c_students LOOP
aux := aux +1;
full_name_list.extend;
full_name_list(aux) := n.stud_name||’ ‘||n.stud_surname;
dbms_output.put_line('Student(' ||aux|| ')'||name_list(aux));
END LOOP;
END listStudent;
END stud_package;
/
=================Package calls==========================================
DECLARE
x art_pieces.id_art_pk%type:= 100011;
BEGIN
stud_package.addStudent(220, 'Andrew', 'Lincoln', ’[email protected]’, 300,
’02/10/2023’);
stud_package.addArtPiece(100022, 220, 05/08/2023, 'oil portrait', 'naturalism');
stud_package.addMatReg(’m09’, 100022, 1, ’piece’, ’04/13/2023’ );
stud_package.addMatReg(’m01’, 100022, 5, ’piece’, ’04/13/2023’ );
stud_package.addMatReg(’m04’, 100022, 11, ’tube’, ’04/13/2023’ );
stud_package.delArtPiece(x);
stud_package.listStudents;
END;
/
- 4. Find out the total cost of the materials used to create a specific art piece.
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line(’No art piece found.’);
WHEN others THEN
dbms_output.put_line(’Error!’);
END;
END get_production_cost;
- How many art pieces were sold at an exposition and increase the scholarship of those students
whose art pieces sold for more than the average price of all sold art pieces from all expositions
(can be an exception if there are no such students)
END;
UPDATE staff
SET salary = 5379
WHERE id_staff_pk = 219;
UPDATE students
SET scholarship = 5783
WHERE id_stud_pk = 206;
BEGIN
INSERT INTO Materials(id_material, article_name, brand, in_stock, price_per_unit) VALUES (m01, ‘paintbrush’,
‘Rembrandt’, 573, 2)
INSERT INTO Materials(id_material, article_name, brand, in_stock, price_per_unit) VALUES (m02, ‘clay’,
‘Rembrandt’, 200, 6)
INSERT INTO Materials(id_material, article_name, brand, in_stock, price_per_unit) VALUES (m03, ‘acrylic paint’,
‘Artist Acryl’, 700, 10)
INSERT INTO Materials(id_material, article_name, brand, in_stock, price_per_unit) VALUES (m04, ‘oil paint’,
‘Phoenix’, 841, 20)
INSERT INTO Materials(id_material, article_name, brand, in_stock, price_per_unit) VALUES (m05, ‘graphite
pens’, ‘Derwent’, 400, 8)
INSERT INTO Materials(id_material, article_name, brand, in_stock, price_per_unit) VALUES (m06, ‘coal,
‘Rembrandt’, 260, 8)
INSERT INTO Materials(id_material, article_name, brand, in_stock, price_per_unit) VALUES (m07, ‘wood block’,
‘Creotime’, 150, 4)
INSERT INTO Materials(id_material, article_name, brand, in_stock, price_per_unit) VALUES (m08, ‘sculpting
tools’, ‘Rembrandt’, 120, 30)
INSERT INTO Materials(id_material, article_name, brand, in_stock, price_per_unit) VALUES (m09, ‘canvas’,
‘Rembrandt’, 300, 18)
INSERT INTO Materials(id_material, article_name, brand, in_stock, price_per_unit) VALUES (m10, ‘paper
booklets’, ‘Clairefontaine’, 200, 10)
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m01’, ‘s01’, 50,
100, to_date(’10-10-2019’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m02’, ‘s02’,
200, 1200, to_date(’10-10-2019’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m03’, ‘s03’, 60,
600, to_date(’10-10-2019’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m04’, ‘s04’, 30,
600, to_date(’10-10-2019’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m05’, ‘s05’, 70,
560, to_date(’10-10-2019’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m06’, ‘s06’,
100, 800, to_date(’10-10-2019’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m07’, ‘s07’, 20,
80, to_date(’10-10-2019’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m08’, ‘s08’, 27,
810, to_date(’10-10-2019’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m09’, ‘s09’, 30,
540, to_date(’10-10-2019’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m10’, ‘s10’, 20,
200, to_date(’10-10-2019’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m01’, ‘s01’, 80,
160, to_date(’05-03-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m02’, ‘s02’, 40,
240, to_date(’05-03-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m05’, ‘s05’,
150, 1200, to_date(’05-03-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m06’, ‘s06’, 35,
280, to_date(’05-03-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m08’, ‘s08’, 15,
450, to_date(’05-03-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m02’, ‘s02’, 78,
468, to_date(’17-06-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m03’, ‘s03’, 80,
800, to_date(’17-06-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m04’, ‘s04’, 50,
1000, to_date(’17-06-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m06’, ‘s06’, 20,
160, to_date(’17-06-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m07’, ‘s07’, 70,
240, to_date(’23-09-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m01’, ‘s01’, 60,
120, to_date(’23-09-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m07’, ‘s07’, 70,
240, to_date(’23-09-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m09’, ‘s09’, 45,
810, to_date(’23-09-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m10’, ‘s10’, 80,
800, to_date(’23-09-2020’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m02’, ‘s02’,
100, 600, to_date(’14-02-2021’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m03’, ‘s03’,
120, 1200, to_date(’14-02-2021’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m01’, ‘s01’, 85,
170, to_date(’28-05-2021’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m04’, ‘s04’, 80,
1600, to_date(’28-05-2021’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m02’, ‘s02’, 25,
300, to_date(’13-11-2021’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m05’, ‘s05’, 60,
480, to_date(’13-11-2021’,’DD-MM-RRRR’))
INSERT INTO Material_orders(id_material, id_supplier, quantity, total_cost, order_date) VALUES(‘m06’, ‘s06’, 30,
240, to_date(’13-11-2021’,’DD-MM-RRRR’))