0% found this document useful (0 votes)
5 views10 pages

Unit - IV Material

This document covers advanced SQL concepts, focusing on transaction control, data control language (DCL), locks, deadlocks, and database objects such as synonyms, sequences, views, and indexes. It explains transaction commands like COMMIT, ROLLBACK, and SAVEPOINT, as well as DCL commands for granting and revoking privileges. Additionally, it details the types of locks, deadlock scenarios, and the creation and manipulation of database objects to enhance data integrity and security.

Uploaded by

Arsh Mansuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Unit - IV Material

This document covers advanced SQL concepts, focusing on transaction control, data control language (DCL), locks, deadlocks, and database objects such as synonyms, sequences, views, and indexes. It explains transaction commands like COMMIT, ROLLBACK, and SAVEPOINT, as well as DCL commands for granting and revoking privileges. Additionally, it details the types of locks, deadlock scenarios, and the creation and manipulation of database objects to enhance data integrity and security.

Uploaded by

Arsh Mansuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Advanced SQL

UNIT -4

Database Objects & Security

1.1 Advanced SQL

 Transaction control and DCL

 Transaction control (Transaction Control language)


 A transaction is a unit of work that is performed against a database.
 A transaction is the propagation of one or more changes to the database.
 For example, if you are creating a record or updating a record or deleting a record from the
table then you are performing transaction on the table.
 It is important to control transactions to ensure data integrity and to handle database
errors.
 Practically you will club many SQL queries into a group and you will execute all of them
together as a part of a transaction.
 Transaction Control:
 There are following commands used to control transactions:
 COMMIT: to save the changes.
 ROLLBACK: to rollback the changes.
 SAVEPOINT: creates points within groups of transactions in which to ROLLBACK
 SET TRANSACTION: Places a name on a transaction.
 Transactional control commands are only used with the DML commands INSERT,
UPDATE, and DELETE only.
 They cannot be used while creating tables or dropping them because these operations
are automatically committed in the database.

 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;

 DCL (Data Control Language)


 Two types of DCL
 Grant
 Revoke
 Grant
 Grants a privilege to a user
 It means that giving authority to other user by administrator
 If you are administrator then only you have authority for grating the other authority to
other user
 Can grant privilege only if you have been granted that privilege
 Syntax:
 GRANT < Object Privileges > ON <ObjectName> TO <UserName> [WITH GRANT
OPTION];
 OBJECT PRIVILEGES

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

 Row Level Locking


 If the WHERE clause evaluates to only one row in the table, a row level lock is used.
 Page Level Locking
 If the WHERE clause evaluates to a set of data, a page level lock is used.

4
Advanced SQL

 Table Level Locking


 If there is no WHERE clause, the query accesses the entire table, a table level lock is used.
 Can't update entire table data when update is done by other user.
 Syntax: LOCK TABLE <table name> [<table name>]….. IN { ROW SHARE / ROW EXCLUSIVE /
SHARE UPDATE / SHARE / SHARE ROW EXCLUSIVE / EXCLUSIVE }[NOWAIT]
 Exclusive: They allow query on the locked resource but prohibit any other activity
 Syntax: Client A > Lock table client_master IN Exclusive Mode NOWAIT;
 Output Table(s) Locked.

 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

 SQL> select * from rp;


 This is display only eno, ename, and sal from emp table

 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

 Creating Simple Index


 An index created on a single column of a table is called a Simple Index. The syntax for
creating simple index that allows duplicate values is as described.
 Syntax: CREATE INDEX <IndexName> ON <TableName> (<ColumnName>);
 Example 1: Create a simple index on client_no column of the client_master table.
 Solution: CREATE INDEX idxclientno ON client_master (client_no);
 Output: Index created.

 Creating Composite Index


 An index created on more than one column is called a Composite Index. The syntax for
creating a composite index that allows duplicate values is:
 Syntax: CREATE INDEX <Index Name> ON <Table Name> (<ColumnNamel> <ColumnName2>);
 Example 1: Create a composite index on the client_master table on columns client_no and
act_no;
 Solution: CREATE INDEX idxclientact ON client_master (client_no, act_no);
 Output: Index created.
 Creation of Unique Index
 A unique index can also be created on one or more columns. If an index is created on a single
column, it is called a Simple Unique Index. The syntax for creating a simple unique index is as
follows:
 Syntax: CREATE UNIQUE INDEX <Index Name> ON <Table Name> (<Column Name>);
 If an index is created on more than one column, it is called a Composite Unique Index. The
syntax for creating a composite unique index is as follows:
 Syntax: CREATE UNIQUE INDEX <Index Name> ON <Table Name> (<Column Name>,
<Column Name>);
 Example 5: Create a unique index on CUST_NO column of the CUST_MSTR table.
 Solution: CREATE UNIQUE INDEX idx_CustNo ON CUST_MSTR (CUST_NO);
 Output: Index created.
 Note: When the user defines a primary key or a unique key constraint at table or column
level, the Oracle engine automatically creates a unique index on the primary key or unique key
column(s).

10

You might also like