PLSQL Basic Interview Questions
Q1) What is the difference between PL SQL and SQL?
Ans.
PL SQL vs SQL | ||
---|---|---|
Comparison | SQL | PL/SQL |
Execution | Single command at a time | Block of code |
Application | Source of data to be displayed | Application created by data acquired by SQL |
Structures include | DDL and DML based queries and commands | Includes procedures, functions, etc |
Recommended while | Performing CRUD operations on data | Creating applications to display data obtained using SQL |
Compatibility with each other | SQL can be embedded into PL/SQL | PL/SQL cant be embedded in SQL |
Q2) What is SQL and also describe types of SQL statements?
Ans: SQL stands for Structured Query Language. SQL is a language used to communicate with the server to access, manipulate, and control data.
There are 5 different types of SQL statements.
- Data Retrieval: SELECT
- Data Manipulation Language (DML): INSERT, UPDATE, DELETE, MERGE
- Data Definition Language (DDL): CREATE, ALTER, DROP, RENAME, TRUNCATE.
- Transaction Control Statements: COMMIT, ROLLBACK, SAVEPOINT
- Data Manipulation Language (DCL): GRANT, REVOKE
Q3) What is an alias in SQL statements?
Ans: Alias is a user-defined alternative name given to the column or table. By default column, alias headings appear in upper case. Enclose the alias in double quotation marks (“ “) to make it case sensitive. “AS” Keyword before the alias name makes the SELECT clause easier to read.
For example Select emp_name AS name from employee; (Here AS is a keyword and “name” is an alias).
Q4) What is a Literal? Give an example of where it can be used?
Ans: A Literal is a string that can contain a character, a number, or a date that is included in the SELECT list and that is not a column name or a column alias. Date and character literals must be enclosed within single quotation marks (‘ ‘), number literals need not.
For exp: Select last_name||’is a’||job_id As “emp details” from the employee; (Here “is a” is a literal).
Q5) What is the difference between SQL and iSQL*Plus?
SQL Vs iSQL*Plus | |
---|---|
SQL | iSQL*Plus |
Is a Language | Is an Environment |
Character and date column headings are left-justified and number column headings are right-justified. | Default heading justification is in the Centre. |
Cannot be Abbreviated (short forms) | Can be Abbreviated |
Does not have a continuation character | Has a dash (-) as a continuation character if the command is longer than one line |
Use Functions to perform some formatting | Use commands to format data |
Q6) Define the order of Precedence used in executing SQL statements.
Order of Precedence used in executing SQL statements | |||
---|---|---|---|
Order Evaluated | Operator | ||
1 | Arithmetic operators (*, /, +, -) | ||
2 | Concatenation operators (||) | ||
3 | Comparison conditions | ||
4 | Is[NOT] NULL, LIKE, [NOT] IN | ||
5 | [NOT] BETWEEN | ||
6 | NOT Logical condition | ||
7 | AND logical condition | ||
8 | OR logical condition |
Q7) What are SQL functions? Describe in brief different types of SQL functions?
Ans: SQL Functions are a very powerful feature of SQL. SQL functions can take arguments but always return some value.
There are two distinct types of SQL functions:
1) Single-Row functions: These functions operate on a single row to give one result per row.
Types of Single-Row functions:
- Character
- Number
- Date
- Conversion
- General
2) Multiple-Row functions: These functions operate on groups of rows to give one result per group of rows.
Types of Multiple-Row functions:
- AVG
- COUNT
- MAX
- MIN
- SUM
- STDDEV
- VARIANCE
Q8) Explain the character, number, and date function in detail?
Ans: Character functions: accept character input and return both character and number values. Types of character function are:
a) Case-Manipulation Functions: LOWER, UPPER, INITCAP
b) Character-Manipulation Functions: CONCAT, SUBSTR, LENGTH, INSTR, LPAD/RPAD, TRIM, REPLACE
Number Functions: accept Numeric input and return numeric values. Number Functions are: ROUND, TRUNC, and MOD
Date Functions: operates on values of the Date data type. (All date functions return a value of DATE data type except the MONTHS_BETWEEN Function, which returns a number. Date Functions are MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY, ROUND, TRUNC.
Q9) What is a Dual Table?
Ans: Dual table is owned by the user SYS and can be accessed by all users. It contains one columnDummy and one row with the value X. The Dual Table is useful when you want to return a value only once. The value can be a constant, pseudocolumn, or expression that is not derived from a table with user data.
Q10) Explain the Conversion function in detail?
Ans: Conversion Functions converts a value from one data type to another. Conversion functions are of two types:
Implicit Data type conversion:
- VARCHAR2 or CHAR To NUMBER, DATE
- NUMBER To VARCHAR2
- DATE To VARCHAR2
Explicit data type conversion:
- TO_NUMBER
- TO_CHAR
- TO_DATE
TO_NUMBER function is used to convert a Character string to Number format. TO_NUMBER function use fx modifier. Format: TO_NUMBER ( char[, ‘ format_model’] ). fx modifier specifies the exact matching for the character argument and number format model of TO_NUMBER function.
TO_CHAR function is used to convert NUMBER or DATE data type to CHARACTER format. TO_CHAR Function uses fm element to remove padded blanks or suppress leading zeros. TO_CHAR Function formats:TO_CHAR (date, ‘format_model’).The format model must be enclosed in single quotation marks and is case sensitive.
For exp: Select TO_CHAR (hire date, ‘MM/YY’) from the employee.
TO_DATE function is used to convert a Character string to date format. TO_DATE function use fx modifier which specifies the exact matching for the character argument and date format model of TO_DATE function. TO_DATE function format: TO_DATE ( char[, ‘ format_model’] ).
For exp: Select TO_DATE (‘May 24, 2007’,’ mon dd RR’) from dual;
Q11) Describe different types of General Function used in SQL?
Ans: General functions are of the following types:
- NVL: Converts a null value to an actual value. NVL (exp1, exp2) .If exp1 is null then the NVL function returns the value of exp2.
- NVL2: If exp1 is not null, nvl2 returns exp2, if exp1 is null, nvl2 returns exp3. The argument exp1 can have any data type. NVL2 (exp1, exp2, exp3)
- NULLIF: Compares two expressions and returns null if they are equal or the first expression if they are not equal. NULLIF (exp1, exp2)
- COALESCE: Returns the first non-null expression in the expression list. COALESCE (exp1, exp2… expn). The advantage of the COALESCE function over the NVL function is that the COALESCE function can take multiple alternative values.
- Conditional Expressions: Provide the use of IF-THEN-ELSE logic within a SQL statement. Example: CASE Expression and DECODE Function.
Q12) What is the difference between COUNT (*), COUNT (expression), COUNT (distinct expression)? (Where expression is any column name of Table)
Ans: COUNT (*): Returns a number of rows in a table including duplicates rows and rows containing null values in any of the columns.
COUNT (EXP): Returns the number of non-null values in the column identified by expression.
COUNT (DISTINCT EXP): Returns the number of unique, non-null values in the column identified by expression.
Q13) What is a Sub Query? Describe its Types?
Ans: A subquery is a SELECT statement that is embedded in a clause of another SELECT statement. A subquery can be placed in WHERE HAVING and FROM clause.
Guidelines for using subqueries:
- Enclose sub queries within parenthesis
- Place subqueries on the right side of the comparison condition.
- Use Single-row operators with single-row subqueries and Multiple-row operators with multiple-row subqueries.
Types of subqueries:
- Single-Row Subquery: Queries that return only one row from the inner select statement. Single-row comparison operators are: =, >, >=, <, <=, <>
- Multiple-Row Subquery: Queries that return more than one row from the inner Select statement. There are also multiple-column subqueries that return more than one column from the inner select statement. Operators include: IN, ANY, ALL.
Q14) What is the difference between ANY and ALL operators?
Ans: ANY Operator compares value to each value returned by the subquery. ANY operator has a synonym SOME operator.
> ANY means more than the minimum.
< ANY means less than the maximum
= ANY is equivalent to IN operator.
ALL Operator compares value to every value returned by the subquery.
> ALL means more than the maximum
< ALL means less than the minimum
Q15) What is a MERGE statement?
Ans: The MERGE statement inserts or updates rows in one table, using data from another table. It is useful in data warehousing applications.
Below Oracle PL/SQL Interview Questions for Experienced Professionals
Q16) What is the difference between the “VERIFY” and the “FEEDBACK” command?
Ans: VERIFY Command: Use VERIFY Command to confirm the changes in the SQL statement (Old and New values). Defined with SET VERIFY ON/OFF.
Feedback Command: Displays the number of records returned by a query.
Q17) What is the use of Double Ampersand (&&) in SQL Queries? Give an example?
Ans: Use “&&” if you want to reuse the variable value without prompting the user each time.
For ex: Select empno, ename, &&column_name from employee order by &column_name;
Q18) What are Joins and how many types of Joins are there?
Ans: Joins are used to retrieve data from more than one table.
There are 5 different types of joins.
types of Joins | |
---|---|
Oracle 8i and Prior | SQL: 1999 (9i) |
Equi Join | Natural/Inner Join |
Outer Join | Left Outer/ Right Outer/ Full Outer Join |
Self Join | Join ON |
Non-Equi Join | Join USING |
Cartesian Product | Cross Join |
Q19) Explain all Joins used in Oracle 8i?
Ans: Cartesian Join: When a Join condition is invalid or omitted completely, the result is a Cartesian product, in which all combinations of rows are displayed. To avoid a Cartesian product, always include a valid join condition in a “where” clause. To Join ‘N’ tables together, you need a minimum of N-1 Join conditions.
For exp: to join four tables, a minimum of three joins is required. This rule may not apply if the table has a concatenated primary key, in which case more than one column is required to uniquely identify each row.
Equi Join: This type of Join involves primary and foreign key relations. Equi Join is also called Simple or Inner Joins.
Non-Equi Joins A Non-Equi Join condition containing something other than an equality operator. The relationship is obtained using an operator other than an equal operator (=). The conditions such as <= and >= can be used, but BETWEEN is the simplest to represent Non-Equi Joins.
Outer Joins: Outer Join is used to fetch rows that do not meet the join condition. The outer join operator is the plus sign (+), and it is placed on the side of the join that is deficient in information. The Outer Join operator can appear on only one side of the expression, the side that has information missing. It returns those rows from one table that has no direct match in the other table. A condition involving an Outer Join cannot use IN and OR operator.
Self Join: Joining a table to itself.
Q20) Explain all Joins used in Oracle 9i and later release?
Ans: Cross Join:
Cross Join clause produces the cross-product of two tables. This is the same as a Cartesian product between the two tables.
Natural Joins:
This is used to join two tables automatically based on the columns which have matching data types and names, using the keyword NATURAL JOIN. It is equal to the Equi-Join. If the columns have the same names but different data types, then the Natural Join syntax causes an error.
Join with the USING clause:
If several columns have the same names but the data types do not match, then the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an equi Join. Use the USING clause to match only one column when more than one column matches. Do not use a table name or alias in the referenced columns. The NATURAL JOIN clause and USING clause are mutually exclusive.
For ex: Select a.city, b.dept_name from loc a Join dept b USING (loc_id) where loc_id=10;
Joins with the ON clause:
Use the ON clause to specify a join condition. The ON clause makes code easy to understand. ON clause is equals to Self Joins. The ON clause can also be used to join columns that have different names.
Left/ Right/ Full Outer Joins:
Left Outer Join displays all rows from the table that is Left to the LEFT OUTER JOIN clause, right outer join displays all rows from the table that is right to the RIGHT OUTER JOIN clause, and full outer join displays all rows from both the tables either left or right to the FULL OUTER JOIN clause.
Q21) What is the difference between Entity, Attribute, and Tuple?
Ans: Entity: A significant thing about which some information is required. For exp: EMPLOYEE (table). Attribute: Something that describes the entity. For exp: empno, empname, emp address (columns). Tuple: A row in a relation is called Tuple.
Q22) What is a Transaction? Describe common errors that can occur while executing any Transaction?
Ans: Transaction consists of a collection of DML statements that forms a logical unit of work.
The common errors that can occur while executing any transaction are:
The violation of constraints.
- Data type mismatch.
- Value too wide to fit in the column.
- The system crashes or the Server gets down.
- The session Killed.
- Locking takes place. Etc.
Q23) What is locking in SQL? Describe its types?
Ans: Locking prevents destructive interaction between concurrent transactions. Locks held until Commit or Rollback. Types of locking are:
Implicit Locking: This occurs for all SQL statements except SELECT.
Explicit Locking: This can be done by the user manually.
Further, there are two locking methods:
- Exclusive: Locks out other users
- Share: Allows other users to access
Q24) What is the difference between Commit, Rollback, and Savepoint?
- COMMIT: Ends the current transaction by making all pending data changes permanent.
- ROLLBACK: Ends the current transaction by discarding all pending data changes.
- SAVEPOINT: Divides a transaction into smaller parts. You can rollback the transaction to a particular named savepoint.
Q25) What are the advantages of COMMIT and ROLLBACK statements?
Ans: Advantages of COMMIT and ROLLBACK statements are:
- Ensure data consistency
- Can preview data changes before making changes permanent.
- Group logically related operations.
Q26) Describe naming rules for creating a Table?
Ans: Naming rules to be considered for creating a table is:
- The table name must begin with a letter,
- The table name can be 1-30 characters long,
- Table name can contain only A-Z, a-z, 0-9,_, $, #.
- The table name cannot duplicate the name of another object owned by the same user.
- The table name cannot be an oracle server reserved word.
Q27) What is a DEFAULT option in a table?
Ans: A column can be given a default value by using the DEFAULT option. This option prevents null values from entering the column if a row is inserted without a value for that column. The DEFAULT value can be a literal, an expression, or a SQL function such as SYSDATE and USER but the value cannot be the name of another column or a pseudo column such as NEXTVAL or CURRVAL.
Q28) What is the difference between USER TABLES and DATA DICTIONARY?
Ans: USER TABLES: This is a collection of tables created and maintained by the user. Contain USER information. DATA DICTIONARY: This is a collection of tables created and maintained by the Oracle Server. It contains database information. All data dictionary tables are owned by the SYS user.
Q29) Describe a few Data Types used in SQL?
Ans: Data Types is a specific storage format used to store column values. Few data types used in SQL are:
- VARCHAR2(size): Minimum size is ‘1’ and Maximum size is ‘4000’
- CHAR(size): Minimum size is ‘1’and Maximum size is ‘2000’
- NUMBER(P,S): ” Precision” can range from 1 to 38 and the “Scale” can range from -84 to 127.
- DATE
- LONG: 2GB
- CLOB: 4GB
- RAW (size): Maximum size is 2000
- LONG RAW: 2GB
- BLOB: 4GB
- BFILE: 4GB
- ROWID: A 64 base number system representing the unique address of a row in the table.
Q30) In what scenario you can modify a column in a table?
Ans: During modifying a column:
- You can increase the width or precision of a numeric column.
- You can increase the width of numeric or character columns.
- You can decrease the width of a column only if the column contains null values or if the table has no rows.
- You can change the data type only if the column contains null values.
- You can convert a CHAR column to the VARCHAR2 data type or convert a VARCHAR2 column to the CHAR data type only if the column contains null values or if you do not change the size.
Q31) Describe a few restrictions on using the “LONG” data type?
Ans: A LONG column is not copied when a table is created using a subquery. A LONG column cannot be included in a GROUP BY or an ORDER BY clause. Only one LONG column can be used per table. No constraint can be defined on a LONG column.
Q32) What is a SET UNUSED option?
Q33) What is the difference between Truncate and Delete?
Ans: The main difference between Truncate and Delete is as below:
SQL Truncate Vs SQL Delete | |||
---|---|---|---|
TRUNCATE | DELETE | ||
Removes all rows from a table and releases storage space used by that table. | Removes all rows from a table but does not release storage space used by that table. | ||
TRUNCATE Command is faster. | DELETE command is slower. | ||
Is a DDL statement and cannot be Rollback. | Is a DDL statement and can be Rollback. | ||
Database Triggers do not fire on TRUNCATE. | Database Triggers fire on DELETE.
|
Q34) What is the main difference between CHAR and VARCHAR2?
Ans: CHAR pads blank spaces to a maximum length, whereas VARCHAR2 does not pad blank spaces.
Q35) What are Constraints? How many types of constraints are there?
Ans: Constraints are used to prevent invalid data entry or deletion if there are dependencies. Constraints enforce rules at the table level. Constraints can be created either at the same time as the table is created or after the table has been created. Constraints can be defined at the column or table level. Constraint defined for a specific table can be viewed by looking at the USER-CONSTRAINTS data dictionary table. You can define any constraint at the table level except NOT NULL which is defined only at the column level. There are 5 types of constraints:
- Not Null Constraint
- Unique Key Constraint
- Primary Key Constraint
- Foreign Key Constraint
- Check Key Constraint.
Q36) Describe types of Constraints in brief?
Ans: NOT NULL: NOT NULL Constraint ensures that the column contains no null values.
UNIQUE KEY: UNIQUE Key Constraint ensures that every value in a column or set of columns must be unique, that is, no two rows of a table can have duplicate values in a specified column or set of columns. If the UNIQUE constraint comprises more than one column, that group of columns is called a Composite Unique Key. There can be more than one Unique key on a table. Unique Key Constraint allows the input of Null values. Unique Key automatically creates an index on the column it is created.
PRIMARY KEY: Uniquely identifies each row in the Table. Only one PRIMARY KEY can be created for each table but can have several UNIQUE constraints. PRIMARY KEY ensures that no column can contain a NULL value. A Unique Index is automatically created for a PRIMARY KEY column. PRIMARY KEY is called a Parent key.
FOREIGN KEY: This is also called Referential Integrity Constraint. FOREIGN KEY is one in which a column or set of columns take references of the Primary/Unique key of the same or another table. FOREIGN KEY is called a child key. A FOREIGN KEY value must match an existing value in the parent table or be null.
CHECK KEY: Defines a condition that each row must satisfy. A single column can have multiple CHECK Constraints. During CHECK constraint following expressions is not allowed:
1) References to CURRVAL, NEXTVAL, LEVEL, and ROWNUM Pseudo columns.
2) Calls to SYSDATE, UID, USER, and USERENV Functions
Q37) What is the main difference between Unique Key and Primary Key?
Ans: The main difference between Unique Key and Primary Key is:
Unique Vs Primary Key | |||
---|---|---|---|
Unique Key | Primary Key | ||
A table can have more than one Unique Key. | A table can have only one Primary Key. | ||
The unique key column can store NULL values. | The primary key column cannot store NULL values. | ||
Uniquely identify each value in a column. | Uniquely identify each row in a table. |
Q38) What is the difference between ON DELETE CASCADE and ON DELETE SET NULL?
Ans: ON DELETE CASCADE Indicates that when the row in the parent table is deleted, the dependent rows in the child table will also be deleted. ON DELETE SET NULL Coverts foreign key values to null when the parent value is removed. Without the ON DELETE CASCADE or the ON DELETE SET NULL options, the row in the parent table cannot be deleted if it is referenced in the child table.
Q39) What is a Candidate Key?
Ans: The columns in a table that can act as a Primary Key are called Candidate Key.
Q40) What are Views and why they are used?
Ans: A View logically represents subsets of data from one or more tables. A View is a logical table based on a table or another view. A View contains no data of its own but is like a window through which data from tables can be viewed or changed. The tables on which a view is based are called Base Tables. The View is stored as a SELECT statement in the data dictionary. View definitions can be retrieved from the data dictionary table: USER_VIEWS.
Views are used:
- To restrict data access
- To make complex queries easy
- To provide data Independence
- Views provide groups of users to access data according to their requirements.
BELOW ARE COMPLEX AND ADVANCED INTERVIEW QUESTIONS
Q41) What is the difference between Simple and Complex Views?
Ans: The main differences between the two views are:
Simple Views Vs Complex Views | |||
---|---|---|---|
Simple View | Complex View | ||
Derives data from only one table. | Derives data from many tables. | ||
Contains no functions or group of data | Contain functions or groups of data. | ||
Can perform DML operations through the view. | Does not always allow DML operations through the view. |
Q42) What are the restrictions of DML operations on Views?
Ans: Few restrictions of DML operations on Views are:
You cannot DELETE a row if the View contains the following:
- Group Functions
- A Group By clause
- The Distinct Keyword
- The Pseudo column ROWNUM Keyword.
You cannot MODIFY data in a View if it contains the following:
- Group Functions
- A Group By clause
- The Distinct Keyword
- The Pseudo column ROWNUM Keyword.
- Columns defined by expressions (Ex; Salary * 12)
You cannot INSERT data through a view if it contains the following:
Q43) What is PL/SQL, Why do we need PL/SQL instead of SQL, Describe your experience working with PLSQL and What are the difficulties faced while working with PL SQL and How did you overcome them?
- PL/SQL is a procedural language extension with SQL Language.
- Oracle introduced PL/SQL
- It is a combination of SQL and Procedural Statements and used for creating applications.
- Basically, PL/SQL is a block-structured programming language whenever we are submitting PL/SQL
- Blocks then all SQL statements are executing separately by using SQL engine and also all procedure statements are executed separately.
- Explain your current and previous projects along with your roles and responsibilities, mention some of the challenging difficulties you’ve faced in your project while working with PL/SQL.
Q44) What are the different functionalities of a Trigger?
Ans: Trigger is also the same as stored procedure & also it will automatically be invoked whenever DML operation performed against table or view.
There are two types of triggers supported by PL/SQL
- Statement Level Trigger.
- Row Level Trigger
Statement Level Trigger: In statement-level trigger, the trigger body is executed only once for the DML statement.
Row Level Trigger: In row-level trigger, the trigger body is executed for each row DML statement. It is the reason, we are employing each row clause and internally stored DML transaction in trigger specification, these qualifiers: old, new, are also called records type variables.
These qualifiers are used in trigger specification & trigger body.
Syntax:
:old.column_name
Syntax:
:new column_name
When we use these qualifiers in trigger specification then we are not allowed to use “:” in form of the names of the qualifiers.
Q45) Write a PL/SQL Program that raises a user-defined exception on Thursday?
Ans:
1 2 3 4 5 6 7 8 9 10 11 12 | declare a exception begin If to_char(sysdate, ‘DY)=’THU’ then raise a; end if ; exception when a then dbms_output.put_line(‘my exception raised on thursday’); end ; |
Output: my exception raised on Thursday
Q46) Write a PL/SQL program to retrieve the emp table and then display the salary?
Ans:
1 2 3 4 5 6 7 | declare v_sal number(10); begin select max(sal)intr v_sal; from emp; dbms_output.put_line(v.sal); end; / |
(or)
1 2 3 4 5 6 7 8 9 10 11 | declare A number(10); B number(10); C number(10); begin a:=70; b:=30; c:=greatest+(a,b); dbms_output.put_line(c); end; / |
Output:70
Q47) Write a PL/SQL cursor program which is used to calculate total salary from emp table without using sum() function?
Ans:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Declare cursor c1 is select sal from emp; v_sal number(10); n.number(10):=0; begin open c1; loop fetch c1 into v_sal; exit when c1%not found; n:=n+v_sal; end loop; dbms_output.put_line(‘tool salary is’||’ ‘ ||n); close c1; end; / Output: total salary is: 36975 |
Q48) Write a PL/SQL cursor program to display all employee names and their salary from the emp table by using % not found attributes?
Ans:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Declare Cursor c1 is select ename, sal from emp; v_ename varchar2(10); v_sal number(10); begin open c1; loop fetch c1 into v_ename, v_sal; exist when c1 % notfound; dbms_output.put_line(v_name ||’ ‘||v_sal); end loop; close c1; end; / |
Q49) What is a Mutating Trigger?
Ans:
- Into a row-level trigger based on a table, the trigger body cannot read data from the same table and also we cannot perform DML operation on the same table.
- If we are trying this oracle server returns mutating error oracle-4091: table is mutating.
- This error is called a mutating error, and this trigger is called a mutating trigger, and the table is called a mutating table.
- Mutating errors are not occurred in statement-level trigger because through this statement-level trigger when we are performing DML operations automatically data committed into the database, whereas in the row-level trigger when we are performing transaction data is not committed and also again we are reading this data from the same table then only mutating errors is occurred.
Q50) What is Triggering Events (or) Trigger Predicate Clauses?
Ans: If we want to perform multiple operations in different tables then we must use triggering events within the trigger body. These are inserting, updating, deleting clauses. These clauses are used in the statement, row-level triggers. These triggers are also called as trigger predicate clauses.
Syntax:
1 2 3 4 | If inserting then stmts; else if updating then stmts; else if deleting then stmts; end if ; |
Q51) What is the Discard File?
Ans:
- This file extension is .dsc
- Discard file we must specify within the control file by using the discard file clause.
- The discard file also stores reflected records based on when clause condition within the control file. This condition must be satisfied in the table clause.
Q52) What is REF CURSOR (or) CURSOR VARIABLE (or) DYNAMIC CURSOR?
Ans:
Oracle 7.2 introduced ref cursor, This is a user-defined type that is used to process multiple records and also this is a record by record process.
In static cursor database servers execute only one select statement at a time for a single active set area wherein ref cursor database servers execute a number of select statements dynamically for a single active set area that’s why those cursors are also called a dynamical cursor.
Generally, we are not allowed to pass static cursor as parameters to use subprograms whereas we can also pass ref cursor as a parameter to the subprograms because basically precursor is a user-defined type in oracle we can also pass all user-defined type as a parameter to the subprograms.
Generally, the static cursor does not return multiple records into the client application whereas the ref cursor is allowed to return multiple records into the client application (Java, .Net, php, VB, C++).
This is a user-defined type so we are creating it in 2 steps process i.e first we are creating a type then only we are creating a variable from that type that’s why this is also called a cursor variable.
Q53) What are The Types of Ref Cursors?
Ans: In all databases having 2 ref cursors.
- Strong ref cursor
- Weak ref cursor
A strong ref cursor is a ref cursor that has a return type, whereas a weak ref cursor has no return type.
Syntax:
1 2 | Type typename is ref cursor return record type data type; Variable Name typename |
Syntax
1 2 | Type typename is ref cursor Variable Name typename; |
In the Weak ref cursor, we must specify a select statement by using open for clause this clause is used in the executable section of the PL/SQL block.
Syntax:
1 | Open ref cursor varname for SELECT * FROM table_name condition; |
Q54) What is the Difference Between the trim, delete collection methods?
Ans:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | SQL> declare type t1 is table of number(10); v_t t1;=t1(10,20,30,40,50,60); beign v_t.trim(2); dbms_output.put_line(‘after deleting last two elements’); for i in v_t.first.. V_t.last loop dbms_output.put_line(v_t(i)); End loop; vt. delete (2); dbms_output.put_line(‘after deleting second element;); for i in v_t.first..v_t.last loop If v_t.exists(i) then dbms_output.put_line(v_t(i)); end if ; end loop; end; / |
Q55) What is Overloading Procedures?
Ans: Overload refers to the same name that can be used for a different purpose, in oracle we can also implement an overloading procedure through the package. Overloading procedure having the same name with different types or different numbers of parameters.
Q56) What are the Global Variables?
Ans: In oracle, we are declaring global variables in Package Specification only.
Q57) What is Forward Declaration?
Ans: In oracle declaring procedures within the package body are called forward declaring generally before we are calling private procedures into public procedure first we must implement private procedure within body otherwise use a forward declaration within the package body.
Q58) What is Invalid_number, Value_Error?
Ans: In oracle when we try to convert “string type to number type” or” data string into data type” then the oracle server returns two types of errors.
1. Invalid. number
2. Value_error (or) numeric_error
a) Invalid_number:
When PL/SQL block has a SQL statement and also those SQL statements try to convert string type to number type or data string into data type then oracle server returns an error: ora-1722-Invalid Number
For handling this error oracle provides number exception Invalid_number exception name.
Example:
1 2 3 4 5 6 | begin Insert intoemp(empno, ename, sal) values(1,’gokul’, ‘abc’) exception when invalid_number then dbms_output.put_line(‘insert proper data only’); end;/ |
b)value_error
Whenever PL/SQL block having procedural statements and also those statements find to convert string type to number type then oracle servers return an error: ora-6502: numeric or value error: character to a number conversion error
For handling, this error oracle provided exception value_error exception name
Example:
1 2 3 4 5 6 7 | begin declare z number(10); begin z:= ‘&x’ + ‘&y’; dbms_output.put_line(z); exception when value_error then dbms_output.put_line(‘enter numeric data value for x & y only’); end;/ |
Output:
1 2 3 4 5 6 7 8 | Enter value for x:3 Enter value for y:2 z:=5 Enter value for x:a Enter value for y:b Error:enter numeric data value for x & y only. |
Q59) What is Flashback Query?
Ans:
- Flashback query is handle by the Database Administrator only flashback queries along allows the content of the table to be retrieved with reference to the specific point of time by using as of clause that is flashback queries retrieves accidental data after committing the transaction also.
- Flashback queries generally use undo file that is flashback queries retrieve old data before committing the transaction oracle to provide two methods for flashback queries
Method1: using the timestamp
Method2: using scn number
Q60) Explain what PL/SQL package consists of?
Ans: PL/SQL consists of two major parts, they are: package specification and package body.
Package specification: it acts as a public interface for your application which includes procedures, types, etc.
Package Body: It contains the code required to implement the Package Specification
Q61) Explain what the benefits of PL/SQL Packages are?
Ans: These are the benefits of PL/SQL Packages
- We can store functions and procedures in a single unit called a package.
- Packages provide security to grant privileges.
- Functions and procedures, within the package, shares a common variable among them.
- Packages support even if the functions are overloaded.
- Packages enhance the performance even when multiple objects loaded into memory.
Q62) explain different methods to trace the PL/SQL code?
Ans: Tracing code is a necessary technique to test the performance of the code during runtime. We have different methods in PL/SQL to trace the code, which are,
- DBMS_ TRACE
- DBMS_ APPLICATION_INFO
- Tkproof utilities and trcsess
- DBMS_SESSION and DBMS_MONITOR
Q63) What does it mean by PL/SQL Cursors?
Ans: In PL/SQL to retrieve and process more it requires a special resource, and that resource is known as Cursor. A cursor is defined as a pointer to the context area. The context area is an area of memory that contains information and SQL statements for processing the statements.
Q64) what is the difference between Implicit and Explicit Cursors?
Ans: Implicit cursor used in PL/SQL to declare, all SQL data manipulation statements. An implicit cursor is used to declare SQL statements such as open, close, fetch, etc.
An explicit cursor is a cursor and which is explicitly designed to select the statement with the help of a cursor. This explicit cursor is used to execute the multirow select function. An explicit function is used PL/SQL to execute tasks such as update, insert, delete, etc.
Q65) what is a trigger?
Ans: It is a program in PL/SQL, stored in the database, and executed instantly before or after the UPDATE, INSERT and DELETE commands.
Q66) what are the uses of database triggers?
Ans: Triggers are programs which are automatically fired or executed when some events happen and are used for:
- To implement complex security authorizations.
- To drive column values.
- To maintain duplicate tables.
- To implement complex business rules.
- To bring transparency in log events.
Q67) Name the two exceptions in PL/SQL?
Ans: Error handling part of PL/SQL is called an exception. We have two types of exceptions, and they are User-defined and predefined.
Q68) which command is used to delete the package?
Ans: To delete the ‘Package’ in PL/SQL we use the DROP PACKAGE command.
Q69) what is the process for PL/SQL compilation?
Ans: The compilation process consists of syntax check, bind, and p-code generation. It checks the errors in PL/SQL code while compiling. Once all errors are corrected, a storage address allocated to a variable that stores this data. This process is called binding. P-Code consists of a list of rules for the PL/SQL engine. It is stored in the database and triggered when the next time it is used.