PLSQL
PLSQL
Delete vs Truncate
Delete Truncate
DML Command DDL Command (autocommit)
Can use where condition with delete Cannot use where condition with truncate
Delete specific rows “where” Condition All rows will be removed from table
Slower, because it uses undo Segment Faster
Triggers will get fired (on delete triggers) No triggers will get invoked
Wont reclaim the space used by Table Reclaim the space used by table
Wont reset the high level watermark Reset the high level watermark
“On Delete Cascade” from “Truncate table <tbl_name> CASCADE”
from oracle 12C from oracle 12C
2. Procedure vs Function
Procedure Function
Procedure can optionally return value Function must always return a value
using “OUT” & “IN OUT” parameter
Can not be called from SQL statement Function can be called from SQL stmnt
Used to implement logical data flow Used for computational purpose
Procedure can have DML statements **Functions having DML statements
----------------------------------------------------------------cannot be called from SQL stmnts.
----------------------------------------------------------------Autonomous transaction functions can
----------------------------------------------------------------be called from sql statement.
“RETURN” keyword to return out of procedure “RETURN” keyword to return the value
o Can we compile a function without the RETURN keyword
Yes however, we can not call that particular function from select statement
o How will you identify the functions without the return keywords
By enabling compiler warning setting & by recompiling all the functions
o From Oracle 12.1 version onwards, by using “With” clause, we can write the procedure &
function in the with clause itself & then we can execute as a part of select stmnt, in this case,
function & procedure are not created inside the database it’s just a part of with stmnt.
NVL2 function will check the first parameter, and return second parameter if the first parameter is not
null, otherwise returns third parameter.
NULLIF will compare the input parameters ( first and second parameter ), and returns NULL of both are
same, otherwise returns the first parameter value.
COALESCE, returns the first not null expression in the given input parameters.
For instance, if you want to store some new data into database, then you need to use "INSERT"
statement. similarly if you want to remove some data, then you need to use "DELETE" statement.
Depends on the functionality of these instructions or statements, the SQL statements are broadly
classified into five sub categories, or five SUB SQL languages as given below,
These language are classified based on the core functionality they perform, Given below commands for
each language.
With DISTINCT
SELECT DISTINCT ROLL_NO, STU_NAME
FROM STUDENT_LIST
ORDER BY ROLL_NO, STU_NAME;
DML Trigger - This trigger will get fired for DML events like INSERT,UPDATE and DELETE
DDL trigger - This trigger will get fired for DDL events like CREATE, ALTER, DROP, TRUNCATE etc.,
System trigger - This trigger will get fired for system events like LOGON, LOGOFF, STARTUP, SHUTDOWN
etc
Instead of trigger - This trigger is created on view to redirect the DML operations to undelying base
tables
compound trigger - This is a single trigger having blocks for all the DML timing events.
9. What is the difference between dml and ddl
Decode
* it is a function
Case
* case is a statement
TRANSLATE() function returns a string with all occurrences of each character in a string replaced by its
corresponding character in another string.
The TRANSLATE() function allows you to make several single-character, one-to-one translations or
substitutions in one operation.
In the Function test_fn1, we’re updating the emp table ; function test_fn1 is created
However, with the SELECT statement it throws an error ORA – 14551
This is because, a select statement is supposed to read the data and it is not supposed to change the
state of the database
As a workaround if we want to change the data, then we can use AUTONOMOUS TRANSACTION which
will be a child transaction rather than the main transaction
Now as we made the function as autonomous transaction function, we are able to successfully call the
function even though there’s a DML statement
Calling the function test_fn1 as a part of expression within a PL/sql block, no error
Calling the function test_fn1 as a part of select statement within a PL/sql block, this returns a error, to
overcome this we need to make the function test_fn1 as a autonomous transaction function.
SELECT statement takes precedence over any PL/sql block
14. Can we use OUT and INOUT parameter in function
25. Types of DML triggers | Order of trigger Execution if more than one
Row level
statement level
26. SQL loader
41. RANK and DENSE RANK as Aggregate Function and Analytical Function
aggregate function
Computing the rank & dense_rank value for a particular value
analytical function
Computes the rank & dense_rank for the given result set, the advantage is that we can use partition by
clause to do that sub partitioning within the result set & do the ranking & dense_ranking
42. Query was running fine yesterday but its very slow today | Tuning
Since oracle engine will generate the explain plan
based on the available statistics information,
chances are it may come up with wrong execution
plan because of the outdated statistics. This might
be the reason why the query is taking lot of time
43. What is cursor and what are the types of cursor in oracle
Implicit Cursor
47. Explain REF Cursor Strongly Typed Ref Cursor and Weakly typed Ref Cursor
Unique index or Unique Constraint both enforces column uniqueness but when to use what
63. How to Exclude duplicate records while insertion| Error Log Table
I want only the “non-duplicate” records to get inserted, duplicated records should get excluded during
insertion. How can I do
To avoid this,
We can use Oracle Error Log Table we can achieve this
64. ORACLE FORCE VIEW | VIEW WITH CHECK OPTION | VIEW WITH READ ONLY
Write a procedure to create a view for all the tables in the given schema. The view name should be
same as table name suffixed with “_V”