SQL: Structured Query Language: Prepared By: Prof Momhamad Ubaidullah Bokhari
SQL: Structured Query Language: Prepared By: Prof Momhamad Ubaidullah Bokhari
SQL: Structured Query Language: Prepared By: Prof Momhamad Ubaidullah Bokhari
Slide 2-13
( Dname VARCHAR2(15) ,
Dnumber NUMBER,
Mgr_ssn CHAR(9)
);
Inserting data into Table syntax
INSERT INTO command-
Slide 2-14
VALUES ( ‘Computer Science’ , 007, ‘COMSCIAMU’ );
EXTRACTING DATA FROM TABLE
SELECT command-
UPDATE department
SET name=‘CS’, id=‘010’
WHERE name=‘CH’;
Modifying the structure of tables
ALTER TABLE command-
Command:
CREATE TABLE student
( Senroll VARCHAR2(10) PRIMARY KEY,
Dno NUMBER REFERENCES department (Dnumber),
Sname VARCHAR2(25),
Voter_id VARCHAR2(25) UNIQUE
);
NOT NULL constraint
CREATE TABLE student
( Senroll VARCHAR2(10) PRIMARY KEY,
Dno NUMBER REFERENCES department (Dnumber),
Sname VARCHAR2(25) NOT NULL,
Voter_id VARCHAR2(25) UNIQUE
);
CHECK constraint
To apply business rule validations:
Account balance should be more than 1000/-
Delivery data can’t be less than Order date.
48
execute an SQL statement. This work area is called a CURSOR.
ClientNo Cname
C0001 Ivan
Active data-set
C0002 Bill
Types of cursor
Implicit-
49
Cursor name- SQL
Managed by oracle engine, so no need to define by user.
Its attributes are used to access information about last insert, update, delete
etc.
Explicit-
Defined by user.
Also has same attributes.
Useful when individual records have to be processed.
Cursor attributes
50
Attribute Name Description
%ISOPEN Returns TRUE if cursor is open, FALSE otherwise
%FOUND Returns TRUE if record was fetched successfully, FALSE
otherwise.
%NOTFOUND Returns TRUE if record was not fetched successfully, FALSE
otherwise.
%ROWCOUNT Returns number of records processed.
Implicit cursor- SQL%FOUND and SQL%NOTFOUND
51
Implicit cursor- SQL%ROWCOUNT
52
Explicit cursor
In PL/SQL, accepting input from the user is not a common situation.
53
The data to be processed is taken from existing tables.
54
Col_1 Col_2 Col_3
- - -
- - -
- - -
- - -
- - -
Explicit cursor
55
Col_1 Col_2 Col_3
- - -
- - -
- - -
- - -
- - -
Explicit cursor
56
Col_1 Col_2 Col_3
- - -
- - -
- - -
- - -
- - -
Explicit cursor
57
Col_1 Col_2 Col_3
- - -
- - -
- - -
- - -
- - -
Explicit cursor
58
Col_1 Col_2 Col_3
- - -
- - -
- - -
- - -
- - -
Explicit cursor
Three commands are used to control explicit cursor:
59
Open: starts a cursor.
Fetch: extracts records one by one.
Close: closes a cursor.
Attributes:
%ISOPEN
%FOUND
%NOTFOUND
%ROWCOUNT
Example
If no transaction took place in an account from last one year then set its status as inactive
and record its account no in INACTV_ACCT_MSTR.
60
Example
If no transaction took place in an account from last one year then set its status as inactive
and record its account no in INACTV_ACCT_MSTR.
61
Example
If no transaction took place in an account from last one year then set its status as inactive
and record its account no in INACTV_ACCT_MSTR.
62
Example
If no transaction took place in an account from last one year then set its status as inactive
and record its account no in INACTV_ACCT_MSTR.
63
Example
List first three highest balance accounts.
64
Example
List first three highest balance accounts.
65
Example
List first three highest balance accounts.
66
Example
List first three highest balance accounts.
67
Procedure and function
A procedure or function is a logically grouped set of PL/SQL statements that
68
perform a specific task.
69
Similar to PL/SQL block except:
Has Input Parameters.
Has Return keyword.
No Declare keyword.
Example- function
70
Procedure- Structure
71
Similar to PL/SQL block except:
Has Input and Output Parameters:
In
Out
In Out
No Declare keyword.
No Return keyword.
Example- Procedure
72
Example- Procedure
73
Example- Procedure
74
Procedure vs Function
A function returns a value, a procedure does not.
75
Procedure has out parameters, function does not.
Procedure vs Function
A function returns a value, a procedure does not.
76
Procedure has out parameters, function does not.
77
Example
78
Deleting Function & Procedure
79
DROP PROCEDURE prodedure_name;
80
Advantages:
To prevent invalid transactions.
Withdrawal amount can not be greater than balance.
81
Row trigger:
A row trigger is fired each time a row in a table is affected.
Before deleting each record, its details should be stored in some other
table.
Statement Trigger:
A statement trigger is fired, independent of the number of rows affected.
When deletion takes place in a table, store the deletion time in a separate
table.
Types of triggers
Based on trigger timing.
82
Before trigger:
Executed before the triggering statement.
Before withdrawing an amount check whether user has sufficient
balance.
After trigger:
Executed after the triggering statement.
After deletion takes place in a table, store the deletion time in a separate
table.
83
Update operation:
:NEW
:OLD
Insert operation:
:NEW
Delete operation:
:OLD
Example
84
Example
85
Example
86
Example
87
Example
88
Example
89
Example
90
Example
91
Example
92
Example
93
Example
94
Example
95
Example
96
Example
97
Example
98
Additional concepts
RAISE_APPLICATION_ERROR:
Stops execution of current operation.
99
Issues user defined error message.
Error number should be in the range -20000 to -20999.
100
Triggers are called implicitly, Procedures are called explicitly.
101
Resources
Software:
Oracle 11g express edition
Freely available
Book:
SQL, PL/SQL by Ivan Bayross