SlideShare a Scribd company logo
1 of 5
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
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.
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:
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
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

More Related Content

What's hot

SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table1keydata
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsAshwin Dinoriya
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteAl-Mamun Sarkar
 
Chapter 07 ddl_sql
Chapter 07 ddl_sqlChapter 07 ddl_sql
Chapter 07 ddl_sqlNazir Ahmed
 
Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Vidyasagar Mundroy
 
Prabu's sql quries
Prabu's sql quries Prabu's sql quries
Prabu's sql quries Prabu Cse
 
Oracle apps financial online training
Oracle apps financial online trainingOracle apps financial online training
Oracle apps financial online trainingmagnifics
 
SQL Fundamentals - Lecture 2
SQL Fundamentals - Lecture 2SQL Fundamentals - Lecture 2
SQL Fundamentals - Lecture 2MuhammadWaheed44
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESVENNILAV6
 

What's hot (16)

Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
Babitha2.mysql
Babitha2.mysqlBabitha2.mysql
Babitha2.mysql
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
Mysql cheatsheet
Mysql cheatsheetMysql cheatsheet
Mysql cheatsheet
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Chapter 07 ddl_sql
Chapter 07 ddl_sqlChapter 07 ddl_sql
Chapter 07 ddl_sql
 
Sql commands
Sql commandsSql commands
Sql commands
 
Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)
 
Prabu's sql quries
Prabu's sql quries Prabu's sql quries
Prabu's sql quries
 
Oracle apps financial online training
Oracle apps financial online trainingOracle apps financial online training
Oracle apps financial online training
 
SQL Fundamentals - Lecture 2
SQL Fundamentals - Lecture 2SQL Fundamentals - Lecture 2
SQL Fundamentals - Lecture 2
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
 

Viewers also liked

The guide to civics
The guide to civics The guide to civics
The guide to civics trevorrocha
 
Dunce Spring Series Workshop #1 - Making the Most of Your Summer
Dunce Spring Series Workshop #1 - Making the Most of Your SummerDunce Spring Series Workshop #1 - Making the Most of Your Summer
Dunce Spring Series Workshop #1 - Making the Most of Your SummerJonas Ellison
 
Imunisasi campak dan polio
Imunisasi campak dan polioImunisasi campak dan polio
Imunisasi campak dan polioFerdiansah Umar
 
Pelatihan kader pin polio 2016
Pelatihan kader pin polio 2016Pelatihan kader pin polio 2016
Pelatihan kader pin polio 2016Angghaw
 
Imunisasi Polio
Imunisasi PolioImunisasi Polio
Imunisasi Poliooksheila
 

Viewers also liked (7)

The guide to civics
The guide to civics The guide to civics
The guide to civics
 
Imunisasi
ImunisasiImunisasi
Imunisasi
 
Dunce Spring Series Workshop #1 - Making the Most of Your Summer
Dunce Spring Series Workshop #1 - Making the Most of Your SummerDunce Spring Series Workshop #1 - Making the Most of Your Summer
Dunce Spring Series Workshop #1 - Making the Most of Your Summer
 
Imunisasi campak dan polio
Imunisasi campak dan polioImunisasi campak dan polio
Imunisasi campak dan polio
 
Imunisasi Polio
Imunisasi PolioImunisasi Polio
Imunisasi Polio
 
Pelatihan kader pin polio 2016
Pelatihan kader pin polio 2016Pelatihan kader pin polio 2016
Pelatihan kader pin polio 2016
 
Imunisasi Polio
Imunisasi PolioImunisasi Polio
Imunisasi Polio
 

Similar to Sql

Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standardsAlessandro Baratella
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, proceduresVaibhav Kathuria
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic ConceptsTony Wong
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for BeginnersAbdelhay Shafi
 
Mysqlppt
MysqlpptMysqlppt
MysqlpptReka
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQueryAbhishek590097
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowPavithSingh
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Trainingbixxman
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfDraguClaudiu
 

Similar to Sql (20)

SQL Query
SQL QuerySQL Query
SQL Query
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
Sql
SqlSql
Sql
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Babitha2 Mysql
Babitha2 MysqlBabitha2 Mysql
Babitha2 Mysql
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Sql commands
Sql commandsSql commands
Sql commands
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Adbms
AdbmsAdbms
Adbms
 
Sql commands
Sql commandsSql commands
Sql commands
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
 
Sql
SqlSql
Sql
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
 

Sql

  • 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