Interacting with the Oracle Server
-
Upload
donnica-taurus -
Category
Documents
-
view
20 -
download
0
description
Transcript of Interacting with the Oracle Server
![Page 1: Interacting with the Oracle Server](https://reader036.fdocuments.us/reader036/viewer/2022082505/56813234550346895d989973/html5/thumbnails/1.jpg)
Interacting with the Oracle Server
![Page 2: Interacting with the Oracle Server](https://reader036.fdocuments.us/reader036/viewer/2022082505/56813234550346895d989973/html5/thumbnails/2.jpg)
SQL Statements in PL/SQL
• Extract a row of data from the database by using the SELECT command
• 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 cursor attributes
![Page 3: Interacting with the Oracle Server](https://reader036.fdocuments.us/reader036/viewer/2022082505/56813234550346895d989973/html5/thumbnails/3.jpg)
SELECT Statements in PL/SQL
• Syntax :
SELECT select_list
INTO (variable_name[, variable_name] …
| record_name}
FROM table
[WHERE condition]
![Page 4: Interacting with the Oracle Server](https://reader036.fdocuments.us/reader036/viewer/2022082505/56813234550346895d989973/html5/thumbnails/4.jpg)
SELECT Statements in PL/SQL
•The INTO clause is required.
•Queries must return one and only one row
•Example
DECLARE v_deptno NUMBER(4); v_loc VARCHAR2(15);BEGIN SELECT deptno, loc INTO v_deptno, v_loc FROM dept WHERE dname = 'SALES'; ...END;
![Page 5: Interacting with the Oracle Server](https://reader036.fdocuments.us/reader036/viewer/2022082505/56813234550346895d989973/html5/thumbnails/5.jpg)
Retrieving Data in PL/SQL
•Return the sum of the salaries for all employees in the specified department.
•ExampleDECLARE v_sum_sal emp.sal%TYPE; v_deptno NUMBER NOT NULL := 10; BEGIN SELECT SUM(sal) -- group function INTO v_sum_sal FROM emp WHERE deptno = v_deptno; DBMS_OUTPUT.PUT_LINE(‘The Sum Salary is ‘ || to_char(v_sum_sal));)END;
![Page 6: Interacting with the Oracle Server](https://reader036.fdocuments.us/reader036/viewer/2022082505/56813234550346895d989973/html5/thumbnails/6.jpg)
Manipulating Data Using PL/SQL
•Make changes to database tables by using DML commands:
– INSERT– UPDATE– DELETE
![Page 7: Interacting with the Oracle Server](https://reader036.fdocuments.us/reader036/viewer/2022082505/56813234550346895d989973/html5/thumbnails/7.jpg)
Inserting Data
•Add new employee information to the EMP table.
•ExampleBEGIN INSERT INTO emp(empno, ename, job, deptno) VALUES (empno_sequence.NEXTVAL, 'HARDING',
'CLERK', 10);END;
![Page 8: Interacting with the Oracle Server](https://reader036.fdocuments.us/reader036/viewer/2022082505/56813234550346895d989973/html5/thumbnails/8.jpg)
Updating Data
•Increase the salary of all employees in the EMP table who are Analysts.
•Example
DECLARE v_sal_increase emp.sal%TYPE := 2000; BEGIN UPDATE emp SET sal = sal + v_sal_increase WHERE job = 'ANALYST';END;
![Page 9: Interacting with the Oracle Server](https://reader036.fdocuments.us/reader036/viewer/2022082505/56813234550346895d989973/html5/thumbnails/9.jpg)
Deleting Data
•Delete rows that belong to department 10 from the EMP table.
•ExampleDECLARE v_deptno emp.deptno%TYPE := 10; BEGIN DELETE FROM emp WHERE deptno = v_deptno;END;
![Page 10: Interacting with the Oracle Server](https://reader036.fdocuments.us/reader036/viewer/2022082505/56813234550346895d989973/html5/thumbnails/10.jpg)
Naming Conventions
• Use a naming convention to avoid ambiguity in the WHERE clause
• Database columns and identifiers should have distinct names
• Syntax errors can arise because PL/SQL checks the database first or a column in the table
• The names of local variables and formal parameters take precedence over the names of database tables
• The names of database table columns take precedence over the names of local variables
![Page 11: Interacting with the Oracle Server](https://reader036.fdocuments.us/reader036/viewer/2022082505/56813234550346895d989973/html5/thumbnails/11.jpg)
Transaction Control Statements
–Initiate a transaction with the first DML command to follow a COMMIT or ROLLBACK.
–Use COMMIT and ROLLBACK SQL statements to terminate a transaction explicitly.