SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums...

28
SQL Within PL / SQL Chapter 4

Transcript of SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums...

Page 1: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

SQL Within PL / SQL

Chapter 4

Page 2: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

2

SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control

Page 3: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

3

SQL StatementsUsing SQL in PL / SQL

The only SQL statements allowed in PL/SQL are: DML (data manipulation language) transaction control

DDL (data definition language) statements are not permitted

Page 4: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

4

SQL StatementsDML in PL / SQL

The DML statements permitted are: Select Insert Update Delete

Page 5: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

5

DML in PL / SQLSelect

SELECT statement in PL/SQL retrieves data from a database table into: PL/SQL record set of PL/SQL variables

Using SELECT in PL/SQL should only return one row

When you need to retrieve more than one row from a table use a cursor

Page 6: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

6

DML in PL / SQLSelect Syntax

SELECT { * | select_list_item }INTO { PL/SQL_record | variables}FROM table_referenceWHERE where_clause

each variable must be compatible with its associated select list item

count of variables must be same as number of items in list

record should contain fields that correspond to the select list in type and count

Page 7: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

7

select.sqlDECLARE

v_StudentRecord students%ROWTYPE;

v_Department classes.department%TYPE;

v_Course classes.course%TYPE;

BEGIN

SELECT *

INTO v_StudentRecord

FROM students

WHERE id = 10000;

SELECT department, course

INTO v_Department, v_Course

FROM classes

WHERE room_id = 99997;

END;

Page 8: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

8

DML in PL / SQLInsert

The INSERT statement inserts data into a database table

There are two variations of the INSERT command add one row to a table using the specified

VALUES list add one or several rows when the insert

command uses a SELECT statement

Page 9: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

9

DML in PL / SQLInsert Syntax

INSERT INTO table_reference[(column_names)]{VALUES (expression) |

select_statement} use column names when the values

are listed in a different order than as defined during table creation

Only a portion of the columns of the table are used during insert

table definition remains unchanged after the new row is inserted

Page 10: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

10

DML in PL / SQLInsert

The word VALUES must precede the list of data to be inserted

Regarding the values in the list: a character string must be in single

quotes numbers can stand by themselves dates must be in single quotes

In the default Oracle date format Converted using TO_DATE function (this is

the suggested method)

Page 11: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

11

Insert Using Built-in Functions You can modify the contents of the values

before they are entered into a column of a table by a VALUES list from a SELECT statement

Use any of the built-in functions supported by PL/SQL character date functions numeric conversion

Page 12: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

12

insert.sqlDECLARE

v_StudentID students.id%TYPE;

BEGIN

SELECT student_sequence.NEXTVAL

INTO v_StudentID

FROM dual;

INSERT INTO students (id, first_name, last_name)

VALUES (v_StudentID, 'Timothy', 'Taller');

INSERT INTO students (id, first_name, last_name)

VALUES (student_sequence.NEXTVAL, 'Patrick', 'Poll');

END;

Page 13: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

13

DML in PL / SQLInsert with Select

It is also possible to insert rows into a table using the results of a SELECT statement

The results of a SELECT statement can return one or several rows based

on the WHERE clause can be a mix of columns from one or

more tables

Page 14: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

14

DML in PL / SQLUpdate

Requires setting specific values for each column you wish to change

Specifying which row or rows to modify using a WHERE clause

You can use built-in functions in setting a value for the update

Page 15: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

15

Update in PL / SQLEmbedded SELECT

It is possible to set values in an UPDATE by embedding a SELECT statement right in the middle of it SELECT has its own WHERE clause UPDATE has its own WHERE clause to

affect the rows You must be certain that the SELECT

will return no more than one row

Page 16: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

16

Embedded SELECTBEGIN

UPDATE comfort set Midnight =

(SELECT temperature

FROM weather

WHERE city = ‘MANCHESTER’)

WHERE city = ‘WALPOLE’

AND SampleDate = TO_DATE (’22-DEC-1999’, ‘DD-MON-YYYY’);

END;

Page 17: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

17

DML in PL / SQLDelete

Removing a row or rows from a table

WHERE clause is necessary to removing only the rows you intend

DELETE without the where clause will delete all of the rows of a table

Page 18: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

18

delete.sqlDECLARE

v_StudentCutoff NUMBER;

BEGIN

v_StudentCutoff := 10;

DELETE FROM classes

WHERE current_students < v_StudentCutoff;

DELETE FROM students

WHERE current_credits = 0

AND major = 'Economics';

END;

Page 19: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

19

DML in PL / SQLTruncate

Another command for deleting records from a table is the TRUNCATE command

TRUNCATE TABLE students;

Does not operate the same as DELETE deletes all rows from a table cannot be rolled back records are unrecoverable does not run any DELETE triggers does not record any information in a snapshot

log

Page 20: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

20

DML in PL / SQLWHERE Clause The SELECT, UPDATE, and DELETE

statements all include the WHERE clause Defines the active set (set of rows):

returned by a SELECT query Acted upon by an UPDATE or DELETE

Consists of conditions, joined together by the boolean operators AND, OR, and NOT

Conditions usually take the form of comparisons using the relational operators (such as: =, <>, >, >=, <, <=)

Page 21: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

21

DML in PL / SQLWHERE Clause with Cursor

The UPDATE, and DELETE statements both include a WHERE clause with a special syntax the WHERE CURRENT OF is used with

a cursor definition often processing done in a fetch loop

modifies the rows that have been retrieved by a cursor

Page 22: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

22

DML in PL / SQLWHERE Clause with Cursor

This method consists of two parts: the FOR UPDATE clause in the cursor

declaration the WHERE CURRENT OF clause in an

UPDATE or DELETE statement

Page 23: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

23

forupdat.sqlDECLARE

v_NumCredits classes.num_credits%TYPE;

CURSOR c_RegisteredStudents IS

SELECT *

FROM students

WHERE id IN (SELECT student_id

FROM registered_students

WHERE department= 'HIS'

AND course = 101)

FOR UPDATE OF current_credits;

Page 24: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

24

forupdat.sql (cont.)BEGIN

FOR v_StudentInfo IN c_RegisteredStudents LOOP

SELECT num_credits

INTO v_NumCredits

FROM classes

WHERE department = 'HIS'

AND course = 101;

UPDATE students

SET current_credits = current_credits + v_NumCredits

WHERE CURRENT OF c_RegisteredStudents;

END LOOP;

COMMIT;

END;

Page 25: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

25

DML in PL / SQLSynonyms

It is possible to create a synonym for a: table view sequence stored

procedure function

package

Page 26: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

26

DML in PL / SQLSynonyms

The syntax for creating a synonym is: CREATE SYNONYM synonym_name FOR

reference;

Where: synonym_name – name of your synonym reference – schema object being referenced

Page 27: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

27

PseudocolumsCurrval and Nextval

CURRVAL and NEXTVAL are used with sequences

A sequence is an Oracle object used to generate unique numbers

Once created, you can access it with its name by: sequence.CURRVAL sequence.NEXTVAL

Page 28: SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

28

PseudocolumsCurrval and Nextval

Sequence values can be used in: SELECT list of a query VALUES clause of an INSERT SET clause of an UPDATE

Sequence values cannot be used in: WHERE clause PL/SQL statement