Set operators The set operaotrs combine the result of two or more component queries into one result....

39
set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound queries. different types of set operators are union operator union all operator intersect operator Minus operator

Transcript of Set operators The set operaotrs combine the result of two or more component queries into one result....

Page 1: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

set operators

The set operaotrs combine the result of two or more component queries into one result.

queries containing set operators are calledcompound queries.

different types of set operators are

union operatorunion all operatorintersect operator

Minus operator

Page 2: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

note: whenever these operators are used select stmt

must have equal no of columns

similar data type columns.

the generic syntax:

<comonent query>

{union|union all|minus|intersect}

<component query>

Page 3: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

union:

union operator to return all rows from multiple tables

and eliminate any duplicate rows. o/p stored in default ascending order

>select job from empwhere deptno=10

unionselect jobfrom emp

where deptno=20;

Page 4: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

union all:combines the result of two select statement into

oneresult set including the duplicates.

select deptno,jobfrom emp

where deptno=10union all

select deptno,jobfrom emp

where deptno=20;

Page 5: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

intersect:

use the intersect operator to return all rows

common to multiple quires.

select job from emp

where deptno=10

intersect

select job

from emp

where deptno=20;

note: intersect does not ignore null values.

Page 6: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

minus:

the minus operator returns rows from the first querythat are not present in the

second query.

select deptno,jobfrom emp

where deptno=10minus

select deptno,jobfrom emp

where deptno=20;

note: the quries are all executed independently but their outputis merged.

only final query ends with a semicolon.

Page 7: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

privileges:privileges are the right to execute particular SQL Stmts.

TWO TYPES:system privilegesobject privileges

schema:a schema is a collection of objects, such as

tables,views,and sequences.

Role:

a role is a named group of related privilegesthat cane be granted to the user.

Page 8: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

working with synonyms

It is a database object, which acts as an alternate name

for an existing object next to view.

Use: with synonym u caneasy referring to a table owned by another user.simply access to objects by creating a synonym.

Note:DML, Description, select allowed on synonyms

user_synonyms tables holds details of synonyms

Page 9: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Two types of synonyms:

Private synonym

public synonym

private synonym:

created by user

used by specific users which have permission

syntax:

create synonym<synonym_name>for table name

Page 10: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

scott/tiger:

create synonym empdept for emp>desc empdept

>grant insert,update,delete on empdept to userb;>insert into empdept values(

);

userb/userb:>select *from scott.empdept;

>delete from scott.empdept where deptno=20;>update scott.empdate job='man' where

job='clerk';

Page 11: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

scott/tiger:

• select *from empdept;

userc/userc:• what are done in userb all are working here also.

userb/userb:• create synonym prabhu for scott.empdate

c/c:• >select *from userb.prabhu;• >delete• >update

Page 12: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Public synonym:

• created by database administrators

• w/o using owner name u can access the permission is called Public.

• we should have create public synonym privilige,and it can accessed by all users.

• syntax: create <public synonym> <synonym name>for <schemaname.tablename>

Page 13: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

sys/mgr: • create public synonym empsdept for scott.emp;• grant select,delete,update on empsdept to

public;

userb/userb:

• select *from empsdept• insert,update,delete done

conn userc:• same above work done.

Page 14: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

diff b/n synonym and views:

synonym view

• it is created for table, view• procedure,function,pacage view can

be created on table and view.

• it is used for hiding the information• of username,objectname• it takes minimum space only.

Page 15: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Indexes:

• index is a schema object, which a pointer locates the physical address of data.

• Index used by the oracle server to speed up the retrieval, manipulated of rows.

• Data placed in hard disk Randomly where the data is stored that address identified by index.

Index creation is Two types:• index can be created explicitly or

Automatically.

Page 16: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

I) Automatic: A unique index is created Automatically when you

define a primary key or unique constraint in a table definition.

II) Manual;• users can create nonunique indexes on columns to

speed up access to the rows.

note;

• when u drop the index column or table corresponding indexes are also dropped.

• and disable the pkey or drop the pkey the related index will be dropped automatically.

• one table more than one index can be created.

Page 17: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

when to create an index:

• A column contains a wide range values.

• A column contains a large number of null values

• the column is used frequently in the where clause or join condition.

when not to create an index:

• the table is small

• the table is updated frequently.

Page 18: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Types of indexes:

• Normal indexes

• Bitmap indexes

• composite

• function based

• unique index

Page 19: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Normal index:• create index salidx on emp(sal);

Bitmap index:• index has to be create with a bitmap for each distinct

key.• Bit map indexes store the rowids• bitmap indexes should be used only when the data is

frequently updated• the oracle optimizer can dynamically convert bitmap

indexes to rowid's during the query processing.

>create bitmap index bitmap job on emp(job);

note: cannot specify both unique and bitmap

Page 20: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Composite Index:

• if we define a index on more than one column,it is called>create unique index eno_ename_cinx on emp(empno,ename);

Function Based Index:

• when we create index on column with function it is called

>create index upper_dept_name_idx on dept(upper (dname));

>select *from deptwhere upper(dname)='sales';

Page 21: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

creating unique indexes

>create unique index eno_unq_idx on emp (empno);

Page 22: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Pseudo columns:

• pseudo columns behave like a Table column but is not actual stored in a table.

• psudo columns only select can be implemented but insert,update,delete cannot perform.

Available pseudo columns:

• currval • nextval • leverl• Rowid• Rownum

Page 23: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

currval: Returns the Current value of sequenceNextval: Increment the sequence and Returns the next value.

• Therse two clause can be used onlyselect stmt,insert stmt and update stmt.

• cannot used in sub query • where• group by• order by• set operators,• views.

syntax: sequencename.<currvalue> sequencename<nextvalue>

Page 24: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Sequence :

• A sequence is a schema object that can be genarate unique sequential values.

• the sequence values are often used for primary key's and unique keys.

• purpose: can be used to generate pkey values automatically.

syntax:• create sequence sequnce nameincrement by nStart with n[maxvalue n/no maxvalue n][minvalue/no minvalue][cycle/no cycle][cache/nocache]order/no order;

Page 25: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

eg: create table tdept• (deptno number(4) constraint tdeptno_pk primary key,dname varchar2(14),dloc varchar2(10));

• create sequence tdept_deptno_seqincrement by 10Start wirh 10maxvalue 1000minvalue 0nocachenocycle;

Page 26: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

• insert into tdept values(tdept_deptno_seq.nextval,'software','hydbad');

• select *from tdept;

note : when ever using cycle u must mention the no cycle.• sequence is not depends on the table.

modifying sequence:• the alter command can be used to change the present status of

sequence.

• except startwith remaining all in syntax can be eaisly modified.

note: maxvalue is less than the current sequence number is not possible.

Page 27: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

how to see the current sequence• select sequence .currval from dual;

Removing sequence:• drop sequence<seqname>• Drop any sequence privilige to remove it of owner• Grant Drop any sequence to user a

Full information about sequences • Desc user_sequences

information of sequences are stored is • desc user_objects

Page 28: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

hierarchical queries:

• these queries that are executed upon tables that contain data.

Start with: it specifies the root of the hierarchical.Connect by: it is used to specify the relation ship b/n

parent rows and child rows of the hierarchy.

where: it is used to restrict the rows returned by the query

eg: select ename,empno,mgr,job from empstartwith sal=5000connect by prior empno=mgr

Page 29: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Pseudo columns:

• pseudo columns behave like a Table column but is not actual stored in a table.

• psudo columns only select can be implemented but insert,update,delete cannot perform.

Available psudo columns:

• currval • nextval • leverl• Rowid• Rownum

currval: Returns the Current value of sequenceNextval: Increment the sequence and Returns the next value.

Page 30: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

• Therse two clause can be used onlyselect stmt, insert stmt and update stmt.

• cannot used in subquery • where• group by• order by• set operators,viwes.

syntax: sequencename.<currvalue>; sequencename.<nextvalue>;

Page 31: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

LEVEL:• we cannot use level pseudo column w/o connect by clause.

select nth highest:

• select level, max(sal)from empwhere level=&levelnoconnect by prior sal>salGroup by level;

select nth lowest:

The level pseudo column returns Root row child of root

• select level,ename,empno from empstartwith job='president'connect by prior empno=mgrorder by level;

Page 32: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Rownum:

• the oracle engine assign a rownum value once retrive data from a table

• The first rows select has rownum of 1,---• it is temporaly.• it can be used at select where group function having

eg: select rownum from emp• select rownum,empno,ename,sal,deptno from emp;

Page 33: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Top salaries:

• select Rownum,empno,ename,sal

from(select *from emp order by sal desc)

group by Rownum,empno,ename,sal

having Rownum='&n';

• having rownum <='&n'

• having Rownum=1

Page 34: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

direct 2nd max sal

• select max(sal) from emp

where sal<(select max(sal) from emp);

direct max sal

• select ename,job,sal

from emp

where sal=(select max(sal)

from emp);

direct min sal:

Page 35: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Rowid:

Rowid is an exact physical address of row.

• Rowid is used by oracle to locate any row

• Rowid 's are unique

• Rowid can never be inserted,updated,deleted

• Rowid used in select stmt,where clause, group by clause.

Eg:

Select rowid from emp

select rownum,rowid,ename,sal,deptno from emp;

Page 36: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Decode function:• the decode function can be used to expand any

abbreviation used in the table.• the function wors on the same priciple as the

if_then_else

syntax: select decode(<colmn name/value>,<coded value>,<decoded

val>,.....)from<table name>;

Eg:>select decode (deptno10, 'accounting',20,'research',30,'sales','other') departments from emp;

Page 37: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Working with case:

• the case expression can be used to perform if_then_else logic in sql• it can be used even for executing conditions on range based comperisionsyntax:>case search_exprwhen expr1 then result 1when expr 2 then result 2else default_resultend;

Eg:>select ename,sal,casewhen sal>=800 and sal<=2000 then 'lowest pay'when sal>=2001 and sal<=4000 then 'moderatepay'else 'highpay'end from emp;

Page 38: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

nulls first/nulls last:

null values behave take highest values priority compare to not null value

• eg:select ename,comm from emp order by comm;note: the default is nulls last

nulls first:

• select comm,rank()over(order by comm desc nulls first)from emp;

nulls last:

• select comm,rank()over(order by comm desc nulls last)from emp where comm is not null;

Page 39: Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.

Ranking with partition:

• select ename,deptno,sal

over(partition by deptno order by sal desc)

from emp;

• select deptno,ename,sal ,sum(sal)

over(partition by deptno o

order by ename

rows 1 preceding) "sliding total"

from emp;