Oracle SQL Joins
Oracle SQL Joins
A view is a logical table which makes a complex query easy.We can even create
a complex view by joining two tables.
(a) They restrict access to the whole data, because they display only selective columns.
(b) They can be used to make complex queries easy. A user can use a simple query on a view
to display data from multiple tables, without having the knowledge of how to join tables in
queries.
(c) Different views can be created from the same data as per the requirements of different
types of use groups.
A view does not contain any data of its own, but is like a window through which data from
other tables can be viewed and changed.
A view does not contain any data of its own, but is like a window through which data from
other tables can be viewed and changed.
An Index is a tree structure that allows direct access to a row in a table. Indexes can be
classified based on their logical design or on their physical implementation.
The Logical classification groups indexes from an application perspective, while the physical
classification is derived from the way the indexes are stored.
A database link is a pointer in the local database that allows you to access on a remote
database.
Oracle allows you to create private, public, and global database links.
Private Database Link: You can create a private database link in a specific schema of a
database. Only the owner of a private database link or PL/SQL subprograms in the schema can
2
use a private database link to access data and database objects in the corresponding remote
database.
Public Database Link : You can create a public database link for a database. All users and
PL/SQL subprograms in the database can use a public database link to access data and
database objects in the corresponding remote database.
Global Database Link - When an Oracle network uses Oracle Names, the names servers in the
system automatically create and manage global database links for every Oracle database in
the network. All users and PL/SQL subprograms in any database can use a global database
link to access data and database objects in the corresponding remote database.
A private database link is more secure than a public or global link, because only the owner of
the private link, or subprograms within the same schema, can use the private link to access
the specified remote database.
When many users require an access path to a remote Oracle database, an administrator can
create a single public database link for all users in a database.
When an Oracle network uses Oracle Names, an administrator can conveniently manage global
database links for all databases in the system. Database link management is centralized and
simple.
Block is the smallest unit of storage in the logical structure of the database where actual table
rows are stored.
An index is a database structure used by the server to have direct access of a row in a table.
An index is automatically created when a unique of primary key constraint clause is specified
in create table common (Ver 7.0)
6 rows selected.
4
12 rows selected.
By declaring this cursor we can update the table emp through z,means wo not need to write
table name for updation,it may be only by "z".
selecting in FOR UPDATE mode locks the result set of rows in update mode, which means that
row cannot be updated or deleted until a commit or rollback is issued which will release the
row(s).
5
10. The procedure executes successfully. COMMIT will not close the cursor.
declare
b boolean;
cursor c is select * from emp for update;
rec c%rowtype;
begin
open c;
fetch c into rec;
dbms_output.put_line('NAME is'||rec.ename);
commit;
if c%isopen then
dbms_output.put_line('Cursor is not closed '||c%rowcount);
end if;
fetch c into rec;
dbms_output.put_line('NAME is'||rec.ename);
end;
O/P
NAME isSMITH
Cursor is not closed 1
NAME isALLEN
Correlated sub query is a sub query which has reference to the main query.
n salaries
24. Display the records between two range I know the nvl function only allows the
same data type(ie. number or char or date Nvl(comm, 0)), if commission is null then
the text “Not Applicable” want to display, instead of blank space. How do I write the
query?
You can use the decode function for the above requirement. Please find the query as below:
select ename,decode(nvl(comm,0),0,'Not Applicable',comm) from scott.emp;
Ex:
8
EMP_NAME DECODE(NVL(COMMISSION,0),0,'NOTAPPLICABL
------------------------- ----------------------------------------
SELVA 5000
RAJ 5660
A S KALA 7000
JESLIN FANTA MALAR Not Applicable
ANITA 10500
MURUGU 1000
SRIVATSAN 1700
SARABOOT Not Applicable
KARTHI SIR Not Applicable
SUDHA Not Applicable
MERCHI 1750
SAVI Not Applicable
12 rows selected.
12 rows selected.
27. If a view on a single base table is manipulated will the changes be reflected on
the base table?
if view on based on a single table then u can execute any DML directly on it and can see the
changes in the base table and if view is based on join of 2 tables then only one base table can
be modified at one time so in order to make changes in both the tables use instead of triggers.
29. How to access the current value and next value from a sequence?
Would like to give you a small example to use the sequence.currval and sequence.nextval
create sequence seq_name.
start with 1
minvalue 1
10
maxvalue 999
increment by 1
nocycle
insert into table_name (sno,name) values (seqname.nextval,'abc');
select seqname.currval from dual;
CYCLE specifies that the sequence continue 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.
12 rows selected.
Functions are named PL/SQL blocks that return a value and can be called with arguments
procedure a named block that can be called with parameter. A procedure all is a PL/SQL
statement by itself, while a Function call is called as part of an expression.
What are different modes of parameters used in functions and procedures?
-IN
-OUT
-INOUT
PL / SQL
Cursor is a stored select statement for that current session. It will not be stored in the
database, it is a logical component.
Function is a set of PL/SQL statements or a PL/SQL block, which performs an operation and
must return a value.
Cursor
cursor is a private sql work area.
Every sql statement executed
by oracle server has an individual
cursor associated with it.
Cursor are two types
1.Implicit cursor
13
2.Explicit cursor
Implicit cursor: Implicit cursors
are declared by pl/sql implicitly
at the time of DML statement and select statement in pl/sql including queries that returns
single row.
cursor have four attributes
1. SQL%ROWCOUNT
2. SQL%ISOPEN
3. SQL%NOTFOUND
4. SQL%FOUND
SQL%ROWCOUNT-Basically it returns
number.
means number of rows
affected by present sql statement.
SQL%ISOPEN-Always evalutes false
because implicit cursor automatically closed after execution of sql statement.
SQL%FOUND-Always evalutes true
because one or more rows are affected by recent sql statement
SQL%NOTFOUND-Always evalutes true when no rows are affected by
present sql statement.
example of explicit cursor:
DECLARE
v_empno employees.employee_id%TYPE;
v_name employees.last_name%TYPE;
CURSOR emp_cur IS
SELECT employee_id,last_name
FROM employees
BEGIN
OPEN emp_cur;
LOOP
FETCH emp_cur INTO v_empno,v_name ;
EXIT WHEN emp_cur%ROWCOUNT>10 OR
emp_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('employee_id:'||TO_CHAR(v_empno) ||
'employee_name:'||'v_name');
END LOOP;
CLOSE emp_cur;
END;
14
END;
/
Implict cursor can be used to handle single record (i.e) the select query used should not yield
more than one row.
if u have handle more than one record then Explict cursor should be used.
A function can be called from sql statements and queries while procedure can be called in a
begin end block only.
In case of function,it must have
return type.
In case of procedure,it may or may n't have return type.
In case of function, only it takes IN parameters
IN case of procedure
only it take IN,OUT,INOUT parameters.
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.
Program 1
SET SERVEROUTPUT ON
BEGIN
FOR emp_record IN (SELECT last_name,department_id FROM employees) LOOP
IF emp_record.department_id=80 THEN
DBMS_OUTPUT.PUT_LINE('employees'||'emp_record.last_name'||'works for sales
department');
END IF;
17
SQL> /
employees SARABOOT works for sales department
PL/SQL procedure successfully completed.
Program 2
SQL> DECLARE
v_empno emp.EMP_NO%TYPE;
v_name emp.EMP_NAME%TYPE;
CURSOR emp_cur IS
SELECT EMP_NO,EMP_NAME FROM emp;
BEGIN
OPEN emp_cur;
DBMS_OUTPUT.PUT_LINE('EMPLOYEE ID'||' '||
'EMPLOYEE NAME');
DBMS_OUTPUT.PUT_LINE('--------------------------------
-------------');
LOOP
FETCH emp_cur INTO v_empno,v_name ;
EXIT WHEN emp_cur%ROWCOUNT>4 OR
emp_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_empno)||'
'|| v_name);
END LOOP;
END;
/