Unit - IV Material
Unit - IV Material
UNIT -4
COMMIT Command:
The COMMIT command is the transactional command used to save changes invoked
by a transaction to the database.
The COMMIT command saves all transactions to the database since the last COMMIT
or ROLLBACK command.
The syntax for COMMIT command is as follows:
COMMIT;
1
Advanced SQL
ROLLBACK Command:
A ROLLBACK does exactly the opposite of COMMIT.
The ROLLBACK command is the transactional command used to undo transactions
that have not already been saved to the database.
The ROLLBACK command can only be used to undo transactions since the last
COMMIT or ROLLBACK command was issued.
The syntax for ROLLBACK command is as follows:
ROLLBACK;
SAVEPOINT Command:
A SAVEPOINT is a point in a transaction when you can roll the transaction back to a
certain point without rolling back the entire transaction.
The syntax for SAVEPOINT command is as follows:
SAVEPOINT SAVEPOINT_NAME;
This command serves only in the creation of a SAVEPOINT among transactional
statements.
The ROLLBACK command is used to undo a group of transactions.
The syntax for rolling back to a SAVEPOINT is as follows:
ROLLBACK TO SAVEPOINT_NAME;
2
Advanced SQL
Each object privilege that is granted authorizes the grantee to perform some operation on the
object. A user can grant all the privileges or grant only specific object privileges.
The list of object privileges is as follows:
ALTER : Allows the grantee to change the table definition with the ALTER TABLE command
DELETE : Allows the grantee to remove the records from the table with the DELETE
command
INDEX : Allows the grantee to create an index on the table with the CREATE INDEX
command
INSERT : Allows the grantee to add records to the table with the INSERT command
SELECT : Allows the grantee to query the table with the SELECT command
UPDATE : Allows the grantee to modify the records in the tables with the UPDATE
command
Example 1: Give the user X permission to only view and modify records in the table
client_master.
GRANT SELECT, UPDATE ON client master TO X;
Revoke
The REVOKE statement is used to deny the grant given on an object.
Revokes a privilege from a user
It is use to taking off or remove of authority or say getting back authority from user
Syntax:
REVOKE < Object Privileges > ON <Object Name> FROM <Username>;
Example 1:
All privileges on the table salesman_master have been granted to X. Take back the Delete
privilege on the table.
REVOKE DELETE ON salesman master FROM X;
3
Advanced SQL
Locks
Definition :
Locks are mechanism used to ensure data integrity. The oracle engine automatically locks table
data while executing SQL statements like Select/insert/UPDATE/DELETE. This type of locking is
called implicit locking
Types of Locks
There are two types of Locks
Shared Lock
Exclusive Lock
Shared Lock
Shared locks are placed on resources whenever a read operation (select) is performed.
Multiple shared locks can be simultaneously set on a resource.
Exclusive Locks
Exclusive locks are placed on resources whenever a write operation (INSERT, UPDATE And
DELETE) are performed.
Only one exclusive lock can be placed on a resource at a time.
i.e. the first user who acquires an exclusive lock will continue to have the sole ownership of
the resource, and no other user can acquire an exclusive lock on that resource
Levels of Locks
Oracle does not provide a field level lock.
Oracle provides the following three levels of Locking.
Row Level
Page Level
Table Level
4
Advanced SQL
Deadlock
In a deadlock, two database operations wait for each other to release a lock.
A deadlock occurs when two users have a lock, each on a separate object, and, they want to
acquire a lock on each other's object.
When this happens, the first user has to wait for the second user to release the lock, but the
second user will not release it until the lock on the first user's object is freed. At this point, both
the users are at an impasse and cannot proceed with their business.
In such a case, Oracle detects the deadlock automatically and solves the problem by aborting one
of the two transactions.
Example 1:
Transaction 1
BEGIN
UPDATE client_master SET salary = 500 WHERE client_no=’c1’;
UPDATE client_master SET salary = 500 WHERE client_no=’c2’;
END
Transaction 2
BEGIN
UPDATE client_master SET salary = 5000 WHERE client_no=’c2’;
UPDATE client_master SET salary = 500 WHERE client_no=’c1’;
END
Assume that Transaction 1 and Transaction2 begin exactly at the same time.
By default Oracle automatically places exclusive lock on data that is being updated.
This causes Transaction 1 to wait for Transaction2 to complete but in turn Transaction2 has
to wait for Transaction 1 to complete.
5
Advanced SQL
Database object
Synonym
A synonym is an alternative name for objects such as tables, views, sequences, stored
procedures, and other database objects.
Creating or replacing a synonym
The syntax for creating a synonym is:
Create [or replace] [public] synonym [schema .] synonym_name for [schema .]
object_name [@ dblink];
The or replace phrase allows you to recreate the synonym (if it already exists) without
having to issue a DROP synonym command.
The public phrase means that the synonym is a public synonym and is accessible to all
users. Remember though that the user must first have the appropriate privileges to the
object to use the synonym.
The schema phrase is the appropriate schema. If this phrase is omitted, Oracle assumes
that you are referring to your own schema.
The object_name phrase is the name of the object for which you are creating the
synonym. It can be one of the following:
Table Package
View materialized view
java class schema
Sequence
object
stored procedure user-defined object
Function synonym
Example:
SQL> create table test (a number);
Table created
SQL> create synonym t for test;
Synonym created
SQL> insert into t values(4);
1 row created
SQL> insert into t values(5);
1 row created
6
Advanced SQL
Sequences
Oracle provides an object called a Sequence that can generate numeric values. The value
generated can have a maximum of 38 digits. A sequence can be defined to:
Generate numbers in ascending or descending order
Provide intervals between numbers
Caching of sequence numbers in memory to speed up their availability
A sequence is an independent object and can be used with any table that requires its
output.
Creating Sequences
The minimum information required for generating numbers using a sequence is:
The starting number
The maximum number that can be generated by a sequence
The increment value for generating the next number.
Syntax:
CREATE SEQUENCE <Sequence Name> [INCREMENT BY <IntegerValue> START
WITH <Integer Value> MAXVALUE <Integer Value> CYCLE ]
Keywords And Parameters
INCREMENT BY: Specifies the interval between sequence numbers. It can be any
positive or negative value but not zero. If this clause is omitted, the default value
is 1.
MAXVALUE: Specifies the maximum value that a sequence can generate.
START WITH: Specifies the first sequence number to be generated. The default
for an ascending sequence is the sequence minimum value (1) and for a
descending sequence, it is the maximum value (-1).
CYCLE: Specifies that the sequence continues to generate repeat values after
reaching either its maximum value.
EXAMPLE
SQL> create sequence X increment by 1 start with 1 maxvalue 999 CYCLE;
SQL> select sequence_name.nextval from dual;
SQL> insert into emp (eno,name) values(X.nextval,’abc’);
7
Advanced SQL
Altering A Sequence
A sequence once created can be altered.
This is achieved by using the ALTER SEQUENCE statement. SYNTAX:
The START value of the sequence cannot be altered
ALTER SEQUENCE <SequenceName> [INCREMENT BY <IntegerValue> MINVALUE
<IntegerValue> ]
Example: Alter sequence X increment by 2;
Views
To reduce REDUNDANT DATA to the minimum possible, Oracle allows the creation of an
object called a VIEW.
A View is mapped, to a SELECT sentence. The table on which the view is based is
described in the FROM clause of the SELECT statement.
Some Views are used only for looking at table data. Other Views can be used to Insert,
Update and Delete table data as well as View data. If a View is used to only look at table
data and nothing else the View is called a Read-Only View. A View that is used to look at
table data as well as Insert, Update and Delete table data is called an Updateable View.
The reasons why views are created are:
When Data security is required
When Data redundancy is to be kept to the minimum while maintaining data security
Let’s spend some time in learning how a View is:
Created
Used for only viewing and / or manipulating table data (i.e. a read-only or
updateable view)
Destroyed
Creating View
The ORDER BY clause cannot be used while creating a view.
The columns of the table are related to the view using a one-to-one relationship.
Syntax:
CREATE VIEW <ViewName> AS SELECT <ColumnName1 > , <ColumnName2>
FROM <TableName> WHERE <ColumnName> = < Expression List>;
GROUP BY <Grouping Criteria> HAVING <Predicate>
Example :
Create a view called employees on the emp table
SQL> create view rp as select eno,ename,sal from emp;
8
Advanced SQL
Updateable Views
Views can also be used for data manipulation (i.e. the user can perform the Insert,
Update and Delete operations).
Views on which data manipulation can be done are called Updateable Views. When
an updateable view name is given in an Insert Update, or Delete SQL statement,
modifications to data in the view will be immediately passed to the underlying table.
For a view to be updateable, it should meet the following criteria:
Views defined from Single table
If the user wants to INSERT records with the help of a view, then the PRIMARY
KEY column(s) and all the NOT NULL columns must be included in the view
The user can UPDATE, DELETE records with the help of a view even if the
PRIMARY KEY column and NOT NULL column(s) are excluded from the view
definition
Example :
When a modify operation is performed using the view:
SQL> create view rp as select eno,ename,sal from emp;
SQL> insert into rp values(‘e1’,’rahul’,25000);
SQL> update rp set ename=’Vishal’ where ename=’rahul’;
SQL> select * from rp;
Index
An Index is an ordered list of contents of a column, (or a group of columns) of a table
When users require some record having some condition than oracle engine must search
that record in the whole table.
This become very slow and very lengthy when there is millions of record in the table.
So indexing a table is an 'access strategy', that is a way to sort and search records in the
table.
By indexing we can improve the speed of searching and retrieving the record from table.
(It is just like searching data in DSM)
Index is created on one or more columns. Based on the number of columns included in
the index, an index cab be:
Simple index.
Composite Index.
9
Advanced SQL
10