SQL PLUS STATEMENTS:
1. What are the types of SQL Statement ?
Data Definition Language : CREATE,ALTER,DROP,TRUNCATE,REVOKE,NO AUDIT & COMMIT.
Data Manipulation Language : INSERT,UPDATE,DELETE,LOCK TABLE,EXPLAIN PLAN & SELECT.
Transactional Control : COMMIT & ROLLBACK. Session Control : ALTERSESSION & SET ROLE. System Control : ALTER SYSTEM.
Data Manipulation Language : INSERT,UPDATE,DELETE,LOCK TABLE,EXPLAIN PLAN & SELECT.
Transactional Control : COMMIT & ROLLBACK. Session Control : ALTERSESSION & SET ROLE. System Control : ALTER SYSTEM.
2. What is a transaction ?
Transaction is logical unit between two commits and commit and rollback.
3. What is difference between TRUNCATE & DELETE ?
TRUNCATE commits after deleting entire table i.e., can not be rolled back. Database triggers do not fire on TRUNCATE, DELETE allows the filtered deletion. Deleted records can be rolled back or committed. Database triggers fire on DELETE.
4. What is a join ? Explain the different types of joins ?
Join is a query which retrieves related columns or rows from multiple tables. Self Join - Joining the table with itself. Equi Join - Joining two tables by equating two common columns. Non-Equi Join - Joining two tables by equating two common columns. Outer Join - Joining two tables in such a way that query can also retrive rows that do not have corresponding join value in the other table.
5. What is the Subquery ?
Subquery is a query whose return values are used in filtering conditions of the main query.
6. What is correlated sub-query ?
Correlated sub_query is a sub_query which has reference to the main query.
7. Explain Connect by Prior ?
Retrives rows in hierarchical order.
e.g. select empno, ename from emp where.
e.g. select empno, ename from emp where.
8. Difference between SUBSTR and INSTR ?
INSTR (String1,String2(n,(m)), INSTR returns the position of the mth occurrence of the string 2 in
string1. The search begins from nth position of string1. SUBSTR (String1 n,m) SUBSTR returns a character string of size m in string1, starting from nth postion of string1.
string1. The search begins from nth position of string1. SUBSTR (String1 n,m) SUBSTR returns a character string of size m in string1, starting from nth postion of string1.
9. Explain UNION,MINUS,UNION ALL, INTERSECT ?
INTERSECT returns all distinct rows selected by both queries. MINUS - returns all distinct rows selected by the first query but not by the second. UNION - returns all distinct rows selected by either query. UNION ALL - returns all rows selected by either query,including all duplicates.
10. What is ROWID ?
ROWID is a pseudo column attached to each row of a table. It is 18 character long, blockno, rownumber are the components of ROWID.
11. What is the fastest way of accessing a row in a table ?
Using ROWID.
CONSTRAINTS
What is an Integrity Constraint ?
Integrity constraint is a rule that restricts values to a column in a table.
What is Referential Integrity ?
Maintaining data integrity through a set of rules that restrict the values of one or more columns of the tables based on the values of primary key or unique key of the referenced table.
What are the usage of SAVEPOINTS ?
SAVEPOINTS are used to subdivide a transaction into smaller parts. It enables rolling back part of a transaction. Maximum of five save points are allowed.
What is ON DELETE CASCADE ?
When ON DELETE CASCADE is specified ORACLE maintains referential integrity by automatically removing dependent foreign key values if a referenced primary or unique key value is removed.
What are the data types allowed in a table ?
CHAR,VARCHAR2,NUMBER,DATE,RAW,LONG and LONG RAW.
What is difference between CHAR and VARCHAR2 ? What is the maximum SIZE allowed for each type ?
CHAR pads blank spaces to the maximum length. VARCHAR2 does not pad blank spaces. For CHAR it is 255 and 2000 for VARCHAR2.
How many LONG columns are allowed in a table ? Is it possible to use LONG columns in WHERE clause or ORDER BY ?
Only one LONG columns is allowed. It is not possible to use LONG column in WHERE or ORDER BY clause.
What are the pre requisites ?To modify datatype of a column. To add a column with NOT NULL constraint. To Modify the datatype of a column the column must be empty. to add a column with NOT NULL constrain, the table must be empty.
Where the integrity constrints are stored in Data Dictionary ?
The integrity constraints are stored in USER_CONSTRAINTS.
How will you a activate/deactivate integrity constraints ?
The integrity constraints can be enabled or disabled by ALTER TABLE ENABLE constraint/DISABLE constraint.
If an unique key constraint on DATE column is created, will it validate the rows that are inserted with SYSDATE ?
It won't, Because SYSDATE format contains time attached with it.
What is a database link ?
Database Link is a named path through which a remote database can be accessed.
How to access the current value and next value from a sequence ? Is it possible to access the current value in a session before accessing next value ?
Sequence name CURRVAL, Sequence name NEXTVAL. It is not possible. Only if you access next value in the session, current value can be accessed.
What is CYCLE/NO CYCLE in a Sequence ?
CYCLE specifies that the sequence continues to generate values after reaching either maximum or minimum value. After pan ascending sequence reaches its maximum value, it generates its minimum value. After a descending sequence reaches its minimum, it generates its maximum. NO CYCLE specifies that the sequence cannot generate more values after reaching its maximum or minimum value.
What are the advantages of VIEW ?
To protect some of the columns of a table from other users. To hide complexity of a query. To hide complexity of calculations.
Can a view be updated/inserted/deleted? If Yes under what conditions ?
A View can be updated/deleted/inserted if it has only one base table if the view is based on columns from one or more tables then insert, update and delete is not possible.
If a View on a single base table is manipulated will the changes be reflected on the base table ?
If changes are made to the tables which are base tables of a view will the changes be reference on the view.
DATA TYPES IN PL/SQL
What are the datatypes a available in PL/SQL ?
Some scalar data types such as NUMBER, VARCHAR2, DATE, CHAR, LONG, BOOLEAN. Some composite data types such as RECORD & TABLE.
What are % TYPE and % ROWTYPE ? What are the advantages of using these over datatypes?
% TYPE provides the data type of a variable or a database column to that variable. % ROWTYPE provides the record type that represents a entire row of a table or view or columns selected in the cursor.
The advantages are : I. Need not know about variable's data type ii. If the database definition of a column in a table changes, the data type of a variable changes accordingly.
What is difference between % ROWTYPE and TYPE RECORD ?
% ROWTYPE is to be used whenever query returns a entire row of a table or view. TYPE rec RECORD is to be used whenever query returns columns of different table or views and variables. E.g. TYPE r_emp is RECORD (eno emp.empno% type,ename emp ename %type); e_rec emp% ROWTYPE;
cursor c1 is select empno,deptno from emp; e_rec c1 %ROWTYPE.
cursor c1 is select empno,deptno from emp; e_rec c1 %ROWTYPE.
What is PL/SQL table ?
Objects of type TABLE are called "PL/SQL tables", which are modelled as (but not the same as) database tables, PL/SQL tables use a primary PL/SQL tables can have one column and a primary key.
CURSORS:
What is a cursor ? Why Cursor is required ?
Cursor is a named private SQL area from where information can be accessed. Cursors are required to process rows individually for queries returning multiple rows.
Explain the two type of Cursors ?
There are two types of cursors, Implict Cursor and Explicit Cursor. PL/SQL uses Implict Cursors for queries.
User defined cursors are called Explicit Cursors. They can be declared and used.
User defined cursors are called Explicit Cursors. They can be declared and used.
What are the PL/SQL Statements used in cursor processing ?
DECLARE CURSOR cursor name, OPEN cursor name, FETCH cursor name INTO or Record types, CLOSE cursor name.
What are the cursor attributes used in PL/SQL ?
%ISOPEN - to check whether cursor is open or not. % ROWCOUNT - number of rows featched/updated/deleted. % FOUND - to check whether cursor has fetched any row. True if rows are featched. % NOT FOUND - to check whether cursor has featched any row. True if no rows are featched. These attributes are proceded with SQL for Implict Cursors and with Cursor name for Explict Cursors.
What is a cursor for loop ?
Cursor for loop implicitly declares %ROWTYPE as loop index,opens a cursor, fetches rows of values from active set into fields in the record and closes when all the records have been processed. eg. FOR emp_rec IN C1 LOOP salary_total := salary_total +emp_rec sal; END LOOP;
What will happen after commit statement ?Cursor C1 is Select empno, ename from emp; Begin open C1; loop Fetch C1 into eno.ename; Exit When C1 %notfound;-----commit; end loop; end;
The cursor having query as SELECT .... FOR UPDATE gets closed after COMMIT/ROLLBACK.
The cursor having query as SELECT.... does not get closed even after COMMIT/ROLLBACK.
Explain the usage of WHERE CURRENT OF clause in cursors ?
WHERE CURRENT OF clause in an UPDATE,DELETE statement refers to the latest row fetched from a cursor. Database Triggers
What is a database trigger ? Name some usages of database trigger ?
Database trigger is stored PL/SQL program unit associated with a specific database table. Usages are Audit data modificateions, Log events transparently, Enforce complex business rules Derive column values automatically, Implement complex security authorizations. Maintain replicate tables.
How many types of database triggers can be specified on a table ? What are they ?
Insert Update Delete. Before Row o.k. o.k. o.k. After Row o.k. o.k. o.k. Before Statement o.k. o.k. o.k. After Statement o.k. o.k. o.k. If FOR EACH ROW clause is specified, then the trigger for each Row affected by the statement. If WHEN clause is specified, the trigger fires according to the retruned boolean value.
Is it possible to use Transaction control Statements such a ROLLBACK or COMMIT in Database Trigger ? Why ?
It is not possible. As triggers are defined for each table, if you use COMMIT of ROLLBACK in a trigger, it affects logical transaction processing.
What are two virtual tables available during database trigger execution ?
The table columns are referred as OLD.column_name and NEW.column_name. For triggers related to INSERT only NEW.column_name values only available. For triggers related to UPDATE only OLD.column_name NEW.column_name values only available. For triggers related to DELETE only OLD.column_name values only available.
What happens if a procedure that updates a column of table X is called in a database trigger of the same table ?
Mutation of table occurs.
Write the order of precedence for validation of a column in a table ?
I. done using Database triggers. ii. done using Integarity Constraints. I & ii.
EXCEPTION:
What is an Exception ? What are types of Exception ?
Exception is the error handling part of PL/SQL block. The types are Predefined and user_defined. Some of Predefined execptions are. CURSOR_ALREADY_OPEN. DUP_VAL_ON_INDEX. NO_DATA_FOUND. TOO_MANY_ROWS. INVALID_CURSOR. INVALID_NUMBER. LOGON_DENIED. NOT_LOGGED_ON. PROGRAM-ERROR. STORAGE_ERROR. TIMEOUT_ON_RESOURCE. VALUE_ERROR. ZERO_DIVIDE. OTHERS.
What is Pragma EXECPTION_INIT ? Explain the usage ?
The PRAGMA EXECPTION_INIT tells the complier to associate an exception with an oracle error. To get an error message of a specific oracle error. e.g. PRAGMA EXCEPTION_INIT (exception name, oracle error number)
What is Raise_application_error ?
Raise_application_error is a procedure of package DBMS_STANDARD which allows to issue an user_defined error messages from stored sub-program or database trigger.
What are the return values of functions SQLCODE and SQLERRM ?
SQLCODE returns the latest code of the error that has occured. SQLERRM returns the relevant error message of the SQLCODE.
Where the Pre_defined_exceptions are stored ?
In the standard package.
PROCEDURES, FUNCTION & PACKAGES:
What is a stored procedure ?
A stored procedure is a sequence of statements that perform specific function.
What is difference between a PROCEDURE & FUNCTION ?
A FUNCTION is alway returns a value using the return statement. A PROCEDURE may return one or more values through parameters or may not return at all.
What are advantages for Stored Procedures ?
Extensiblility,Modularity, Reusability, Maintainability and one time compilation.
What are the modes of parameters that can be passed to a procedure ?
IN,OUT,IN-OUT parameters.
What are the two parts of a procedure ?
Procedure Specification and Procedure Body.
Give the structure of the procedure ?
PROCEDURE name (parameter list.....) is local variable declarations BEGIN Executable statements.
Exception. exception handlers end;
Exception. exception handlers end;
Give the structure of the function ?
FUNCTION name (argument list .....) Return datatype is local variable declarations Begin executable statements Exception execution handlers End;
Explain how procedures and functions are called in a PL/SQL block ?
Function is called as part of an expression. sal := calculate_sal ('a822'); procedure is called as a PL/SQL statement calculate_bonus ('A822');
What is Overloading of procedures ?
The Same procedure name is repeated with parameters of different datatypes and parameters in different positions, varying number of parameters is called overloading of procedures.
e.g. DBMS_OUTPUT put_line
What is a package ? What are the advantages of packages ?
Package is a database object that groups logically related procedures. The advantages of packages are Modularity, Easier Applicaton Design, Information. Hiding,. reusability and Better Performance.
What are two parts of package ?
The two parts of package are PACKAGE SPECIFICATION & PACKAGE BODY.
Package Specification contains declarations that are global to the packages and local to the schema. Package Body contains actual procedures and local declaration of the procedures and cursor declarations.
What is difference between a Cursor declared in a procedure and Cursor declared in a package specification ?
A cursor declared in a package specification is global and can be accessed by other procedures or procedures in a package. A cursor declared in a procedure is local to the procedure that can not be accessed by other procedures.
How packaged procedures and functions are called from the following?
a. Stored procedure or anonymous block
a. Stored procedure or anonymous block
b. an application program such a PRC *C, PRO* COBOL, c. SQL *PLUS
a. PACKAGE NAME.PROCEDURE NAME (parameters);
variable := PACKAGE NAME.FUNCTION NAME (arguments);
EXEC SQL EXECUTE
b. BEGIN
PACKAGE NAME.PROCEDURE NAME (parameters)
variable := PACKAGE NAME.FUNCTION NAME (arguments);
END;
END EXEC;
c. EXECUTE PACKAGE NAME.PROCEDURE if the procedures does not have any
out/in-out parameters. A function can not be called.
variable := PACKAGE NAME.FUNCTION NAME (arguments);
EXEC SQL EXECUTE
b. BEGIN
PACKAGE NAME.PROCEDURE NAME (parameters)
variable := PACKAGE NAME.FUNCTION NAME (arguments);
END;
END EXEC;
c. EXECUTE PACKAGE NAME.PROCEDURE if the procedures does not have any
out/in-out parameters. A function can not be called.
Name the tables where characteristics of Package, procedure and functions are stored ?
User_objects, User_Source and User_error.
No comments:
Post a Comment
If you Like my blog Spread it and help friends for whom this blog is useful for their career.