0% found this document useful (0 votes)
31 views5 pages

Ubd Lab3

The document describes a series of exercises using PL/SQL blocks to manipulate data in tables. It has the user modify and run blocks to: select data from tables; declare and assign variables; insert, update, and delete rows; and use savepoints and rollbacks. The goal is to predict the outcome of running each block and test the predictions by executing the blocks.

Uploaded by

Laurentiu Danuta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Ubd Lab3

The document describes a series of exercises using PL/SQL blocks to manipulate data in tables. It has the user modify and run blocks to: select data from tables; declare and assign variables; insert, update, and delete rows; and use savepoints and rollbacks. The goal is to predict the outcome of running each block and test the predictions by executing the blocks.

Uploaded by

Laurentiu Danuta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Tema nr.

3
Observație!
Scrieți rezolvarea direct în acest document!

1. Enter and run the following anonymous block, observing that it executes successfully.

DECLARE
v_emp_lname employees.last_name%TYPE;
v_emp_salary employees.salary%TYPE;
BEGIN
SELECT last_name, salary INTO v_emp_lname, v_emp_salary
FROM employees
WHERE job_id = 'AD_PRES';
DBMS_OUTPUT.PUT_LINE(v_emp_lname||' '||v_emp_salary);
END;

A. Now modify the block to use ‘IT_PROG’ instead of ‘AD_PRES’ and re-run it. Why does
it fail this time?
Comanda into returneaza mai multe inregistrari
B. Now modify the block to use ‘IT_PRAG’ instead of ‘IT_PROG’ and re-run it. Why does
it still fail?
Nu exista angajati cu acest id

The following questions use a copy of the departments table. Execute the following SQL
statement to create the copy table.
CREATE TABLE new_depts AS SELECT * FROM departments;

2. Examine and run the following PL/SQL code, which obtains and displays the maximum
department_id from new_depts.

DECLARE
v_max_deptno new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: '|| v_max_deptno);
END;

The maximum department id is: 190

3. Modify the code to declare two additional variables, (assigning a new department name to one
of them) by adding the following two lines to your Declaration section:
v_dept_name new_depts.department_name%TYPE:= 'A New Department' ;
v_dept_id new_depts.department_id%TYPE;

DECLARE
v_max_deptno new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE:=’IT’;
v_dept_id new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE(‘The maximum department id is: ‘|| v_max_deptno):
END;

4. Modify the code to add 10 to the current maximum department number and assign the result to
v_dept_id.

DECLARE
v_max_deptno new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE:=’IT’;
v_dept_id new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE(‘The maximum department id is: ‘|| v_max_deptno):
v_dept_id:= v_max_deptno+10;
DBMS_OUTPUT.PUT_LINE(‘v_dept_id is: ‘|| v_dept_id);
END;

5. Modify the code to include an INSERT statement to insert a new row into the new_depts
table, using v_dept_id and v_dept_name to populate the department_id and department_name
columns. Insert NULL into the location_id and manager_id columns. Save your code.

DECLARE
v_max_deptno new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE:=’IT’;
v_dept_id new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE(‘The maximum department id is: ‘|| v_max_deptno):
v_dept_id:= v_max_deptno+10;
DBMS_OUTPUT.PUT_LINE(‘v_dept_id is: ‘|| v_dept_id);
INSERT INTO new_depts(department_id, department_name,location_id,manager_id)
VALUES(v_dept_id,v_dept_name, NULL,NULL);
END;
6. Execute the block and check that the new row has been inserted.

7. Now modify the code to use SQL%ROWCOUNT to display the number of rows inserted, and
execute the block again.

DECLARE
v_max_deptno new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE:=’IT’;
v_dept_id new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE(‘The maximum department id is: ‘|| v_max_deptno):
v_dept_id:= v_max_deptno+10;
DBMS_OUTPUT.PUT_LINE(‘v_dept_id is: ‘|| v_dept_id);
INSERT INTO new_depts(department_id, department_name,location_id,manager_id)
VALUES(v_dept_id,v_dept_name, NULL,NULL);
DBMS_OUTPUT.PUT_LINE(‘Number of rows modified: ‘|| TO_CHAR(SQL
%ROWCOUNT));
END;

8. Now modify the block, removing the INSERT statement and adding a statement that will
UPDATE all rows with location_id = 1700 to location_id = 1400. Execute the block again to see
how many rows were updated.

DECLARE
v_max_deptno new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE:=’IT’;
v_dept_id new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE(‘The maximum department id is: ‘|| v_max_deptno):
v_dept_id:= v_max_deptno+10;
DBMS_OUTPUT.PUT_LINE(‘v_dept_id is: ‘|| v_dept_id);
UPDATE new_depts
SET LOCATION_ID=1400
WHERE LOCATION_ID=1700;
INSERT INTO new_depts(department_id, department_name,location_id,manager_id)
DBMS_OUTPUT.PUT_LINE(‘Number of rows modified: ‘|| TO_CHAR(SQL
%ROWCOUNT));
END;

9. Create the endangered_species table by running the following statement in Application


Express:

CREATE TABLE endangered_species


(species_id NUMBER(4) CONSTRAINT es_spec_pk PRIMARY KEY,
common_name VARCHAR2(30) CONSTRAINT es_com_name_nn NOT NULL,
scientific_name VARCHAR2(30) CONSTRAINT es_sci_name_nn NOT NULL);

Table created.

10. Examine the following block. If you were to run this block, what data do you think would be
saved in the database?

BEGIN
INSERT INTO endangered_species
VALUES (100, 'Polar Bear','Ursus maritimus');
SAVEPOINT sp_100;
INSERT INTO endangered_species
VALUES (200, 'Spotted Owl','Strix occidentalis');
SAVEPOINT sp_200;
INSERT INTO endangered_species
VALUES (300, 'Asiatic Black Bear','Ursus thibetanus');
ROLLBACK TO sp_100;
COMMIT;
END;

100,’Plar Bear’,’Ursusmaritimus’

11. Run the block to test your theory. Select from the table to confirm the result.

SELECT * FROM endangered_species

12. Examine the following block. If you were to run this block, what data do you think would be
saved in the database?
BEGIN
INSERT INTO endangered_species
VALUES (400, 'Blue Gound Beetle','Carabus intricatus');
SAVEPOINT sp_400;
INSERT INTO endangered_species
VALUES (500, 'Little Spotted Cat','Leopardus tigrinus');
ROLLBACK;
INSERT INTO endangered_species
VALUES (600, 'Veined Tongue-Fern','Elaphoglossum nervosum');
ROLLBACK TO sp_400;
END;

Nu se insereaza nimic, dar se genereaza o eroare deoarece primul rollback sterge comenzile
anterioare, inclusive declararea punctului de safepoint sp_400=>ROLLBACK TO sp_400 nu are
unde sa revina.

13. Run the block to test your theory.

You might also like