A cursor is a temporary work area that stores data retrieved from a database. It contains information on a SQL statement and the rows accessed. Cursors can be implicit, created by default for DML statements, or explicit, which must be created for SQL statements that return multiple rows. A trigger is a stored procedure that automatically executes when a data change occurs to the table it is defined on via insert, update or delete operations. Triggers can be row-level, firing for each row change, or statement-level, firing once per SQL statement. SQL is used to select, insert, update and delete data from database tables.
1. What are Cursors?
A cursor is a temporary work area created in the system memory when a SQL statement is executed. A
cursor contains information on a select statement and the rows of data accessed by it. This temporary
work area is used to store the data retrieved from the database, and manipulate this data. A cursor can
hold more than one row, but can process only one row at a time. The set of rows the cursor holds is
called theactive set.
There are two types of cursors in PL/SQL:
Implicit cursors:
These are created by default when DML statements like, INSERT, UPDATE, and DELETE statements are
executed. They are also created when a SELECT statement that returns just one row is executed.
Explicit cursors:
They must be created when you are executing a SELECT statement that returns more than one row.
Even though the cursor stores multiple records, only one record can be processed at a time, which is
called as current row. When you fetch a row the current row position moves to next row.
What is a Trigger?
A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is
executed on a database table. A trigger is triggered automatically when an associated DML statement is
executed.
Syntax of Triggers
The Syntax for creating a trigger is:
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- sql statements
2. END;
Types of PL/SQL Triggers
There are two types of triggers based on the which level it is triggered.
1) Row level trigger - An event is triggered for each row upated, inserted or deleted.
2) Statement level trigger - An event is triggered for each sql statement executed.
/////////////////////////////////////////////////////////////////////////////////////
http://www.1keydata.com/sql/sqldistinct.html
What do we use SQL commands for? A common use is to select data from the tables located in a
database. Immediately, we see two keywords: we need to SELECTinformation FROM a table. (Note that
a table is a container that resides in the database where the data is stored. For more information about
how to manipulate tables, go to theTable Manipulation Section). Hence we have the most basic SQL
query structure:
SELECT "column_name" FROM "table_name"
To illustrate the above example, assume that we have the following table:
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
We shall use this table as an example throughout the tutorial (this table will appear in all sections). To
select all the stores in this table, we key in,
SELECT store_name FROM Store_Information
Result:
store_name
Los Angeles
San Diego
Los Angeles
Boston
Multiple column names can be selected, as well as multiple table names.
3. The SELECT keyword allows us to grab all information from a column (or columns) on a table. This, of
course, necessarily mean that there will be redundancies. What if we only want to select
each DISTINCT element? This is easy to accomplish in SQL. All we need to do is to
add DISTINCT after SELECT. The syntax is as follows:
SELECT DISTINCT "column_name"
FROM "table_name"
For example, to select all distinct stores in TableStore_Information,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
we key in,
SELECT DISTINCT store_name FROM Store_Information
Result:
store_name
Los Angeles
San Diego
Boston
Next, we might want to conditionally select the data from a table. For example, we may want to only
retrieve stores with sales above $1,000. To do this, we use the WHERE keyword. The syntax is as
follows:
4. SELECT "column_name"
FROM "table_name"
WHERE "condition"
For example, to select all stores with sales above $1,000 in Table Store_Information,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
we key in,
SELECT store_name
FROM Store_Information
WHERE Sales > 1000
Result:
store_name
Los Angeles
In the previous section, we have seen that the WHEREkeyword can be used to conditionally select data
from a table. This condition can be a simple condition (like the one presented in the previous section), or it
can be a compound condition. Compound conditions are made up of multiple simple conditions
connected by AND or OR. There is no limit to the number of simple conditions that can be present in a
single SQL statement.
The syntax for a compound condition is as follows:
SELECT "column_name"
FROM "table_name"
WHERE "simple condition"
{[AND|OR] "simple condition"}+
The {}+ means that the expression inside the bracket will occur one or more times. Note
that AND and OR can be used interchangeably. In addition, we may use the parenthesis sign () to
indicate the order of the condition.
For example, we may wish to select all stores with sales greater than $1,000 or all stores with sales less
than $500 but greater than $275 in Table Store_Information,
Table Store_Information
store_name Sales Date
5. Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
San Francisco $300 Jan-08-1999
Boston $700 Jan-08-1999
we key in,
SELECT store_name
FROM Store_Information
WHERE Sales > 1000
OR (Sales < 500 AND Sales > 275)
Result:
store_name
Los Angeles
San Francisco