15
Interacting With The Oracle Server

Interacting With The Oracle Server. Objectives After completing this lesson, you should be able to do the following: Write a successful SELECT statement

Embed Size (px)

Citation preview

Interacting With The Oracle Server

ObjectivesAfter completing this lesson, you should be able to do the following:Write a successful SELECT statement in PL/SQLDeclare the datatype and size of a PL/SQL variable dynamically.Write DML statement in PL/SQL.Control transactions in PL/SQL.Determine the outcome of SQL DML statements.

SQL Statements in PL/SQLExtract a row of data from the database by

using the SELECT command. Only a single set of values can be returned.

Make changes to rows in the database by using DML commands.

Control a transaction with the COMMIT, ROLLBACK, or SAVEPOINT command.

Determine DML outcome with implicit cursors.

SELECT statement in PL/SQLRetrieve data from the database with SELECT.Syntax

SELECT select_listINTO { variable_name [, variable_name]….

| record_name}FROM tableWHERE condition;

select_list Is a list of at least one column and can include SQL expressions, functions, or group functions.

variable_name

Is a scalar variable to hold the retrieved value.

record_name

Is the PL/SQL RECORD to hold the retrieved values

table Specifies the database table name

condition Is a composed of column names, expressions, constants, and operators, including PL/SQL variables and constants.

SELECT Statement in PL/SQLThe INTO clause is requiredExample:

DECLAREv_dept_id NUMBER(2);v_loc_idVARCHAR(15);BEGINSELECT department id, location idINTO v_dept_id, v_loc_idFROM departmentsWHERE department_name =‘SALES’;….END;

Manipulating Data Using PL/SQLMake changes to database tables by using

DML commands:INSERTUPDATEDELETE

Inserting DataThe basic syntax:

INSERT INTO table_name(column_list) VALUES (value_list);

The column list is optional if values

will be inserted for all columns and

the value list conforms to the

order of columns in the table.

Ensure that each value conforms to

the corresponding

column’s datatype.

Inserting Data

BEGININSERT INTO employees

VALUES (207, ‘Jun’, ‘Aziz’, ‘JAZIZ’,

‘590.423.3256’, SYSDATE, ‘IT_PROG’,

6000, NULL, 103, 60);

END;/

Here we are inserting a record

for a new programmer in

the IT department.

Updating DataThe basic syntax:

UPDATE table_nameSET column_name = new_valueWHERE update_condition

The WHERE clause is

optional if all of the records

in the table are to be updated.

Updating Data

BEGINUPDATE employeesSET salary = salary * 1.1WHERE employee_id = 207;

DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ‘ row(s) updated’);END;/

Omitting the condition will increase the salary for all employees.

Deleting DataThe basic syntax:

DELETE FROM table_nameWHERE delete_condition

The WHERE clause is

optional if all of the records

in the table are to be deleted.

Deleting Data

BEGINDELETE FROM employeesWHERE employee_id = 207;

DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ‘ row(s) updated’);END;/

Omitting the condition will

delete all employee records.

Example: Increasing the Salary of All Employees by a User-supplied Percentage

SELECT employee_id, salary FROM employees

SET SERVEROUTPUT ONACCEPT p_percent NUMBER PROMPT ‘Enter the percentage of salary increase: ’

BEGIN

UPDATE employeesSET salary = salary + (salary * &p_percent / 100);

DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' employee salaries updated');

END;/

SET SERVEROUTPUT OFFUNDEFINE p_percent

SELECT employee_id, salary FROM employees

ExtrasIssue the ROLLBACK command in iSQL Plus

if you want to undo an insertion, update or deletion

Exercise1. Retrieve the records of employees that are

receiving minimum salaries.2. Increase the minimum salary for all jobs by

10%.3. Update the salaries of the employees retrieved

earlier.4. Insert a job history record for a clerk who will

be promoted to the post Administration Assistant for his/her department.

5. Update his/her employee record.6. Delete location records that do not have any

departments.