- RDBMS
Sequence
Sequence is a one type of oracle object which is use to generate the numeric value in manner of sequence
number.
Sequence is use to generate a number in sequence manner either in order or non order.
The value generate by sequence is maximum 38 digit number
Sequence can be define :
1. Generated number in ascending or descending order
2. Provide interval between number
3. Caching of sequence number in memory to speed up there availability.
4. A sequence is independent object and can be used with any table
Syntax :
CREATE SEQUENCE <sequence_name> Increment by
<value>
Startwith<value>
Maxvalue<value>
Nomaxvalue
Minvalue<value>
Nominvalue
Cycle
Nocycle
Cache
Nocache
Order
Noorder;
Note :
Minimum requirement is start with, maxvalue, increment by.
1. Increment by
Specify the interval between sequence number. By default 1 increment.
2. Start with
specify the starting value of sequence. By default 1 is start
3. Maxvalue
Specify the maximum value of sequence.By default 1027
4. Minvalue
Specify the minimum value of sequence.
Prof. Jaimini N.Patel [ SDJ International College ]
- RDBMS
5. Nominvalue
By default -1027
6. Cycle
Repeat a value of sequence after rich at maximum value.
7. Nocycle
No repeat value after rich at maximum value.
8. Cache
Specify the number of block in memory.
9. Nocache
By default 20 block of memory.
10. Order
Generate the number of sequence in order.
11. Noorder
Generate the number of sequence in no order.
Example :
CREATE SEQUENCE s1
Start with 1
Increment by 1
Maxvalue 20
Cycle;
How to get value of sequence
There are basically two pseudo column available for accessing data of sequence.
1. Nextval
2. Currval
1) Nextval
Nextval is rutern next sequence value from current position.
Syntax :
<Sequence_name>.nextval;
Example :
SELECT s1.nextval from dual;
Prof. Jaimini N.Patel [ SDJ International College ]
- RDBMS
2) Currval
Currval is return current value of sequence number from the current position.
Syntax :
<Sequence_name>.currval;
Example :
SELECT s1.currval from dual;
Alter sequence
Syntax :
ALTER SEQUENCE <sequence_name>
Increment by <value>
Startwith<value>
Maxvalue<value>
Nomaxvalue
Minvalue<value>
Nominvalue
Cycle
Nocycle
Cache
Nocache
Order
Noorder;
Example :
ALTER SEQUENCE s1
Increment by 5
Nocycle;
Prof. Jaimini N.Patel [ SDJ International College ]
- RDBMS
Drop sequence
Syntax :
Drop sequence <sequence_name>;
Example :
Drop sequence s1;
How to use sequence with table Example :
CREATE TABLE emp(eno number(5) PRIMARY KEY, ename varchar(20));
CREATE SEQUENCE s
Start with 101
Increment by 1
Maxvalue 1000
Nocycle
Order;
INSERT INTO EMP TABLE:
INSERT INTO emp VALUES(s.nextval,’raju’);
Prof. Jaimini N.Patel [ SDJ International College ]
- RDBMS
CURSOR :
Cursor is a pl/sql private work area.it point out to the no of rows that being affected or retrieve while
executing a pl/sql block.
Whenever you executing DDL, DQL language this cursor is being in action means while you perform insert
, update or delete the implicit cursor in a action name SQL.Implicit cursor is not needed to define.
Oracle creates a memory area, known as context area/ temporary storage area, for processing an SQL
statement, which contains all information needed for processing the statement.
A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor. A cursor holds
the rows (one or more) returned by a SQL statement.
The set of rows the cursor holds is referred to as the active set.
There are two types of cursors :
1. Implicit Cursor [ In-built Cursor ]
2. Explicit Cursor [ User Define Cursor ]
Implicit Cursor :
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed,
Programmers cannot control the implicit cursors and the information in it.
In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor.
Prof. Jaimini N. Patel[SDJ International college ]
- RDBMS
Attribute:
Attribute Description
SQL%FOUND Returns TRUE if an INSERT, UPDATE, or DELETE statement affected
one or more rows or a SELECT INTO statement returned one or more rows.
Otherwise, it returns
FALSE.
SQL%NOTFOUND The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or
DELETE statement affected no rows, or a SELECT INTO
statement returned no rows. Otherwise, it returns FALSE.
SQL%ISOPEN Always returns FALSE for implicit cursors, because Oracle closes the
SQL cursor automatically after executing its associated SQL statement.
SQL%ROWCOUNT Returns the number of rows affected by an INSERT,
UPDATE, or
DELETE statement, or returned by a SELECT INTO statement.
Explicit Cursor:
Explicit cursors are programmer defined cursors for gaining more control over the context area.
An explicit cursor should be defined in the declaration section of the PL/SQL Block. It is created on a
SELECT Statement which returns more than one row.
Syntax
CURSOR cursor_name IS SELECT statement;
Working with an explicit cursor involves four steps:
1. Declaring the cursor for initializing in the memory
2. Opening the cursor for allocating memory
3. Fetching the cursor for retrieving data
4. Closing the cursor to release allocated memory
1. Declaring the cursor :
Prof. Jaimini N. Patel[SDJ International college ]
- RDBMS
Declaring the cursor defines the cursor with a name and the associated SELECT statement.
Syntax :
CURSOR cursor_name IS SELECT statement ;
2. Opening the cursor :
Opening the cursor allocates memory for the cursor and makes it ready for fetching the rows returned
by the SQL statement into it.
Syntax
OPEN cursor_name ;
3. Fetching the cursor :
Fetching the cursor involves accessing one row at a time.
Syntax
FETCH cursor_name INTO variable1, variable2, ..
4. Closing the cursor :
Closing the cursor means releasing the allocated memory.
Syntax
CLOSE cursor_name ;
The following program will update the table and increase the salary of each customer by 500 and use the
SQL%ROWCOUNT attribute to determine the number of rows affected
IN PL /SQL BLOCK :
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
Prof. Jaimini N. Patel[SDJ International college ]
- RDBMS
END IF;
END;
/
Package :
PL/SQL packages are schema objects that groups logically related PL/SQL types, variables and
subprograms.
A package will have two mandatory parts :
Package Specification
Package Body or definition
Package Specification :
The specification is the interface to the package. It just DECLARES the types, variables, constants, exceptions,
cursors, and subprograms that can be referenced from outside the package. In other words, it contains all
information about the content of the package, but excludes the code for the subprograms.
All objects placed in the specification are called public objects. Any subprogram not in the package
specification but coded in the package body is called a private object.
You can have many global variables defined and multiple procedures or functions inside a package.
Syntax : PACKAGE SPECIFICATION
CREATE OR REPLACE PACKAGE package_name AS
No. of Procedure and function declaration ;
END package_name ;
/
Example
CREATE OR REPLACE PACKAGE simple
AS
PROCEDURE JAIMINI ;
FUNCTION funJAIMINI RETURN varchar2 ;
END simple ;
Package Body or Definition :
The package body has the codes for various methods declared in the package specification. The
CREATE PACKAGE BODY Statement is used for creating the package body.
Syntax :
CREATE OR REPLACE PACKAGE BODY package_name
AS
Prof. Jaimini N. Patel[SDJ International college ]
- RDBMS
Definition of procedure or function ;
END package_name ;
Example
CREATE OR REPLACE PACKAGE BODY simple
AS
PROCEDURE JAIMINI
As
BEGIN
dbms_output.put_line(‘ HI JAIMINI ’) ;
END JAIMINI;
FUNCTION funJAIMINI RETURN varchar2
AS
BEGIN
dbms_output.put_line(‘HELLO JAIMINI ’) ;
END funjaimini;
END simple ;
How to use Package Element :
Syntax
package_name . element_name ;
Example
Simple.jaimini ;
Example : (WHITE SCREEN)
DECLARE
n number( 5 ) ;
BEGIN
simple .JAIMINI ;
Prof. Jaimini N. Patel[SDJ International college ]
- RDBMS
n := simple . funJAIMINI ;
END ;
/
INDEX
Indexes are special lookup tables that the database search engine can use to speed up data retrieval. Simply put, an
index is a pointer to data in a table. An index in a database is very similar to an index in the back of a book.
For example, if you want to reference all pages in a book that discusses a certain topic, you first refer to the index,
which lists all the topics alphabetically and are then referred to one or more specific page numbers.
An index helps to speed up SELECT queries and WHERE clauses, but it slows down data input, with the UPDATE and
the INSERT statements. Indexes can be created or dropped with no effect on the data.
Creating an index involves the CREATE INDEX statement, which allows you to name the index, to specify the table
and which column or columns to index, and to indicate whether the index is in an ascending or descending order.
Indexes can also be unique, like the UNIQUE constraint, in that the index prevents duplicate entries in the column
or combination of columns on which there is an index.
The CREATE INDEX Command
The basic syntax of a CREATE INDEX is as follows.
CREATE INDEX index_name ON table_name(COLUMN_NAME);
Single-Column Indexes
A single-column index is created based on only one table column. The basic syntax is as follows.
CREATE INDEX index_name
ON table_name (column_name);
Unique Indexes
Unique indexes are used not only for performance, but also for data integrity. A unique index does not allow any
duplicate values to be inserted into the table. The basic syntax is as follows.
CREATE UNIQUE INDEX index_name
on table_name (column_name);
Composite Indexes:
A composite index is an index on two or more columns of a table. Its basic syntax is as follows.
CREATE INDEX index_name
Prof. Jaimini N. Patel [SDJ International College]
- RDBMS
on table_name (column1, column2);
Whether to create a single-column index or a composite index, take into consideration the column(s) that
you may use very frequently in a query's WHERE clause as filter conditions.
Should there be only one column used, a single-column index should be the choice. Should there be two or
more columns that are frequently used in the WHERE clause as filters, the composite index would be the
best choice.
The DROP INDEX Command
An index can be dropped using SQL DROP command. Care should be taken when dropping an index
because the performance may either slow down or improve.
The basic syntax is as
follows − DROP INDEX
index_name;
You can check the INDEX Constraint chapter to see some actual examples on Indexes.
When should indexes be avoided?
Although indexes are intended to enhance a database's performance, there are times when they should be
avoided. The following guidelines indicate when the use of an index should be reconsidered.
Indexes should not be used on small tables.
Tables that have frequent, large batch updates or insert operations.
Indexes should not be used on columns that contain a high number of NULL values.
Columns that are frequently manipulated should not be indexed.
VIEW :
View is a virtual table define by query.
After a table is created and population with data, it may become necessary to prevent all user from
accessing all column of a table for data security reason.
This would mean creating several tables having the appropriate number of column and accessing specific
user to each table as required.
This will answer data security requirement very well but give rise to a great deal of redundant data being
resident in table in the database.
To reduce redundant data to minimum possible. Oracle allow the creation of an object called view. Some
Prof. Jaimini N. Patel [SDJ International College]
- RDBMS
view are use only for looking at the table data and other view can be use to insert, update and delete table
data as well as view data if the view use to only look at table data and nothing else the view is called read
only view.
A view that is used to look at table data as well as insert, update and delete table data is called
updatable view.
Reason why view are created:
Reason why view are created.When data security is require.
When data redundancy is to kept to the minimum while maintaining data security.
Syntax :
CREATE VIEW <viewname>
AS
SELECT <columns> FROM <tablename>
WHERE <condition>;
Example :
CREATE VIEW v1
AS
SELECT * FROM emp;
CREATE VIEW v1
AS
SELECT eno,ename
FROM emp;
Renaming view name
The column of the view can take on different names from the table column.
Syntax :
CREATE VIEW <viewname>
AS
SELECT <column1> “new_name”,<column2> “new_name”
FROM <tablename>
WHERE <condition>;
Example :
CREATE VIEW v3
AS
Prof. Jaimini N. Patel [SDJ International College]
- RDBMS
SELECT eno “employee”
FROM emp;
Selecting data from view
Syntax :
SELECT <columns>
FROM <viewname>
WHERE <condition>;
Example :
SELECT * FROM v1;
Updatable view
View can also be used for data manipulation. View on which data manipulation can be done are called
updatable view.
When updatable view name is given in an insert update or delete SQL statement modification to data in
the view will be immediately passed to the underline table.
Rules for update view :
View define from single table.
If the user wants to insert record with the help of view, then the primary key column and all the
NOT NULL column must be include in the view.
The user can update, delete record with the help of view even if the primary key column and
NOT NULL column are excluded from the view define.
Common rules for update view (allow) :
The view is defined based on one and only one table.
The view must include the PRIMARY KEY of the table based upon which the view has been
created.
The view should not have any field made out of aggregate functions.
The view must not have any DISTINCT clause in its definition.
If the view you want to update is based upon another view, the later should be updatable.
Any of the selected output fields (of the view) must not use constants, strings or value
expressions.
Syntax :
UPDATE <viewname>
SET <column1>=<value1>,<column2>=<value2>
WHERE <condition>;
Example :
UPDATE v2
SET ename=”pratik”
WHERE eno=1;
Prof. Jaimini N. Patel [SDJ International College]
- RDBMS
Destroy view
Syntax :
DROP VIEW <viewname>;
Example :
DROP VIEW v1;
View define from multi table
If a view is create from multi table which well creating using a reference clause, then through the
primary key column as well as the NOT NULL column are include in the definition.
Rules
An insert operation not allow.
The delete or modify operation do not affect the master table.
The view can be used to modify the column of the detail table.
CREATE VIEW <viewname> AS
SELECT table1.column,table2.column
FROM table1,table2;
Example :
CREATE VIEW v1 AS
SELECT emp.ename,cast.cname
FROM emp,cast;
Prof. Jaimini N. Patel [SDJ International College]