Vous êtes sur la page 1sur 7

1.

Write PL/SQL code to create a procedure for inserting a new prospect customer into the
prospect table.

CREATE OR REPLACE PROCEDURE new_prospect (


p_name IN customer.custname%TYPE,
p_make IN car.carmake%TYPE,
p_model IN car.carmodel%TYPE,
p_year IN car.caryear%Type,
p_color IN car.carcolor%Type,
p_trim IN car.cartrim%Type,
p_ocode IN options.optioncode%Type
)
AS
BEGIN
INSERT INTO prospect (custname, carmake, carmodel, caryear, carcolor,
cartrim, optioncode)
VALUES(p_name, p_make, p_model, p_year, p_color, p_trim, p_ocode);
END;

2. Create a script that uses substitution variables and the procedure created in question above to
enter a new prospect.

SET SERVEROUTPUT ON
SET HEADING OFF
SET VERIFY OFF

ACCEPT p_name PROMPT 'Enter prospect name: '


ACCEPT p_make PROMPT 'Enter make: '
ACCEPT p_model PROMPT 'Enter model: '
ACCEPT p_year PROMPT 'Enter year: '
ACCEPT p_color PROMPT 'Enter color: '
ACCEPT p_trim PROMPT 'Enter trim: '
ACCEPT p_code PROMPT 'Enter option code: '

BEGIN
new_prospect('&p_name', '&p_make', '&p_model', '&p_year',
'&p_color', '&p_trim', '&p_code');
COMMIT;
END;

3. Add to question 1 a user-defined excetion that would print "Car is too old" if the prospect is
asking for a car older that 6 years (2013) as well as a system-defined exception for handling all
other errors such as referential integrity, wrong data etc..

CREATE OR REPLACE PROCEDURE new_prospect (


p_name IN customer.custname%TYPE,
p_make IN car.carmake%TYPE,
p_model IN car.carmodel%TYPE,
p_year IN car.caryear%Type,
p_color IN car.carcolor%Type,
p_trim IN car.cartrim%Type,
p_ocode IN options.optioncode%Type
)
AS
old_car EXCEPTION;
BEGIN
IF TO_NUMBER(p_year) < 2013 THEN
RAISE old_car;
END IF;

INSERT INTO prospect (custname, carmake, carmodel, caryear, carcolor,


cartrim, optioncode)
VALUES(p_name, p_make, p_model, p_year, p_color, p_trim, p_ocode);
EXCEPTION
WHEN old_car THEN
DBMS_OUTPUT.PUT_LINE('Car is too old');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Some error occurred');
END;

4. Write a function that calculates and returns the number of customers, who are asking for a
certain make and model

CREATE OR REPLACE FUNCTION countProspect(


v_make IN car.carmake%TYPE,
v_model IN car.carmodel%TYPE)
RETURN NUMBER
IS
v_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO v_count
FROM prospect
WHERE carmake = UPPER(v_make) AND model = UPPER(v_model);

RETURN v_count;
END;
/

5. Create a script that uses substitution variables and the function created in question above.

SET SERVEROUTPUT ON
SET HEADING OFF
SET VERIFY OFF
ACCEPT p_make PROMPT 'Enter make: '
ACCEPT p_model PROMPT 'Enter model: '

BEGIN
DBMS_OUTPUT.put_line('There are :' || countProspect('&p_make', '&p_model') ||
'
customers looking for ' || '&p_make' || ' ' || '&p_model' );
END;
/

6. Create a log table for keeping the logs of the changes made in the prospect table and write a
trigger that would write all data changes to that log table

DROP TABLE prospectlog;


CREATE TABLE prospectlog
(username VARCHAR2(40),
operation CHAR(6),
opdate DATE,
name CHAR(20),
make CHAR(10),
model CHAR(8),
year CHAR(6),
color CHAR(12),
trim CHAR(16),
code CHAR(4));

CREATE OR REPLACE TRIGGER prospectTrigger


BEFORE DELETE OR INSERT OR UPDATE ON prospect
FOR EACH ROW
BEGIN
IF INSERTING THEN
INSERT INTO prospectlog
VALUES (USER,'INSERT',SYSDATE,:NEW.custname, :NEW.carmake, :NEW.carmodel,
:NEW.caryear, :NEW.carcolor, :NEW.cartrim, :NEW.optioncode);
END IF;
IF UPDATING THEN
INSERT INTO prospectlog
VALUES (USER,'UPDATE',SYSDATE,:OLD.custname, :OLD.carmake, :OLD.carmodel,
:OLD.caryear, :OLD.carcolor, :OLD.cartrim, :OLD.optioncode);
END IF;
IF DELETING THEN
INSERT INTO prospectlog
VALUES (USER,'DELETE',SYSDATE,:OLD.custname, :OLD.carmake, :OLD.carmodel,
:OLD.caryear, :OLD.carcolor, :OLD.cartrim, :OLD.optioncode);
END IF;
END;
/
MCQ

1. You would use a BEFORE trigger for all but one of the following. Which one is it?
a. to enforce data consistency
b. to create an audit table
c. to insert a sequence value into a column
d. to correct mixed-case customer names

2. Which of the following is true about PL/SQL programs?


a. PL/SQL programs can exist with or without any SQL statements.
b. PL/SQL programs can exist only with any SQL statements.
c. PL/SQL programs can exist only without any SQL statements.
d. SQL programs can exist only with PL/SQL statements.

3. How does an execution block start and end in PL/SQL?


a. Starts with START and ends with END
b. Starts with BEGIN and ends with END
c. Starts with START and ends with ; (semi colon)
d. Starts with BEGIN and ends with ; (semi colon)

4. How should any declaration statement end in PL/SQL?


a. Using a colon (:)
b. Using a period (.)
c. Using a semi colon (;)
d. All of the above

5. While using SELECT INTO statements where are the database columns specified?
a. After the SELECT and before INTO
b. After the SELECT and after INTO
c. After the SELECT and FROM clause and before INTO clause
d. After the SELECT ,FROM clause and INTO clause and before WHERE clause

6. When should a CLOSE statement be used in PL/SQL?


a. For both implicit and explicit cursors
b. Only for explicit cursors
c. Only for implicit cursors
d. Only for cursor expressions

b
7. Which of the following is incorrect about functions?
a. A function is a module that returns a value.
b. A function is a standalone executable statement.
c. A function can be said to have a datatype.
d. A function can be used in place of an expression in a PL/SQL statement.

Short answer questions

1. To print output to the console from a PL/SQL block, you can use the procedures in the
_____________ package.

DBMS_OUTPUT

2. To repeatedly execute a statement or set of statements when you’re using PL/SQL, you can
code a simple loop, a WHILE loop, or a/an ______________ loop.

FOR

3. Assume that this is the start of the code for creating a stored procedure:

CREATE OR REPLACE PROCEDURE update_credits

in_param NUMBER,

cr_param NUMBER

AS

..........

Then, code the statement that you would use within a script to call the stored procedure and
pass the values 47 and 200 to it.

__________________________________

update_credits(47,200) OR update_credits(in_param=>47, cr_param=>200);

4. When you create a scalar-valued function, you use the ________________ statement to specify
the value that’s returned by the function.
RETURN

5. To enforce data consistency, you can create a trigger that is fired _____________ each row in a
table is updated.

BEFORE

6. What keywords are used to determine a PL/SQL block?

BEGIN and END

7. What is the difference between a FOR loop and a WHILE loop?

A FOR is used when the number of iteration is known in advance. A WHILE loop generally is
used when the number of iterations is not known in advance.

8. What is the INTO clause for?

The INTO clause is used to tell PL/SQL where to put data retrieved from a SELECT statement

9. What is an Exception?

An Exception is an identifier within PL/SQL that can be used to trap for a specific condition.
Exceptions are typically associated with an error. Exceptions are either raised automatically by
PL/SQL or they can be raised explicitly.

10. What is an Exception Handler?

An Exception Handler is a section of PL/SQL code that is there purely to deal with any raised
exceptions.

11. What is a named cursor?

A named cursor is a construct within PL/SQL that is used to retrieve data from the database.

12. Name 3 keywords used with a named cursor.

CURSOR OPEN FETCH CLOSE or FOR

13. What is the difference between stored subprograms such as procedures, functions and triggers,
and anonymous blocks?

Stored programs are stored within the database in compiled form and executed on the database,
whereas anonymous blocks are usually held in a host file and are parsed and compiled at
runtime, they are explicitly executed in a client tool, typically SQL*Plus

14. What is an argument list?


The arguments define what parameters a function or procedure accepts

15. What is a trigger?

A trigger is a named PL/SQL block that fires implicitly when a particular database event occurs.

Vous aimerez peut-être aussi