0% found this document useful (1 vote)
328 views3 pages

Database Programming With PL/SQL 3-3: Practice Activities

The document provides examples of PL/SQL code using implicit and explicit cursors to manipulate data in tables using DML statements like SELECT, INSERT, UPDATE, DELETE. It includes practice questions walking through modifying blocks of PL/SQL code to retrieve data from tables, insert and update rows, and use attributes like SQL%ROWCOUNT to return the number of rows affected by DML statements.

Uploaded by

Naski Kuafni
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (1 vote)
328 views3 pages

Database Programming With PL/SQL 3-3: Practice Activities

The document provides examples of PL/SQL code using implicit and explicit cursors to manipulate data in tables using DML statements like SELECT, INSERT, UPDATE, DELETE. It includes practice questions walking through modifying blocks of PL/SQL code to retrieve data from tables, insert and update rows, and use attributes like SQL%ROWCOUNT to return the number of rows affected by DML statements.

Uploaded by

Naski Kuafni
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 3

academy.oracle.

com

Database Programming with PL/SQL


3-3: Manipulating Data in PL/SQL
Practice Activities
Vocabulary
Identify the vocabulary word for each definition below:

Select Defined automatically by Oracle for all SQL data manipulation statements,
and for queries that return only one row.
DML Defined by the programmer for queries that return more than one row.

Merge Statement selects rows from one table to update and/or insert into
another table. The decision whether to update or insert into the target
table is based on a condition in the ON clause.
Insert Statement adds new rows to the table.
Delete Statement removes rows from the table.
Update Statement modifies existing rows in the table.

Try It / Solve It
1. True or False: When you use DML in a PL/SQL block, Oracle uses explicit cursors to track
the data changes.
FALSE

2. SQL%FOUND, SQL%NOTFOUND, and SQL%ROWCOUNT are attributes and are available


when you use Implicit cursors.
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

3. Examine and run the following PL/SQL code, which obtains and displays the maximum
department_id from new_depts. What is the maximum department id?
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;

270

4. 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;

set serveroutput on;


DECLARE
v_max_deptno new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE := 'A New Department';
v_dept_id new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno FROM new_depts;
SELECT department_name, department_id into v_dept_name, v_dept_id from
new_depts where department_id = v_max_deptno;
v_dept_name := 'A New Department: ' || v_dept_name;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: ' || v_dept_id);
DBMS_OUTPUT.PUT_LINE('The maximum department name is: ' || v_dept_name);
END;

5. Modify the code to add 10 to the current maximum department number and assign the result
to v_dept_id.
set serveroutput on;
DECLARE
v_dept_id new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_dept_id FROM new_depts;
select department_name into v_dept_name from new_depts where department_id =
v_dept_id;
update new_depts set department_id = department_id + 10 where v_dept_id =
department_id;
SELECT department_id into v_dept_id FROM new_depts where department_name =
v_dept_name;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: ' || v_dept_id);
END;

6. 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. Execute your code and
confirm that the new row has been inserted.
set serveroutput on;
DECLARE
v_dept_id new_depts.department_id%TYPE := 300;
v_dept_name new_depts.department_name%TYPE := 'HR';
BEGIN
insert into new_depts
(department_id,department_name,manager_id,location_id)
VALUES (v_dept_id,v_dept_name,NULL,NULL);
END;
7. Now modify the code to use SQL%ROWCOUNT to display the number of rows inserted, and
execute the block again.
set serveroutput on;
DECLARE
v_dept_id new_depts.department_id%TYPE := 310;
v_dept_name new_depts.department_name%TYPE := 'New Department';
BEGIN
insert into new_depts
(department_id,department_name,manager_id,location_id)
VALUES (v_dept_id,v_dept_name,NULL,NULL);
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' insert row');
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.
set serveroutput on;
DECLARE
v_dept_id new_depts.department_id%TYPE := 310;
v_dept_name new_depts.department_name%TYPE := 'New Department';
v_count_update NUMBER(10);
BEGIN
delete from new_depts WHERE department_id = v_dept_id;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' delete row');
update new_depts set location_id = 1200 where location_id > 1400 or location_id <= 1700;
v_count_update := SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE(v_count_update || ' update row');
END;

You might also like