Pl Sql Interview Questions
1.What is Cursor?Why cursor is required?
cursor is named private sql area from where information can be accessed.cursor are required to process rows individually for queries returning multiple rows
2) What are the Back ground processes in Oracle and what are they?
Data Base Writer (DBWR): Data Base Writer Writes Modified blocks from Database buffer cache to Data Files. This is required since the data is not written whenever a transaction is committed.
Log Writer (LGWR): Log writer writes the redo log entries to disk.Redo Log data is generated in redo log buffer of SGA. As transaction commits and logs buffer fills, LGWR writes log entries into an online redo log file.
System Monitor (SMON): The System Monitor performs instance recovery at instance startup. This is useful for recovery from system failure
Process Monitor (PMON): The Process Monitor performs process recovery when user Process fails. PMON Clears and Frees resources that process was using.
Check Point (CKPT): At Specified times, all modified database buffers in SGA are written to data files by DBWR at Checkpoints and Updating all data files and control files of database to indicate the most recent checkpoint
Achieves (ARCH): The Archiver copies online redo log files to archival storal when they are busy.
Recover or (RECO): The Recovered is used to resolve the distributed transaction in network
Dispatcher (Dnnn): The Dispatcher is useful in Multi Threaded Architecture
Lckn: We can have up to 10 lock processes for interred instance locking in parallel SQL.
3) What is a Transaction in Oracle
A transaction is a Logical unit of work that compromises one or more SQL Statements executed by a single User. According to ANSI,a transaction begins with first executable statement and ends when it is explicitly committed or rolled back.
4) What are Procedure, functions and Packages?
Procedures and functions consist of set of PL/SQL statements that are grouped together as a unit to solve a specific problem or perform set of related tasks. Procedures do not return values while Functions return one Value.
Packages: Packages provide a method of encapsulating and storing related procedures, functions, variables and other Package Contents
5) How many types of SQL Statements are there in Oracle
There are basically 6 types of SQL statements. They are
Data Definition Language (DDL): The DDL statements define and maintain objects and drop objects.
Data Manipulation Language (DML): The DML statements manipulate database data.
Transaction Control Statements: Manage change by DML
Session Control: Used to control the properties of current session enabling and disabling roles and changing.
E.g. Alter Statements,
System Control Statements: Change Properties of Oracle Instance.
E.g.: Alter System
Embedded SQL: Incorporate DDL, DML and T.C.S in Programming Language.
E.g. Using the SQL Statements in languages such as ‘C’, Open, Fetch,
6) Key Words Used in Oracle
The Key words that are used in Oracle are
Committing: A transaction is said to be committed when the transaction makes permanent changes resulting from the SQLstatements.
Rollback: A transaction that retracts any of the changes resulting from SQL statements in Transaction.
Save Point: For long transactions that contain many SQL statements, intermediate markers or save points are declared. Save points can be used to divide a transaction into smaller points.
Rolling Forward: Process of applying redo log during recovery is called rolling forward.
Cursor: A cursor is a handle (name or a pointer) for the memory associated with a specific stamen. A cursor is basically an area allocated by Oracle for executing the SQL Statement. Oracle uses an implicit cursor statement for Single row query and Uses Explicit cursor for a multi row query.
System Global Area (SGA): The SGA is a shared memory region allocated by the Oracle that contains Data and control information for one Oracle Instance. It consists of Database Buffer Cache and Redo log Buffer.
Program Global Area (PGA): The PGA is a memory buffer that contains data and control information for server process.
Database Buffer Cache: Database Buffer of SGA stores the most recently used blocks of database data. The set of database buffers in an instance is called Database Buffer Cache.
Redo log Buffer: Redo log Buffer of SGA stores all the redo log entries.
Redo Log Files: Redo log files are set of files that protect altered database data in memory that has not been written to Data Files. They are basically used for backup when a database crashes.
Process: A Process is a ‘thread of control’ or mechanism in Operating System that executes series of steps.
7) What are Database Triggers and Stored Procedures?
Database Triggers : Database Triggers are Procedures that are automatically executed as a result of insert in, update to, or delete from table.
Database triggers have the values old and new to denote the old value in the table before it is deleted and the new indicated the new value that will be used. Database Triggers are useful for implementing complex business rules, which cannot be enforced using the integrity rules. We can have the trigger as before trigger or After Trigger and at Statement or Row level.
E.g.: operations insert, update, and delete 3
Before, after 3*2 A total of 6 combinations
At statement level (once for the trigger) or row level (for every execution) 6 * 2 A total of 12.
Thus a total of 12 combinations are there and the restriction of usage of 12 triggers has been lifted from Oracle 7.3 Onwards.
Stored Procedures: Stored Procedures are Procedures that are stored in Compiled form in the database. The advantage of using the stored procedures is that many users can use the same procedure in compiled and ready to use format.
8) How many Integrity Rules are there and what are they?
There are Three Integrity Rules. They are as follows:
Entity Integrity Rule: The Entity Integrity Rule enforces that the Primary key cannot be Null
Foreign Key Integrity Rule: The FKIR denotes that the relationship between the foreign key and the primary key has to be enforced. When there is data in Child Tables the Master tables cannot be deleted.
Business Integrity Rules: The Third Integrity rule is about the complex business processes which cannot be implemented by the above 2 rules.
9) What are the Various Master and Detail Relation ships?
The various Master and Detail Relationship are
Non Isolated: The Master cannot be deleted when a child is existing
Isolated: The Master can be deleted when the child is existing
Cascading: The child gets deleted when the Master is deleted.
10) What are the Various Block Coordination Properties?
The various Block Coordination Properties are Immediate Default Setting. The Detail records are shown when the Master Record are shown. Deferred with Auto Query Oracle Forms defer fetching the detail records until the operator navigates to the detail block.Deferred with No Auto Query The operator must navigate to the detail block and explicitly execute a query.
11) What are the Different Optimisation Techniques?
The Various Optimization techniques are
Execute Plan: we can see the plan of the query and change it accordingly based on the indexes
Select /*+ First_Rows */ Deptno,Dname,Loc,Rowid
where (Deptno > 25)
Optimize_Sql: By setting the Optimize_Sql = No, Oracle Forms assigns a single cursor for all SQL statements.
This slow downs the processing because for evertime the SQL must be parsed whenever they are executed.
F45run module = my_firstform userid = scott/tiger optimize_sql= No
Optimize_Tp: By setting the Optimize_Tp= No, Oracle Forms assigns seperate cursor only for each query SELECT statement. All other SQL statements reuse the cursor.
f45run module = my_firstform userid = scott/tiger optimize_Tp =No
12) How do u implement the If statement in the Select Statement?
We can implement the if statement in the select statement by using the Decode statement.
e.g select DECODE (EMP_CAT,’1′,’First’,’2′,’Second’Null);
Here the Null is the else statement where null is done .
13) How many types of Exceptions are there
There are 2 types of exceptions. They are System Exceptions:
e.g. When no_data_found, When too_many_rows
User Defined Exceptions: e.g. My_exception exception,When My_exception then
14) What are the inline and the precompiler directives?
The inline and precompiler directives detect the values directly
15) How do you use the same lov for 2 columns
We can use the same lov for 2 columns by passing the return values in global values and using the global values in the code.
16) How many minimum groups are required for a matrix report
The minimum number of groups in matrix report are 4
17) What are snap shots and views?
Snapshots are mirror or replicas of tables. Views are built using the columns from one or more tables. The Single Table View can be updated but the view with multi table cannot be updated.
18) What are the OOPS concepts in Oracle?
Oracle does implement the OOPS concepts. The best example is the Property Classes. We can categories the properties by setting the visual attributes and then attach the property classes for the objects. OOPS supports the concepts of objects and classes and we can consider the property classes as classes and the items as objects.
19) What is the difference between candidate key, unique key and primary key?
Candidate keys are the columns in the table that could be the primary keys and the primary key is the key that has been selected to identify the rows. Unique key is also useful for identifying the distinct rows in the table.
20) What is concurrency?
Concurrency is allowing simultaneous access of same data by different users. Locks useful for accessing the database are
a) Exclusive: The exclusive lock is useful for locking the row when an insert, update or delete is being done. This lock should not be applied when we do only select from the row.
b) Share lock: We can do the table as Share Lock as many share locks can be put on the same resource.
21) Privileges and Grants
Privileges are the right to execute a particular type of SQL statements.
E.g.: Right to Connect, Right to create, Right to resource
Grants are given to the objects so that the object might be accessed accordingly. The grant has to be given by the owner of the object.
22) What are the Pct Free and Pct Used?
Pct Free is used to denote the percentage of the free space that is to be left when creating a table. Similarly Pct Used is used to denote the percentage of the used space that is to be used when creating a table
eg. Pctfree 20, Pctused 40
23) Table Space, Data Files, Parameter File, Control Files?
Table Space: The table space is useful for storing the data in the database. When a database is created two table spaces are created.
a) System Table space: This data file stores all the tables related to the system and DBA tables
b) User Table space: This data file stores all the user related tables We should have separate table spaces for storing the tables and indexes so that the access is fast.
Data Files: Every Oracle Data Base has one or more physical data files. They store the data for the database. Every data file is associated with only one database. Once the data file is created the size cannot change. To increase the size of the database tostore more data we have to add data file.
Parameter Files: Parameter file is needed to start an instance. A parameter file contains the list of instance configuration parameters
E.g. Db_block_buffers = 500
Byname = ORA7
db_domain = u.s.acme lang
Control Files: Control files record the physical structure of the data files and redo log files .They contain the Db name, name and location of dbs, data files, redo log files and time stamp.
24) Physical Storage of the Data
The finest level of granularity of the database are the data blocks.
Data Block: One Data Block correspond to specific number of physical database space
Extent: Extent is the number of specific number of contigious data blocks.
Segments: Set of Extents allocated for Extents. There are three types of Segments
a) Data Segment: Non Clustered Table has data segment data of every table is stored in cluster data segment
b) Index Segment: Each Index has index segment that stores data
c) Roll Back Segment: Temporarily store ‘undo’ information
25) What is Row Chaining
The data of a row in a table may not be able to fit the same data block. Data for row is stored in a chain of data blocks.
26) What is a 2 Phase Commit
Two Phase commit is used in distributed data base systems. This is useful to maintain the integrity of the database so that all the users see the same values. It contains DML statements or Remote Procedural calls that reference a remote object. There are basically 2 phases in a 2 phase commit.
a) Prepare Phase: Global coordinator asks participants to prepare
b) Commit Phase: Commit all participants to coordinator to Prepared, Read only or abort Reply
27) What is the difference between deleting and truncating of tables
Deleting a table will not remove the rows from the table but entry is there in the database dictionary and it can be retrieved But truncating a table deletes it completely and it cannot be retrieved.
28) What are mutating tables
When a table is in state of transition it is said to be mutating.
E.g. If a row has been deleted then the table is said to be mutating and no operations can be done on the table except select.
29) What are Codd Rules
Codd Rules describe the ideal nature of a RDBMS. No RDBMS satisfies all the 12 codd rules and Oracle Satisfies 11 of the 12 rules and is the only Rdbms to satisfy the maximum number of rules.
30) What is Normalization?
Normalization is the process of organising the tables to remove the redundancy. There are mainly 5 Normalisation rules.
a) 1 Normal Form : A table is said to be in 1st Normal Form when the attributes are atomic
b) 2 Normal Form: A table is said to be in 2nd Normal Form when all the candidate keys are dependant on the primary key
c) 3rd Normal Form : A table is said to be third Normal form when it is not dependant transitively
31) What is the Difference between a post query and a pre query?
A post query will fire for every row that is fetched but the pre query will fire only once.
32) Deleting the Duplicate rows in the table?
We can delete the duplicate rows in the table by using the Rowid
DELETE FROM table_name a
Where rowid>(select min(rowid) from table_name b where a.table_no=b.table_no);
33) Can U disable database trigger? How?
Yes. With respect to table
ALTER TABLE TABLE [DISABLE all_trigger ]
34) What is pseudo columns ? Name them?
A pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudocolumns, but you cannot insert, update, or delete their values. This section describes these pseudocolumns:
*CURRVAL, *NEXTVAL, *LEVEL, *ROWID, * ROWNUM
35) How many columns can table have?
The number of columns in a table can range from 1 to 254.
36) Is space acquired in blocks or extents?
In extents .
37) what is clustered index?
In an indexed cluster, rows are stored together based on their cluster key values .
Can not applied for HASH.
38) what are the datatypes supported By oracle (INTERNAL)?
Varchar2, Number, Char, MLSLABEL.
39) What are attributes of cursor?
%FOUND , %NOTFOUND , %ISOPEN,%ROWCOUNT
40)Can you use select in FROM clause of SQL select?
41) what is the significance of SQLCODE & SQLERRM in pl/SQL exception handler?
For internal exceptions, SQLCODE returns the number of the Oracle error. The number that SQLCODE returns is negative unless the Oracle error is no data found, in which case SQLCODE returns +100.
SQLERRM: returns the corresponding error message. The message begins with the Oracle error code. For user-defined exceptions, SQLCODE returns +1 and SQLERRM returns the message
User-Defined Exception: If no exception has been raised, SQLCODE returns zero and SQLERRM returns the message
42) what are INSTEAD OF triggers?
INSTEAD OF triggers provide a transparent way of modifying views that cannot be modified directly through SQL DML statements (INSERT, UPDATE, and DELETE). These triggers are called INSTEAD OF triggers because, unlike other types of triggers, Oracle fires the trigger instead of executing the triggering statement. The trigger performs update, insert, or delete operations directly on the underlying tables.
43) What is a Result Set? when is it created?
The result set can be defined as a logical set of rows that meet the query submitted to the server process. The only way to reference this object is through the cursor handle. this is created when the Cursor is opened.
44) Can one only retreive rows X to Y from a table?
(SELECT Rownum rn, tab1.*
WHERE rn between lower_value and Upper_value;
Tab1 will be the name of the table and lower_value will be the Lower bound and upper_bound will be the Upper bound value
45) How can I view the dependencies of a PL/SQL block?
Basically there are two types of dependencies a pl/sql block may have. Dependency on another pl/sql block called as procedural dependency. Dependency on a database object called as object dependency. For viewing the procedural dependencies we have the following group of views.
USER_DEPENDECIES, ALL_DEPENDENCIES and DBA_DEPENDENCIES
in the data dictionary
DBA_DEPENDENCIES: Shows both the dependencies. This views primarily has these following columns OWNER, NAME, TYPE, REFERENCED_OWNER, REFERENCED_NAME, REFERENCED_TYPE,REFERENCED_LINK_NAME
Another view can be used to find the object dependencies called as DEPTREE and IDEPTREE for indirect dependencies.
46) What are the two ways to specify a column alias?
Naming the alias after the column specification separated by a space
Ex: Select empid id, fname first from employee
2. Use of an ‘AS’ word to specify the alais more clearly
Ex: Select empid as id, fname as first from employee
47) How does one get the value of a sequence into a PL/SQLvariable?
Well, oracle doesn’t allow to assign a sequence value to a PL/SQL variable as
i := sq_sequence.NEXTVAL;(for some silly reason).
But you can do this:
select my_sequence.NEXTVAL into :i from dual; and then use the variable in the needed area.
48) Can one read/write files from PL/SQL?
Included in Oracle is an UTL_FILE package that can read and write operating system files. The directory you intend writing to has to be set in the init.ora by using UTL_FILE_DIR parameter. It can be set to * to allow writes to any directory in the file system before Oracle 7.3 the only means of writing a file was to use DBMS_OUTPUT with the SQL*Plus SPOOL command.
following is the example to open and write to a file ‘file1’ and then close.
fileHandler := UTL_FILE.FOPEN(‘/tmp’, ‘file1’, ‘w’);
UTL_FILE.PUTF(fileHandler, ‘the lines of text\n’);
WHEN utl_file.invalid_path THEN
raise_application_error(-20000, ‘ERROR: Invalid path for file or
path not in INIT.ORA.’);
49) What are the various types of Exceptions?
User defined and Predefined Exceptions.
50.Can we define exceptions twice in same block?
please click below part2,part3,part4 for more Pl Sql Interview Questions.