Tuesday 4 April 2017

System Privileges V/s Object Privileges

System Privileges:- system privileges as privileges that don't relate to a particular instance of an object. eg: create index ,create procedure etc Object Privileges:- For example granting SELECT on a specific table is an object privilege because it applies to a particular object based on the grant statement. However granting SELECT ANY TABLE is a system privilege because it is a privilege that allows you to issue selects against all tables. eg: select ,insert ,update etc Difference between With grant and With Admin option :- With grant :- a) can be used only for object privileges. b) User1 => User2 => User3 Here user1 granted access to user2 with "WITH GRANT " So it means User2 got authorization to grant same privilege to anybody.

Eg : User2 granted same privilege to User3 Important point here is ,whoever granted privilege to another user they can only revoke access from them.

It means user1 can revoke access only from User2 but not from User3. SO user3 access can be revoked only by User2 because he granted . Final point is here,When User1 revokes permission from User2 ,it will automatically revokes access from User3 also only thing is direct revoke access is not possible. eg:-grant select on Emp to Venky with grant option; With Admin :- a) can be used only for System privilege. b) User1 => User2 => User3 Its opposite to "With grant ".Here user1 can revoke access from user2 as well as User3. Here suppose if we revoke the privileges from User2 means it wouldn't revoke the privileges of User3. eg:-grant create index to Venky with admin option;

Monday 3 April 2017

Case V/s Decode

The Difference Between DECODE and CASE with JUSTIFICATIONS

1. CASE can work with logical operators other than ‘=’

 
DECODE performs an equality check only. CASE is capable of other logical comparisons such as < > etc. It takes some complex coding – forcing ranges of data into discrete form – to achieve the same effect with DECODE.
An example of putting employees in grade brackets based on their salaries. This can be done elegantly with CASE.


SQL> select ename
2 , case
3 when sal < 1000
4 then 'Grade I'
5 when (sal >=1000 and sal < 2000)
6 then 'Grade II'
7 when (sal >= 2000 and sal < 3000)
8 then 'Grade III'
9 else 'Grade IV'
10 end sal_grade
11 from emp
12 where rownum < 4;

 
ENAME SAL_GRADE
---------- ---------
SMITH Grade I
ALLEN Grade II
WARD Grade II


2. CASE can work with predicates and searchable sub queries

 
DECODE works with expressions that are scalar values only. CASE can work with predicates and sub queries in searchable form.


An example of categorizing employees based on reporting relationship, showing these two uses of CASE.


SQL> select e.ename,
2 case
3 -- predicate with "in"
4 -- set the category based on ename list
5 when e.ename in ('KING','SMITH','WARD')
6 then 'Top Bosses'
7 -- searchable subquery
8 -- identify if this emp has a reportee
9 when exists (select 1 from emp emp1
10 where emp1.mgr = e.empno)
11 then 'Managers'
12 else
13 'General Employees'
14 end emp_category
15 from emp e
16 where rownum < 5;

ENAME EMP_CATEGORY
---------- -----------------
SMITH Top Bosses
ALLEN General Employees
WARD Top Bosses
JONES Managers

3. CASE can work as a PL/SQL construct
DECODE can work as a function inside SQL only. CASE can be an efficient substitute for IF-THEN-ELSE in PL/SQL.


SQL> declare
2 grade char(1);
3 begin
4 grade := 'b';
5 case grade
6 when 'a' then dbms_output.put_line('excellent');
7 when 'b' then dbms_output.put_line('very good');
8 when 'c' then dbms_output.put_line('good');
9 when 'd' then dbms_output.put_line('fair');
10 when 'f' then dbms_output.put_line('poor');
11 else dbms_output.put_line('no such grade');
12 end case;
13 end;
14 /

PL/SQL procedure successfully completed.


4) CASE can even work as a parameter to a procedure call, while DECODE cannot.

 
SQL> var a varchar2(5);
SQL> exec :a := 'THREE';
PL/SQL procedure successfully completed.
SQL>
SQL> create or replace procedure proc_test (i number)
2 as
3 begin
4 dbms_output.put_line('output = '||i);
5 end;
6 /

Procedure created.
SQL> exec proc_test(decode(:a,'THREE',3,0));
BEGIN proc_test(decode(:a,'THREE',3,0)); END;
*
ERROR at line 1:
ORA-06550: line 1, column 17:
PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL
statement only
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

SQL> exec proc_test(case :a when 'THREE' then 3 else 0 end);
output = 3
PL/SQL procedure successfully completed.

4. Careful! CASE handles NULL differently

 
Check out the different results with DECODE vs NULL.


SQL> select decode(null
2 , null, 'NULL'
3 , 'NOT NULL'
4 ) null_test
5 from dual;

NULL
----
NULL
SQL> select case null
2 when null
3 then 'NULL'
4 else 'NOT NULL'
5 end null_test
6 from dual;

NULL_TES
--------
NOT NULL
The “searched CASE” works as does DECODE.
SQL> select case
2 when null is null
3 then 'NULL'
4 else 'NOT NULL'
5 end null_test
6* from dual

SQL> /
NULL_TES
--------
NULL

5. CASE expects datatype consistency, DECODE does not
 

Compare the two examples below- DECODE gives you a result, CASE gives a datatype mismatch error.

SQL> select decode(2,1,1,
2 '2','2',
3 '3') t
4 from dual;

T
----------
2
SQL> select case 2 when 1 then '1'
2 when '2' then '2'
3 else '3'
4 end
5 from dual;

when '2' then '2'
*
ERROR at line 2:
ORA-00932: inconsistent datatypes: expected NUMBER got CHAR


6. CASE is ANSI SQL-compliant
CASE complies with ANSI SQL. DECODE is proprietary to Oracle.

7. The difference in readability

 
In very simple situations, DECODE is shorter and easier to understand than CASE.
SQL> -- An example where DECODE and CASE
SQL> -- can work equally well, and
SQL> -- DECODE is cleaner
SQL> select ename
2 , decode (deptno, 10, 'Accounting',
3 20, 'Research',
4 30, 'Sales',
5 'Unknown') as department
6 from emp
7 where rownum < 4;

ENAME DEPARTMENT
---------- ----------
SMITH Research
ALLEN Sales
WARD Sales
SQL> select ename
2 , case deptno
3 when 10 then 'Accounting'
4 when 20 then 'Research'
5 when 30 then 'Sales'
6 else 'Unknown'
7 end as department
8 from emp
9 where rownum < 4;

ENAME DEPARTMENT
---------- ----------
SMITH Research
ALLEN Sales
WARD Sales


Complicated logical comparisons in DECODE, even if technically achievable, are a recipe for messy, bug-prone code. When the same can be done more cleanly with CASE, go for CASE.

Dedicated V/s Share Server (Basic Idea)

When a user makes a connection to the Oracle instance, a process is created on the server. For Unix/Linux servers, this process is called 'oracleSID' where "SID" is the Oracle identifier.

This process is dedicated to that one user's session. Should another user connect to the Oracle instance, another process is dedicated to that user's session. These dedicated server processes act as a go-between from the application to the Oracle instance and back again. Each of these dedicated server processes consumes memory on the database server. As the number of concurrent connections increases, the amount of memory required by all dedicated server processes increases as well. The database server may not have enough physical memory (RAM) for all of the server processes to support all of the concurrent application users. Additionally, many of the dedicated server processes may be idle. They are typically waiting for the end user to submit another request to the database. After all, human users are not as fast as computer systems.

It takes time for the user to read the information presented in the application and submit another request to the database. So the dedicated server processes can use up a lot of memory for many users who are idle at a specific point in time. At another point in time, the user may be doing something, but other users are idle. Shared server With shared server (formally called Multi-Threaded Server or MTS), users share the server connections. Instead of a one-to-one ratio between server processes and end users, there is a one-to-many relationship. One shared server process is used by many connected application users. Shared server is used for scalability, it means to connect many users to the instance at the same time. Query to check which server you are using? Select username,sid, server from v$session;

How Correlated Subquery works in Oracle

Let's assume we have following table in our  schema.















Let’s check how correlated sub query works using above table.

Here I’m going to write a query to get 2nd highest salary.

Select *from emp_chk e1 where 2= (Select Count(distinct(sal)) From Emp_Chk e2 Where e2.sal >=e1.sal)


As we know definition of correlated sub query is ,main query would execute first and for that result set ,inner query would execute once per each row (of main query output).

here inner query is dependent on main query.
 
Let’s go through in practical for above query:-
 
As per definition we will get all records from emp_chk table but here we need to concentrate on sal column because that column used in join condition.
So 67760,32000,25000,34345 are values for sal columns.

1st step: Pass 67760 in inner query.
Select *from emp_chk e1 where 2= (Select Count(distinct(sal)) From Emp_Chk e2 Where e2.sal >= 67760)

Here I have replaced e1.sal with 67760 because that is 1st value which would come and check that query condition. So if you see there, we would get count only one as we have only 1 record which is >=67760.

It means where condition doesn’t satisfy (i.e. where 2=1).so we don’t get any result.

2nd step: Pass 32000 in inner query
Select *from emp_chk e1 where 2= (Select Count(distinct(sal)) From Emp_Chk e2 Where e2.sal >= 32000)

Here we ill get count 3 as we have 3 records which are greater than or equal to 32000 so this one also doesn’t satisfy condition ( i.e. where 2=3). So we don’t get any result.


3rd step: Pass 25000 in inner query
Select *from emp_chk e1 where 2= (Select Count(distinct(sal)) From Emp_Chk e2 Where e2.sal >= 25000)

Here we ill get count 4 as we have 4 records which are greater than or equal to 25000 so this one also doesn’t satisfy condition ( i.e. where 2=4). So we don’t get any result.

4th step: Pass 34345 in inner query
Select *from emp_chk e1 where 2= (Select Count(distinct(sal)) From Emp_Chk e2 Where e2.sal >= 34345)

Here we ill get count 2 as we have 2 records which are greater than or equal to 34345 so finally our where condition got satisfied (i.e. where 2=2) for sal value 34345.So oracle would return this record as output.

In this way we can find out nth highest salary using correlated subquery.