Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Interacting With The Oracle Server

2 Objectives After completing this lesson, you should be able to do the following: Write a successful SELECT statement in PL/SQL Declare 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.

3 SQL Statements in PL/SQL Extract 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.

4 SELECT statement in PL/SQL Retrieve data from the database with SELECT. Syntax SELECTselect_list INTO { variable_name [, variable_name]…. | record_name} FROMtable WHEREcondition; select_listIs a list of at least one column and can include SQL expressions, functions, or group functions. variable_nameIs a scalar variable to hold the retrieved value. record_nameIs the PL/SQL RECORD to hold the retrieved values tableSpecifies the database table name conditionIs a composed of column names, expressions, constants, and operators, including PL/SQL variables and constants.

5 SELECT Statement in PL/SQL The INTO clause is required Example: DECLARE v_dept_idNUMBER(2); v_loc_idVARCHAR(15); BEGIN SELECT department id, location id INTO v_dept_id, v_loc_id FROM departments WHERE department_name =‘SALES’; …. END;

6 Manipulating Data Using PL/SQL Make changes to database tables by using DML commands: INSERT UPDATE DELETE

7 Inserting Data The basic syntax: INSERT INTOtable_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.

8 Inserting Data BEGIN INSERT INTOemployees 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.

9 Updating Data The basic syntax: UPDATEtable_name SETcolumn_name = new_value WHEREupdate_condition The WHERE clause is optional if all of the records in the table are to be updated.

10 Updating Data BEGIN UPDATEemployees SETsalary = salary * 1.1 WHEREemployee_id = 207; DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ‘ row(s) updated’); END; / Omitting the condition will increase the salary for all employees.

11 Deleting Data The basic syntax: DELETE FROMtable_name WHEREdelete_condition The WHERE clause is optional if all of the records in the table are to be deleted.

12 Deleting Data BEGIN DELETE FROM employees WHEREemployee_id = 207; DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ‘ row(s) updated’); END; / Omitting the condition will delete all employee records.

13 Example: Increasing the Salary of All Employees by a User- supplied Percentage SELECT employee_id, salary FROM employees SET SERVEROUTPUT ON ACCEPT p_percent NUMBER PROMPT ‘Enter the percentage of salary increase: ’ BEGIN UPDATE employees SET salary = salary + (salary * &p_percent / 100); DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' employee salaries updated'); END; / SET SERVEROUTPUT OFF UNDEFINE p_percent SELECT employee_id, salary FROM employees

14 Extras Issue the ROLLBACK command in iSQL Plus if you want to undo an insertion, update or deletion

15 Exercise 1. 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.


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

Similar presentations


Ads by Google