Download LECTURE 4 DATABASE PROGRAMMING SQL FUNCTION ON PL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Oracle Database wikipedia , lookup

Relational algebra wikipedia , lookup

Microsoft Access wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Tandem Computers wikipedia , lookup

Serializability wikipedia , lookup

Btrieve wikipedia , lookup

Database wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Concurrency control wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Null (SQL) wikipedia , lookup

Relational model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
LECTURE 4
DATABASE PROGRAMMING
SQL FUNCTION IN PL/SQL
OBJECTIVES




SQL & PL/SQL Overview .
DML in PL/SQL.
Transaction processing.
SQL Built in Functions and PL/SQL.
SQL in PL/SQL

You can run SQL statements inside PL/SQL.

You can use DML (Data Manipulation Language) which
includes statements like SELECT, INSERT, UPDATE, and
DELETE,
and
transaction
control
statements,
like
COMMIT, ROLLBACK, SAVEPOINT

You cannot, however, execute Data Definition Language
(DDL) statements (such as CREATE TABLE, ALTER
TABLE, or DROP TABLE). in PL/SQL unless you run them
as dynamic SQL.
DDL in PL/SQL (Dynamic SQL)

To execute your DDL statements in PL/SQL. Use EXECUTE IMMEDIATE
statement which takes the SQL statement as an argument to execute your DDL
statement.
DML in PL/SQL
 SQL Data Manipulation Language statement support:
1. INSERT
2. UPDATE
3. DELETE
4. SELECT
DML in PL/SQL
INSERT
 INSERT: used to insert new record into a table.
 Example:
DECLARE
my_sal NUMBER(7,2) := 3040.22;
my_ename CHAR(25) := ‘WANDA’;
my_hiredate DATE := ‘08-SEP-01’;
BEGIN
INSERT INTO emp
(empno, ename, job, hiredate, sal , deptno)
VALUES (2345,my_ename,’cabDriver’,my_hiredate,my_sal,20);
END;
DML in PL/SQL
UPDATE
 UPDATE: change a value of record in a table.
 Example:
DECLARE
max_allowed CONSTANT NUMBER := 5000;
good_cust
CHAR(8) := ‘VIP’;
BEGIN
UPDATE account
SET credit_limit = max_allowed
WHERE type = ‘EMPOLEE ‘OR type = good_cust ;
END;
DML in PL/SQL
DELETE
 DELETE: used to remove unwanted rows from a table.
 Example:
DECLARE
bad_child_type CHAR(8) := ‘NAUGHTY’;
BEGIN
DELETE FROM santas_gift_list
WHERE kid_rating = bad_child_type ;
END;
DML in PL/SQL
APPLICATION
VAR1
EMPNO ENAME
7644
TURNER
7400
ALLEN
SAL
1500
1600
VAR2
VAR3
Notes - SELECT INTO
1 A SELECT statement is the only DML that returns data .You must
provide location for this data to be stored via the INTO clause.
2. A SELECT..INTO statement must return exactly one row .
Multiple returned rows result in an error.
3. For multi-row SELECTs use cursors (discussed later).
DML in PL/SQL
SELECT Syntax
SELECT col1,col2……INTO var1,var2.. FROM table_name
WHERE ...
SELECT Exmple
DECLARE
part_name
parts.name%TYPE;
num_in_stock
parts.num%TYPE;
BEGIN
SELECT name, num INTO part_name, num_in_stock FROM
PARTS WHERE part_id = 234;
Transaction Control processing

SQL provides many transaction management features. SQL commands
COMMIT, ROLLBACK and SAVEPOINT helps in managing the transaction.


The COMMIT command is the transactional command used to save changes
made by a transaction to the database. The COMMIT command will save all
changes to the database since the last COMMIT or ROLLBACK command.
Frequent commits in the case of transaction involving large amount of data is
recommended. But too many commits can affect performance. In many
implementations, an implicit commit of all the changes is done, if user logs off
from the database.

The ROLLBACK command is the transactional control command to undo the
transactions that have not already been committed to the database. The
ROLLBACK command can be issued to undo the changes since the last
COMMIT or ROLLBACK.
Transaction Control processing
 SAVEPOINT : used to mark an intermediate point in
transaction processing.
Syntax
SAVEPOINT
< marker_name >;
 ROLLBACK : used for discarding any changes that were
made to the database.
Syntax
ROLLBACK [WORK] TO SAVEPOINT < marker_name >;
 COMMIT: used to make the database changes permanent.
Transaction Control processing
SAVEPOINT , ROLLBACK TO , COMMIT Example:
BEGIN
INSERT INTO temp VALUES (1,1 ‘ROW 1’);
SAVEPOINT A;
INSERT INTO temp VALUES (2,2 ‘ROW 2’);
SAVEPOINT B ;
….
ROLLBACK TO SAVEPOINT B;
COMMIT ;
END;
SQL Built in Functions and PL/SQL
• Available in procedural statements:
– Single-row number
– Single-row character
– Data type conversion
– Date
– Timestamp
– GREATEST and LEAST
• Not available in procedural statements:
– DECODE
– Group functions: AVG, MIN, MAX, COUNT, SUM, STDDEV, and
VARIANCE.
NOTE:
Group functions apply to groups of rows in a table and therefore are
available only in SQL statements in a PL/SQL block.
SQL Built in Functions and PL/SQL
 SQL Functional support(within a SQL ):
1. Numeric (e.g. SQRT, ROUND, POWER)
2. Character (e.g. LENGTH, UPPER)
3. Date (e.g. ADD_MONTHS, MONTH_BETWEEN);
4. Group(e.g. AVG, MAX, COUNT)
ROUND (number) function


ROUND returns n rounded to integer places to the right of the
decimal point.
If you omit integer, then n is rounded to 0 places.
Examples

SELECT ROUND(15.193,1) "Round" FROM DUAL;
Round
---------15.2
SELECT ROUND(1.5), ROUND(2.5) FROM DUAL;
ROUND(1.5) ROUND(2.5)
------------------2
3

LOWER function
LOWER returns char, with all letters lowercase.
 Syntax:
LOWER(string)
Example:
The following example returns a string in lowercase:

SELECT LOWER('MR. SCOTT MCMILLAN') FROM dual;
Lowercase
-------------------mr. scott mcmillan
UPPER function


UPPER returns char, with all letters uppercase.
Syntax:
UPPER (string)
Example:
The following example returns each employee's last name in
uppercase:
SELECT UPPER(last_name) FROM employees;
LENGTH function

LENGTH function returns the length of the specified string.

Syntax:
LENGTH (string)
Example:
The following example returns the length of a string:
SELECT LENGTH ('CANDIDE') FROM dual;
Length
--------------7

The next example assumes a double-byte database character set.
SELECT LENGTHB ('CANDIDE') "Length in bytes"
FROM DUAL;
Length in bytes
--------------14
LAST_DAY function



LAST_DAY returns the date of the last day of the month that
contains date.
The return type is always DATE, regardless of the datatype
of date.
Syntax:
LAST_DAY(d)
LAST_DAY function
Example:
The following statement determines how many days are left in
the current month.
SELECT SYSDATE,
LAST_DAY(SYSDATE) "Last",
LAST_DAY(SYSDATE) - SYSDATE "Days Left"
FROM DUAL;
SYSDATE
Last
Days Left
----------------- ---------30-MAY-01 31-MAY-01
1
MONTHS_BETWEEN function

MONTHS_BETWEEN returns number of months between
dates date1 and date2

Syntax:
MONTHS_BETWEEN(d1,d2)
MONTHS_BETWEEN function
Example:
The following example calculates the months between two dates:

SELECT MONTHS_BETWEEN
(TO_DATE('02-02-1995','MM-DD-YYYY'),
TO_DATE('01-01-1995','MM-DD-YYYY') ) "Months"
FROM DUAL;
Months
---------1.03225806
GREATEST function
GREATEST returns the greatest of the list of one or more
expressions.
Example:

The following statement selects the string with the greatest value:
-SELECT GREATEST ('HARRY', 'HARRIOT', 'HAROLD')
"Greatest" FROM DUAL;
-SELECT GREATEST (-1, 0, 1,2,3)
"Greatest" FROM DUAL;
Greatest
-------HARRY
Greatest
-------3
LEAST function

LEAST returns the least of the list of exprs
Example:
The following statement selects the string with the least value:
SELECT LEAST('HARRY','HARRIOT','HAROLD') "LEAST"
FROM DUAL;
SELECT LEAST(-1, 0, 1,2,3) "LEAST"
FROM DUAL;
LEAST
-----HAROLD
LEAST
------1
TO_DATE function

TO_DATE converts char of CHAR, VARCHAR2, NCHAR,
or NVARCHAR2 datatype to a value of DATE datatype.

Syntax:
to_date( string1, [ format_mask ], [ nls_language ] )

TO_DATE function
Example:
The following example converts a character string into a date:

SELECT TO_DATE(
'January 15, 1989, 11:00 A.M.',
'Month dd, YYYY, HH:MI A.M.')
FROM DUAL;
TO_DATE
--------15-JAN-89
SUMMARY

SQL & PL/SQL Overview .

DML in PL/SQL.

Transaction processing.

SQL Built in Functions and PL/SQL.