Sql

177 views
133 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
177
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql

  1. 1. What are Cursors?A cursor is a temporary work area created in the system memory when a SQL statement is executed. Acursor contains information on a select statement and the rows of data accessed by it. This temporarywork area is used to store the data retrieved from the database, and manipulate this data. A cursor canhold more than one row, but can process only one row at a time. The set of rows the cursor holds iscalled 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 areexecuted. 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 iscalled 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 isexecuted on a database table. A trigger is triggered automatically when an associated DML statement isexecuted.Syntax of TriggersThe 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. 2. END;Types of PL/SQL TriggersThere 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.htmlWhat do we use SQL commands for? A common use is to select data from the tables located in adatabase. Immediately, we see two keywords: we need to SELECTinformation FROM a table. (Note thata table is a container that resides in the database where the data is stored. For more information abouthow to manipulate tables, go to theTable Manipulation Section). Hence we have the most basic SQLquery structure:SELECT "column_name" FROM "table_name"To illustrate the above example, assume that we have the following table:Table Store_Informationstore_name Sales DateLos Angeles $1500 Jan-05-1999San Diego $250 Jan-07-1999Los Angeles $300 Jan-08-1999Boston $700 Jan-08-1999We shall use this table as an example throughout the tutorial (this table will appear in all sections). Toselect all the stores in this table, we key in,SELECT store_name FROM Store_InformationResult:store_nameLos AngelesSan DiegoLos AngelesBostonMultiple column names can be selected, as well as multiple table names.
  3. 3. The SELECT keyword allows us to grab all information from a column (or columns) on a table. This, ofcourse, necessarily mean that there will be redundancies. What if we only want to selecteach DISTINCT element? This is easy to accomplish in SQL. All we need to do is toadd 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_Informationstore_name Sales DateLos Angeles $1500 Jan-05-1999San Diego $250 Jan-07-1999Los Angeles $300 Jan-08-1999Boston $700 Jan-08-1999we key in,SELECT DISTINCT store_name FROM Store_InformationResult:store_nameLos AngelesSan DiegoBostonNext, we might want to conditionally select the data from a table. For example, we may want to onlyretrieve stores with sales above $1,000. To do this, we use the WHERE keyword. The syntax is asfollows:
  4. 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_Informationstore_name Sales DateLos Angeles $1500 Jan-05-1999San Diego $250 Jan-07-1999Los Angeles $300 Jan-08-1999Boston $700 Jan-08-1999we key in,SELECT store_nameFROM Store_InformationWHERE Sales > 1000Result:store_nameLos AngelesIn the previous section, we have seen that the WHEREkeyword can be used to conditionally select datafrom a table. This condition can be a simple condition (like the one presented in the previous section), or itcan be a compound condition. Compound conditions are made up of multiple simple conditionsconnected by AND or OR. There is no limit to the number of simple conditions that can be present in asingle 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. Notethat AND and OR can be used interchangeably. In addition, we may use the parenthesis sign () toindicate 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 lessthan $500 but greater than $275 in Table Store_Information,Table Store_Informationstore_name Sales Date
  5. 5. Los Angeles $1500 Jan-05-1999San Diego $250 Jan-07-1999San Francisco $300 Jan-08-1999Boston $700 Jan-08-1999we key in,SELECT store_nameFROM Store_InformationWHERE Sales > 1000OR (Sales < 500 AND Sales > 275)Result:store_nameLos AngelesSan Francisco

×