Oracle Database

  Home  Oracle  Oracle Database


“Learn Oracle database programming with hundreds of Interview Questions and Answers and examples and get a better job as an Oracle DBA with this basic and advance Oracle Database Interview Questions and Answers guide”



430 Oracle Database Questions And Answers

161⟩ What Happens to the Current Transaction If a DDL Statement Is Executed?

If a DDL statement is executed, the current transaction will be committed and ended. All the database changes made in the current transaction will become permanent. This is called an implicit commit by a DDL statement. The following tutorial exercise shows you that the CREATE TABLE statement forced the current transaction to be committed and ended. The subsequent ROLLBACK statement has no effects on the closed transaction.

SQL> connect HR/globalguideline

SQL> INSERT INTO ggl_links (url, id)

2 VALUES ('oracle.com', 112);

SQL> INSERT INTO ggl_links (url, id)

2 VALUES ('sql.com', 113);

SQL> CREATE TABLE ggl_temp AS (SELECT * FROM ggl_links);

Table created.

SQL> ROLLBACK;

Rollback complete.

SQL> SELECT * FROM ggl_links;

  ID URL   NOTES  COUNTS CREATED

------- ---------------- ---------- ---------- ---------

101 rendc.org 07-MAY-06

110 rendc.org/html 07-MAY-06

112 oracle.com 07-MAY-06

113 sql.com 07-MAY-06

 147 views

162⟩ What Happens to the Current Transaction If the Session Is Ended?

If a session is ended, the current transaction in that session will be committed and ended. All the database changes made in the current transaction will become permanent. This is called an implicit commit when session is ended. The following tutorial exercise shows you that the "disconnect" command forces the current transaction to be committed and ended. When the session is reconnected, you can see the changes made by the UPDATE statements.

SQL> connect HR/globalguideline

SQL> UPDATE ggl_links SET url = 'globalguideline.COM'

2 WHERE id = 101;

SQL> UPDATE ggl_links SET url = 'globalguideline.COM/html'

2 WHERE id = 110;

SQL> disconnect

SQL> connect HR/globalguideline

SQL> SELECT * FROM ggl_links;

  ID URL   NOTES  COUNTS CREATED

------- ---------------- ---------- ---------- ---------

101 globalguideline.COM 07-MAY-06

110 globalguideline.COM/html 07-MAY-06

112 oracle.com 07-MAY-06

113 sql.com 07-MAY-06

 109 views

163⟩ What Happens to the Current Transaction If the Session Is Killed?

If a session is killed by the DBA, the current transaction in that session will be rolled back and ended. All the database changes made in the current transaction will be removed. This is called an implicit rollback when session is killed. The following tutorial exercise shows you that the DBA KILL SESSION command forces the current transaction to be rolled back with all the changes uncommitted.

SQL> connect HR/globalguideline

SQL> SELECT * FROM ggl_links;

  ID URL   NOTES  COUNTS CREATED

------- ---------------- ---------- ---------- ---------

101 globalguideline.COM 07-MAY-06

110 globalguideline.COM 07-MAY-06

112 oracle.com 07-MAY-06

113 sql.com 07-MAY-06

SQL> DELETE FROM ggl_links where id = 112;

1 row deleted.

SQL> DELETE FROM ggl_links where id = 113;

1 row deleted.

SQL> SELECT * FROM ggl_links;

  ID URL   NOTES  COUNTS CREATED

------- ---------------- ---------- ---------- ---------

101 globalguideline.COM 07-MAY-06

110 globalguideline.COM 07-MAY-06

 137 views

164⟩ How Does Oracle Handle Read Consistency?

Oracle supports two options for you on how to maintain read consistency:

* READ WRITE (the default option), also called statement-level read consistency.

* READ ONLY, also called transaction-level read consistency.

 112 views

165⟩ What Is a READ WRITE Transaction in Oracle?

A READ WRITE transaction is a transaction in which the read consistency is set at the statement level. In a READ WRITE transaction, a logical snapshot of the database is created at the beginning of the execution of each statement and released at the end of the execution. This guaranties that all reads within a single statement get consistent data from the database.

For example, if you have a query statement that takes 10 minutes to be executed, a snapshot of the database will be created for this statement for 10 minutes. If a subquery is used in this statement, it will get the consistent data no matter when it gets executed within this 10 minutes. In another word, data changes made during this 10 minutes by other users will not impact the execution of this query statement.

By default, all transactions are started as READ WRITE transactions.

 131 views

166⟩ What Is a READ ONLY Transaction in Oracle?

A READ ONLY transaction is a transaction in which the read consistency is set at the transaction level. In a READ ONLY transaction, a logical snapshot of the database is created at the beginning of the transaction and released at the end of the transaction. This guaranties that all reads in all statements within this transaction get consistent data from the database.

For example, if you have a transaction with many statements that takes 10 hours to be executed, a snapshot of the database will be created for this transaction for 10 hours. If a query statement is executed at the beginning of the transaction and at the end of the transaction, it will return the same result guarantied. In another word, data changes made during this 10 hours by other users will not impact the execution of statements within this transaction.

 114 views

167⟩ What Are the Restrictions in a Oracle READ ONLY Transaction?

There are lots of restrictions in a READ ONLY transaction:

* You can not switch to READ WRITE mode.

* You can not run any INSERT, UPDATE, DELETE statements.

* You can run SELECT query statements.

The tutorial exercise below shows you some of the restrictions:

SQL> connect HR/globalguideline

SQL> SET TRANSACTION READ ONLY;

Transaction set.

SQL> SET TRANSACTION READ WRITE;

ORA-01453: SET TRANSACTION must be first statement of

transaction

SQL> INSERT INTO ggl_links (url, id)

2 VALUES ('sql.com', 113);

ORA-01456: may not perform insert/delete/update operation

inside a READ ONLY transaction

SQL> DELETE FROM ggl_links where id = 110;

ORA-01456: may not perform insert/delete/update operation

inside a READ ONLY transaction

SQL> SELECT * FROM ggl_links;

  ID URL   NOTES  COUNTS CREATED

------- ---------------- ---------- ---------- ---------

101 globalguideline.COM 07-MAY-06

110 globalguideline.COM 07-MAY-06

 122 views

168⟩ How To Set a Transaction To Be READ ONLY in Oracle?

If you want a transaction to be set as READ ONLY, you need to the transaction with the SET TRANSACTION READ ONLY statement. Note that a DML statement will start the transaction automatically. So you have to issue the SET TRANSACTION statement before any DML statements. The tutorial exercise below shows you a good example of READ ONLY transaction:

SQL> connect HR/globalguideline

SQL> SET TRANSACTION READ ONLY;

Transaction set.

SQL> SELECT * FROM ggl_links;

  ID URL   NOTES  COUNTS CREATED

------- ---------------- ---------- ---------- ---------

101 globalguideline.COM 07-MAY-06

110 globalguideline.COM 07-MAY-06

112 oracle.com 07-MAY-06

113 sql.com 07-MAY-06

 133 views

169⟩ What Is a Connect Identifier?

A "connect identifier" is an identification string of a single set of connection information to a specific target database instance on a specific Oracle server.

Connect identifiers are defined and stored in a file called tnsnames.ora located in $ORACLE_HOME/network/admin/ directory. Here is one example of a "connect identifier" definition:

ggl_XE =

(DESCRIPTION =

(ADDRESS =

(PROTOCOL = TCP)

(HOST = www.rendc.org)

(PORT = 1521)

)

(CONNECT_DATA =

(SERVER = DEDICATED)

(SERVICE_NAME = XE)

)

)

The above "connect identifier" defines "TNS_XE" with the following connection information:

* The network hostname: www.rendc.org.

* The network port number: 1521.

* The name of the target database instance: XE.

 124 views

170⟩ What Are the General Rules on Data Consistency?

► All SQL statements always work with a snapshot of the database to provide data consistency.

► For READ WRITE transactions, the snapshot is taken when each statement starts.

► For READ ONLY transactions, the snapshot is taken when the transaction starts.

► The snapshot never include uncommitted changes from other transactions.

► The snapshot always include uncommitted changes from its own transaction.

 132 views

171⟩ What Are Transaction Isolation Levels Supported by Oracle?

Oracle supports two transaction isolation levels:

► READ COMMITTED (the default option). If the transaction contains DML that requires row locks held by another transaction, then the DML statement waits until the row locks are released.

► SERIALIZABLE. If a serializable transaction contains data manipulation language (DML) that attempts to update any resource that may have been updated in a transaction uncommitted at the start of the serializable transaction, then the DML statement fails.

 123 views

172⟩ What Is a Data Lock in Oracle?

A data lock is logical flag the Oracle server is placed on data objects to give an exclusive right to a transaction. Statements in other transactions needs to respect data locks based on certain rules. Rules on data locks are:

► SELECT query statements do not create any data locks.

► INSERT, UPDATE, and DELETE statements create data locks on the affected rows.

► Data locks are released when the owner transaction ends.

 117 views

173⟩ How Data Locks Are Respected in Oracle?

Here are the rules on how data locks are respected:

► All statements ignore data locks owned its own transaction.

► SELECT query statements ignores data locks owned by any transactions.

► INSERT, UPDATE, and DELETE statements in a READ COMMITTED transaction will wait for data locks on their targeted rows by other transactions to be released.

► INSERT, UPDATE, and DELETE statements in a SERIALIZABLE transaction will fail if their targeted rows has data locks owned by other transactions.

 119 views

174⟩ How To Experiment a Data Lock in Oracle?

If you want to have some experience with data locks, you can create two windows runing two SQL*Plus sessions. In session 1, you can run a UPDATE statements to create a data lock. Before committing session 2, switch to session 2, and run a UPDATE statements on the same row as session 1. The UPDATE statement will be put into wait status because of the data lock. Follow the tutorial exercise below to experience yourself:

(session 1)

SQL> connect HR/globalguideline

SQL> SET TRANSACTION

ISOLATION LEVEL

READ COMMITTED;

Transaction set.

SQL> SELECT * FROM ggl_links;

ID URL NOTES

--- ---------------- --------

101 globalguideline.COM

110 globalguideline.COM

SQL> UPDATE ggl_links

SET url='rendc.org'

WHERE id=101;

1 row updated.

(lock created on row id=101)

 136 views

175⟩ How To View Existing Locks on the Database?

As can see from the pervious tutorial exercise, performance of the second session is greatly affected by the data lock created on the database. To maintain a good performance level for all sessions, you need to monitor the number of data locks on the database, and how long do they last.

Oracle maintains current existing data locks in a Dynamic Performance View called V$LOCK with columns like:

* SID - Session ID to identify the session that owns this lock.

* TYPE - The type of the lock, like TM (DML enqueue) and TX (Transaction enqueue).

* LMODE - The lock mode in which the session holds the lock.

* REQUEST - The lock mode in which the session requests the lock.

* CTIME - The time since current lock mode was granted

* BLOCK - A value of either 0 or 1, depending on whether or not the lock in question is the blocker.

The following tutorial exercise shows you how to view existing locks on the database:

(session 1)

SQL> connect HR/globalguideline

SQL> UPDATE ggl_links

SET url='rendc.org'

WHERE id=110;

1 row updated.

 133 views

176⟩ What Is a Dead Lock in Oracle?

A dead lock is phenomenon happens between two transactions with each of them holding a lock that blocks the other transaction as shown in the following diagram:

(transaction 1)   (transaction 2)

update row X to create lock 1

update row Y to create lock 2

update row X

(blocked by lock 1)

update row Y

(blocked by lock 2)

(dead lock created)

 132 views

177⟩ How Oracle Handles Dead Locks?

Oracle server automatically detects dead locks. When a dead lock is detected, Oracle server will select a victim transaction, and fail its statement that is blocked in the dead lock to break the dead lock. The tutorial exercise below shows you an example of statements failed by Oracle server because of dead locks:

(session 1)

SQL> connect HR/globalguideline

SQL> UPDATE ggl_links

SET notes='Session 1'

WHERE id=101;

1 row updated.

       (session 2)

SQL> connect HR/globalguideline

SQL> UPDATE ggl_links

SET notes='Session 2'

WHERE id=110;

1 row updated.

SQL> UPDATE ggl_links

SET notes='Session 2'

WHERE id=101;

(blocked by lock id=101)

SQL> UPDATE ggl_links

SET notes='Session 1'

WHERE id=110;

(blocked by lock on row id=110)

ORA-00060: deadlock

detected w

 147 views

178⟩ What Is an Oracle User Account?

A user account is identified by a user name and defines the user's attributes, including the following:

► Password for database authentication

► Privileges and roles

► Default tablespace for database objects

► Default temporary tablespace for query processing work space

 132 views

179⟩ What Is an Oracle User Role?

A user role is a group of privileges. Privileges are assigned to users through user roles. You create new roles, grant privileges to the roles, and then grant roles to users.

 120 views

180⟩ What Is the Relation of a User Account and a Schema?

User accounts and schemas have a one-to-one relation. When you create a user, you are also implicitly creating a schema for that user. A schema is a logical container for the database objects (such as tables, views, triggers, and so on) that the user creates. The schema name is the same as the user name, and can be used to unambiguously refer to objects owned by the user.

 156 views