PLSQL 3 2 Practice 1630353

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6
At a glance
Powered by AI
Some key takeaways from the document are that PL/SQL allows embedding SQL statements to retrieve and manipulate data. Variables need to be declared with proper datatypes to store retrieved data. Column names, table names and variables cannot be duplicated.

To retrieve data in PL/SQL, a SELECT statement is used to select the required columns into variables of compatible datatypes. The INTO clause is used to store the retrieved column values into variables.

Valid SQL statements in PL/SQL include SELECT, INSERT, UPDATE, DELETE. Not valid statements include DDL statements like CREATE, ALTER, DROP and GRANT/REVOKE. DML statements are allowed but some like ALTER USER are not.

www.oracle.

com/academy

Jesús Emanuel Galvan Díaz 1630353

Database Programming with PL/SQL


3-2: Retrieving Data in PL/SQL
Practice Activities
Vocabulary
No new vocabulary for this lesson

Try It / Solve It
1. State whether each of the following SQL statements can be included directly in a PL/SQL block.

Statement Valid in Not Valid in


PL/SQL PL/SQL

ALTER USER SET password = 'oracle'; x

CREATE TABLE test (a NUMBER); x

DROP TABLE test; x

SELECT emp_id INTO v_id FROM employees; x

GRANT SELECT ON employees TO PUBLIC; x

INSERT INTO grocery_items (product_id, brand, x


description) VALUES (199, 'Coke', 'Soda');

REVOKE UPDATE ON employees FROM PUBLIC; x

ALTER TABLE employees x


RENAME COLUMN employee_id TO emp_id;

DELETE FROM grocery_items x


WHERE description = 'Soap';

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
2

2. Create a PL/SQL block that selects the maximum department_id in the departments table and
stores it in the v_max_deptno variable. Display the maximum department_id. Declare
v_max_deptno to be the same datatype as the department_id column. Include a SELECT
statement to retrieve the highest department_id from the departments table. Display the variable
v_max_deptno.

DECLARE v_max_deptno depataments.departament_id%TYPE;


BEGIN
SELECT MAX(departamento_id) INTO v_max_deptno FROM departments;
DBMS_OUTPUT.PUT_LINE (‘el departamento es: ’|| v_max_deptno);
END;
3. The following code is supposed to display the lowest and highest elevations for a country name
entered by the user. However, the code does not work. Fix the code by following the guidelines for
retrieving data that you learned in this lesson.

DECLARE
v_country_name countries.country_name%TYPE := Federative Republic of Brazil;
v_lowest_elevation countries.lowest_elevation%TYPE; v_highest_elevation
countries.highest_elevation%TYPE;
BEGIN
SELECT lowest_elevation, highest_elevation
FROM countries;
DBMS_OUTPUT.PUT_LINE('The lowest elevation in '
|| v_country_name || ' is ' || v_lowest_elevation
|| ' and the highest elevation is ' || v_highest_elevation || '.');
END;

DECLARE
v_country_name countries.country_name%TYPE := 'Federative Republic of Brazil';
v_lowest_elevation countries.lowest_elevation%TYPE;
v_highest_elevation countries.highest_elevation%TYPE;
BEGIN
SELECT MIN(lowest_elevation), MAX(highest_elevation) INTO v_lowest_elevation ,
v_highest_elevation
FROM countries;
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
3

DBMS_OUTPUT.PUT_LINE('The lowest elevation in ' || v_country_name || ' is ' ||


v_lowest_elevation || ' and the highest elevation is ' || v_highest_elevation || '.');
END;

4. Run the following anonymous block. It should execute 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;
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?
Por que hay varios empleados con el mismo ID

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
4

B. Now modify the block to use ‘IT_PRAG’ instead of ‘IT_PROG’ and re-run it. Why does it still
fail?
Porque IT_PRAG no existe.

5. Use (but don't execute) the following code to answer this question:

DECLARE
last_name VARCHAR2(25) := 'Fay';
BEGIN
UPDATE emp_dup SET first_name = 'Jennifer'
WHERE last_name = last_name;
END;

What do you think would happen if you ran the above code? Write your answer here and then
follow the steps below to test your theory.
Marcará error porque hay redundancia
“WHERE last_name = last_name;”

A. Create a table called emp_dup that is a duplicate of employees.


CREATE TABLE emp_dup as
(select * from employees);

B. Select the first_name and last_name values for all rows in emp_dup.
SELECT first_name, last_name
FROM emp_dup;

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
5

C. Run the anonymous PLSQL block shown at the beginning of this question.

D. Select the first_name and last_name columns from emp_dup again to confirm your
theory.

E. Now we are going to correct the code so that it changes only the first name for the employee
whose last name is “Fay”. Drop emp_dup and re-create it.
drop table emp_dup;

F. Modify the code shown at the beginning of this question so that for the employee whose
last_name = ”Fay”, the first_name is updated to Jennifer. Run your modified block.
DECLARE
V_LAST_NAME VARCHAR2(25) := ‘FAY’;
BEGIN
UPDATE EMP_DUP SET FISRT_NAME = ‘JENNIFER’
WHERE LAST_NAME = V_LAST_NAME;
END;

G. Confirm that your update statement worked correctly.


Al ejecutarlo corrió correctamente.

6. Is it possible to name a column in a table the same name as the table? Create a table to test this
question. Don't forget to populate the table with data.
CREATE TABLE GRPCERY_ITEM(
PRODUCT_ID NUMBER(6,0) PRIMARY KEY,
BRAND VARCHAR2(100),
GROCERY_ITEM VARCHAR(100)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
6

);
INSERT INTO GROCERY ITEM (PRODUCT_ID, BRAND, GROCERY_ITEM) VALUES
(100,’COLGATE’, ‘PASTA DENTAL’);
SI ES POSIBLE

7. Is it possible to have a column, table, and variable, all with the same name? Using the table you
created in the question above, write a PL/SQL block to test your theory.
CREATE TABLE GRPCERY_ITEM(

GROCERY_ITEM NUMBER(6,0) PRIMARY KEY,

GROCERY_ITEM VARCHAR2(100),

GROCERY_ITEM VARCHAR(100)

);

NO ES POSIBLE

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.

You might also like