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 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> 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 emp where deptno=10 union select job from emp where deptno=20; union all: combines the result of two select statement into one result set including the duplicates. select deptno,job from emp where deptno=10 union all select deptno,job from emp where deptno=20; 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. minus: the minus operator returns rows from the first query that are not present in the second query. select deptno,job from emp where deptno=10 minus select deptno,job from emp where deptno=20; note: the quries are all executed independently but their output is merged. only final query ends with a semicolon. privileges: privileges are the right to execute particular SQL Stmts. TWO TYPES: system privileges object privileges schema: a schema is a collection of objects, such as tables,views,and sequences. Role: a role is a named group of related privileges that cane be granted to the user. 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 can easy 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 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 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'; 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 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> 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. diff b/n synonym and views: synonym view • it is created for table, view • procedure,function,pacage be created on view can table and view. • it is used for hiding the information • of username,objectname • it takes minimum space only. 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. 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. 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. Types of indexes: • • • • • Normal indexes Bitmap indexes composite function based unique index 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 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 dept where upper(dname)='sales'; creating unique indexes >create unique index eno_unq_idx on emp (empno); 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 currval: Returns the Current value of sequence Nextval: Increment the sequence and Returns the next value. • Therse two clause can be used only select stmt,insert stmt and update stmt. • • • • • • cannot used in sub query where group by order by set operators, views. syntax: sequencename.<currvalue> sequencename<nextvalue> 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 name increment by n Start with n [maxvalue n/no maxvalue n] [minvalue/no minvalue] [cycle/no cycle] [cache/nocache] order/no order; eg: create table tdept • ( deptno number(4) constraint tdeptno_pk primary key, dname varchar2(14), dloc varchar2(10) ); • create sequence tdept_deptno_seq increment by 10 Start wirh 10 maxvalue 1000 minvalue 0 nocache nocycle; • 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. 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 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 emp startwith sal=5000 connect by prior empno=mgr 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 sequence Nextval: Increment the sequence and Returns the next value. • Therse two clause can be used only select stmt, insert stmt and update stmt. • • • • • cannot used in subquery where group by order by set operators,viwes. syntax: sequencename.<currvalue>; sequencename.<nextvalue>; LEVEL: • we cannot use level pseudo column w/o connect by clause. select nth highest: • select level, max(sal) from emp where level=&levelno connect by prior sal>sal Group by level; select nth lowest: The level pseudo column returns Root row child of root • select level,ename,empno from emp startwith job='president' connect by prior empno=mgr order by level; 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; 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 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: 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; 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 (deptno 10, 'accounting', 20,'research', 30,'sales', 'other') departments from emp; 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 comperision syntax: >case search_expr when expr1 then result 1 when expr 2 then result 2 else default_result end; Eg:>select ename,sal, case when sal>=800 and sal<=2000 then 'lowest pay' when sal>=2001 and sal<=4000 then 'moderatepay' else 'highpay' end from emp; 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; 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;