<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-2511702443521701494</atom:id><lastBuildDate>Thu, 26 Mar 2026 11:29:12 +0000</lastBuildDate><title>PLSQL in 21 Hours</title><description></description><link>http://naveen-plsql.blogspot.com/</link><managingEditor>noreply@blogger.com (Naveen)</managingEditor><generator>Blogger</generator><openSearch:totalResults>31</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-4280350528977411792</guid><pubDate>Wed, 21 May 2014 17:36:00 +0000</pubDate><atom:updated>2014-05-21T23:06:14.684+05:30</atom:updated><title>Oracle Basic Architecture</title><description>&lt;p&gt;&lt;b&gt;&lt;u&gt;Oracle Architecture (Basic):&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Below is the typical architecture of Oracle 11g. When you fire a SQL from any of the client tools like (SQL Developer/Toad/ or even SQLPLUS) below processes gets triggered at the back end.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Background Processes&lt;/u&gt;&lt;/b&gt;: It start with the database instance and perform maintenance tasks such as performing instance recovery, cleaning up processes, and writing redo buffers to disk, and so on.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Server Processes:&lt;/u&gt;&lt;/b&gt; perform work based on a client request.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0drojf6-19XdQ7GUZN-B7RLtIC_9vgOKrNUhyphenhyphenBNbz_OUgVpH7MLaXynkwqRD5vmesw5cccANMv3y4j40bH_HpTY3AuTFCQ3Roq6HqtMJ91rsp_odkHHzyR3SPnthlFyriTWKBh6hgBf2a/s1600-h/clip_image001%25255B5%25255D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px&quot; title=&quot;clip_image001&quot; border=&quot;0&quot; alt=&quot;clip_image001&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjktmSxkp_uDBWSu86yJp-g6lLq6bIzNrRBPXQIKq-JlBJO_PUEeCc1xj6Z2PVL51xyj1aVq-_icF04I9C1PYWI-vwZc03cyrsuRHDdeVGG5jLWi3DVkSvs5uHw43-fMHk0eOLl5ZuIf4Cy/?imgmax=800&quot; width=&quot;480&quot; height=&quot;353&quot; /&gt;&lt;/a&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Credits:&lt;b&gt;&lt;u&gt; &lt;/u&gt;&lt;a href=&quot;http://docs.oracle.com/cd/E11882_01/server.112/e40540/process.htm#CNCPT89079&quot;&gt;docs.oracle.com&lt;/a&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Terminologies &amp;amp; Functionality:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;1. SGA&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;System global area. A group of shared memory structures that contain data and control information for one Oracle database instance.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;2. PGA&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Program global area. A memory buffer that contains data and control information for a server process.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;3. Shared pool&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Portion of the SGA that contains shared memory constructs such as shared SQL areas.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;4. Large pool&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Optional area in the SGA that provides large memory allocations for backup and restore operations, I/O server processes, and session memory for the shared server and Oracle XA.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;5. Database buffer cache&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The portion of the system global area (SGA) that holds copies of data blocks. All client processes concurrently connected to the instance share access to the buffer cache.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;6. Redo log buffer&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Memory structure in the SGA that stores redo entries—a log of changes made to the database. The database writes the redo entries stored in the redo log buffers to an online redo log file, which is used if instance recovery is necessary.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;7. Online redo log&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The set of two or more &lt;a name=&quot;CHDJJHHI&quot;&gt;&lt;/a&gt;online redo log files that record all changes made to Oracle Database data files and control file. When a change is made to the database, Oracle Database generates a redo record in the redo buffer. log writer (LGWR) writes the contents of the redo buffer to the online redo log.&lt;/p&gt;  &lt;p&gt;Redo Log has set of files that protect altered database data in memory that has not been written to the data files. The redo log can consist of two parts: the online redo log and the archived redo log.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;8. Shared SQL area:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;An area in the shared pool that contains the parse tree and execution plan for a SQL statement. Only one shared SQL area exists for a unique statement.&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;9. Back Ground Processes:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;A process that consolidates functions that would otherwise be handled by multiple Oracle programs running for each client process. The &lt;a name=&quot;CHDBJJGB&quot;&gt;&lt;/a&gt;background processes asynchronously perform I/O and monitor other Oracle processes.&lt;/p&gt;  &lt;p&gt;1. &lt;b&gt;Mandatory Background Processes&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;These processes run by default in a database instance started with a minimally configured initialization parameter file&lt;/p&gt;  &lt;p&gt;&lt;b&gt;i. &lt;/b&gt;&lt;b&gt;&lt;u&gt;Process Monitor Process (PMON) :&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;PMON is responsible for cleaning up the database buffer cache and freeing resources that the client process was using. PMON also registers information about the instance and dispatcher processes with the Oracle Net listener&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ii. &lt;/b&gt;&lt;b&gt;&lt;u&gt;System Monitor Process (SMON): &lt;/u&gt;&lt;/b&gt;is in charge of a variety of system-level cleanup duties like Performing instance recovery, Recovering terminated transactions, cleaning up unused temporary segments, Coalescing contiguous free extents.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;iii. &lt;/b&gt;&lt;b&gt;&lt;u&gt;Database Writer Process (DBWn): &lt;/u&gt;&lt;/b&gt;It writes the contents of database buffers to data files. Typically database buffer is the memory area that stores copies of data blocks read from data files.&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;iv. &lt;/b&gt;&lt;b&gt;&lt;u&gt;Log Writer Process (LGWR) :&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The log writer process (LGWR) manages the redo log buffer. LGWR writes one contiguous portion of the buffer to the online redo log.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;v. &lt;/b&gt;&lt;b&gt;&lt;u&gt;Checkpoint Process (CKPT) :&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The checkpoint process (CKPT) updates the control file and data file headers with checkpoint information and signals DBWn to write blocks to disk&lt;/p&gt;  &lt;p&gt;&lt;b&gt;vi. &lt;/b&gt;&lt;b&gt;&lt;u&gt;Manageability Monitor Processes (MMON and MMNL):&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The manageability monitor process (MMON) performs many tasks related to the Automatic Workload Repository (AWR). For example, MMON writes when a metric violates its threshold value, taking snapshots, and capturing statistics value for recently modified SQL objects.&lt;/p&gt;  &lt;p&gt;The manageability monitor lite process (MMNL) writes statistics from the Active Session History (ASH) buffer in the SGA to disk. MMNL writes to disk when the ASH buffer is full.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;vii. &lt;/b&gt;&lt;b&gt;&lt;u&gt;Recoverer Process (RECO)&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;In a distributed database, the Recoverer process (RECO) automatically resolves failures in distributed transactions. The RECO process of a node automatically connects to other databases involved in an in-doubt distributed transaction.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;2. &lt;b&gt;&lt;u&gt;Optional Background Processes:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;i. &lt;/b&gt;&lt;b&gt;&lt;u&gt;Archiver Processes (ARCn):&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The Archiver processes (ARCn) copy online redo log files to offline storage after a redo log switch&lt;a name=&quot;sthref1915&quot;&gt;&lt;/a&gt;&lt;a name=&quot;sthref1916&quot;&gt;&lt;/a&gt; occurs. These processes can also collect transaction redo data and transmit it to standby database destinations. &lt;a name=&quot;sthref1917&quot;&gt;&lt;/a&gt;ARCn processes exist only when the database is in ARCHIVELOG mode and automatic archiving is enabled.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ii. &lt;/b&gt;&lt;b&gt;&lt;u&gt;Job Queue Processes (CJQ0 and Jnnn):&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Oracle Database uses job queue processes to run user jobs, often in batch mode.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;iii. &lt;/b&gt;&lt;b&gt;&lt;u&gt;Flashback Data Archiver Process (FBDA):&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;It archives historical rows of tracked tables into Flashback Data Archives. When a transaction containing DML on a tracked table commits, this process stores the pre-image of the rows into the Flashback Data Archive.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;iv. &lt;/b&gt;&lt;b&gt;&lt;u&gt;Space Management Coordinator Process (SMCO) :&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The SMCO process coordinates the execution of various space management related tasks, such as proactive space allocation and space reclamation. SMCO dynamically spawns slave processes (Wnnn) to implement the task.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;3. &lt;b&gt;&lt;u&gt;Slave Processes&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Slave processes are background processes that perform work on behalf of other processes.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;i. &lt;/b&gt;&lt;b&gt;&lt;u&gt;I/O Slave Processes:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;I/O slave processes (Innn) simulate asynchronous I/O for systems and devices that do not support it. In asynchronous I/O, there is no timing requirement for transmission, enabling other processes to start before the transmission has finished.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ii. &lt;/b&gt;&lt;b&gt;&lt;u&gt;Parallel Query Slaves:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;In&lt;a name=&quot;sthref1925&quot;&gt;&lt;/a&gt;&lt;a name=&quot;sthref1926&quot;&gt;&lt;/a&gt;&lt;a name=&quot;sthref1927&quot;&gt;&lt;/a&gt;&lt;a name=&quot;sthref1928&quot;&gt;&lt;/a&gt;&lt;a name=&quot;sthref1929&quot;&gt;&lt;/a&gt; &lt;a name=&quot;sthref1930&quot;&gt;&lt;/a&gt;parallel execution or parallel processing, multiple processes work together simultaneously to run a single SQL statement. By dividing the work among multiple processes, Oracle Database can run the statement more quickly. For example, four processes handle four different quarters in a year instead of one process handling all four quarters by itself.&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2014/05/oracle-basic-architecture.html</link><author>noreply@blogger.com (Naveen)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjktmSxkp_uDBWSu86yJp-g6lLq6bIzNrRBPXQIKq-JlBJO_PUEeCc1xj6Z2PVL51xyj1aVq-_icF04I9C1PYWI-vwZc03cyrsuRHDdeVGG5jLWi3DVkSvs5uHw43-fMHk0eOLl5ZuIf4Cy/s72-c?imgmax=800" height="72" width="72"/><thr:total>141</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-2097183972180740070</guid><pubDate>Wed, 21 May 2014 15:30:00 +0000</pubDate><atom:updated>2014-05-21T21:00:49.984+05:30</atom:updated><title>Frequently Used Data Dictionary Queries</title><description>&lt;p&gt;&lt;b&gt;1.Index Table :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Select INDEX_NAME ,INDEX_TYPE,TABLE_OWNER,TABLE_NAME,NUM_ROWS,&lt;/p&gt;  &lt;p&gt;LAST_ANALYZED,PARTITIONED From All_Indexes;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. Index Columns :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select INDEX_NAME,TABLE_OWNER,TABLE_NAME,COLUMN_NAME,&lt;/p&gt;  &lt;p&gt;COLUMN_POSITION, COLUMN_LENGTH,CHAR_LENGTH from ALL_IND_COLUMNS;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;3. Table Column Level Stats :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Select TABLE_NAME,COLUMN_NAME,NUM_DISTINCT,LAST_ANALYZED,AVG_COL_LEN from All_Tab_Col_Statistics;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;4. Constraints on a table:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,SEARCH_CONDITION,&lt;/p&gt;  &lt;p&gt;LAST_CHANGE,INDEX_OWNER,INDEX_NAME from All_Constraints;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;5. Text of a View:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select view_name,text,owner from All_Views;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;6. Text of a Trigger :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select&amp;#160;&amp;#160; trigger_name,trigger_type,triggering_event,table_owner,table_name,&lt;/p&gt;  &lt;p&gt;description,trigger_body from all_triggers;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;7. Locks on Table:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select c.owner,c.object_name,c.object_type,c.object_id,b.sid,b.serial#,&lt;/p&gt;  &lt;p&gt;b.status,b.osuser,b.machine,a.LOCKED_MODE from v$locked_object &lt;/p&gt;  &lt;p&gt;a ,v$session b,dba_objects c where b.sid = a.session_id and&amp;#160; a.object_id =&lt;/p&gt;  &lt;p&gt; c.object_id;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Locking Modes Description:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;0- none&lt;/p&gt;  &lt;p&gt;1 - null (NULL)&lt;/p&gt;  &lt;p&gt;2 - row-S (SS)&lt;/p&gt;  &lt;p&gt;3 - row-X (SX)&lt;/p&gt;  &lt;p&gt;4 - share (S)&lt;/p&gt;  &lt;p&gt;5 - S/Row-X (SSX)&lt;/p&gt;  &lt;p&gt;6 - exclusive (X)&lt;/p&gt;  &lt;p&gt;&lt;b&gt;8. Query to Kill a Session :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;alter system kill session(sid,serial no)&lt;/p&gt;  &lt;p&gt;&lt;b&gt;9.Find the Actual size of a Database&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SELECT SUM (bytes) / 1024 / 1024 / 1024 AS GB FROM dba_data_files;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;10.Find the size occupied by Data in a Database or Database usage details&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SELECT SUM (bytes) / 1024 / 1024 / 1024 AS GB FROM dba_segments;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;11. Last SQL fired by the User on Database&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SELECT S.USERNAME || &#39;(&#39; || s.sid || &#39;)-&#39; || s.osuser UNAME, s.program || &#39;-&#39; ||&lt;/p&gt;  &lt;p&gt; s.terminal || &#39;(&#39; || s.machine || &#39;)&#39; PROG,s.sid || &#39;/&#39; || s.serial# sid, s.status&lt;/p&gt;  &lt;p&gt; &amp;quot;Status&amp;quot;,p.spid,sql_text sqltext FROM v$sqltext_with_newlines t,&lt;/p&gt;  &lt;p&gt; V$SESSION s, v$process p&amp;#160;&amp;#160; WHERE t.address = s.sql_address&amp;#160;&amp;#160; AND&lt;/p&gt;  &lt;p&gt; p.addr = s.paddr(+) AND t.hash_value = s.sql_hash_value&amp;#160; ORDER BY s.sid,&lt;/p&gt;  &lt;p&gt; t.piece;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;12. CPU usage of the USER&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SELECT ss.username, se.SID, VALUE / 100 cpu_usage_seconds FROM&lt;/p&gt;  &lt;p&gt; v$session ss, v$sesstat se, v$statname sn WHERE&amp;#160; se.STATISTIC# =&lt;/p&gt;  &lt;p&gt; sn.STATISTIC#&amp;#160; AND NAME LIKE &#39;%CPU used by this session%&#39; AND se.SID&lt;/p&gt;  &lt;p&gt; = ss.SID AND ss.status = &#39;ACTIVE&#39; AND ss.username IS NOT NULL ORDER&lt;/p&gt;  &lt;p&gt; BY VALUE DESC;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;13. Long Query progress in database&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SELECT a.sid,a.serial#,b.username,opname OPERATION,target&lt;/p&gt;  &lt;p&gt; OBJECT,TRUNC (elapsed_seconds, 5) &amp;quot;ET (s)&amp;quot;,TO_CHAR (start_time,&lt;/p&gt;  &lt;p&gt; &#39;HH24:MI:SS&#39;) start_time,ROUND ( (sofar / totalwork) * 100, 2) &amp;quot;COMPLETE&lt;/p&gt;  &lt;p&gt; (%)&amp;quot; FROM v$session_longops a, v$session b&amp;#160; WHERE&amp;#160;&amp;#160;&amp;#160;&amp;#160; a.sid = b.sid&amp;#160; AND&lt;/p&gt;  &lt;p&gt; b.username NOT IN (&#39;SYS&#39;, &#39;SYSTEM&#39;) AND totalwork &amp;gt; 0 ORDER BY&lt;/p&gt;  &lt;p&gt; elapsed_seconds;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;14.Last DDL SQL Fired from particular Schema or Table:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SELECT CREATED, TIMESTAMP, last_ddl_time&amp;#160; FROM all_objects&amp;#160; WHERE&amp;#160;&amp;#160;&amp;#160;&amp;#160; OWNER = &#39;MYSCHEMA&#39;&amp;#160; AND OBJECT_TYPE = &#39;TABLE&#39; AND OBJECT_NAME = &#39;EMPLOYEE_TABLE&#39;;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;15.Find Top 10 SQL by reads per execution&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SELECT * FROM (&amp;#160; SELECT ROWNUM,SUBSTR (a.sql_text, 1, 200)&lt;/p&gt;  &lt;p&gt; sql_text,TRUNC (a.disk_reads / DECODE (a.executions, 0, 1, a.executions))&lt;/p&gt;  &lt;p&gt;reads_per_execution,a.buffer_gets,a.disk_reads,a.executions,a.sorts,a.address &lt;/p&gt;  &lt;p&gt;FROM v$sqlarea a ORDER BY 3 DESC) WHERE ROWNUM &amp;lt; 10; &lt;/p&gt;  &lt;p&gt;&lt;b&gt;16.Oracle SQL query that show the opened connections group by the program that opens the connection.&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SELECT program application, COUNT (program) Numero_Sesiones FROM&lt;/p&gt;  &lt;p&gt; v$session GROUP BY program ORDER BY Numero_Sesiones DESC;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;17.Get number of objects per owner&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SELECT owner, COUNT (owner) number_of_objects FROM dba_objects&lt;/p&gt;  &lt;p&gt; Group By Owner ORDER BY number_of_objects DESC;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;18.Oracle SQL query that shows Oracle users connected and the sessions number for user&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SELECT username Usuario_Oracle, COUNT (username) Numero_Sesiones &lt;/p&gt;  &lt;p&gt;FROM v$session Group By Username ORDER BY Numero_Sesiones DESC;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;19. DML Operation Audit&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select username ,obj_name ,to_char(timestamp,&#39;dd-mon-yy hh24:mi&#39;) &lt;/p&gt;  &lt;p&gt;event_time&amp;#160; ,substr(ses_actions,4,1) del&amp;#160; ,substr(ses_actions,7,1) &lt;/p&gt;  &lt;p&gt;ins ,substr(ses_actions,10,1) sel&amp;#160; ,substr(ses_actions,11,1) upd&amp;#160; from dba_audit_object;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;20.SQL Statements with Maximum Wait&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select ash.user_id, u.username, s.sql_text, sum(ash.wait_time + &lt;/p&gt;  &lt;p&gt;ash.time_waited) ttl_wait_time from v$active_session_history ash, &lt;/p&gt;  &lt;p&gt;v$sqlarea s, dba_users u where ash.sample_time between sysdate - &lt;/p&gt;  &lt;p&gt;60/2880 and sysdate and ash.sql_id = s.sql_id and ash.user_id = u.user_id &lt;/p&gt;  &lt;p&gt;group by ash.user_id,s.sql_text, u.username order by ttl_wait_time ;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;21. SQL Text:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select sid, sql_text from v$session s, v$sql q where sid in (* ,*) and &lt;/p&gt;  &lt;p&gt;(q.sql_id = s.sql_id or q.sql_id = s.prev_sql_id);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Note:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Provide the Sid for which you wish to see the SQL Text else list will be exhaustive.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;22.query to find out which session is currently using the most undo&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select s.sid, t.name, s.value from v$sesstat s, v$statname t where &lt;/p&gt;  &lt;p&gt;s.statistic# = t.statistic# and t.name = &#39;undo change vector size&#39; order by &lt;/p&gt;  &lt;p&gt;s.value desc; &lt;/p&gt;  &lt;p&gt;&lt;b&gt;23. Monitoring Temporary Tablespace Usage&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select * from (select a.tablespace_name,sum(a.bytes/1024/1024) &lt;/p&gt;  &lt;p&gt;allocated_mb&amp;#160; from dba_temp_files a where a.tablespace_name = upper&lt;/p&gt;  &lt;p&gt;(&#39;&amp;amp;&amp;amp;temp_tsname&#39;) group by a.tablespace_name) x, (select sum&lt;/p&gt;  &lt;p&gt;(b.bytes_used/1024/1024) used_mb, sum(b.bytes_free/1024/1024) &lt;/p&gt;  &lt;p&gt;free_mb&amp;#160; from v$temp_space_header b where b.tablespace_name=upper&lt;/p&gt;  &lt;p&gt;(&#39;&amp;amp;&amp;amp;temp_tsname&#39;) group by b.tablespace_name);&lt;/p&gt;  &lt;p&gt;&lt;b&gt;24. query to find out which sessions are using space in the temporary tablespace. &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select s.sid || &#39;,&#39; || s.serial# sid_serial, s.username, s.osuser, &lt;/p&gt;  &lt;p&gt;p.spid,s.module,s.program,sum (o.blocks) * t.block_size / 1024 / 1024 &lt;/p&gt;  &lt;p&gt;mb_used, o.tablespace,count(*) sorts from v$sort_usage o, v$session s, &lt;/p&gt;  &lt;p&gt;dba_tablespaces t, v$process p where o.session_addr = s.saddr and &lt;/p&gt;  &lt;p&gt;s.paddr = p.addr and o.tablespace = t.tablespace_name group by s.sid, &lt;/p&gt;  &lt;p&gt;s.serial#, s.username, s.osuser, p.spid, s.module, s.program, t.block_size, &lt;/p&gt;  &lt;p&gt;o.tablespace order by sid_serial; &lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2014/05/frequently-used-data-dictionary-queries.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-3831934213511522516</guid><pubDate>Tue, 07 Jun 2011 12:39:00 +0000</pubDate><atom:updated>2011-06-07T18:28:07.807+05:30</atom:updated><title>Wishing Thanks to My Readers.</title><description>&lt;p&gt;&lt;em&gt;Hi reader’s,&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;I’m very much happy to see that lot of people from across the globe visited my blog and visitor’s count has crossed 3000 + with in 40 days of starting the blog. I would request my readers to start posting your valuable comments and requests so that I can make the blog more informative &amp;amp;&amp;#160; interactive . For viewers information who visit my Informatica blog, to visit my SQL or Data warehousing blogs too as I spent a lot of time posting the information, which is very useful for the job seekers or Data warehousing professionals in your day today activities at work or can also be used as the quick reference guide before taking up any interview.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;I’m planning to publish a book on informatica soon which will cover all major concepts of the data warehousing in detail, including the SQL concepts used by an ETL consultants, Basic Unix Commands and Introduction to the Scheduler tools like Autosys, Tivoli and off course Informatica with simple examples.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;I would highly appreciate if few people come forward and post the interview questions or the scenarios which they have encountered in there day to day work for discussion at the discussion board.&lt;/em&gt;&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/06/wishing-thanks-to-my-readers.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-7721416088016865833</guid><pubDate>Mon, 09 May 2011 07:40:00 +0000</pubDate><atom:updated>2011-05-09T13:10:23.581+05:30</atom:updated><title>Oracle Interview Based Queries-4</title><description>&lt;p&gt;&lt;strong&gt;151) My boss has changed his mind. Now he doesn&#39;t want to pay more than 10,000.so revoke that salary constraint.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;alter table emp modify constraint chk_001 enable;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;152) Add column called as mgr to your emp table;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;alter table emp add(mgr number(5));&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;153) Oh! This column should be related to empno. Give a command to add this constraint.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;ALTER TABLE EMP ADD CONSTRAINT MGR_DEPT FOREIGN KEY(MGR) REFERENCES EMP(EMPNO)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;154) Add deptno column to your emp table;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;alter table emp add(deptno number(5));&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;155) This deptno column should be related to deptno column of dept table;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;alter table emp add constraint dept_001 foreign key(deptno) &lt;/p&gt;  &lt;p&gt;reference dept(deptno) [deptno should be primary key]&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;156) Give the command to add the constraint.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;alter table &amp;lt;table_name) add constraint &amp;lt;constraint_name&amp;gt; &lt;/p&gt;  &lt;p&gt;&amp;lt;constraint type&amp;gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;157) Create table called as newemp. Using single command create this table as well as get data into this table(use create table as);&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;create table newemp as select * from emp;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;Create table called as newemp. This table should contain only &lt;/p&gt;  &lt;p&gt;empno,ename,dname.&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;create table newemp as select empno,ename,dname from emp,dept where 1=2;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;158) Delete the rows of employees who are working in the company for more than 2 years.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;delete from emp where (sysdate-hiredate)/365&amp;gt;2;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;159) Provide a commission(10% Comm Of Sal) to employees who are not earning any commission.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select sal*0.1 from emp where comm is null&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;160) If any employee has commission his commission should be incremented by 10% of his salary.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;update emp set comm=sal*.1 where comm is not null;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;161) Display employee name and department name for each employee.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select empno,dname from emp,dept where emp.deptno=dept.deptno&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;162)Display employee number,name and location of the department in which he is working.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select empno,ename,loc,dname from emp,dept where emp.deptno=dept.deptno;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;163) Display ename,dname even if there are no employees working in a particular department(use outer join).&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,dname from emp,dept where emp.deptno=dept.deptno(+)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;164) Display employee name and his manager name.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select p.ename,e.ename from emp e,emp p where e.empno=p.mgr;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;165) Display the department name and total number of employees in each department.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select dname,count(ename) from emp,dept where emp.deptno=dept.deptno group by dname;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;166)Display the department name along with total salary in each department.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select dname,sum(sal) from emp,dept where emp.deptno=dept.deptno group by dname;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;167) Display itemname and total sales amount for each item.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select itemname,sum(amount) from item group by itemname;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;168) Write a Query To Delete The Repeated Rows from emp table;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;Delete from emp where rowid not in(select min(rowid)from emp group by ename)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;169) TO DISPLAY 5 TO 7 ROWS FROM A TABLE&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where rowid in(select rowid from emp where rownum&amp;lt;=7 minus select rowid from empi where rownum&amp;lt;5)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;170) DISPLAY TOP N ROWS FROM TABLE?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT * FROM (SELECT * FROM EMP ORDER BY ENAME DESC)WHERE ROWNUM &amp;lt;10;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;171) DISPLAY TOP 3 SALARIES FROM EMP;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT SAL FROM ( SELECT * FROM EMP ORDER BY SAL DESC ) WHERE ROWNUM &amp;lt;4&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;172) DISPLAY 9th FROM THE EMP TABLE?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT ENAME FROM EMPWHERE ROWID=(SELECT ROWID FROM EMP WHERE ROWNUM&amp;lt;=10 MINUS SELECT ROWID FROM EMP WHERE ROWNUM &amp;lt;10)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;173) select second max salary from emp;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;select max(sal) fromemp where sal&amp;lt;(select max(sal) from emp);&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/05/oracle-interview-based-queries-4.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>5</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-6865508265329721208</guid><pubDate>Mon, 09 May 2011 07:33:00 +0000</pubDate><atom:updated>2011-05-09T13:03:03.969+05:30</atom:updated><title>Oracle Interview Based Queries-3</title><description>&lt;p&gt;&lt;strong&gt;101) Display name and salary of ford if his salary is equal to hisal of his grade.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;a)select ename,sal,grade from emp,salgrade where sal between losal and hisal and ename =&#39;FORD&#39; AND HISAL=SAL;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;102) Display employee name,job,depart name ,manager name,his grade and make out an under department wise?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT E.ENAME,E.JOB,DNAME,EMP.ENAME,GRADE FROM EMP,EMP E,SALGRADE,DEPT WHERE EMP.SAL BETWEEN LOSAL AND HISAL AND EMP.EMPNO=E.MGR AND EMP.DEPTNO=DEPT.DEPTNO ORDER BY DNAME&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;103) List out all employees name,job,salary,grade and depart name for every one in the company except &#39;CLERK&#39;.Sort on salary display the highest salary?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT ENAME,JOB,DNAME,SAL,GRADE FROM EMP,SALGRADE,DEPT WHERE SAL BETWEEN LOSAL AND HISAL AND EMP.DEPTNO=DEPT.DEPTNO AND JOB NOT IN(&#39;CLERK&#39;)ORDER BY SAL ASC;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;104) Display the employee name,job and his manager.Display also employee who are without manager?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select e.ename,e.job,eMP.ename AS Manager from emp,emp e where emp.empno(+)=e.mgr&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;105) Find out the top 5 earners of company?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT DISTINCT SAL FROM EMP E WHERE 5&amp;gt;=(SELECT COUNT(DISTINCT SAL) FROM EMP A WHERE A.SAL&amp;gt;=E.SAL)ORDER BY SAL DESC;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;106) Display name of those employee who are getting the highest salary?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where sal=(select max(sal) from emp);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;107) Display those employee whose salary is equal to average of maximum and minimum?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where sal=(select max(sal)+min(sal)/2 from emp);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;108) Select count of employee in each department where count greater than 3?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select count(*) from emp group by deptno having count(deptno)&amp;gt;3&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;109) Display dname where at least 3 are working and display only department name?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select distinct d.dname from dept d,emp e where d.deptno=e.deptno and 3&amp;gt;any (select count(deptno) from emp group by deptno)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;110) Display name of those managers name whose salary is more than average salary of his company?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT E.ENAME,EMP.ENAME FROM EMP,EMP E WHERE EMP.EMPNO=E.MGR AND E.SAL&amp;gt;(SELECT AVG(SAL) FROM EMP);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;111)Display those managers name whose salary is more than average salary of his employee?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT DISTINCT EMP.ENAME FROM EMP,EMP E WHERE E.SAL &amp;lt;(SELECT AVG(EMP.SAL) FROM EMP WHERE EMP.EMPNO=E.MGR GROUP BY EMP.ENAME) AND EMP.EMPNO=E.MGR;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;112) Display employee name,sal,comm and net pay for those employee &lt;/strong&gt;&lt;strong&gt;whose net pay is greter than or equal to any other employee salary of &lt;/strong&gt;&lt;strong&gt;the company?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,sal,comm,sal+nvl(comm,0) as NetPay from emp where sal+nvl(comm,0) &amp;gt;any (select sal from emp)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;113) Display all employees names with total sal of company with each employee name?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT ENAME,(SELECT SUM(SAL) FROM EMP) FROM EMP;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;114) Find out last 5(least)earners of the company.?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT DISTINCT SAL FROM EMP E WHERE 5&amp;gt;=(SELECT COUNT(DISTINCT SAL) FROM EMP A WHERE A.SAL&amp;lt;=E.SAL) ORDER BY SAL DESC;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;115) Find out the number of employees whose salary is greater than their manager salary?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT E.ENAME FROM EMP ,EMP E WHERE EMP.EMPNO=E.MGR AND EMP.SAL&amp;lt;E.SAL;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;116) Display those department where no employee working?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select dname from emp,dept where emp.deptno not in(emp.deptno)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;117) Display those employee whose salary is ODD value?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select * from emp where sal&amp;lt;0;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;118) Display those employee whose salary contains at least 3 digits?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select * from emp where length(sal)&amp;gt;=3;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;119) Display those employee who joined in the company in the month of Dec?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where to_char(hiredate,&#39;MON&#39;)=&#39;DEC&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;120) Display those employees whose name contains &amp;quot;A&amp;quot;?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where instr(ename,&#39;A&#39;)&amp;gt;0;&lt;/p&gt;  &lt;p&gt;or&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where ename like(&#39;%A%&#39;);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;121) Display those employee whose deptno is available in salary?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select emp.ename from emp, emp e where emp.sal=e.deptno;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;122) Display those employee whose first 2 characters from hiredate -last 2 &lt;/strong&gt;&lt;strong&gt;characters of salary?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,SUBSTR(hiredate,1,2)||ENAME||substr(sal,-2,2) from emp&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;123) Display those employee whose 10% of salary is equal to the year of &lt;/strong&gt;&lt;strong&gt;joining?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where to_char(hiredate,&#39;YY&#39;)=sal*0.1;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;124) Display those employee who are working in sales or research?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT ENAME FROM EMP WHERE DEPTNO IN(SELECT DEPTNO FROM DEPT WHERE DNAME IN(&#39;SALES&#39;,&#39;RESEARCH&#39;));&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;125) Display the grade of jones?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT ENAME,GRADE FROM EMP,SALGRADE WHERE SAL BETWEEN LOSAL AND HISAL AND Ename=&#39;JONES&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;126) Display those employees who joined the company before 15 of the month?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;select ename from emp where to_char(hiredate,&#39;DD&#39;)&amp;lt;15;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;127) Display those employee who has joined before 15th of the month.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;a)select ename from emp where to_char(hiredate,&#39;DD&#39;)&amp;lt;15;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;128) Delete those records where no of employees in a particular department is less than 3.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;delete from emp where deptno=(select deptno from emp group by deptno having count(deptno)&amp;lt;3);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;129) Display the name of the department where no employee working.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt; SELECT E.ENAME,E.JOB,M.ENAME,M.JOB FROM EMP E,EMP M WHERE E.MGR=M.EMPNO&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;130) Display those employees who are working as manager.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT M.ENAME MANAGER FROM EMP M ,EMP E WHERE E.MGR=M.EMPNO GROUP BY M.ENAME&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;131) Display those employees whose grade is equal to any number of sal but not equal to first number of sal?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt; SELECT ENAME,GRADE FROM EMP,SALGRADE WHERE GRADE NOT IN(SELECT SUBSTR(SAL,0,1)FROM EMP)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;132) Print the details of all the employees who are Sub-ordinate to BLAKE?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select emp.ename from emp, emp e where emp.mgr=e.empno and e.ename=&#39;BLAKE&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;133) Display employee name and his salary whose salary is greater than highest average of department number?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT SAL FROM EMP WHERE SAL&amp;gt;(SELECT MAX(AVG(SAL)) FROM EMP GROUP BY DEPTNO);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;134) Display the 10th record of emp table(without using rowid)&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT * FROM EMP WHERE ROWNUM&amp;lt;11&lt;/p&gt;  &lt;p&gt;MINUS&lt;/p&gt;  &lt;p&gt;SELECT * FROM EMP WHERE ROWNUM&amp;lt;10 &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;135) Display the half of the ename&#39;s in upper case and remaining lowercase?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT&amp;#160; SUBSTR(LOWER(ENAME),1,3)||SUBSTR(UPPER(ENAME),3,LENGTH(ENAME)) FROM EMP;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;136) Display the 10th record of emp table without using group by and rowid?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT * FROM EMP WHERE ROWNUM&amp;lt;11&lt;/p&gt;  &lt;p&gt;MINUS&lt;/p&gt;  &lt;p&gt;SELECT * FROM EMP WHERE ROWNUM&amp;lt;10 &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Delete the 10th record of emp table.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;DELETE FROM EMP WHERE EMPNO=(SELECT EMPNO FROM EMP WHERE ROWNUM&amp;lt;11 MINUS SELECT EMPNO FROM EMP WHERE ROWNUM&amp;lt;10)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;137) Create a copy of emp table;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;create table new_table as select * from emp where 1=2;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;138) Select ename if ename exists more than once.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp e group by ename having count(*)&amp;gt;1;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;139) Display all enames in reverse order?(SMITH:HTIMS).&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT REVERSE(ENAME)FROM EMP;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;140) Display those employee whose joining of month and grade is equal.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT ENAME FROM EMP WHERE SAL BETWEEN (SELECT LOSAL FROM SALGRADE WHERE GRADE=TO_CHAR(HIREDATE,&#39;MM&#39;)) AND (SELECT HISAL FROM SALGRADE WHERE GRADE=TO_CHAR(HIREDATE,&#39;MM&#39;));&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;141) Display those employee whose joining DATE is available in deptno.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT ENAME FROM EMP WHERE TO_CHAR(HIREDATE,&#39;DD&#39;)=DEPTNO&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;142) Display those employees name as follows &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A ALLEN&lt;/p&gt;  &lt;p&gt;B BLAKE&lt;/p&gt;  &lt;p&gt;SQL&amp;gt; SELECT SUBSTR(ENAME,1,1),ENAME FROM EMP;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;143) List out the employees ename,sal,PF(20% OF SAL) from emp;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT ENAME,SAL,SAL*.2 AS PF FROM EMP;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;144) Create table emp with only one column empno;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;Create table emp as select empno from emp where 1=2;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;145) Add this column to emp table ename varchar2(20).&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;alter table emp add(ename varchar2(20));&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;146) Oops I forgot give the primary key constraint. Add in now.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;alter table emp add primary key(empno);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;147) Now increase the length of ename column to 30 characters.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;alter table emp modify(ename varchar2(30));&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;148) Add salary column to emp table.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;alter table emp add(sal number(10));&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;149) I want to give a validation saying that salary cannot be greater 10,000 (note give a name to this constraint)&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;alter table emp add constraint chk_001 check(sal&amp;lt;=10000)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;150) For the time being I have decided that I will not impose this validation.My boss has agreed to pay more than 10,000.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;again alter the table or drop constraint with alter table emp drop constraint chk_001 (or)Disable the constraint by using alter table emp modify constraint chk_001 disable;&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/05/oracle-interview-based-queries-3.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>28</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-4358475898038314454</guid><pubDate>Mon, 09 May 2011 05:44:00 +0000</pubDate><atom:updated>2011-05-09T11:16:42.755+05:30</atom:updated><title>Oracle Interview based Queries - part2</title><description>&lt;p&gt;&lt;strong&gt;51) Display the name of the employee who earns highest salary.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where sal=(select max(sal) from emp);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;52) Display the employee number and name for employee working as clerk and earning highest salary among clerks.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select empno,ename from emp where where job=&#39;CLERK&#39; and sal=(select max(sal) from emp where job=&#39;CLERK&#39;);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;53) Display the names of salesman who earns a salary more than the highest salary of any clerk.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,sal from emp where job=&#39;SALESMAN&#39; and sal&amp;gt;(select max(sal) from emp where job=&#39;CLERK&#39;);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;54) Display the names of clerks who earn a salary more than the lowest salary of any salesman.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where job=&#39;CLERK&#39; and sal&amp;gt;(select min(sal) from emp where job=&#39;SALESMAN&#39;);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Display the names of employees who earn a salary more than that of &lt;/strong&gt;&lt;strong&gt;Jones or that of salary grether than that of scott.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,sal from emp where sal&amp;gt;(select sal from emp where ename=&#39;JONES&#39;)and sal&amp;gt; (select sal from emp where ename=&#39;SCOTT&#39;);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;55) Display the names of the employees who earn highest salary in their respective departments.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,sal,deptno from emp where sal in(select max(sal) from emp group by deptno);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;56) Display the names of the employees who earn highest salaries in their respective job groups.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,sal,job from emp where sal in(select max(sal) from emp &lt;/p&gt;  &lt;p&gt;group by job)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;57) Display the employee names who are working in accounting department.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where deptno=(select deptno from dept where dname=&#39;ACCOUNTING&#39;)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;58) Display the employee names who are working in Chicago.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where deptno=(select deptno from dept where LOC=&#39;CHICAGO&#39;)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;59) Display the Job groups having total salary greater than the maximum salary for managers.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT JOB,SUM(SAL) FROM EMP GROUP BY JOB HAVING SUM(SAL)&amp;gt;(SELECT MAX(SAL) FROM EMP WHERE JOB=&#39;MANAGER&#39;);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;60) Display the names of employees from department number 10 with salary greater than that of any employee working in other department.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where deptno=10 and sal&amp;gt;any(select sal from emp where deptno not in 10).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;61) Display the names of the employees from department number 10 with &lt;/strong&gt;&lt;strong&gt;salary greater than that of all employee working in other departments.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where deptno=10 and sal&amp;gt;all(select sal from emp where deptno not in 10).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;62) Display the names of the employees in Uppercase.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select upper(ename)from emp&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;63) Display the names of the employees in Lower case&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select lower(ename)from emp&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;64) Display the names of the employees in Proper case.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select initcap(ename)from emp;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;65) Display the length of Your name using appropriate function.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select length(&#39;name&#39;) from dual&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;66) Display the length of all the employee names.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select length(ename) from emp;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;67) select name of the employee concatenate with employee number. &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename||empno from emp;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;68) User appropriate function and extract 3 characters starting from 2 &lt;/strong&gt;&lt;strong&gt;characters from the following string &#39;Oracle&#39;. i.e the out put should be &#39;ac&#39;.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select substr(&#39;oracle&#39;,3,2) from dual&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;69) Find the First occurance of character &#39;a&#39; from the following string i.e &lt;/strong&gt;&lt;strong&gt;&#39;Computer Maintenance Corporation&#39;.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT INSTR(&#39;Computer Maintenance Corporation&#39;,&#39;a&#39;,1) FROM DUAL&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;70) Replace every occurance of alphabhet A with B in the string Allens(use &lt;/strong&gt;&lt;strong&gt;translate function)&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select translate(&#39;Allens&#39;,&#39;A&#39;,&#39;B&#39;) from dual&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;71) Display the informaction from emp table.Where job manager is found it &lt;/strong&gt;&lt;strong&gt;should be displayed as boos(Use replace function).&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select replace(JOB,&#39;MANAGER&#39;,&#39;BOSS&#39;) FROM EMP;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;72) Display empno,ename,deptno from emp table.Instead of display department numbers display the related department name(Use decode function).&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select empno,ename,decode(deptno,10,&#39;ACCOUNTING&#39;,20,&#39;RESEARCH&#39;,30,&#39;SALES&#39;,40,&#39;OPRATIONS&#39;) from emp;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;73) Display your age in days.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select to_date(sysdate)-to_date(&#39;10-sep-77&#39;)from dual&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;74) Display your age in months.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select months_between(sysdate,&#39;10-sep-77&#39;) from dual&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;75) Display the current date as 15th August Friday Nineteen Ninety Seven.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select to_char(sysdate,&#39;ddth Month day year&#39;) from dual&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;76) Display the following output for each row from emp table.scott has joined the company on Wednesday 13th August nineteen ninety.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ENAME||&#39; HAS JOINED THE COMPANY ON &#39;||to_char(HIREDATE,&#39;day ddth Month year&#39;) from EMP;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;77) Find the date for nearest saturday after current date.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT NEXT_DAY(SYSDATE,&#39;SATURDAY&#39;)FROM DUAL;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;78) Display current time.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select to_char(sysdate,&#39;hh:MM:ss&#39;) from dual.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;79) Display the date three months Before the current date.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select add_months(sysdate,3) from dual;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;80) Display the common jobs from department number 10 and 20.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select job from emp where deptno=10 and job in(select job from emp &lt;/p&gt;  &lt;p&gt;where deptno=20);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;81) Display the jobs found in department 10 and 20 Eliminate duplicate jobs.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select distinct(job) from emp where deptno=10 or deptno=20&lt;/p&gt;  &lt;p&gt;(or)&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select distinct(job) from emp where deptno in(10,20);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;82) Display the jobs which are unique to department 10.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select distinct(job) from emp where deptno=10&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;83) Display the details of those who do not have any person working under them.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select e.ename from emp,emp e where emp.mgr=e.empno group by e.ename having count(*)=1;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;84) Display the details of those employees who are in sales department and grade is 3.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select * from emp where deptno=(select deptno from dept where dname=&#39;SALES&#39;)and sal between(select losal from salgrade where grade=3)and (select hisal from salgrade where grade=3);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;85) Display those who are not managers and who are managers any one.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;i)display the managers names&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select distinct(m.ename) from emp e,emp m where m.empno=e.mgr;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;ii)display the who are not managers &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where ename not in(select distinct(m.ename) from emp e,emp m where m.empno=e.mgr);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;86) Display those employee whose name contains not less than 4 characters.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where length(ename)&amp;gt;4;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;87) Display those department whose name start with &amp;quot;S&amp;quot; while the location name ends with &amp;quot;K&amp;quot;.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select dname from dept where dname like &#39;S%&#39; and loc like &#39;%K&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;88) Display those employees whose manager name is JONES.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select p.ename from emp e,emp p where e.empno=p.mgr and e.ename=&#39;JONES&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;89) Display those employees whose salary is more than 3000 after giving 20% increment.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,sal from emp where (sal+sal*.2)&amp;gt;3000;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;90) Display all employees while their dept names;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,dname from emp,dept where emp.deptno=dept.deptno&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;91) Display ename who are working in sales dept.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where deptno=(select deptno from dept where dname=&#39;SALES&#39;);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;92) Display employee name,deptname,salary and comm for those sal in between 2000 to 5000 while location is chicago.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,dname,sal,comm from emp,dept where sal between 2000 and 5000 and loc=&#39;CHICAGO&#39; and emp.deptno=dept.deptno;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;93)Display those employees whose salary greter than his manager salary.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select p.ename from emp e,emp p where e.empno=p.mgr and p.sal&amp;gt;e.sal&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;94) Display those employees who are working in the same dept where his manager is work.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select p.ename from emp e,emp p where e.empno=p.mgr and p.deptno=e.deptno;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;95) Display those employees who are not working under any manager.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where mgr is null&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;96) Display grade and employees name for the dept no 10 or 30 but grade is not 4 while joined the company before 31-dec-82.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,grade from emp,salgrade where sal between losal and hisal and deptno in(10,30) and grade&amp;lt;&amp;gt;4 and hiredate&amp;lt;&#39;31-DEC-82&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;97) Update the salary of each employee by 10% increment who are not eligible for commission.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;update emp set sal=sal+sal*10/100 where comm is null;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;98) SELECT those employee who joined the company before 31-dec-82 while their dept location is newyork or Chicago.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;SELECT EMPNO,ENAME,HIREDATE,DNAME,LOC FROM EMP,DEPTWHERE (EMP.DEPTNO=DEPT.DEPTNO)AND HIREDATE &amp;lt;&#39;31-DEC-82&#39; AND DEPT.LOC IN(&#39;CHICAGO&#39;,&#39;NEW YORK&#39;);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;99) DISPLAY EMPLOYEE NAME,JOB,DEPARTMENT,LOCATION FOR ALL WHO ARE WORKING AS MANAGER?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,JOB,DNAME,LOCATION from emp,DEPT where mgr is not null;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;100) DISPLAY THOSE EMPLOYEES WHOSE MANAGER NAME IS JONES? --[AND ALSO DISPLAY THEIR MANAGER NAME]?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt; SELECT P.ENAME FROM EMP E, EMP P WHERE E.EMPNO=P.MGR AND E.ENAME=&#39;JONES&#39;;&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/05/oracle-interview-based-queries-part2.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>40</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-6100153416205553807</guid><pubDate>Mon, 09 May 2011 05:35:00 +0000</pubDate><atom:updated>2011-05-09T11:05:02.565+05:30</atom:updated><title>Oracle interview based Queries- 1</title><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;It is pre-assumed that one has Emp,Dept tables in their Database.All one needs to do is to copy and paste the following queries and execute them.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;1) Display the details of all employees&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;Select * from emp;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;2) Display the depart information from department table&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select * from dept;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;3) Display the name and job for all the employees&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,job from emp;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;4) Display the name and salary for all the employees&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,sal from emp;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;5) Display the employee no and totalsalary for all the employees&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select empno,ename,sal,comm, sal+nvl(comm,0) as&amp;quot;total salary&amp;quot; from &lt;/p&gt;  &lt;p&gt;emp&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;6) Display the employee name and annual salary for all employees.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename, 12*(sal+nvl(comm,0)) as &amp;quot;annual Sal&amp;quot; from emp&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;7) Display the names of all the employees who are working in depart number 10.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select emame from emp where deptno=10;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;8) Display the names of all the employees who are working as clerks and &lt;/strong&gt;&lt;strong&gt;drawing a salary more than 3000.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where job=&#39;CLERK&#39; and sal&amp;gt;3000;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;9) Display the employee number and name who are earning comm.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select empno,ename from emp where comm is not null;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;10) Display the employee number and name who do not earn any comm.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select empno,ename from emp where comm is null;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;11) Display the names of employees who are working as clerks,salesman or &lt;/strong&gt;&lt;strong&gt;analyst and drawing a salary more than 3000.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where job=&#39;CLERK&#39; OR JOB=&#39;SALESMAN&#39; &lt;/p&gt;  &lt;p&gt;OR JOB=&#39;ANALYST&#39; AND SAL&amp;gt;3000;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;12) Display the names of the employees who are working in the company for the past 5 years;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where to_char(sysdate,&#39;YYYY&#39;)-to_char(hiredate,&#39;YYYY&#39;)&amp;gt;=5;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;13) Display the list of employees who have joined the company before &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;30-JUN-90 or after 31-DEC-90.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;a)select ename from emp where hiredate &amp;lt; &#39;30-JUN-1990&#39; or hiredate &amp;gt; &lt;/p&gt;  &lt;p&gt;&#39;31-DEC-90&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;14) Display current Date.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select sysdate from dual;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;15) Display the list of all users in your database(use catalog table).&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select username from all_users;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;16) Display the names of all tables from current user;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select tname from tab;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;17) Display the name of the current user.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;show user&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;18) Display the names of employees working in depart number 10 or 20 or 40 or employees working as CLERKS,SALESMAN or ANALYST.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where deptno in(10,20,40) or job &lt;/p&gt;  &lt;p&gt;in(&#39;CLERKS&#39;,&#39;SALESMAN&#39;,&#39;ANALYST&#39;);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;19) Display the names of employees whose name starts with alaphabet S.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where ename like &#39;S%&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;20) Display the Employee names for employees whose name ends with alaphabet S.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where ename like &#39;%S&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;21) Display the names of employees whose names have second alphabet A in their names.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where ename like &#39;_A%&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;22) select the names of the employee whose names is exactly five characters in length.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where length(ename)=5;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;23) Display the names of the employee who are not working as MANAGERS.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where job not in(&#39;MANAGER&#39;);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;24) Display the names of the employee who are not working as SALESMAN OR CLERK OR ANALYST.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp where job not in (&#39;SALESMAN&#39;,&#39;CLERK&#39;,&#39;ANALYST&#39;);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;25) Display all rows from emp table.The system should wait after every &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;screen full of informaction.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;set pause on &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;26) Display the total number of employee working in the company.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select count(*) from emp;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;27) Display the total salary beiging paid to all employees.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select sum(sal) from emp;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;28) Display the maximum salary from emp table.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select max(sal) from emp;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;29) Display the minimum salary from emp table.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select min(sal) from emp;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;30) Display the average salary from emp table.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select avg(sal) from emp;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;31) Display the maximum salary being paid to CLERK.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select max(sal) from emp where job=&#39;CLERK&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;32) Display the maximum salary being paid to depart number 20.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select max(sal) from emp where deptno=20;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;33) Display the minimum salary being paid to any SALESMAN.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select min(sal) from emp where job=&#39;SALESMAN&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;34) Display the average salary drawn by MANAGERS.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select avg(sal) from emp where job=&#39;MANAGER&#39;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;35) Display the total salary drawn by ANALYST working in depart number 40.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select sum(sal) from emp where job=&#39;ANALYST&#39; and deptno=40;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;36) Display the names of the employee in order of salary i.e the name of &lt;/strong&gt;&lt;strong&gt;the employee earning lowest salary should salary appear first.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename from emp order by sal;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;37) Display the names of the employee in descending order of salary.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;a)select ename from emp order by sal desc;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;38) Display the names of the employee in order of employee name.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;a)select ename from emp order by ename;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;39) Display empno,ename,deptno,sal sort the output first base on name and within name by deptno and with in deptno by sal.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select empno,ename,deptno,sal from emp order by&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;40) Display the name of the employee along with their annual salary(sal*12).The name of the employee earning highest annual salary should apper first.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,sal*12 from emp order by sal desc;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;41) Display name,salary,hra,pf,da,total salary for each employee. The output should be in the order of total salary,hra 15% of salary,da 10% of salary,pf 5% salary,total salary will be(salary+hra+da)-pf.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select ename,sal,sal/100*15 as hra,sal/100*5 as pf,sal/100*10 as &lt;/p&gt;  &lt;p&gt;da, sal+sal/100*15+sal/100*10-sal/100*5 as total from emp; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;42) Display depart numbers and total number of employees working in each &lt;/strong&gt;&lt;strong&gt;department.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select deptno,count(deptno)from emp group by deptno;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;43) Display the various jobs and total number of employees within each job group.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select job,count(job)from emp group by job;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;44) Display the depart numbers and total salary for each department.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select deptno,sum(sal) from emp group by deptno;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;45) Display the depart numbers and max salary for each department.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select deptno,max(sal) from emp group by deptno;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;46) Display the various jobs and total salary for each job&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select job,sum(sal) from emp group by job;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;47) Display the various jobs and total salary for each job&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select job,min(sal) from emp group by job;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;48) Display the depart numbers with more than three employees in each dept.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select deptno,count(deptno) from emp group by deptno having &lt;/p&gt;  &lt;p&gt;count(*)&amp;gt;3;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;49) Display the various jobs along with total salary for each of the jobs &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;where total salary is greater than 40000.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select job,sum(sal) from emp group by job having sum(sal)&amp;gt;40000;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;50) Display the various jobs along with total number of employees in each &lt;/strong&gt;&lt;strong&gt;job.The output should contain only those jobs with more than three employees.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt;select job,count(empno) from emp group by job having count(job)&amp;gt;3&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/05/oracle-interview-based-queries-1.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>106</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-8413506607843838344</guid><pubDate>Thu, 05 May 2011 12:59:00 +0000</pubDate><atom:updated>2011-05-05T18:29:50.197+05:30</atom:updated><title>Oracle Query Tuning</title><description>&lt;p&gt;If query taking long time then First will run the query in Explain Plan, The explain plan process stores data in the PLAN_TABLE.&lt;/p&gt;  &lt;p&gt;it will give us execution plan of the query like whether the query is using the relevant indexes on the joining columns or indexes to support the query are missing.&lt;/p&gt;  &lt;p&gt;If joining columns doesn’t have index then it will do the full table scan if it is full table scan the cost will be more then will create the indexes on the joining columns and will run the query it should give better performance and also needs to analyze the tables if analyzation happened long back. The ANALYZE statement can be used to gather statistics for a specific table, index or cluster using&lt;/p&gt;  &lt;p&gt;ANALYZE TABLE employees COMPUTE STATISTICS;&lt;/p&gt;  &lt;p&gt;If still have performance issue then will use HINTS, hint is nothing but a clue. We can use hints like&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;ALL_ROWS     &lt;br /&gt;One of the hints that &#39;invokes&#39; the &lt;a href=&quot;http://www.adp-gmbh.ch/ora/tuning/cbo/index.html&quot;&gt;Cost based optimizer&lt;/a&gt;      &lt;br /&gt;ALL_ROWS is usually used for &lt;i&gt;batch processing&lt;/i&gt; or &lt;i&gt;data warehousing&lt;/i&gt; systems. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;(/*+ ALL_ROWS */)&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;FIRST_ROWS     &lt;br /&gt;One of the hints that &#39;invokes&#39; the &lt;a href=&quot;http://www.adp-gmbh.ch/ora/tuning/cbo/index.html&quot;&gt;Cost based optimizer&lt;/a&gt;      &lt;br /&gt;FIRST_ROWS is usually used for &lt;i&gt;OLTP&lt;/i&gt; systems. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;(/*+ FIRST_ROWS */)&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;CHOOSE One of the hints that &#39;invokes&#39; the &lt;a href=&quot;http://www.adp-gmbh.ch/ora/tuning/cbo/index.html&quot;&gt;Cost based optimizer&lt;/a&gt;      &lt;br /&gt;This hint lets the server choose (between ALL_ROWS and FIRST_ROWS, based on &lt;a href=&quot;http://www.adp-gmbh.ch/ora/tuning/statistics.html&quot;&gt;statistics gathered&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;HASH Hashes one table (full scan) and creates a hash index for that table. Then hashes other table and uses hash index to find corresponding records. Therefore not suitable for &amp;lt; or &amp;gt; join conditions.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;/*+ use_hash */&lt;/p&gt;  &lt;p&gt;Hints are most useful to optimize the query performance.&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/05/oracle-query-tuning.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-1209949212201465789</guid><pubDate>Wed, 23 Mar 2011 14:25:00 +0000</pubDate><atom:updated>2011-03-23T19:55:46.704+05:30</atom:updated><title>Explain Plan</title><description>&lt;p&gt;&lt;font color=&quot;#0000ff&quot;&gt;&lt;strong&gt;Explain Plan :&lt;/strong&gt;&lt;/font&gt; It is a statement that allows you to have oracle generate execution plan for any sql statement with out actually executing it.You will be able to examine the execution plan by querying the plan table.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;Plan Table :&lt;/font&gt;&lt;/strong&gt; A Plan table holds execution plans generated by the explain plan statements.Create Plan table by&amp;#160; running utlxplan.sql located in &lt;/p&gt;  &lt;p&gt;$oracle_home/rdbms/admin.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Explain Plan Syntax :&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Explain plan [set statement_id=&amp;lt;string in single quotes&amp;gt;]&lt;/p&gt;  &lt;p&gt;[into &amp;lt;plan table name&amp;gt;]&lt;/p&gt;  &lt;p&gt;for &lt;/p&gt;  &lt;p&gt;&amp;lt;sql statements&amp;gt;;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Example to Explain Plan:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Sql&amp;gt;&lt;/strong&gt; Explain plan set statement_id=’demo’ for&lt;/p&gt;  &lt;p&gt;select a.customer_name,a.customer_number,b.invoice_number,&lt;/p&gt;  &lt;p&gt;b.invoice_type,b.invoice_data,b.total_amount,&lt;/p&gt;  &lt;p&gt;c.line_number,c.part_number,c.quantity,c.unit_cost&lt;/p&gt;  &lt;p&gt;from customers a, invoices b, invoice_items c&lt;/p&gt;  &lt;p&gt;where c.invoice_id=:b1&lt;/p&gt;  &lt;p&gt;and c.line_number=:b2&lt;/p&gt;  &lt;p&gt;and b.invoice_id=c.invoice_id&lt;/p&gt;  &lt;p&gt;and a.customer_id=b.customer_id&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Sql&amp;gt; &lt;/strong&gt;@ explain.sql&lt;/p&gt;  &lt;p&gt;Enter statement_id : Demo&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/explain-plan.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-6928710211129369283</guid><pubDate>Wed, 23 Mar 2011 14:09:00 +0000</pubDate><atom:updated>2011-03-23T19:44:04.160+05:30</atom:updated><title>SQL Loader</title><description>&lt;p&gt;SQL*Loader (&lt;i&gt;sqlldr&lt;/i&gt; ) is the utility to use for high performance data loads. The data can be loaded from any text file and inserted into the database.&lt;/p&gt;  &lt;p&gt;During processing, SQL*Loader writes messages to the log file, bad rows to the bad file, and discarded rows to the discard file.&lt;/p&gt;  &lt;p&gt;&lt;a name=&quot;_Toc41060427&quot;&gt;&lt;strong&gt;The Control File&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The SQL*Loader control file contains information that describes how the data will be loaded. It contains the table name, column data types, field delimiters, etc. It simply provides the guts for all SQL*Loader processing.&lt;/p&gt;  &lt;p&gt;&lt;a name=&quot;_Toc41060429&quot;&gt;&lt;strong&gt;SQL*Loader Options&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;SQL*Loader provides the following options, which can be specified either on the command line or within a parameter file:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;bad&lt;/i&gt;&lt;/b&gt; – A file that is created when at least one record from the input file is rejected. The rejected data records are placed in this file. A record could be rejected for many reasons, including a non-unique key or a required column being null.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;bindsize&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– [256000] The size of the bind array in bytes.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;columnarrayrows&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– [5000] Specifies the number of rows to allocate for direct path column arrays.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;control&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– The name of the control file. This file specifies the format of the data to be loaded.&lt;/li&gt;    &lt;li&gt;&lt;b&gt;&lt;i&gt;data&lt;/i&gt;&lt;/b&gt; – The name of the file that contains the data to load.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;direct&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– [FALSE] Specifies whether or not to use a direct path load or conventional.&lt;/li&gt;    &lt;li&gt;&lt;b&gt;&lt;i&gt;discard&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– The name of the file that contains the discarded rows. Discarded rows are those that fail the WHEN clause condition when selectively loading records. &lt;/li&gt;    &lt;li&gt;&amp;#160; &lt;b&gt;&lt;i&gt;discardmax&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– [ALL] The maximum number of discards to allow. &lt;/li&gt;    &lt;li&gt;&lt;b&gt;&lt;i&gt;errors&lt;/i&gt;&lt;/b&gt; – [50] The number of errors to allow on the load.&lt;/li&gt;    &lt;li&gt;&lt;b&gt;&lt;i&gt;external_table&lt;/i&gt;&lt;/b&gt;&lt;b&gt; –&lt;/b&gt; [NOT_USED] Determines whether or not any data will be loaded using external tables. The other valid options include GENERATE_ONLY and EXECUTE.&lt;/li&gt;    &lt;li&gt;&lt;b&gt;&lt;i&gt;file&lt;/i&gt;&lt;/b&gt; – Used only with parallel loads, this parameter specifies the file to allocate extents from.&lt;/li&gt;    &lt;li&gt;&lt;b&gt;&lt;i&gt;load&lt;/i&gt;&lt;/b&gt; – [ALL] The number of logical records to load.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;log&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– The name of the file used by SQL*Loader to log results.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;multithreading&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– The default is TRUE on multiple CPU systems and FALSE on single CPU systems.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;strong&gt;&lt;em&gt;parfile&lt;/em&gt;&lt;/strong&gt; – [Y] The name of the file that contains the parameter options for SQL*Loader.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;parallel&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– [FALSE] Specifies a filename that contains index creation statements. &lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;readsize&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– The size of the buffer used by SQL*Loader when reading data from the input file. This value should match that of &lt;i&gt;bindsize&lt;/i&gt;.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;resumable&lt;/i&gt;&lt;/b&gt; – [N] Enables and disables resumable space allocation. When “Y”, the parameters &lt;i&gt;resumable_name&lt;/i&gt; and &lt;i&gt;resumable_timeout&lt;/i&gt; are utilized.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;resumable_name&lt;/i&gt;&lt;/b&gt; – User defined string that helps identify a resumable statement that has been suspended. This parameter is ignored unless &lt;i&gt;resumable&lt;/i&gt;&lt;i&gt; = Y&lt;/i&gt;. &lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;resumable_timeout&lt;/i&gt;&lt;/b&gt; – [7200 seconds] The time period in which an error must be fixed. This parameter is ignored unless &lt;i&gt;resumable&lt;/i&gt;&lt;i&gt; = Y&lt;/i&gt;. &lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;rows&lt;/i&gt;&lt;/b&gt; – [64] The number of rows to load before a&lt;i&gt; commit &lt;/i&gt;is issued (conventional path only). For direct path loads, rows are the number of rows to read from the data file before saving the data in the datafiles.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;silent&lt;/i&gt;&lt;/b&gt; – Suppress errors during data load. A value of ALL will suppress all load messages. Other options include DISCARDS, ERRORS, FEEDBACK, HEADER, and PARTITIONS. &lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;skip&lt;/i&gt;&lt;/b&gt; – [0] Allows the skipping of the specified number of logical records.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;skip_unusable_indexes&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– [FALSE] Determines whether SQL*Loader skips the building of indexes that are in an unusable state. &lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;skip_index_maintenance&lt;/i&gt;&lt;/b&gt; – [FALSE] Stops index maintenance for direct path loads only.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;streamsize&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– [256000] Specifies the size of direct path streams in bytes.&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;&lt;i&gt;userid&lt;/i&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;– The Oracle username and password.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;To drive the concept of the SQL loader home.., let us assume that we have text file which is the usual emp table.&lt;/li&gt;    &lt;li&gt;Now open the cmd prompt and type sqlldr.&lt;/li&gt;    &lt;li&gt;later the things should look some thing like this.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;font color=&quot;#000000&quot;&gt;sqlldr# userid=username/pwd@ora&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;load data&lt;/p&gt;  &lt;p&gt;in file ‘C:\emp.txt’&lt;/p&gt;  &lt;p&gt;into table emp_oracle&lt;/p&gt;  &lt;p&gt;fields terminated by “,”optionally enclosed by’ ” ’&lt;/p&gt;  &lt;p&gt;(empno,ename,job,mgr,sal,comm,deptno)&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/sql-loader.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-7302364228803631406</guid><pubDate>Wed, 23 Mar 2011 13:54:00 +0000</pubDate><atom:updated>2011-03-23T19:26:52.925+05:30</atom:updated><title>Materialized view</title><description>&lt;p&gt;A materialized view is a &lt;a href=&quot;http://en.wikipedia.org/wiki/Schema_object&quot;&gt;database object&lt;/a&gt; that contains the results of a &lt;a href=&quot;http://en.wikipedia.org/wiki/Query&quot;&gt;query&lt;/a&gt;. They are local copies of data located remotely, or are used to create summary &lt;a href=&quot;http://en.wikipedia.org/wiki/Table_%28database%29&quot;&gt;tables&lt;/a&gt; based on aggregations of a table&#39;s data.Materialized view and the Query rewrite feature is added from ORACLE 8i.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;A materialized view log&lt;/b&gt; is a schema object that records changes to a master table&#39;s data so that a materialized view defined on the master table can be refreshed incrementally.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;If you delete any record from your Materialized view it is goanna impact your Source table once it is refreshed.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Examples to the Simple Materialized view’s is given below .&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#ff0000&quot;&gt;Eg 1 :&lt;/font&gt;&lt;/strong&gt;Create materialized_view MV refresh as select * from emp;&lt;/p&gt;  &lt;p&gt;Execute dbms_mview.refresh(‘MV’);&lt;/p&gt;  &lt;p&gt;&lt;font color=&quot;#ff0000&quot;&gt;&lt;strong&gt;Refresh Complete :&lt;/strong&gt; &lt;/font&gt;To perform a &lt;strong&gt;complete refresh&lt;/strong&gt; of a materialized view, the server that manages the materialized view executes the materialized view&#39;s defining query, which essentially recreates the materialized view. To refresh the materialized view, the result set of the query replaces the existing materialized view data. Oracle can perform a complete refresh for any materialized view. Depending on the amount of data that satisfies the defining query, a complete refresh can take a substantially longer amount of time to perform than a fast refresh.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Create Materialized_view MV Refresh complete as select * from emp;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;execute DBMS_mview.refresh(List=&amp;gt;’MV’,Method=&amp;gt;’c’);&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color=&quot;#ff0000&quot;&gt;&lt;strong&gt;Refresh Fast :&lt;/strong&gt;&lt;/font&gt;To perform a &lt;strong&gt;fast refresh&lt;/strong&gt;, the master that manages the materialized view first identifies the changes that occurred in the master since the most recent refresh of the materialized view and then applies these changes to the materialized view. Fast refreshes are more efficient than complete refreshes when there are few changes to the master because the participating server and network replicate a smaller amount of data.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Create Materialized_view MV Refresh fast as select * from emp;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;execute DBMS_mview.refresh(list=&amp;gt;’MV’,Method=&amp;gt;’F’);&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#ff0000&quot;&gt;Primary Key Materialized Views :&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The following statement creates the primary-key materialized view on the table emp located on a remote database.&lt;/p&gt;  &lt;pre&gt;SQL&amp;gt; 	CREATE MATERIALIZED VIEW mv_emp_pk&lt;br /&gt;   	REFRESH FAST START WITH SYSDATE &lt;br /&gt;	NEXT  SYSDATE + 1/48&lt;br /&gt;	WITH PRIMARY KEY &lt;br /&gt;	AS SELECT * FROM &lt;a href=&quot;mailto:emp@remote_db;&quot;&gt;emp@remote_db;&lt;br /&gt;&lt;/a&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Note: When you create a materialized view using the FAST option you will need to create a view log on the master tables(s) as shown below:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;SQL&amp;gt; CREATE MATERIALIZED VIEW LOG ON emp;Materialized view log created.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#ff0000&quot;&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#ff0000&quot;&gt;Rowid Materialized Views :&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The following statement creates the rowid materialized view on table emp located on a remote database:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;SQL&amp;gt;CREATE MATERIALIZED VIEW mv_emp_rowid REFRESH WITH ROWID&amp;#160;&amp;#160;&amp;#160; AS SELECT * FROM &lt;a href=&quot;mailto:emp@remote_db&quot;&gt;emp@remote_db&lt;/a&gt;; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Materialized view log created.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;font color=&quot;#ff0000&quot;&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;font color=&quot;#ff0000&quot;&gt;&lt;strong&gt;Creating Materialized Aggregate Views :&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;CREATE MATERIALIZED VIEW sales_mv BUILD IMMEDIATE REFRESH FAST ON COMMIT AS SELECT t.calendar_year, p.prod_id, SUM(s.amount_sold) AS sum_sales FROM times t, products p, sales s WHERE t.time_id = s.time_id AND p.prod_id = s.prod_id GROUP BY t.calendar_year, p.prod_id;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#ff0000&quot;&gt;Creating Materialized Join Views :&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;CREATE MATERIALIZED VIEW sales_by_month_by_state&lt;br /&gt;     TABLESPACE example&lt;br /&gt;     PARALLEL 4&lt;br /&gt;     BUILD IMMEDIATE&lt;br /&gt;     REFRESH COMPLETE&lt;br /&gt;     ENABLE QUERY REWRITE&lt;br /&gt;     AS SELECT t.calendar_month_desc, c.cust_state_province,&lt;br /&gt;        SUM(s.amount_sold) AS sum_sales&lt;br /&gt;        FROM times t, sales s, customers c&lt;br /&gt;        WHERE s.time_id = t.time_id AND s.cust_id = c.cust_id&lt;br /&gt;        GROUP BY t.calendar_month_desc, c.cust_state_province;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#ff0000&quot;&gt;Periodic Refresh of Materialized Views: &lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;CREATE MATERIALIZED VIEW emp_data &lt;br /&gt;   PCTFREE 5 PCTUSED 60 &lt;br /&gt;   TABLESPACE example &lt;br /&gt;   STORAGE (INITIAL 50K NEXT 50K)&lt;br /&gt;   REFRESH FAST NEXT sysdate + 7 &lt;br /&gt;   AS SELECT * FROM employees; &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#ff0000&quot;&gt;Automatic Refresh Times for Materialized Views&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;CREATE MATERIALIZED VIEW all_customers&lt;br /&gt;   PCTFREE 5 PCTUSED 60 &lt;br /&gt;   TABLESPACE example &lt;br /&gt;   STORAGE (INITIAL 50K NEXT 50K) &lt;br /&gt;   USING INDEX STORAGE (INITIAL 25K NEXT 25K)&lt;br /&gt;   REFRESH START WITH ROUND(SYSDATE + 1) + 11/24 &lt;br /&gt;   NEXT NEXT_DAY(TRUNC(SYSDATE), &#39;MONDAY&#39;) + 15/24 &lt;br /&gt;   AS SELECT * FROM sh.customers@remote &lt;br /&gt;         UNION&lt;br /&gt;      SELECT * FROM sh.customers@local;&lt;/pre&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/materialized-view.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-7364576358463853761</guid><pubDate>Wed, 23 Mar 2011 13:06:00 +0000</pubDate><atom:updated>2011-03-23T18:36:54.366+05:30</atom:updated><title>Function</title><description>&lt;p&gt;A function is a sub program that executes a set of statements and returns a value to the main program. The basic difference between Functions and procedures is, function returns a value to the calling program or main program where as a procedure does not return any value&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Syntax of A Function&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Create [ Or Replace ] Function &amp;lt; Function Name &amp;gt; ( Argument1 [Mode] &amp;lt;Data type&amp;gt;, Argument2 [Mode] &amp;lt;data type&amp;gt;,---) return data type&lt;/p&gt;  &lt;p&gt;Is / As&lt;/p&gt;  &lt;p&gt;Local Variables Declarations;&lt;/p&gt;  &lt;p&gt;------------&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;Executable Statements;&lt;/p&gt;  &lt;p&gt;---------------&lt;/p&gt;  &lt;p&gt;[Exception &lt;/p&gt;  &lt;p&gt;Exception Handling; ]&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : 1&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Write a program to find sum of two numbers&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Sol :&lt;/p&gt;  &lt;p&gt;ed function1&lt;/p&gt;  &lt;p&gt;Create or replace function addition(x integer, y integer) return integer &lt;/p&gt;  &lt;p&gt;is &lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;Return x+y;&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;b&gt;mainfunction1&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Declare&lt;/p&gt;  &lt;p&gt;A integer :=&amp;amp;a;&lt;/p&gt;  &lt;p&gt;B integer :=&amp;amp;b;&lt;/p&gt;  &lt;p&gt;C integer;&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;C:=addition(a,b);&lt;/p&gt;  &lt;p&gt;Dbms_output.put_line(&#39;sum of two numbers is &#39;|| c );&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Example : 2&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;wirte a function which accepts item number and quantity and then return the total amount by fetching rate from the item table&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create or replace function itembill(q in number,r in number) return number &lt;/p&gt;  &lt;p&gt;is&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;return q*r;&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ed mainfunction2&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;declare&lt;/p&gt;  &lt;p&gt;q1 number:=&amp;amp;quantity;&lt;/p&gt;  &lt;p&gt;r1 number;&lt;/p&gt;  &lt;p&gt;res number;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;select rate into r1 from item where itno=&amp;amp;itno;&lt;/p&gt;  &lt;p&gt;res:=itembill(q1,r1);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39; Total Amount is &#39;||res);&lt;/p&gt;  &lt;p&gt;exception&lt;/p&gt;  &lt;p&gt;when no_data_found then&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39; No Such Item Exits&#39;);&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/function.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-8504047649137484065</guid><pubDate>Wed, 23 Mar 2011 13:05:00 +0000</pubDate><atom:updated>2011-03-23T18:35:05.976+05:30</atom:updated><title>Stored Procedure</title><description>&lt;p&gt;A procedure is a set of instructions(usually combining sql and plsql commands) saved for calling and repeated execution&lt;/p&gt;  &lt;p&gt;A procedure is a sub program that performs a specific action. A procedure can be called from any pl/sql block. A procedure has two parts one is &lt;b&gt;Specification &lt;/b&gt;and Other is &lt;b&gt;Body&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The &lt;b&gt;procedure specification &lt;/b&gt;begins with the keyword procedure followed by procedure name and an option list of arguments enclosed with in the parenthesis. &lt;/p&gt;  &lt;p&gt;The &lt;b&gt;procedure body &lt;/b&gt;begins with &lt;b&gt;is &lt;/b&gt;or &lt;b&gt;as &lt;/b&gt;and ends with an end statement.&lt;/p&gt;  &lt;p&gt;It consists of three parts&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;&lt;b&gt;Declarative Part&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;&lt;b&gt;Execution Part&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;3. &lt;/b&gt;&lt;b&gt;Exception Handling Part&lt;/b&gt;&lt;/p&gt;  &lt;h3&gt;Syntax of Procedure&lt;/h3&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Create [ Or Replace ] Procedure &amp;lt; Procedure Name &amp;gt; ( Argument1 [Mode] &amp;lt;Data type&amp;gt;, Argument2 [Mode] &amp;lt;data type&amp;gt;,---)&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Is / As&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Local Variables Declarations;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;------------&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Begin&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Executable Statements;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;---------------&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;[Exception &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Exception Handling; ]&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;End;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;/&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;where Mode refers to the type of arguments such as &lt;b&gt;In , Out &lt;/b&gt;or &lt;b&gt;InOut&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Modes In Procedures And Functions&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;IN : &lt;/b&gt;the In parameter lets the user to pass values to the called sub programs with in the sub program, the IN parameter acts like a constant. There fore it cannot be modified.&lt;/li&gt; &lt;/ol&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;OUT : &lt;/b&gt;The Out parameter lets the user returned values to the calling block. Inside the sub program the out parameter acts like a un initialized variable. Its value can not be assigned to another variable or itself&lt;b&gt;&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;INOUT : &lt;/b&gt;the INOUT parameter lets the user pass initial values to the called sub program and return updated values to the calling block &lt;b&gt;&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;the default mode of an argument is &amp;quot;IN&amp;quot; &lt;/p&gt;  &lt;p&gt;Show Errors&lt;b&gt; : &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;This Command is used to show the errors that are occurred during the procedure creation&lt;/p&gt;  &lt;p&gt;Ex : when ever we create a procedure and executed, if it show procedure created with compilation errors, the we can see those errors using the following statement&lt;/p&gt;  &lt;p&gt;&lt;b&gt;show Errors&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Example :1&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Write a procedure to show a simple message&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Sol. &lt;b&gt;Steps&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Write the sub Program&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Ed subprocedure1&lt;/p&gt;  &lt;p&gt;Create or replace procedure disp is &lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;Dbms_output.put_line(&#39;This is a Sub Program&#39;);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Compile the above procedure to find errors in the procedure, with the following statement&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;@ subprocedure1&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Write the main program&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;Dbms_output.put_line(&#39;This is Main Program&#39;);&lt;/p&gt;  &lt;p&gt;Disp;&lt;/p&gt;  &lt;p&gt;Dbms_output.put_line(&#39;Again Continuing the Main Program&#39;);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Run the above main procedure with the following statement&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;@mainprogram1&lt;/p&gt;  &lt;p&gt;&lt;b&gt;output :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt; @ mainprogram1&lt;/p&gt;  &lt;p&gt;This is Main Program&lt;/p&gt;  &lt;p&gt;This is a Sub Program&lt;/p&gt;  &lt;p&gt;Again Continuing the Main Program&lt;/p&gt;  &lt;p&gt;PL/SQL procedure successfully completed.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Example :2 &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Write a procedure to show a message when updated any row in a particular table&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Sol. &lt;b&gt;Steps&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Write the sub Program&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Ed subprocedure2&lt;/p&gt;  &lt;p&gt;Create or replace procedure upd(d number)&lt;/p&gt;  &lt;p&gt;is &lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;update employee set sal=sal+(sal*0.2) where empno=d;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(sql%rowcount||&#39; Record Updated...&#39;);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Compile the above procedure to find errors in the procedure, with the following statement&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;@ subprocedure2&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Write the main program&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;upd(&amp;amp;empno);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Run the above main procedure with the following statement&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;@mainprogram2&lt;/p&gt;  &lt;p&gt;&lt;b&gt;output :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt; @ mainprogram2&lt;/p&gt;  &lt;p&gt;Enter value for empno: 7369&lt;/p&gt;  &lt;p&gt;old 2: upd(&amp;amp;empno);&lt;/p&gt;  &lt;p&gt;new 2: upd(7369);&lt;/p&gt;  &lt;p&gt;1 Record Updated...&lt;/p&gt;  &lt;p&gt;PL/SQL procedure successfully completed.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example 3:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create a procedure which adds the given three numbers using “in” and “out” parameters&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Sol. &lt;b&gt;Steps&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Write the sub Program&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Ed subprocedure3&lt;/p&gt;  &lt;p&gt;Create or replace procedure sum_numbers(n1 in number, n2 in &lt;/p&gt;  &lt;p&gt;number, res out number)&lt;/p&gt;  &lt;p&gt;is &lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;res:=n1+n2;&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Compile the above procedure to find errors in the procedure, with the following statement&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;@ subprocedure3&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Write the main program&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Declare&lt;/p&gt;  &lt;p&gt;a number;&lt;/p&gt;  &lt;p&gt;b number;&lt;/p&gt;  &lt;p&gt;c number;&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;a:=&amp;amp;a;&lt;/p&gt;  &lt;p&gt;b:=&amp;amp;b;&lt;/p&gt;  &lt;p&gt;sum_numbers(a,b,c);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;sum of three numbers is &#39;||c);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Run the above main procedure with the following statement&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;@mainprogram3&lt;/p&gt;  &lt;p&gt;&lt;b&gt;output :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt; @ mainprogram3&lt;/p&gt;  &lt;p&gt;Enter value for a: 10&lt;/p&gt;  &lt;p&gt;old 6: a:=&amp;amp;a;&lt;/p&gt;  &lt;p&gt;new 6: a:=10;&lt;/p&gt;  &lt;p&gt;Enter value for b: 20&lt;/p&gt;  &lt;p&gt;old 7: b:=&amp;amp;b;&lt;/p&gt;  &lt;p&gt;new 7: b:=20;&lt;/p&gt;  &lt;p&gt;sum of three numbers is30&lt;/p&gt;  &lt;p&gt;PL/SQL procedure successfully completed.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example 4:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create a procedure which Updates the sal with the increment value that you give according to the given emp number &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Sol. &lt;b&gt;Steps&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Write the sub Program&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Ed subprocedure4&lt;/p&gt;  &lt;p&gt;Create or replace procedure incr(eno employee.empno%type,s &lt;/p&gt;  &lt;p&gt;out number, i number)&lt;/p&gt;  &lt;p&gt;Is&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;Update employee set sal=sal+i where empno=eno;&lt;/p&gt;  &lt;p&gt;Dbms_output.put_line(&#39;Record Updated----&#39;);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Compile the above procedure to find errors in the procedure, with the following statement&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;@ subprocedure4&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Write the main program&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Declare&lt;/p&gt;  &lt;p&gt;e number:=&amp;amp;empno;&lt;/p&gt;  &lt;p&gt;Incr_value number:=&amp;amp;increment;&lt;/p&gt;  &lt;p&gt;s employee.sal%type;&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;Select sal into s from employee where empno=e;&lt;/p&gt;  &lt;p&gt;Incr(e,s,incr_value);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;Next, Run the above main procedure with the following statement&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;@mainprogram4&lt;/p&gt;  &lt;p&gt;&lt;b&gt;output :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SQL&amp;gt; @ mainprogram4&lt;/p&gt;  &lt;p&gt;Enter value for empno: 7369&lt;/p&gt;  &lt;p&gt;old 2: e number:=&amp;amp;empno;&lt;/p&gt;  &lt;p&gt;new 2: e number:=7369;&lt;/p&gt;  &lt;p&gt;Enter value for increment: 48&lt;/p&gt;  &lt;p&gt;old 3: incr_value number:=&amp;amp;increment;&lt;/p&gt;  &lt;p&gt;new 3: incr_value number:=48;&lt;/p&gt;  &lt;p&gt;Record Updated----&lt;/p&gt;  &lt;p&gt;PL/SQL procedure successfully completed.&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/stored-procedure.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-7220178165331756700</guid><pubDate>Wed, 23 Mar 2011 13:02:00 +0000</pubDate><atom:updated>2011-03-23T18:32:25.133+05:30</atom:updated><title>Trigger</title><description>&lt;p&gt;Trigger is a database object ie used to execute an action basing on an even. Triggers are event-based programs, which are executed when an event occurs or raises or fires&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Types of Triggers&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Trigger Type is defined by the type of triggering transaction and by the level at which the trigger is executed&lt;/p&gt;  &lt;p&gt;Triggers Are Of Two Types&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. Row Level Triggers&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. Statement Level Triggers&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;Row Level Triggers&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A row trigger is fired each time a row in the table is affected by the triggering statement. For example, if an UPDATE statement updates multiple rows of a table, a row trigger is fired once for each row affected by the update statement. If the triggering statement affects no rows, the trigger is not executed at all. Row triggers should be used when some processing is required whenever a triggering statement affects a single row in a table.&lt;/p&gt;  &lt;p&gt;Row level triggers are created using the &amp;quot;For Each Row&amp;quot; Clause in the Create Trigger Command&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;Statement triggers&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A statement trigger is fried once on behalf of the triggering statement, independent of the number of rows the triggering statement affects (even if not rows are affected) statement triggers should be used when a triggering statement affects rows in a table but the processing required is completely independent of the number of rows affected &lt;/p&gt;  &lt;p&gt;Statement level triggers are the default type of trigger created via Create Trigger Command&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;create or replace trigger &amp;lt;trigger_name&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;{before/after/instead of}&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;{insert/update/delete}&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;[ of &amp;lt;column name&amp;gt; on &amp;lt;table name&amp;gt; ]&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;[ for each row [when &amp;lt;condition&amp;gt;] ]&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Declare&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;variables declarations&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;-----------------&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;begin&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Executable statements&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;-------------&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Exception&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Exception statements&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;---------------&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;end;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;/&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Syntax Explanation :&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;or replace : &lt;/b&gt;Recreates the trigger if it already exists. this option can be used to change the definition of an existing trigger without requiring the user to drop the trigger first&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Trigger Name : &lt;/b&gt;is the name of the trigger to be created&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Before : &lt;/b&gt;Indicates that oracle fires the trigger before executing the trigger statement&lt;/p&gt;  &lt;p&gt;&lt;b&gt;After : &lt;/b&gt;Indicates that oracle fires the trigger After executing the trigger statement&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Insert : &lt;/b&gt;Indicates that oracle fires the trigger whenever an INSERT statement adds a row to a table.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Delete : &lt;/b&gt;Indicates that oracle fires the trigger whenever a DELETE statement removes a row from the table.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Update : &lt;/b&gt;Indicates that oracle fires the trigger whenever an UPDATE statement changes a value in one of the columns specified in the OF clause. if the OF clause is omitted, the oracle fires the trigger whenever an UPDATE statement changes a value in any column of the table.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;for Each Row : &lt;/b&gt;Designates the trigger to be a row trigger. the oracle engine fires a row trigger once for each row that is affected by the triggering statement and meets the optional trigger constraint defined in the When clause. if this clause is omitted the trigger is a statement trigger.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;When : &lt;/b&gt;specifies the trigger restriction. the trigger restriction contains a SQL condition that must be satisfied for the oracle to fire the trigger.&lt;/p&gt;  &lt;p&gt;Basing on the above &lt;b&gt;2 types of triggers, &lt;/b&gt;they are &lt;b&gt;further classified into 3 types&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. DML Triggers&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. DDL Triggers and&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;3. Instead of Triggers&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;1. DML Triggers&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;These triggers are executed before or after. we apply any dml operations on a table.&lt;/p&gt;  &lt;p&gt;When we create a table. the trigger definition is stored in the database, which is identified with the trigger name. the code in the trigger is processed when we apply any command on the database or table&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Examples :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#ff0000&quot;&gt;Steps for Creating a Trigger&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;1. First Create a trigger, next set the server on with the following statement (set Serveroutput on)&lt;/p&gt;  &lt;p&gt;2. Run that Trigger with the following statement&lt;/p&gt;  &lt;p&gt;@ &amp;lt;trigger name&amp;gt;&lt;/p&gt;  &lt;p&gt;3. perform some action (ie either insert or update or delete etc)&lt;/p&gt;  &lt;p&gt;&lt;font color=&quot;#ff0000&quot;&gt;&lt;strong&gt;Statement Level Triggers&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;1. &lt;strong&gt;Create A Trigger, Which Displays A Message When Ever You Insert A New Row In To Sample1 Table&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;Create or replace trigger instrig1 before insert on sample1 &lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;one record inserted successfully.....&#39;);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;2. &lt;strong&gt;Create A Trigger, Which Displays A Message When Ever You Update An Existing Row In The Table Sample1&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Create or replace trigger updtrig1 before update on sample1 &lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;one record updated successfully.....&#39;);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;Create A Trigger, Which Displays A Message When Ever You Delete A Row From The Table Sample1&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Create or replace trigger deltrig1 before delete on sample1 &lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;record(s) deleted successfully.....&#39;);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;Row Level Triggers&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;1. &lt;strong&gt;Create A Trigger, Which Displays A Message When Ever You Insert A New Row Into A Table Sample1&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Create or replace trigger instrig2 before insert on sample1 &lt;/p&gt;  &lt;p&gt;for each row&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(:new.sno||&#39; record inserted successfully.....&#39;);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;Create a trigger, which displays a message when ever you update a row in the table sample1&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Create or replace trigger updtrig2 before update on sample1 for each row&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(:old.sno||&#39; record updated to &#39;||:new.sno);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;Create A Trigger, Which Displays A Message When Ever You Delete A Row From The Table Sample1&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Create or replace trigger deltrig2 after delete on sample1 for each row&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(:old.sno||&#39; record deleted successfully.....&#39;);&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;h6&gt;&lt;font color=&quot;#0000ff&quot;&gt;DDL TRIGGERS&lt;/font&gt;&lt;/h6&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;Create A Trigger, Which Displays An Error Message When Ever You Create a New Table which starts with Letter ‘A’&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Create or replace trigger ctrig1 before create on scott.schema&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;if dictionary_obj_name like &#39;a%&#39; then&lt;/p&gt;  &lt;p&gt;raise_application_error(-20001,&#39;object name can not start with a&#39;);&lt;/p&gt;  &lt;p&gt;End if;&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;Create A Trigger, Which Displays An Error Message When Ever You try to drop any Table &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Create or replace trigger prevent_drop after drop on scott.schema&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;if ora_dict_obj_type=&#39;table&#39; then&lt;/p&gt;  &lt;p&gt;raise_application_error(-20001,&#39;object can not be dropped&#39;);&lt;/p&gt;  &lt;p&gt;End if;&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;Create A Trigger, Which Displays An Error Message When Ever You try to Alter any Table &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;create or replace trigger prevent_alter before alter on scott.schema&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;if ora_dict_obj_type=&#39;TABLE&#39; then&lt;/p&gt;  &lt;p&gt;Raise_Application_Error(-20001,&#39;Object Can not be altered&#39;);&lt;/p&gt;  &lt;p&gt;end if;&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;h6&gt;&lt;font color=&quot;#0000ff&quot;&gt;INSTEAD OF TRIGGER&lt;/font&gt;&lt;/h6&gt;  &lt;p&gt;&lt;b&gt;Create a trigger, which inserts the given values in to the relevant table through a Composite view&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Create or replace trigger instrig instead of &lt;/p&gt;  &lt;p&gt;Insert on empdept for each row&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;if :new.deptno is not null and :new.dname is not null then&lt;/p&gt;  &lt;p&gt;insert into department(deptno,dname) values (:new.deptno,:new.dname);&lt;/p&gt;  &lt;p&gt;end if;&lt;/p&gt;  &lt;p&gt;if :new.empno is not null and :new.ename is not null then&lt;/p&gt;  &lt;p&gt;insert into employee(empno,ename) values (:new.empno,:new.ename);&lt;/p&gt;  &lt;p&gt;end if;&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/trigger.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-612788851670121490</guid><pubDate>Wed, 23 Mar 2011 12:57:00 +0000</pubDate><atom:updated>2011-03-23T18:27:35.367+05:30</atom:updated><title>Exception Handling</title><description>&lt;p&gt;Types of Exceptions&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;&lt;b&gt;Pre Defined Exceptions&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;&lt;b&gt;User Defined Exceptions&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;Pre Defined Exceptions&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;These exceptions are used to handle some logical errors known to the system are pre defined. The following are some of the important pre defined Exceptions&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;no_data_found &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This Exception raises when there is no rows to be retrieved from a table &lt;/p&gt;  &lt;p&gt;according to given condition&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;dup_val_on_index&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This Exception raises when ever you try to store duplicate values into a &lt;/p&gt;  &lt;p&gt;table, which has been indexed (unique indexed)&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;cur_already_open &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This Exception raises when ever your program attempts to open an already &lt;/p&gt;  &lt;p&gt;opened cursor.&lt;/p&gt;  &lt;p&gt;A cursor must be closed before it can be re opened. A cursor for loop &lt;/p&gt;  &lt;p&gt;automatically opens the cursor to which it refers. So Your program can not open that cursor inside the loop&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;invalid_cursor &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This Exception raises when ever your program attempts an illegal cursor operation, such as closing an un opened cursor&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;zero_divide &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This Exception raises when ever your program attempts to divide a number &lt;/p&gt;  &lt;p&gt;by zero&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;program_error &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This Exception raises when ever PL/SQL has internal problem&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;storage_error &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This Exception raises when ever PL/SQL runs out of memory &lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;too_many_rows &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This Exception raises when ever a select statement returns more than one &lt;/p&gt;  &lt;p&gt;row&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;login_denied &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This Exception raises when ever your program attempts to logon to oracle &lt;/p&gt;  &lt;p&gt;with an invalid user name and/or password&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;value_error &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This Exception raises when ever an arithmetic conversion or size constraint error occurs&lt;/p&gt;  &lt;p&gt;For ex, when your program selects a column value into a variable. If the value is longer than the declared length of the variable, pl/sql abords the assignment and raise the exception value_error&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Programs On Exceptions&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;1. &lt;strong&gt;Write a program for handling an error when we try to open an already opened cursor&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;declare&lt;/p&gt;  &lt;p&gt;cursor c1 is select * from emp;&lt;/p&gt;  &lt;p&gt;x emp%rowtype;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;open c1;&lt;/p&gt;  &lt;p&gt;fetch c1 into x;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(x.empno||&#39; &#39;||x.ename);&lt;/p&gt;  &lt;p&gt;--open c1;&lt;/p&gt;  &lt;p&gt;close c1;&lt;/p&gt;  &lt;p&gt;exception&lt;/p&gt;  &lt;p&gt;when cursor_already_open then&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39; sorry friend, cursor already opened&#39;);&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;2. &lt;strong&gt;Write a program to display a message when no data found in the main table&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;declare&lt;/p&gt;  &lt;p&gt;e emp%rowtype;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;select * into e from emp where empno=&amp;amp;empno;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;Empno Ename job&#39;);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(e.empno||&#39; &#39;||e.ename||&#39; &#39;||e.job);&lt;/p&gt;  &lt;p&gt;exception&lt;/p&gt;  &lt;p&gt;when No_data_found then&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39; sorry friend, no data found&#39;);&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;3. &lt;strong&gt;Write a program for handling an error when we insert a duplicate value into a column&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;declare&lt;/p&gt;  &lt;p&gt;n sample%rowtype;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;insert into sample values(&amp;amp;sno,&#39;&amp;amp;sname&#39;,&#39;&amp;amp;class&#39;);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;Record Inserted....&#39;);&lt;/p&gt;  &lt;p&gt;exception&lt;/p&gt;  &lt;p&gt;when dup_val_on_index then&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39; sorry friend, you entered a duplicate value&#39;);&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;create a table with unique constraint, for dup_val_on_index, &lt;/p&gt;  &lt;pre&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;create table sample(sno number(3),sname varchar2(10));&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt; CREATE UNIQUE INDEX IDX_SAM ON SAMPLE(SNO);&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;4. &lt;strong&gt;Write a program for handling an error when we try to close an un opened cursor&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;declare&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;cursor c1 is select * from emp;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;begin&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;--open c1;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;dbms_output.put_line(&#39; cursor is Opened&#39;);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;close c1;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;exception&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;when invalid_cursor then&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;dbms_output.put_line(&#39; sorry friend, No such cursor is opened&#39;);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;end;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;/&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;if u remove comment for open c1 then output is cursor is opened, other wise the you will get “sorry friend, no such cursor is opened ..”, since you r trying to close an unopened cursor&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;5. &lt;strong&gt;Write a program for handling an error when we try to divide a value with zero&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;declare&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;a number:=&amp;amp;a;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;b number:=&amp;amp;b;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;c number;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;begin&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;c:=a/b;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;dbms_output.put_line(c);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;exception&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;when zero_divide then&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;dbms_output.put_line(&#39;Zero Divide error&#39;);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;end;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;/&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;6. &lt;strong&gt;Write a program for handling an error when ever the program retrieves too many rows&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;declare&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;e emp%rowtype;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;begin&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;select * into e from emp;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;exception&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;when too_many_rows then&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;dbms_output.put_line(&#39; sorry friend, too many rows are selected&#39;);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;end;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;/&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;User Defined Exceptions&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;A user can define or create exceptions, which must be raised automatically, because the system does not knows when to execute the user defined errors. There are 3 steps while working with user defined exceptions&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;1. &lt;/b&gt;&lt;b&gt;Creating Exception&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;2. &lt;/b&gt;&lt;b&gt;Raising Exception&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;3. &lt;/b&gt;&lt;b&gt;Handling Exception&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The main purpose of user defined exceptions is, we can display a message in predefined error format in the instances not known to the system or that are not predefined&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Declare &lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&amp;lt;Exception Name&amp;gt; Exception;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;begin&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Raise Exception;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Exception&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;When &amp;lt;Exception name&amp;gt; then&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Message&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;End;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;/&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Raise_Application_Error &lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is a keyword, which is used to generate an error message in pre defined error format for the given user defined error&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Syntax : &lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Raise_Application_Error(Error number, ‘ message ‘);&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Example : &lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Raise_Application_Error(-20173, ‘ Some Error ‘);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;Error Number for (user defined Exceptions) must be in between &lt;b&gt;–20001 to -20999&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;1. &lt;strong&gt;Write a program for creating an exception and raising it when the basic is &amp;lt;3000, while inserting rows into a table and also handle it to perform any other action&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Declare&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;invalid_salary exception;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;s employee.sal%type:=&amp;amp;sal;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;begin&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;if s&amp;lt;3000 then&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;raise invalid_salary;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;else&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;insert into employee(sal) values (s);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;dbms_output.put_line(&#39; Record Inserted.....&#39;);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;end if;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;exception&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;when invalid_salary then&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;dbms_output.put_line(&#39; salary must be &amp;gt;3000 &#39;);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;end;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;/&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;2. &lt;strong&gt;Write a program for creating an exception and raising it when ever you try to insert any negative number into a table&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Declare&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;invalid_number exception;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;e employee.empno%type:=&amp;amp;empno;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;begin&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;if e&amp;lt;0 then&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;raise invalid_number;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;else&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;insert into employee(empno) values (e);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;dbms_output.put_line(&#39; Record Inserted.....&#39;);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;end if;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;exception&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;when invalid_number then&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;dbms_output.put_line(&#39; Number must be positive &#39;);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;end;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;/&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/exception-handling.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-8920873264873569751</guid><pubDate>Wed, 23 Mar 2011 12:53:00 +0000</pubDate><atom:updated>2011-03-23T18:24:26.693+05:30</atom:updated><title>Cursor</title><description>&lt;p&gt;Cursor is a location in the memory where the SQL statement is processed. That is when ever we send an SQL statement; the system allocates some memory by giving a name. After processing the statement it automatically cleans that memory. There are four steps, which are processed by the system whenever we send an SQL statement they are&lt;/p&gt;  &lt;p&gt;   &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;    &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;   &lt;b&gt;1.Creating cursor : &lt;/b&gt;This step allocates memory&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;2.Opening cursor :&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In this step process the sql statement&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;3.Fetching cursor:&lt;/strong&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The values satisfied by the sql statement are fetched from the &lt;/p&gt;  &lt;p&gt;table into cursor row by row&lt;/p&gt;  &lt;p&gt;&lt;b&gt;4.Closing cursor : &lt;/b&gt;This statement close the memory allocated for the cursor&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;Types Of Cursors&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;There Are Two Types Of Cursors&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;&lt;b&gt;Implicit cursors or simple cursors&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;&lt;b&gt;Explicit Cursors&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;Implicit Cursors&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Oracle implicitly opens a cursor to process each sql statement not associated with explicitly declared cursor. Pl/sql lets you refer to the most recent implicit cursors as the sql cursor.&lt;/p&gt;  &lt;p&gt;Implicit cursors have four attributes, you can use in your program&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;sql%rowcount &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;It returns the number of rows affected by an insert, update or delete&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;sql%found&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This attributes evaluates to true, if an insert or update or delete&lt;/p&gt;  &lt;p&gt;affected to one or more rows. It evaluates to false if no row is affected&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;sql%notfound &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This attributes is logical opposite to &lt;b&gt;sql%found&lt;/b&gt;. It evaluates to true,&lt;/p&gt;  &lt;p&gt;if an insert or update or delete does not affected to any row. It evaluates to &lt;/p&gt;  &lt;p&gt;false if any one row is affected&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;%isopen&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;It checks whether the cursor has been opened or not&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : 1&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;write a program to check whether there is atleast one row satisfying the given select statement or not using implicit cursors&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Declare&lt;/p&gt;  &lt;p&gt;e emp%rowtype;&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;select * into e from emp where empno=&amp;amp;empno;&lt;/p&gt;  &lt;p&gt;if sql%found then&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;~~~~~~~~~~~~~~~~~~~~&#39;);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39; Record Found &#39;);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;~~~~~~~~~~~~~~~~~~~~&#39;);&lt;/p&gt;  &lt;p&gt;End if;&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : 2&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;write a program to change the ename column values to upper case&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;using implicit cursors&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;update employee set ename=upper(ename);&lt;/p&gt;  &lt;p&gt;if sql%found then&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;~~~~~~~~~~~~~~~~~~~~&#39;);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(sql%rowcount || &#39; Records Updated &#39;);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;~~~~~~~~~~~~~~~~~~~~&#39;);&lt;/p&gt;  &lt;p&gt;else&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;~~~~~~~~~~~~~~~~~~~~&#39;);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39; Sorry Changes Are not Made &#39;);&lt;/p&gt;  &lt;p&gt;end if;&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : 3&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;write a program to update the salary of those employees to 20% whose salary is greater than the given salary, using implicit cursors&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;update employee set sal=sal+(sal*20)/100 where sal&amp;gt;&amp;amp;sal;&lt;/p&gt;  &lt;p&gt;if sql%found then&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;~~~~~~~~~~~~~~~~~~~~&#39;);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(sql%rowcount || &#39; Records Updated &#39;);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;~~~~~~~~~~~~~~~~~~~~&#39;);&lt;/p&gt;  &lt;p&gt;end if;&lt;/p&gt;  &lt;p&gt;if sql%notfound then&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;~~~~~~~~~~~~~~~~~~~~&#39;);&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39; Sorry No Record is Satisfied &#39;);&lt;/p&gt;  &lt;p&gt;end if;&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;font color=&quot;#0000ff&quot;&gt;&lt;strong&gt;Explicit Cursors&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The user creates these cursors for applying 2 or more DML operations on a table. using a cursor we can fetch all the row values from the table into a variable for printing them or performing some action with them&lt;/p&gt;  &lt;p&gt;In an explicit cursor, there are four steps related to execute cursors by the user. It is also used to compare a variable value with all values of the column.&lt;/p&gt;  &lt;p&gt;The four steps are&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;&lt;b&gt;Declaring a Cursor&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;&lt;b&gt;Opening a Cursor&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;3. &lt;/b&gt;&lt;b&gt;Fetching a Cursor&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;4. &lt;/b&gt;&lt;b&gt;Closing a Cursor&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Declaring a Cursor&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;A cursor is defined in the declarative part of the PL/SQL block by naming it and associating it with a query&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Syntax :&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Declare &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Cursor &amp;lt;cursor_name&amp;gt; is &amp;lt;select Statement&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Opening a Cursor&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;The cursor must be initialized or opened with the open statement. Opening the cursor excutes the query and identifies the result set which consists of all the rows that meets the query search criteria&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Syntax :&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Declare &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Cursor &amp;lt;cursor_name&amp;gt; is &amp;lt;select Statement&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Begin&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Open &amp;lt;cursor Name&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;-----------&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;End;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Fetching With a Cursor&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;This statement is used to retrieve the cursors current row. Fetching can be excuted repeatedly until all rows have been retrieved&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Syntax :&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Declare &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Cursor &amp;lt;cursor_name&amp;gt; is &amp;lt;select Statement&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Begin&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Open &amp;lt;cursor Name&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Fetch &amp;lt;cursor Name&amp;gt; into &amp;lt;variable1&amp;gt;,&amp;lt;variable2&amp;gt;,----&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;-----------&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;End;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Closing a Cursor&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;When the last row has been processed, the cursor is closed with the close statement. The close statement disable the cursor&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Syntax :&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Declare &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Cursor &amp;lt;cursor_name&amp;gt; is &amp;lt;select Statement&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Begin&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Open &amp;lt;cursor Name&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Fetch &amp;lt;cursor Name&amp;gt; into &amp;lt;variable1&amp;gt;,&amp;lt;variable2&amp;gt;,----&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;-----------&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Close &amp;lt;cursor Name&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;End;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Examples&lt;/p&gt;  &lt;p&gt;1. &lt;strong&gt;Write a program using an explicit cursor, which displays all the rows from the table&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Declare&lt;/p&gt;  &lt;p&gt;Cursor c1 is select * from emp;&lt;/p&gt;  &lt;p&gt;e emp%rowtype;&lt;/p&gt;  &lt;p&gt;Begin&lt;/p&gt;  &lt;p&gt;Open c1;&lt;/p&gt;  &lt;p&gt;Loop&lt;/p&gt;  &lt;p&gt;Fetch c1 into e;&lt;/p&gt;  &lt;p&gt;Dbms_output.put_line(e.empno||&#39; &#39;||e.ename||&#39; &#39;||e.sal);&lt;/p&gt;  &lt;p&gt;Exit when c1%notfound;&lt;/p&gt;  &lt;p&gt;End loop;&lt;/p&gt;  &lt;p&gt;Close c1;&lt;/p&gt;  &lt;p&gt;End;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2.Write a program using an explicit cursor, to insert values into a table empc from emp table, whose experience is &amp;gt;=23 years&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Declare&lt;/p&gt;  &lt;p&gt;cursor c2 is select * from emp where (sysdate-hiredate)/365&amp;gt;=23;&lt;/p&gt;  &lt;p&gt;e emp%rowtype;&lt;/p&gt;  &lt;p&gt;x number:=0;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;open c2;&lt;/p&gt;  &lt;p&gt;loop&lt;/p&gt;  &lt;p&gt;fetch c2 into e;&lt;/p&gt;  &lt;p&gt;exit when c2%notfound;&lt;/p&gt;  &lt;p&gt;insert into empc values(e.empno,e.ename,e.job,e.sal,e.hiredate);&lt;/p&gt;  &lt;p&gt;x:=x+sql%rowcount;&lt;/p&gt;  &lt;p&gt;end loop;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(x||&#39; Records inserted Successfully....&#39;);&lt;/p&gt;  &lt;p&gt;close c2;&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;3.Write a program using an explicit cursor, to display first 5 records&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;Declare&lt;/p&gt;  &lt;p&gt;cursor c3 is select * from emp;&lt;/p&gt;  &lt;p&gt;e emp%rowtype;&lt;/p&gt;  &lt;p&gt;x number;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;open c3;&lt;/p&gt;  &lt;p&gt;loop&lt;/p&gt;  &lt;p&gt;fetch c3 into e;&lt;/p&gt;  &lt;p&gt;exit when c3%rowcount&amp;gt;5;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(e.empno||&#39; &#39;||e.ename||&#39; &#39;||e.sal);&lt;/p&gt;  &lt;p&gt;end loop;&lt;/p&gt;  &lt;p&gt;close c3;&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;font color=&quot;#0000ff&quot;&gt;&lt;strong&gt;Cursor For Loop&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;A cursor for loop implicitly declares its loop index as a %rowtype, opens a cursor, repeatedly fetches the rows of values from the result set into fields in the records and closes the cursor when all rows have been processed or exit command is encountered&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Declare&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Cursor &amp;lt;cursor_name&amp;gt; is &amp;lt;select statement&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Begin&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;For &amp;lt;index&amp;gt; in &amp;lt;cursor name&amp;gt; loop&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;---------&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;end loop;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;end;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;/&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : 1&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Write a program to display all employee’s details working in the accepted department number using cursor for loop&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;declare&lt;/p&gt;  &lt;p&gt;cursor c1 is select * from emp where deptno=&amp;amp;deptno;&lt;/p&gt;  &lt;p&gt;e c1%rowtype;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;****************&#39;);&lt;/p&gt;  &lt;p&gt;for e in c1 loop&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(e.empno||&#39; &#39;||e.ename||&#39; &#39;||e.job||&#39; &#39;||e.sal);&lt;/p&gt;  &lt;p&gt;end loop;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39;****************&#39;);&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : 2&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Write a program to display all employee’s details working in the accepted department number and also display all department details for the given department number using cursor for loops&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;declare&lt;/p&gt;  &lt;p&gt;cursor c1 is select * from emp where deptno=&amp;amp;deptno;&lt;/p&gt;  &lt;p&gt;e c1%rowtype;&lt;/p&gt;  &lt;p&gt;cursor c2 is select * from dept where deptno=&amp;amp;deptno;&lt;/p&gt;  &lt;p&gt;d c2%rowtype;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39; ****Employees Details ***** &#39;);&lt;/p&gt;  &lt;p&gt;for e in c1 loop&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(e.empno||&#39; &#39;|| e.ename||&#39; &#39;||e.job||&#39; &#39;||e.sal);&lt;/p&gt;  &lt;p&gt;end loop;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(&#39; ****Department Details ***** &#39;);&lt;/p&gt;  &lt;p&gt;for d in c2 loop&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(d.dname||&#39; &#39;||d.loc&lt;/p&gt;  &lt;p&gt;||&#39; &#39;||d.deptno);&lt;/p&gt;  &lt;p&gt;end loop;&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/cursor.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-775784261404584826</guid><pubDate>Wed, 23 Mar 2011 12:46:00 +0000</pubDate><atom:updated>2011-03-23T18:16:35.513+05:30</atom:updated><title>Control structures</title><description>&lt;p&gt;&lt;strong&gt;Control structures&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Control structures are used to control the flow of programs execution. The control structures are classified into the following categories:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;b&gt;Conditional Control&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;b&gt;Iterative Control&lt;/b&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#ff8000&quot;&gt;Conditional Control&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;PL/SQL supports the following types of Conditional control statements or Selection Statements&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;Simple if statement&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;If...else statement&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;If ...elsif statement&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;1.Simple If&lt;u&gt; &lt;/u&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The simple if statement is used to check a simple condition, if it is true it will execute the statements that are present between &amp;quot;if &amp;quot;and &amp;quot;end if&amp;quot;. Otherwise it will not execute those statements.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Syntax : &lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;If ( Condition ) then&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Statements ;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;------------- ;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;End If;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;2.If...Else Statement&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The if …else statement is used to define two blocks of statements, in order to execute only one block.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Syntax : &lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;If (Condition ) then&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;True Block Statements ;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;------------------------- ;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Else&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;False Block Statements ;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;-------------------------- ;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;End if;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;if the condition is true, it will execute the True Block statements otherwise it will execute the false block statements.&lt;/p&gt;  &lt;p&gt;&lt;font color=&quot;#0000ff&quot;&gt;&lt;strong&gt;3.If...ElsIf Statement&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The if …elsIf statement is used to define Several blocks of statements, in order to execute only one block.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Syntax : &lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;If (condition 1) then&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;statements 1 ;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;--------------- ;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Elsif (condition 2) then&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;statements 2;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;------------- ;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Elsif (condition 3) then&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;statements 3;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;------------;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;|&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;|&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Elsif (condition n) then&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;statements n;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;------------;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Else&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;statements;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;------------;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;End if;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;if &lt;b&gt;condition1 &lt;/b&gt;is &lt;b&gt;true&lt;/b&gt;, it will &lt;b&gt;execute statements1 &lt;/b&gt;otherwise it will &lt;b&gt;check Condition2&lt;/b&gt;, if it is &lt;b&gt;true &lt;/b&gt;it will &lt;b&gt;execute statements2&lt;/b&gt;. if not it will &lt;b&gt;go to condition3&lt;/b&gt;, like this the process continues until the condition matches. If no condition matches, it will execute the else block statements.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Eg 1. Write a program to check whether the given number is a positive number of a negative number using simple if statement&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;declare&lt;/p&gt;  &lt;p&gt;n number:=&amp;amp;n;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;if(n&amp;gt;=0) then&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(n||&#39; is a Positive Number&#39;);&lt;/p&gt;  &lt;p&gt;end if;&lt;/p&gt;  &lt;p&gt;if(n&amp;lt;0) then&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(n ||&#39; is a Negative Number&#39;);&lt;/p&gt;  &lt;p&gt;end if;&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;p&gt;Branching Statements&lt;/p&gt;  &lt;p&gt;declare&lt;/p&gt;  &lt;p&gt;x integer:=&amp;amp;x;&lt;/p&gt;  &lt;p&gt;y integer:=&amp;amp;y;&lt;/p&gt;  &lt;p&gt;p char(1):=&#39;&amp;amp;Operator&#39;;&lt;/p&gt;  &lt;p&gt;r number;&lt;/p&gt;  &lt;p&gt;begin&lt;/p&gt;  &lt;p&gt;if p=&#39;+&#39; then&lt;/p&gt;  &lt;p&gt;goto Addition;&lt;/p&gt;  &lt;p&gt;elsif p=&#39;-&#39; then&lt;/p&gt;  &lt;p&gt;goto Subtraction;&lt;/p&gt;  &lt;p&gt;elsif p=&#39;*&#39; then&lt;/p&gt;  &lt;p&gt;goto Multiplication;&lt;/p&gt;  &lt;p&gt;elsif p=&#39;/&#39; then&lt;/p&gt;  &lt;p&gt;goto Division;&lt;/p&gt;  &lt;p&gt;else&lt;/p&gt;  &lt;p&gt;goto Expt;&lt;/p&gt;  &lt;p&gt;end if;&lt;/p&gt;  &lt;p&gt;&amp;lt;&amp;lt;Addition&amp;gt;&amp;gt;&lt;/p&gt;  &lt;p&gt;r:=x+y;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(r);&lt;/p&gt;  &lt;p&gt;return;&lt;/p&gt;  &lt;p&gt;&amp;lt;&amp;lt;Subtraction&amp;gt;&amp;gt;&lt;/p&gt;  &lt;p&gt;r:=x-y;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(r);&lt;/p&gt;  &lt;p&gt;return;&lt;/p&gt;  &lt;p&gt;&amp;lt;&amp;lt;Multiplication&amp;gt;&amp;gt;&lt;/p&gt;  &lt;p&gt;r:=x*y;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(r);&lt;/p&gt;  &lt;p&gt;return;&lt;/p&gt;  &lt;p&gt;&amp;lt;&amp;lt;Division&amp;gt;&amp;gt;&lt;/p&gt;  &lt;p&gt;r:=x/y;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(r);&lt;/p&gt;  &lt;p&gt;return;&lt;/p&gt;  &lt;p&gt;&amp;lt;&amp;lt;Expt&amp;gt;&amp;gt;&lt;/p&gt;  &lt;p&gt;dbms_output.put_line(sqlerrm);&lt;/p&gt;  &lt;p&gt;end;&lt;/p&gt;  &lt;p&gt;/&lt;/p&gt;  &lt;pre&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h6&gt;Loops&lt;/h6&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;PL/SQL supports, the following types of loops&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;1. &lt;/b&gt;&lt;b&gt;Simple Loop&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;2. &lt;/b&gt;&lt;b&gt;While Loop&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;3. &lt;/b&gt;&lt;b&gt;For Loop&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;1.Simple Loop&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This loop is used to execute a series of statements as long as the condition is &lt;b&gt;false&lt;/b&gt;. If the condition becomes true, it will exit from that loop&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Syntax :&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Loop &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Statements;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;-----------&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Exit When &amp;lt;condition&amp;gt;;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Increment/decrement Operations&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End Loop;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Eg 2 :Write a program to print first 25 natural numbers using simple loop&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Declare&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;N number:=1;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Begin&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Loop&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Exit When N&amp;gt;25;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Dbms_output.put_line(N);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;N:=N+1;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End loop;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;/&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;2.While Loop&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The while Loop is used to execute a series of statements as long as the condition is &lt;b&gt;True&lt;/b&gt;. If the &lt;b&gt;condition &lt;/b&gt;becomes &lt;b&gt;false&lt;/b&gt;, it will &lt;b&gt;exit &lt;/b&gt;from that &lt;b&gt;loop&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Syntax :&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;While &amp;lt;condition&amp;gt; Loop &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Statements;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;-----------&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;[ Exit When &amp;lt;condition&amp;gt;; ]&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Increment/decrement Operations&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End Loop;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Eg 3: Write a program to print first 25 natural numbers using While loop&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Declare&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;N number:=1;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Begin&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;While N&amp;lt;=25 Loop&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Dbms_output.put_line(N);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;N:=N+1;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End loop;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;/&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;3.For Loop&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The For loop is used to execute a set of statements as long as the condition is false. If the condition becomes true, it will exit from that loop&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Syntax :&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;For &amp;lt;variable&amp;gt; in [reverse] &amp;lt;start value&amp;gt; .. &amp;lt;end Value&amp;gt; Loop &lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Statements;&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;-----------&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;End Loop;&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Eg 4: Write a program to print first 25 natural numbers using For loop&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Declare&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;N number:=1;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Begin&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;For N in 1..25 Loop&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Dbms_output.put_line(N);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End loop;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;/&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Eg 5 :Write a program to print first 25 natural numbers using For loop in Reverse order&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Declare&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;N number:=1;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Begin&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;For N in Reverse 1..25 Loop&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Dbms_output.put_line(N);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End loop;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;/&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Eg 6: Write a program to print multiplication table for the given number&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Declare&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;i number:=1;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;r number:=&amp;amp;number;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Begin&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;While i&amp;lt;=10 loop&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;dbms_output.put_line(r||&#39;*&#39;|| i ||&#39;=&#39;||r*i);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;i:=i+1;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End loop;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;/&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/control-structures.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-5487518563816362281</guid><pubDate>Wed, 23 Mar 2011 12:32:00 +0000</pubDate><atom:updated>2011-03-23T18:02:55.376+05:30</atom:updated><title>Partitions</title><description>&lt;h4&gt;&lt;u&gt;Partitions&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;Partition is a data base object, it allows VLDB objects (Very Large Databases such as tables, indexes etc). By splitting large database objects into smaller, more manageable units. You can divide the rows of a single table into different parts called as partitioning a table&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Advantages of Using Partitions&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1.Reducing data base Retrieve time&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;By splitting the large data base objects into smaller, more manageable partitions, allows you to perform any type of action on individual units of the database&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2.Backup and Recovery&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Partitioning improves performance of Backup and Recovery operations. Smaller size of partitions offers more options for backing up and recovering from the partitions&lt;/p&gt;  &lt;p&gt;&lt;b&gt;3.Query Performance&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Partitions can be created on ranges of certain columns. This focus reduce the search time, as a queries require a full table scan to search for the given criteria. This can be resolved by using partitions on a table&lt;/p&gt;  &lt;p&gt;&lt;b&gt;4.Partition Transparency&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Partition implementation is transparent to the end users and developers. They do not need to be aware of the physical implementation of the database objects&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create table &amp;lt;table name&amp;gt; (columns specifications,-------)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Partition by Range (column name)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;(partition &amp;lt;partition name&amp;gt; values less than (some value),&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;partition &amp;lt;partition name&amp;gt; values less than (some value),&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;----------------------------&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;partition &amp;lt;partition name&amp;gt; values less than (max value));&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create table partitiondemo(sno number(3),sname varchar2(10))&lt;/p&gt;  &lt;p&gt;partition by range (sno)&lt;/p&gt;  &lt;p&gt;(partition p1 values less than(200),&lt;/p&gt;  &lt;p&gt;partition p2 values less than(300),&lt;/p&gt;  &lt;p&gt;partition p3 values less than(600),&lt;/p&gt;  &lt;p&gt;partition p4 values less than(999))&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;select * from partitiondemo;&lt;/p&gt;  &lt;p&gt;SNO SNAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;100 Nithya&lt;/p&gt;  &lt;p&gt;105 Saloni&lt;/p&gt;  &lt;p&gt;109 Namitha&lt;/p&gt;  &lt;p&gt;199 Aruna&lt;/p&gt;  &lt;p&gt;204 Priya&lt;/p&gt;  &lt;p&gt;&lt;b&gt;250 Mahesh&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;306 Nandhini&lt;/p&gt;  &lt;p&gt;450 Pooja &lt;/p&gt;  &lt;p&gt;560 Uma &lt;/p&gt;  &lt;p&gt;328 Janshi&lt;/p&gt;  &lt;p&gt;900 Rani&lt;/p&gt;  &lt;p&gt;904 Kavya&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;select * from partitiondemo partition(p1);&lt;/p&gt;  &lt;p&gt;SNO SNAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;100 Nithya&lt;/p&gt;  &lt;p&gt;105 Saloni&lt;/p&gt;  &lt;p&gt;109 Namitha&lt;/p&gt;  &lt;p&gt;199 Aruna&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;3. &lt;/b&gt;select * from partitiondemo partition(p2);&lt;/p&gt;  &lt;p&gt;SNO SNAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;204 Priya&lt;/p&gt;  &lt;p&gt;&lt;b&gt;250 Mahesh&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;4. &lt;/b&gt;select * from partitiondemo partition(p3);&lt;/p&gt;  &lt;p&gt;SNO SNAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;306 Nandhini&lt;/p&gt;  &lt;p&gt;450 Pooja &lt;/p&gt;  &lt;p&gt;560 Uma &lt;/p&gt;  &lt;p&gt;328 Janshi&lt;/p&gt;  &lt;p&gt;900 Rani&lt;/p&gt;  &lt;p&gt;904 Kavya&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;5. &lt;/b&gt;select * from partitiondemo partition(p3);&lt;/p&gt;  &lt;p&gt;SNO SNAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;900 Rani&lt;/p&gt;  &lt;p&gt;904 Kavya&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Now, &lt;/b&gt;insert some values again&lt;/p&gt;  &lt;p&gt;&lt;b&gt;insert into partitiondemo values(125,&#39;Sound&#39;);&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;select * from partitiondemo partition(p1);&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SNO SNAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;100 Nithya&lt;/p&gt;  &lt;p&gt;105 Saloni&lt;/p&gt;  &lt;p&gt;109 Namitha&lt;/p&gt;  &lt;p&gt;199 Aruna&lt;/p&gt;  &lt;p&gt;125 Sound&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/partitions-partition-is-data-base.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-3542303586040809205</guid><pubDate>Wed, 23 Mar 2011 12:30:00 +0000</pubDate><atom:updated>2011-03-23T18:00:25.812+05:30</atom:updated><title></title><description>&lt;h4&gt;&lt;u&gt;Clusters&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Tables that are frequently accessed together may be physically stored together. Clustering is a method of storing tables that are closely related and are often joined together, into the same area on the disk.&lt;/p&gt;  &lt;p&gt;A cluster is created to hold the tables. A cluster contains the values of a table according to the column that we define. When ever we insert values into the table the same values are automatically stored in the database object name cluster.&lt;/p&gt;  &lt;p&gt;Cluster can be created by using create cluster command. To create a cluster use the following syntax&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create cluster &amp;lt;cluster name&amp;gt; (columns specifications,------)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;To create any cluster&lt;/b&gt;&lt;u&gt; &lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;First create a cluster with the column names that you want &lt;/li&gt;    &lt;li&gt;Next create an index on that cluster. Otherwise the cluster will not work properly&lt;/li&gt;    &lt;li&gt;Next create a table with the cluster specification and list of columns&lt;/li&gt;    &lt;li&gt;Now insert some values into that table, if you see the cluster the same values will be available from the cluster also&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Note : &lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;We can not insert or update or delete any row from the cluster&lt;/li&gt;    &lt;li&gt;We can not even drop the cluster until the source table is present&lt;/li&gt;    &lt;li&gt;To drop the cluster, first we have to drop the source table and then we can drop that particular cluster&lt;/li&gt; &lt;/ol&gt;  &lt;h6&gt;Creation of cluster for a table&lt;/h6&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;First create the cluster,&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;create cluster sampleclust (sno number(3), sname varchar2(10));&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Next create the index,&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;create index idx_sampleclust on cluster sampleclust;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Attaching the cluster to the table,&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;create table sample2 (sno number(3),sname varchar2(10)) cluster &lt;/p&gt;  &lt;p&gt;sampleclust(sno,sname)&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;To display details of the Table&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;select * from sample2;&lt;/p&gt;  &lt;p&gt;SNO SNAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;100 Nithya&lt;/p&gt;  &lt;p&gt;101 Saloni&lt;/p&gt;  &lt;p&gt;102 Aruna&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;To display details of the Table&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;select * from cluster sampleclust;&lt;/p&gt;  &lt;p&gt;SNO SNAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;100 Nithya&lt;/p&gt;  &lt;p&gt;101 Saloni&lt;/p&gt;  &lt;p&gt;102 Aruna&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;Even if you delete any row in the table, that will not affected to the cluster&lt;/b&gt;      &lt;ol&gt;       &lt;li&gt;delete from sample2 where sno=100;&lt;/li&gt;        &lt;li&gt;select * from sample2;&lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;SNO SNAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;101 Saloni&lt;/p&gt;  &lt;p&gt;102 Aruna&lt;/p&gt;  &lt;p&gt;3. select * from cluster sampleclust;&lt;/p&gt;  &lt;p&gt;SNO SNAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;100 Nithya&lt;/p&gt;  &lt;p&gt;101 Saloni&lt;/p&gt;  &lt;p&gt;102 Aruna&lt;/p&gt;  &lt;p&gt;4. delete from sampleclust where sno=102&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ERROR at line 1:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ORA-00942: table or view does not exist&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;We can not delete any data from the cluster&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Drop cluster&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;This command is used to drop any cluster&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax : drop Cluster &amp;lt;cluster name&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;h6&gt;&lt;u&gt;To drop any cluster&lt;/u&gt;&lt;/h6&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Drop table sample2;&lt;/p&gt;  &lt;p&gt;Drop cluster sampleclust;&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/clusters-tables-that-are-frequently.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-8743401934345839036</guid><pubDate>Wed, 23 Mar 2011 12:28:00 +0000</pubDate><atom:updated>2011-03-23T17:58:47.442+05:30</atom:updated><title>Indexes</title><description>&lt;h4&gt;&lt;u&gt;Indexes&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;It is a database object used to sort the values of a column for retrieving data fastly from a table. By default the rows in a table are identified with their row id’s &lt;/p&gt;  &lt;p&gt;When ever you create an index on a table, the system gives index id’s which are stored in the index. If a table is having index then the system retrieves the rows basing on the index id’s else it retrieves value using row id’s&lt;/p&gt;  &lt;h4&gt;&lt;u&gt;Types of Index&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;There are two types of indexes&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;&lt;b&gt;Simple Index&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;&lt;b&gt;Composite Index&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;There are two ways to create an index. They are&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;Creating index on a single column&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;Creating index on multiple columns&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Simple Index&lt;/font&gt; &lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Any index created on a single table is called a simple index&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax : create index &amp;lt;index name&amp;gt; on &amp;lt;table name&amp;gt;(column);&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Creating Index On A Single Column&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;When you create an index on a single column, &lt;/p&gt;  &lt;p&gt;Create Table temporary(sno number(3), sname varchar2(10));&lt;/p&gt;  &lt;p&gt;&lt;b&gt;NOTE : &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Index name must be unique in the database and it is user choice. Since index is a data base object, system allocates some memory in the database. The index stores all index id’s allocated for a column.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note :&lt;/b&gt;&lt;b&gt; &lt;/b&gt;we can not create any index on views. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;SQL&amp;gt; select * from temporary;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SNO NAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;110 Nithya&lt;/p&gt;  &lt;p&gt;111 Saloni&lt;/p&gt;  &lt;p&gt;&lt;b&gt;112 Mahesh&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;113 Priya&lt;/p&gt;  &lt;p&gt;114 Prasad&lt;/p&gt;  &lt;p&gt;115 Aruna&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;creating an index&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create index idx_temp on temporary(sno);&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Note : &lt;/u&gt;&lt;/b&gt;We can not see the Details of Index file&lt;/p&gt;  &lt;p&gt;select * from idx_temp;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ERROR at line 1:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ORA-00942: table or view does not exist&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Creating Unique indexes on a Single column&lt;/b&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create unique index &amp;lt;index name&amp;gt; on &amp;lt;table name&amp;gt; (&amp;lt;column&amp;gt;)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;Unique index can be created on any table, except it does not contain any duplicate values on the column for which you are creating a unique index&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create unique index idx_temp on sample1(sno);&lt;/p&gt;  &lt;p&gt;insert into sample1 values(&amp;amp;sno,’&amp;amp;sname’);&lt;/p&gt;  &lt;p&gt;enter any value for sno: &lt;b&gt;1&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;enter any value for sname : &lt;b&gt;Mahesh&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;enter any value for sno: &lt;b&gt;2&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;enter any value for sname : &lt;b&gt;Prasad&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;enter any value for sno: &lt;b&gt;1&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;enter any value for sname : &lt;b&gt;Nithya&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Error : &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Ora-00001: unique constraint (scott.idx_temp) violated&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Once if u create any index on a particular column, then that column can not be re indexed. To re index on that column, first remove that existing index using drop index command and then create new index on that column&lt;b&gt;&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;If you create an &lt;b&gt;index &lt;/b&gt;on an &lt;b&gt;existing index&lt;/b&gt;, it will show an error message&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;create unique index idx_temp1 on temporary(sno)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ERROR at line 1:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ORA-01408: such column list already indexed&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Dropping an Index&lt;/b&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Drop Index : &lt;/b&gt;This Command Is Used To Drop Any Index&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Syntax : Drop Index &amp;lt;Index Name&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : &lt;/b&gt;drop index idx_temp;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;h6&gt;&lt;/h6&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Composite index&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;An index created on 2 or more columns in a table is called composite index.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create index &amp;lt;index name&amp;gt; on &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&amp;lt;table name&amp;gt; (&amp;lt;column1&amp;gt;, &amp;lt;column2&amp;gt;,-------)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;first create a table&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create table bank(accno number(3),accna varchar2(10),ddno number(6), &lt;/p&gt;  &lt;p&gt;amt number(6));&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;next Create the index on that table&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create index idx_bank on bank (accno,ddno);&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;next Insert some values into that table&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; insert into bank values (100,&#39;Prasad&#39;,11111,2343)&lt;/li&gt;    &lt;li&gt;insert into bank values (102,&#39;Mahesh&#39;,22222,4334)&lt;/li&gt;    &lt;li&gt;insert into bank values (102,&#39;Nithya&#39;,22222,4334) &lt;b&gt;&lt;/b&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;select * from bank;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;ACCNO ACCNA DDNO AMT&lt;/p&gt;  &lt;p&gt;--------- ---------- --------- -------------&lt;/p&gt;  &lt;p&gt;&lt;b&gt;100 Prasad 11111 2343&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;102 Mahesh 22222 4334&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;102 Nithya 22222 4334&lt;/p&gt;  &lt;p&gt;100 Saloni 11111 3433&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Composite unique index&lt;/b&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;We can create unique index on two or more columns also using composite unique index&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create unique index &amp;lt;index name&amp;gt; on &amp;lt;table name&amp;gt; &lt;/b&gt;&lt;b&gt;(&amp;lt;column1&amp;gt;,&amp;lt;column2&amp;gt;,-------)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create unique index idx_bank1 on bank1(accno,ddno);&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; insert into bank values (100,&#39;Priya&#39;,11111,3445)&lt;/li&gt;    &lt;li&gt;insert into bank values (102,&#39;Mahi&#39;,22222,4334)&lt;/li&gt;    &lt;li&gt;insert into bank values (102,&#39;Sound&#39;,22222,4334) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ERROR at line 1:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ORA-00001: unique constraint (SCOTT.IDX_BANK1) violated&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;h6&gt;Note : &lt;/h6&gt;  &lt;h6&gt;1. There is a problem in the above index, the index will give an error only when both columns value are given duplicate values. If any one is different the index will accept those values.&lt;/h6&gt;  &lt;h6&gt;2. Once a column is indexed already, it is not possible to reindex that column to any other index file&lt;/h6&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Drop index&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;This command is used to drop any index&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax : Drop index &amp;lt;index name&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Ex : &lt;/b&gt;Drop Index Idx_Sample;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;There are &lt;b&gt;two built in objects, which&lt;/b&gt; gives information about the indexs that are in the data base.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;&lt;u&gt;user_indexes : &lt;/u&gt;&lt;/b&gt;contains all user defined index’s information&lt;b&gt;&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;select * from user_indexes;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;select index_name, table_name, table_owner from user_indexes;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;&lt;u&gt;All_indexes :&lt;/u&gt;&lt;/b&gt;&lt;b&gt; &lt;/b&gt;contains all user defined index’s and pre defined index’s information&lt;b&gt;&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;select * from all_indexes;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;select index_name, table_name, table_owner from all_indexes;&lt;/b&gt;&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/indexes.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-3501372864358454777</guid><pubDate>Wed, 23 Mar 2011 12:24:00 +0000</pubDate><atom:updated>2011-03-23T17:54:47.837+05:30</atom:updated><title>Sequence</title><description>&lt;h4&gt;&lt;u&gt;Sequences&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;The sequence is a database object, which is used to generate unique integers for use as primary key for a column. There are two steps while working with a sequence&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;Creating Sequence&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;Attaching or Binding Sequence&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;h4&gt;&lt;u&gt;Creating Sequence&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;Since a sequence is a database object, it contains the values with which it generates starting, increment value and maximum value etc.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create sequence &amp;lt;sequence name &amp;gt;&lt;/b&gt;&lt;b&gt;Start with &amp;lt;value&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Increment by &amp;lt;value&amp;gt;&lt;/b&gt;&lt;b&gt;Max value / [no max value] &amp;lt;value&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Min value / [no min value] &amp;lt;value&amp;gt;&lt;/b&gt;&lt;b&gt;Cycle/no cycle&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Cache/no cache &amp;lt;value&amp;gt;&lt;/b&gt;&lt;b&gt;Order/no order&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Start with : &lt;/b&gt;it is a keyword to specifying the starting value in the sequence creation&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Increment by : &lt;/b&gt;this parameter accepts the number by which we want to increment the value in the sequence. If you want to decrement you must specify the negative value. By default the increment value is ‘1’ (not zero)&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Max value : &lt;/b&gt;specifies the maximum value or limit that the sequence can generate&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;No max value : &lt;/b&gt;if you cannot specify the maximum limit, use this parameter. The default value is No max value.(not compulsory)&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Min value : &lt;/b&gt;specifies the sequences minimum value or limit. It represent the number upto which you want to decrement the value.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;No Min value : &lt;/b&gt;The default value is No Max value.(not compulsory)&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Cycle : &lt;/b&gt;specifies that the sequence continues to generate value after reaching either its maximum or minimum value. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;No cycle : &lt;/b&gt;it represents that the sequence should stop generation of numbers when the max value is reached. This is the default one.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Cache : &lt;/b&gt;it specifies how many values of the sequence oracle pre allocates and keeps in the memory for the fast access. This keyword generates ‘n’ numbers immediately after creating sequence. Where ‘n’ is the number followed by cache. Default is ‘20’&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;No cache : &lt;/b&gt;this is the default one.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Order : &lt;/b&gt;generates the sequences in the order of request&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;No order : &lt;/b&gt;this is the default one&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Attaching Sequence&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;If sequence is created it can be attached to the table for storing the value of the sequence into the table or for generating the column values according to the parameters in the sequence.&lt;/p&gt;  &lt;p&gt;There are two ways the sequence can be attached&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;Insert Command&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;Update Command&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;currval : &lt;/b&gt;specifies sequence current value &lt;/p&gt;  &lt;p&gt;&lt;b&gt;nextval : &lt;/b&gt;specifies sequence next value&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;/p&gt;  &lt;h4&gt;&lt;u&gt;Using Insert Command&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Insert into &amp;lt;table name&amp;gt;(columns list) values(sequencename.nextval,……);&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;First create a dummy table&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;b&gt;create table temporary(sno number(3), name varchar2(10));&lt;/b&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Next, create a sequence with the following way&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;b&gt;create sequence myseq start with 100 increment by 1 maxvalue 110&amp;#160; &lt;/b&gt;&lt;b&gt;min value 100;&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;insert into temporary values(myseq.nextval,&#39;&amp;amp;sname&#39;);&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;similarly insert values in to the temporary table, when the sequence.next Val reaches to 110, it automatically gives an error message.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ERROR at line 1:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ORA-08004: sequence MYSEQ.NEXTVAL exceeds MAXVALUE and cannot be instantiated&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;select * from temporary;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;SNO NAME&lt;/p&gt;  &lt;p&gt;--------- ----------&lt;/p&gt;  &lt;p&gt;100 one&lt;/p&gt;  &lt;p&gt;101 two&lt;/p&gt;  &lt;p&gt;102 three&lt;/p&gt;  &lt;p&gt;103 four&lt;/p&gt;  &lt;p&gt;104 five&lt;/p&gt;  &lt;p&gt;105 six&lt;/p&gt;  &lt;p&gt;106 seven&lt;/p&gt;  &lt;p&gt;107 eight&lt;/p&gt;  &lt;p&gt;108 nine&lt;/p&gt;  &lt;p&gt;109 ten&lt;/p&gt;  &lt;p&gt;110 eleven&lt;/p&gt;  &lt;h4&gt;&lt;u&gt;&lt;/u&gt;&lt;/h4&gt;  &lt;h4&gt;&lt;u&gt;Using Update Command&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Update &amp;lt;table name&amp;gt; set &amp;lt;column Name&amp;gt;=&amp;lt;sequencename.nextval&amp;gt;, ------;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create a sequence with the following way&lt;/p&gt;  &lt;p&gt;&lt;b&gt;create sequence myseq1 start with 1 increment by 1 maxvalue 10 &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;min value 1;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Updating only one value&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;update sample set sno=myseq1.nextval where sname=’kk’;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Updating Multiple Values&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;update sample set sno=myseq1.nextval;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;select * from sample;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;EMPNO ENAME SAL&lt;/p&gt;  &lt;p&gt;--------- ---------- ---------&lt;/p&gt;  &lt;p&gt;1 Nithya 5600&lt;/p&gt;  &lt;p&gt;2 Saloni 5400&lt;/p&gt;  &lt;p&gt;3 Pooja 5400&lt;/p&gt;  &lt;p&gt;4 Aruna 7600&lt;/p&gt;  &lt;p&gt;5 Sound 5600&lt;/p&gt;  &lt;p&gt;&lt;b&gt;6 Mahesh 4500&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;7 Nandhini 3500&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;If you are updating a table using a sequence, which contains more rows than the sequence can generate, we will get an &lt;b&gt;error message&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;For example, if a table contains 12 rows, but the sequence can generate maximum of 11 rows only. So we get error message&lt;/li&gt; &lt;/ol&gt;  &lt;h3&gt;&lt;u&gt;Altering a Sequence&lt;/u&gt;&lt;/h3&gt;  &lt;p&gt;A sequence can be altered using the Alter Command. But we can not alter the start with value.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Alter sequence &amp;lt;sequence name &amp;gt; &lt;/b&gt;&lt;b&gt;Increment by &amp;lt;value&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Maxvalue /NoMaxValue &amp;lt;value&amp;gt;&lt;/b&gt;&lt;b&gt;Minvalue /NoMinValue &amp;lt;value&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Cycle / NoCycle &lt;/b&gt;&lt;b&gt;Cache/NoCache &amp;lt;value&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Order/noorder&lt;/b&gt;&lt;/p&gt;  &lt;h4&gt;Example &lt;/h4&gt;  &lt;p&gt;&lt;b&gt;create sequence myseq2 start with 100 increment by 1 maxvalue 110 &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;minvalue 100;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;we altered the above sequence&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Alter sequence myseq2 increment by 2 maxvalue 120;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Update sample set sno=myseq2.nextval;&lt;/p&gt;  &lt;p&gt;select * from sample;&lt;/p&gt;  &lt;p&gt;EMPNO ENAME SAL&lt;/p&gt;  &lt;p&gt;--------- ---------- ---------&lt;/p&gt;  &lt;p&gt;100 Nithya 5600&lt;/p&gt;  &lt;p&gt;102 Saloni 5400&lt;/p&gt;  &lt;p&gt;104 Pooja 5400&lt;/p&gt;  &lt;p&gt;106 Aruna 7600&lt;/p&gt;  &lt;p&gt;108 Sound 5600&lt;/p&gt;  &lt;p&gt;&lt;b&gt;110 Mahesh 4500&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;112 Nandhini 3500 &lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Again Altering the above sequence&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Alter sequence myseq1 nomaxvalue;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;update sample set sno=myseq1.nextval;&lt;/p&gt;  &lt;p&gt;now you can update the values as many times as you want, since there is no maximum limit to stop the generation of numbers&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Drop sequence :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;This command is used to drop any sequence&lt;/p&gt;  &lt;h4&gt;Syntax : drop sequence &amp;lt;sequence name&amp;gt;&lt;/h4&gt;  &lt;p&gt;&lt;b&gt;Example : &lt;/b&gt;drop sequence myseq1;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Even though u altered the sequence, it will generate numbers from where it has left before altering the sequence and continues according to ur altered form&lt;/p&gt;  &lt;p&gt;&lt;b&gt;RowID:&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;When ever we insert a row, the system automatically generates Rowid’s and appends the rowid column and stores the values in it. each rowid is unique from one another.&lt;/p&gt;  &lt;p&gt;To see Rowid’s of emp table, give the following command&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Select rowid from emp;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Select rowid from dept;&lt;/b&gt;&lt;/p&gt;  &lt;h4&gt;To delete any Duplicate Rows (Using Row id’s)&lt;/h4&gt;  &lt;p&gt;If any duplicate rows are present in any table, then with the help of rowid, we can able to delete that particular records&lt;/p&gt;  &lt;p&gt;&lt;b&gt;select rowid, eno, ename, sal from sample;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;ROWID SNO NAME&lt;/p&gt;  &lt;p&gt;-------------------------------------- ----------&lt;/p&gt;  &lt;p&gt;AAAAcUAACAAAAEbAAA 1 Nithya&lt;/p&gt;  &lt;p&gt;AAAAcUAACAAAAEbAAB 2 Saloni&lt;/p&gt;  &lt;p&gt;&lt;b&gt;AAAAcUAACAAAAEbAAC 1 Mahesh&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;AAAAcUAACAAAAEbAAD 4 Aruna&lt;/p&gt;  &lt;p&gt;AAAAcUAACAAAAEbAAE 2 Saloni&lt;/p&gt;  &lt;p&gt;AAAAcUAACAAAAEbAAF 2 Saloni&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Delete from customer where rowid=’ AAAAcUAACAAAAEbAAF’;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Delete from customer where rowid=’ AAAAcUAACAAAAEbAAE’&lt;/b&gt;&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/sequence.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-557419424823881259</guid><pubDate>Wed, 23 Mar 2011 12:19:00 +0000</pubDate><atom:updated>2011-03-23T17:49:51.006+05:30</atom:updated><title>Views</title><description>&lt;h4&gt;&lt;u&gt;Views&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;A view is like a window through which data on a table can be viewed or changed. It is a virtual table that is – it does not have any data of its own, but derives the data from the table it is associated with. It manipulates data in the underlying base table.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax : &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Create [or Replace] View &amp;lt;view name&amp;gt;&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;&lt;b&gt;[ ( column1, column2,----)] as Select &amp;lt;select list&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Replace Option :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;This option is used to replace the definition of existing view with new definition. It can be replaced with another view only, but not with a table.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Advantages of Views&lt;/b&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Views restrict access to the database. Selecting from a view can display a restricted portion of the data base&lt;/li&gt;    &lt;li&gt;Views allows users to make simple queries to retrieve the results from complicated queries&lt;/li&gt;    &lt;li&gt;As a view does not store any data, the redundancy problem does not arise&lt;/li&gt;    &lt;li&gt;One view can be used to transparently retrieve data from several tables.&lt;/li&gt;    &lt;li&gt;Views also allow the same data to be seen by different users in different ways&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;Types of View&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;There are two types of view&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;&lt;b&gt;Simple View&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;&lt;b&gt;Composite View&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Simple View&lt;/font&gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;When a view is create basing on a single table then it is called a simple view&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create [or replace ] view &amp;lt;view name&amp;gt; &lt;/b&gt;&lt;b&gt;as Select &amp;lt;select list&amp;gt; from &amp;lt;table1&amp;gt;;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Examples :&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;Create a view, which shows all employees details whose salary is between 2000 and 3000 &lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; Create view empsal as select * from emp where sal&amp;gt;=2000 and sal&amp;lt;=3000;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;To see the details of a view, give the following command&lt;/b&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Select * from &amp;lt;view name&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Ex : &lt;/b&gt;Select * from sample;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;&lt;b&gt;write a query which Creates a view, that shows all employees details who are belonging to dept 20&lt;/b&gt;&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;p&gt;Create or replace view empsal as select * from emp where deptno=20 &lt;/p&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;Ex : Select * from empsal;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;3. &lt;/b&gt;&lt;b&gt;Create a view, which shows all employees details whose salary is between 2000 and 3000 and who are belonging to dept 20&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;p&gt;Create or replace view empsal as select * from emp where deptno=20 and Sal between 2000 and 3000;&lt;/p&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;We can change column names in a view at the time of creation&lt;/b&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create a view, which shows all employees details who belong to dept 20&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; create or replace view emp20 as select * from emp where deptno=20&lt;/li&gt;    &lt;li&gt; create or replace view dept20(emp_no, emp_name, salary) as select empno, ename, sal from emp;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;select * from dept20;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;EMP_NO EMP_NAME SALARY&lt;/p&gt;  &lt;p&gt;----------- ---------- --------------&lt;/p&gt;  &lt;p&gt;7369 SMITH 800&lt;/p&gt;  &lt;p&gt;7499 ALLEN 1600&lt;/p&gt;  &lt;p&gt;7521 WARD 1250&lt;/p&gt;  &lt;p&gt;7566 JONES 2975&lt;/p&gt;  &lt;p&gt;7654 MARTIN 1250&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;We can Create a view, using another view&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; create or replace view emp21 (ename,sal) as select empname,basic from emp20;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt; select * from emp21;&lt;/p&gt;  &lt;p&gt;ENAME SAL&lt;/p&gt;  &lt;p&gt;----------- -----&lt;/p&gt;  &lt;p&gt;SMITH 800&lt;/p&gt;  &lt;p&gt;JONES 2975&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Mahesh 3000&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;ADAMS 1100&lt;/p&gt;  &lt;p&gt;FORD 3000&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;If column names are not given then it defaultly takes all the given column names in the table&lt;/li&gt;    &lt;li&gt;Number of columns in view ie in Bracket must be equal to number of columns in the select statement&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;Manipulating the Base Tables Through Views&lt;/b&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;The insert, update and delete commands can also be used with views. Using these commands with views is an indirect way of manipulating tables. Since a view nothing but a virtual table. Any manipulations ie deletion, updation will automatically affects the main table.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create a view, which shows All students details Whose avg marks is &amp;gt;=60&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;h6&gt;Create or replace view studview as select * from students where average&amp;gt;=60;&lt;/h6&gt;   &lt;/li&gt;    &lt;li&gt; select * from studview&lt;/li&gt;    &lt;li&gt; Update students set roll=100 where roll=1;&lt;/li&gt;    &lt;li&gt;Select * from studview;&lt;/li&gt;    &lt;li&gt;Insert into students values(101,&#39;Nayana Tara&#39;);&lt;/li&gt;    &lt;li&gt;Select * from students;&lt;/li&gt;    &lt;li&gt;delete from studview where roll=101;&lt;/li&gt;    &lt;li&gt;select * from studview;&lt;/li&gt;    &lt;li&gt; Select * from students;&lt;b&gt;&lt;/b&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h6&gt;&lt;/h6&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;What ever operations you perform on views will be affected to the table that is associated with the view&lt;/p&gt;  &lt;p&gt;&lt;b&gt;To Create Read Only Views&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;We can create read only view. When a view is created with &lt;b&gt;any arithmetic expression &lt;/b&gt;or &lt;b&gt;with any group by functions &lt;/b&gt;then it cannot be modified or changed with insert or update or delete statements. So this can be called as a &lt;b&gt;Read Only Views&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Creates a view, with all department numbers and number of Employee’s working under each department &lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;create view deptgroup as select deptno,count(*) &amp;quot;no of emps&amp;quot; from emp group by deptno&lt;/li&gt;    &lt;li&gt;select * from deptgroup;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;EPTNO no of emps&lt;/p&gt;  &lt;p&gt;----- ----------&lt;/p&gt;  &lt;p&gt;10 3&lt;/p&gt;  &lt;p&gt;20 5&lt;/p&gt;  &lt;p&gt;30 6&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;When Ever We Apply Any Dml Operation On This View, We Will Get An Error Message&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; insert into deptgroup values(40,2);&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;*&lt;b&gt;ERROR at line 1:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ORA-01732: data manipulation operation not legal on this view&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; Delete from deptgroup where deptno=10&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;ERROR at line 1:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ORA-01732: data manipulation operation not legal on this view&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Composite View&lt;/font&gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;A view created by retrieving data from 2 or more tables. A composite view can not be updated or we can not apply any DML operations except select statement. Because it contains different columns from different tables.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Create or replace view &amp;lt;view name&amp;gt; as select &amp;lt;fields list&amp;gt; from &lt;b&gt;&amp;lt;table1&amp;gt;,&amp;lt;table2&amp;gt;,……..&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. create view which should show all columns in emp and dept table whose &lt;/b&gt;&lt;b&gt;deptno in emp is equal to deptno in dept table&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;create or replace view empdept as select empno, ename, sal, dname, loc, dept.deptno from emp, dept where emp.deptno=dept.deptno&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt; select * from empdept;&lt;/p&gt;  &lt;p&gt; insert into empdept values(1000,&#39;hello&#39;,5000,&#39;Production&#39;,&#39;tirupati&#39;,20)&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ERROR at line 1:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ORA-01776: cannot modify more than one base table through a join view&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note :&lt;/b&gt; Since here two table values are included. So at a time we cann’t insert or modify two tables data at a time&lt;/p&gt;  &lt;h4&gt;But if you give like this ,&lt;/h4&gt;  &lt;p&gt;update empdept set empno=1000 where empno=8900;&lt;/p&gt;  &lt;p&gt;update empdept set empno=2000,ename=&#39;jj&#39;,sal=1000 where empno=1000&lt;/p&gt;  &lt;p&gt;update empdept set dname=&#39;cmp&#39;,loc=&#39;tpt&#39;,deptno=30 where empno=2000&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;Records will be updated, since you are updating values only on a single table related columns, if you apply any operations on both table columns then can not perform any operation&lt;/p&gt;  &lt;p&gt;select * from empdept;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Dropping a view&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Drop View : &lt;/b&gt;This command is used to drop any view from the database&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Drop view &amp;lt;view name&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Example : Drop View empdept;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note :&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;To show all Views details (both user defined and pre defined views)&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Select * from all_Views;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;&lt;b&gt;To show all User Defined Views details&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; Select * from user_Views;&lt;/li&gt;    &lt;li&gt;Select view_name from all_Views;&lt;/li&gt; &lt;/ul&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/views.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-5870425659001234283</guid><pubDate>Wed, 23 Mar 2011 12:12:00 +0000</pubDate><atom:updated>2011-03-23T17:42:04.641+05:30</atom:updated><title>Joins</title><description>&lt;h4&gt;&lt;u&gt;Joins&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;One of the most important features of SQL is the ability to define relationships between multiple tables and draw information from them in terms of these relationships, all within a single command.&lt;/p&gt;  &lt;p&gt;With joins we can combine columns from different tables. The connection between tables is established through the &lt;b&gt;WHERE clause&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Table and column alias names&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The full name of a column of a table actually consists of the table name followed by a dot and then the column name e.g. emp.empno, emp.ename etc&lt;/p&gt;  &lt;p&gt;User can omit the table names if one is querying only single table at a time. Even when querying on multiple tables one can still be able to omit the table names provided that all the column names are different.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Types of Joins&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;There are five types of joins, they are&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;&lt;b&gt;Equi Join ( = )&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;&lt;b&gt;Non Equi Join ( &amp;lt; , &amp;gt; , &amp;gt;=, &amp;lt;=, !=)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;3. &lt;/b&gt;&lt;b&gt;Outer Join ( + )&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;4. &lt;/b&gt;&lt;b&gt;Self Join &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;5. &lt;/b&gt;&lt;b&gt;Cross Join (Introduced in Orace 9i)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;General Syntax :&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Select &amp;lt; select list &amp;gt; from &amp;lt;table1&amp;gt;,&amp;lt;table2&amp;gt;,-----, &amp;lt;table N&amp;gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Where &amp;lt;table1.column1&amp;gt;=&amp;lt;table2.column2&amp;gt; and ------&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Equi Join&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;When two tables joined together using equality operator, then it is called Equi Join. Table prefixes are utilized to prevent ambiguity and the Where clause specifies the columns being joined&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;List the employee numbers, names, department numbers and dept names&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; Select empno,ename,emp.deptno,dname from emp,dept &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;where emp.deptno=dept.deptno;&lt;/p&gt;  &lt;p&gt;Here, the deptno column exists in both the tables. To avoid ambiguity, the column name should be qualified with the table name ( or with an alias of table name)&lt;/p&gt;  &lt;p&gt;Both the table names need to be specified(emp and dept) the where clause defines the joining condition ie joining the deptno of emp table to the deptno of dept table. Here, it checks for the equality of values in these columns&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Using Table Aliases&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;It can be very tedious to type table names repeatedly. Temporary labels ( or aliases) can be used in the FROM clause. These temporary names are valid only for the current select statement. Table aliases should also be specified in the select clause. Table aliases can be up to 30 characters in length, but the shorter they are the better.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : List Employee Numbers, Names, Department Numbers, And Department Names From Emp And Dept Tables Using Aliases&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; select e.empno, e.ename, e.deptno, d.dname from emp e, dept d &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;where e.deptno=d.deptno;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Non Equi Join&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Using this we can retrieve data from two or more tables by specifying a condition on the common column with any non-equi join operators (&amp;gt;, &amp;lt;, &amp;gt;=, &amp;lt;=,!=) &lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Write a query to display those employees details whose salary of emp1 table &amp;gt;= salary of emp table employees&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;select eno,ena,emp1.sal,emp.sal from emp1,emp where emp1.sal&amp;gt;=emp.sal&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Outer Join&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;It is used to retrieve the common data from both tables and all values from the table having outer join operator (+)&lt;/p&gt;  &lt;p&gt;There are 2 types of outer joins&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;Left Outer Join&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;Right Outer Join&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Left Outer Join&lt;/font&gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;It is used to retrieve all rows from the table having the outer join &lt;/p&gt;  &lt;p&gt;operator which is left side to the outer join and common values from another &lt;/p&gt;  &lt;p&gt;table&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;b&gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Display the list of employees working in each department. Display the department information even if no employee belongs to that department&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;select empno,ename,sal,emp.deptno,dname,loc from emp, dept where &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;emp.deptno(+)=dept.deptno&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Right Outer Join&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;It is used to retrieve all rows from the table having the outer join &lt;/p&gt;  &lt;p&gt;operator which is right side to the outer join and common values from another &lt;/p&gt;  &lt;p&gt;table&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;b&gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Display the list of employees working in each department. Display the employee information even if no such department belongs to the dept table&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; select empno,ename,sal,emp.deptno,dname,loc from emp, dept where &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;emp.deptno=dept.deptno(+)&lt;/p&gt;  &lt;p&gt;if the symbol (+) is placed on the other side of the equation then all the employee details with no corresponding department name and location , will be displayed with NULL values in Dname and Loc columns&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Rules to place ( + ) Operator&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;1. The outer join symbol (+) can not be on both the sides&lt;/p&gt;  &lt;p&gt;2. We can not outer join the same table to more than one other table in a single Select statement&lt;/p&gt;  &lt;p&gt;3. A condition involving an outer join may not use the IN operator or be linked to another condition by the OR operator&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Self Join&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;To join a table to itself means that each row of the table is combined with itself with every other row of the table. The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;b&gt; list out the names of the manager with the employees in the emp table&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; Select worker.ename, manager.ename ‘Manager’ from emp worker, emp manager where worker.mgr=manager.empno;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Cross Join&lt;/font&gt; (oracle 9i concept)&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;A cross joins returns what’s known as a Cartesian product. This means that the join combines every row from the left table with every row in the right table. This type of join can be used in situations where it is desired, to select all possible combinations of rows and columns from both tables. This kind of join is usually not preferred as it may run for a long time and produce a huge result set that may not be useful&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Select &amp;lt;select list &amp;gt; from table1 alias &lt;b&gt;cross join &lt;/b&gt;table2 alias&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;b&gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Display the list of employees working in each department. Display the employee information even if no such department belongs to the dept table and also display the department details even if no employee belongs to that department&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;select empno,ename,sal,emp.deptno,dname,loc from emp Cross join dept&lt;/li&gt; &lt;/ul&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/joins.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-3960008533146248288</guid><pubDate>Tue, 22 Mar 2011 17:44:00 +0000</pubDate><atom:updated>2011-05-05T18:27:41.559+05:30</atom:updated><title>Sub Queries Or Nested Queries</title><description>&lt;h4&gt;&lt;u&gt;Sub Queries Or Nested Queries&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;A query within another query is called a sub query. We can define any number of sub queries with in a query. But the system executes the inner most query first. If we are using relational operators between the queries then the sub query must return a single value.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;The Following Points should be kept in Mind While using SubQueries&lt;/u&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The inner Query must be enclosed in parentheses &lt;/li&gt;    &lt;li&gt;The Inner query must be on the right hand side of the condition &lt;/li&gt;    &lt;li&gt;The sub query may not have an order by clause &lt;/li&gt;    &lt;li&gt;The Order By clause appears at the end of the main select statement &lt;/li&gt;    &lt;li&gt;Sub queries are always executed from the most deeply nested to the least deeply nested &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;Note : &lt;/b&gt;If we use operators “in” and “not in” then the sub query followed by the operators can return more than one value.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Example Queries&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;1.&lt;/u&gt;&lt;b&gt;List the employee’s Details who belong to the department of “Ward”&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;In general, first &lt;/b&gt;we have to know; what the department of “Ward” and then we can display all the employee’s details belonging to “Ward”&lt;/p&gt;  &lt;p&gt;&lt;b&gt;select * from emp where ename=&#39;WARD&#39;;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO&lt;/p&gt;  &lt;p&gt;--------- ---------- --------- --------- ------------ --------- --------- ---------&lt;/p&gt;  &lt;p&gt;7521 WARD salesman 7698 22-FEB-81 1250 500 30&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; select * from emp where deptno=30;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO&lt;/p&gt;  &lt;p&gt;--------- ---------- -------------- ----------- ------------- --------- --------- ---------&lt;/p&gt;  &lt;p&gt;7499 ALLEN salesman 7698 20-FEB-81 1600 300 30&lt;/p&gt;  &lt;p&gt;7521 WARD salesman 7698 22-FEB-81 1250 500 30&lt;/p&gt;  &lt;p&gt;7654 MARTIN salesman 7698 28-SEP-81 1250 1400 30&lt;/p&gt;  &lt;p&gt;7698 BLAKE manager 7839 01-MAY-81 2850 30&lt;/p&gt;  &lt;p&gt;7844 TURNER salesman 7698 08-SEP-81 1500 0 30&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Using Sub Queries,&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; select * from emp where deptno=(select deptno from emp where ename=&#39;WARD&#39;);&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO&lt;/p&gt;  &lt;p&gt;--------- ---------- -------------- ----------- ------------- --------- --------- ---------&lt;/p&gt;  &lt;p&gt;7499 ALLEN salesman 7698 20-FEB-81 1600 300 30&lt;/p&gt;  &lt;p&gt;7521 WARD salesman 7698 22-FEB-81 1250 500 30&lt;/p&gt;  &lt;p&gt;7654 MARTIN salesman 7698 28-SEP-81 1250 1400 30&lt;/p&gt;  &lt;p&gt;7698 BLAKE manager 7839 01-MAY-81 2850 30&lt;/p&gt;  &lt;p&gt;7844 TURNER salesman 7698 08-SEP-81 1500 0 30&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2.List the employee’s details whose salary is greater than the average salary of all the employees&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;In general, first &lt;/b&gt;we have to know; average sal of all employee’s and then we can display all the employee’s details whose salary is &amp;gt; average salary of all employees&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; select avg(sal) from emp;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;AVG(SAL)&lt;/p&gt;  &lt;p&gt;---------&lt;/p&gt;  &lt;p&gt;2161.3636&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; select * from emp where sal&amp;gt;=2161;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO&lt;/p&gt;  &lt;p&gt;--------- ---------- ------------- --------- ------------- --------- --------- -----------&lt;/p&gt;  &lt;p&gt;7566 JONES Manager 7839 02-APR-81 2975 20&lt;/p&gt;  &lt;p&gt;7698 BLAKE Manager 7839 01-MAY-81 2850 30&lt;/p&gt;  &lt;p&gt;7782 CLARK Manager 7839 09-JUN-81 2450 10&lt;/p&gt;  &lt;p&gt;7788 SCOTT Analyst 7566 19-APR-87 3000 20&lt;/p&gt;  &lt;p&gt;7839 KING President 17-NOV-81 5000 10&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Using Sub Queries,&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; select * from emp where sal&amp;gt;(select avg(sal) from emp);&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO&lt;/p&gt;  &lt;p&gt;--------- ---------- ------------- --------- ------------- --------- --------- -----------&lt;/p&gt;  &lt;p&gt;7566 JONES Manager 7839 02-APR-81 2975 20&lt;/p&gt;  &lt;p&gt;7698 BLAKE Manager 7839 01-MAY-81 2850 30&lt;/p&gt;  &lt;p&gt;7782 CLARK Manager 7839 09-JUN-81 2450 10&lt;/p&gt;  &lt;p&gt;7788 Mahesh Analyst 7566 14-FEB-78 3000 20&lt;/p&gt;  &lt;p&gt;7839 KING President 17-NOV-81 5000 10&lt;/p&gt;  &lt;p&gt;&lt;b&gt;3.Write a Query to find the details of the department whose manager’s empcode ‘7698’&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;In general, first &lt;/b&gt;we have to know; department number whose manager empcode is “7698” and then we can display all the department details of that employee&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; select * from dept;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;DEPTNO DNAME LOC&lt;/p&gt;  &lt;p&gt;--------- -------------- -----------------&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;10 ACCOUNTING NEW YORK&lt;/p&gt;  &lt;p&gt;20 RESEARCH DALLAS&lt;/p&gt;  &lt;p&gt;30 SALES CHICAGO&lt;/p&gt;  &lt;p&gt;40 OPERATIONS BOSTON&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; select deptno from emp where mgr=&#39;7698&#39;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;DEPTNO&lt;/p&gt;  &lt;p&gt;---------&lt;/p&gt;  &lt;p&gt;30&lt;/p&gt;  &lt;p&gt;30&lt;/p&gt;  &lt;p&gt;30&lt;/p&gt;  &lt;p&gt;30&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Using sub Query,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;select * from dept where deptno=(select deptno from emp where mgr=&#39;7698&#39;)&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ERROR at line 1:&lt;/b&gt;&lt;/p&gt;  &lt;h4&gt;ORA-01427: single-row subquery returns more than one row&lt;/h4&gt;  &lt;ul&gt;   &lt;li&gt; select * from dept where deptno in (select deptno from emp where mgr=&#39;7698&#39;)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;DEPTNO DNAME LOC&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;--------- -------------- -------------&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;30 SALES &lt;/b&gt;&lt;b&gt;CHICAGO&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;or you can give, like this&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; select * from dept where deptno=(select distinct deptno from emp where mgr=&#39;7698&#39;);&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;DEPTNO DNAME LOC&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;--------- -------------- -------------&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;30 SALES &lt;/b&gt;&lt;b&gt;CHICAGO&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;4.List the details of all the employee’s who earn lowest salary in each department&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt; select * from emp where sal in(select min(sal) from emp group by deptno)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO&lt;/p&gt;  &lt;p&gt;-------- ---------- --------- --------- ------------- --------- --------- ---------&lt;/p&gt;  &lt;p&gt;7369 SMITH Clerk 7902 17-DEC-80 800 20&lt;/p&gt;  &lt;p&gt;7521 WARD Salesman 7698 22-FEB-81 1250 500 30&lt;/p&gt;  &lt;p&gt;7654 MARTIN Salesman 7698 28-SEP-81 1250 1400 30&lt;/p&gt;  &lt;p&gt;7782 CLARK Manager 7839 09-JUN-81 2450 10&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Co related Sub query:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A sub query is executed once for the parent statement whereas the correlated sub query is executed once for each row of the parent query.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Find all employees who earn more than the average salary in their department.&lt;/p&gt;  &lt;p&gt;SELECT last-named, salary, department_id FROM employees A WHERE salary &amp;gt; (SELECT AVG (salary) FROM employees B WHERE B.department_id =A.department_id Group by B.department_id)&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/sub-queries-or-nested-queries.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2511702443521701494.post-5030915125573367006</guid><pubDate>Tue, 22 Mar 2011 17:41:00 +0000</pubDate><atom:updated>2011-03-22T23:11:06.898+05:30</atom:updated><title>Constraints</title><description>&lt;h4&gt;&lt;u&gt;Constraints&lt;/u&gt;&lt;/h4&gt;  &lt;p&gt;These are the conditions or rules that we impose on any column for entering valid data into the table. Constraints are a part of the table definition that are used to limit the values entered into its columns. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note :&lt;/b&gt;&lt;b&gt; &lt;/b&gt;Constraints can be imposed in two ways &lt;/p&gt;  &lt;p&gt;   &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;   &lt;b&gt;(i) Table Level :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Imposing constraints on a &lt;b&gt;table level &lt;/b&gt;by specifying the constraint type at the end of the columns is called &lt;b&gt;Table Level Constraints. &lt;/b&gt;In Table Level, the Constraint key word followed by Constraint name must be defined otherwise error comes.&lt;i&gt;&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;(ii) Column Level :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Imposing constraints on a &lt;b&gt;column &lt;/b&gt;by specifying the constraint type beside the column name is called Column Level Constraints&lt;/p&gt;  &lt;p&gt;Constraints can be defined in two ways &lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;&lt;b&gt;In the Table Definition itself&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;&lt;b&gt;Using Alter Command&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The following are a list of constraints&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;NOT NULL CONSTRAINT&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;UNIQUE CONSTRAINT&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;PRIMARY KEY CONSTRAINT&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;CHECK CONSTRAINT&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;DEFAULT&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;REFERENCES (FOREIGN KEY CONSTRAINT )&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;NOT NULL&lt;/font&gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The &lt;b&gt;Not Null &lt;/b&gt;constraint specifies that a column can not contain &lt;b&gt;Nulls&lt;/b&gt;. To satisfy this constraint every row in the table must contain a value for the column.&lt;/p&gt;  &lt;p&gt;if you do not specify not null, the column can contain nulls by default&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Not Null in Column Level&lt;/b&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;Ø &lt;b&gt;Create table &amp;lt;Table Name&amp;gt; ( Column &amp;lt;Data type&amp;gt;&amp;lt;size&amp;gt; Not Null,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Column2 &amp;lt;Data Type&amp;gt;&amp;lt;size&amp;gt; ,-------);&lt;/b&gt;&lt;/p&gt;  &lt;h3&gt;&lt;u&gt;&lt;/u&gt;&lt;/h3&gt;  &lt;p&gt;&lt;b&gt;Adding Not Null Using Alter Command for Existing Table&lt;/b&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Ø &lt;b&gt;Alter Table &amp;lt;Table Name&amp;gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Modify (&amp;lt;Column Name &amp;gt; &amp;lt;data type&amp;gt; (size) not null)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note :&lt;/b&gt;&lt;b&gt; &lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;Column&lt;/b&gt; must be empty to impose the not Null constraint with alter table statement. &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Table Level is &lt;/b&gt;not applicable for &lt;b&gt;Not Null constraint&lt;/b&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create table sample1(sno number(3), sname varchar2(20) not null);&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Ø insert into sample1 values(&amp;amp;sno,&#39;&amp;amp;sna&#39;);&lt;/p&gt;  &lt;p&gt;Enter value for sno: &lt;b&gt;100&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Enter value for sna: &lt;b&gt;Mahesh&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Enter value for sno: 101&lt;/p&gt;  &lt;p&gt;Enter value for sna: (nothing is entered) &lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;Error Comes&lt;/h4&gt;  &lt;p&gt;mandatory (NOT NULL) column is missing or NULL during insert&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Unique Constraint&lt;/font&gt;&lt;/b&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;This constraint does not allow duplicate values into any column.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax: (For Column Level)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create Table &amp;lt;Table Name&amp;gt;(Column1 &amp;lt;Data Type&amp;gt;&amp;lt;Size&amp;gt; Unique,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Column2 &amp;lt;Data Type&amp;gt;&amp;lt;Size&amp;gt;,--------);&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax: (For Table Level)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create Table &amp;lt;Table Name&amp;gt;(Column1 &amp;lt;Data Type&amp;gt;&amp;lt;Size&amp;gt;,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Column2 &amp;lt;Data Type&amp;gt;&amp;lt;Size&amp;gt;,--------,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Constraint &amp;lt;Constraint Name&amp;gt; Unique &amp;lt;Column Name&amp;gt;);&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;( Or )&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;h4&gt;Example : Imposing unique constraint Table Level&lt;/h4&gt;  &lt;p&gt;&lt;b&gt;create table customer(cno number(3),cname varchar2(20),&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;constraint con_un unique(cno));&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Ø insert into customer values(&amp;amp;cno,&#39;&amp;amp;cname&#39;);&lt;/p&gt;  &lt;p&gt;Enter value for cno: &lt;b&gt;100&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Enter value for cname: &lt;b&gt;Mahesh&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Enter value for cn: &lt;b&gt;100&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Enter value for c: &lt;b&gt;Nithya&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Error Comes&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;unique constraint (SCOTT.CON_UN) violated&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : Imposing unique constraint Column Level&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;create table customer1(cno number(3) unique ,cname varchar2(20));&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Ø insert into customer1 values(&amp;amp;cno, &#39;&amp;amp;cname&#39;);&lt;/p&gt;  &lt;p&gt;Enter value for cno: &lt;b&gt;100&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Enter value for cname: &lt;b&gt;Mahi&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Enter value for cn: &lt;b&gt;100&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Enter value for c: &lt;b&gt;Prasad&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Error Comes&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;unique constraint (SCOTT.SYS_C00385) violated&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Default Constraint :&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The default constraint is used to specify a default value for the column, when you want to given default values. You can give default value as NULL or some other value.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note :&lt;/b&gt;&lt;b&gt; &lt;/b&gt;Only &lt;b&gt;Column level &lt;/b&gt;definition can be &lt;b&gt;applied &lt;/b&gt;to &lt;b&gt;“Default constraint”&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax : for column level&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create Table &amp;lt;Table Name&amp;gt;(Column1 &amp;lt;Data Type&amp;gt;&amp;lt;Size&amp;gt; default &amp;lt;value&amp;gt;,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Column2 &amp;lt;Data Type&amp;gt;&amp;lt;Size&amp;gt;,--------);&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Ø create table sample2(sno number(3) default 10,sname varchar2(20), phone &lt;/p&gt;  &lt;p&gt;varchar2(10) default null)&lt;/p&gt;  &lt;p&gt;Ø insert into sample2 values(1,&lt;b&gt;&#39;Mahesh&#39;&lt;/b&gt;,&#39;11111&#39;)&lt;/p&gt;  &lt;p&gt;Ø insert into sample2 values(2,&lt;b&gt;&#39;Prasad&#39;&lt;/b&gt;,&#39;22222&#39;)&lt;/p&gt;  &lt;p&gt;Ø insert into sample2(sno, sname) values (3,&#39;Nithya&#39;)&lt;/p&gt;  &lt;p&gt;Ø insert into sample2(sno, sname) values (4,&#39;Saloni&#39;)&lt;/p&gt;  &lt;p&gt;Ø select * from sample2;&lt;/p&gt;  &lt;p&gt;SNO SNAME PHONE&lt;/p&gt;  &lt;p&gt;--------- -------------------- ----------&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1 Mahesh 11111&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;2 Prasad 22222&lt;/p&gt;  &lt;p&gt;3 Nithya&lt;/p&gt;  &lt;p&gt;4 Saloni&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example : 2&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create table sample3(sno number(3) default 100, sname varchar2(10) not null);&lt;/p&gt;  &lt;p&gt;Ø insert into sample3 values(200,&#39;Nithya&#39;)&lt;/p&gt;  &lt;p&gt;Ø insert into sample3 values(201,&#39;Aruna&#39;)&lt;/p&gt;  &lt;p&gt;Ø insert into sample3(sname) values(&#39;Mahesh&#39;)&lt;/p&gt;  &lt;p&gt;Ø insert into sample3(sname) values(&#39;Saloni&#39;)&lt;/p&gt;  &lt;p&gt;&lt;b&gt;select * from sample3;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;SNO SNAME&lt;/p&gt;  &lt;p&gt;--------- ----------------&lt;/p&gt;  &lt;p&gt;200 Nithya&lt;/p&gt;  &lt;p&gt;201 Aruna&lt;/p&gt;  &lt;p&gt;&lt;b&gt;100 Mahesh&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;100 Saloni&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Check Constraint&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The check constraint explicitly defines a condition. It will put restrictions on the range of the column values and the values to be accepted into the column. The condition of a check constraint can refer to any column in the table. But it can not refer to any column in other tables.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax : (column Level)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Create table &amp;lt;table name&amp;gt; (&amp;lt;column1&amp;gt; &amp;lt;datatype&amp;gt; [size] check(condition),&lt;/p&gt;  &lt;p&gt;&amp;lt;column2&amp;gt;&amp;lt;data type&amp;gt; &amp;lt;size&amp;gt;,-----);&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Create table items(itemno number(3) check(itemno&amp;gt;=100),itemname varchar2(10));&lt;/p&gt;  &lt;p&gt;Ø Insert into items values (100,&#39;Rice&#39;)&lt;/p&gt;  &lt;p&gt;Ø insert into items values(101,&#39;Paste&#39;)&lt;/p&gt;  &lt;p&gt;Ø insert into items values(99,&#39;Chacolate&#39;) &lt;b&gt;à&lt;/b&gt;&lt;b&gt; Error comes&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax : (Table Level)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Create table &amp;lt;table name&amp;gt; &lt;/p&gt;  &lt;p&gt;(&amp;lt;column1&amp;gt; &amp;lt;datatype&amp;gt; [size],&lt;/p&gt;  &lt;p&gt;&amp;lt;column2&amp;gt;&amp;lt;data type&amp;gt; &amp;lt;size&amp;gt;,&lt;/p&gt;  &lt;p&gt;-------------------&lt;/p&gt;  &lt;p&gt;constraint &amp;lt;constraint name&amp;gt; check(&amp;lt;column name&amp;gt; with condition));&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;create table item1(itno number(3), Itname varchar2(10),&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Constraint con_check check (itno&amp;gt;=100)); &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Ø Insert into item1 values (100,&#39;Rice&#39;)&lt;/p&gt;  &lt;p&gt;Ø insert into item1 values(101,&#39;Paste&#39;)&lt;/p&gt;  &lt;p&gt;Ø insert into item1 values(99,&#39;Chacolate&#39;) &lt;b&gt;à&lt;/b&gt;&lt;b&gt; Error comes&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Primary Key Constraint&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;It avoids null values and also it does not allows duplicate values. Ie A &lt;b&gt;Primary key &lt;/b&gt;is nothing but combination of not null and unique constraints&lt;/p&gt;  &lt;p&gt;To Impose Primary Key On Any Column, You Can Use Either Table Level Or Column Level&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax: Table Level&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;create table &amp;lt;tablename&amp;gt;(column1 &amp;lt;datatype&amp;gt;&amp;lt;size&amp;gt;,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;column2 &amp;lt;datatype&amp;gt;&amp;lt;size&amp;gt;,--------,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;constraint &amp;lt;constraint name&amp;gt; primary key &amp;lt;column name&amp;gt;);&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Ex:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create table items1(itemno number(3),itemname varchar2(20),&lt;/p&gt;  &lt;p&gt;constraint con_pk primary key(itemno));&lt;/p&gt;  &lt;p&gt;Ø insert into items1 values(100,’Rice’);&lt;/p&gt;  &lt;p&gt;Ø insert into items1 values(100,’paste’);&lt;/p&gt;  &lt;h4&gt;Error Comes&lt;/h4&gt;  &lt;p&gt;unique constraint (SCOTT.CON_PK) violated&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax : Column Level&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;create table &amp;lt;tablename&amp;gt;(column1 &amp;lt;datatype&amp;gt;&amp;lt;size&amp;gt; primary key ,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;column2 &amp;lt;datatype&amp;gt;&amp;lt;size&amp;gt;,--------);&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create table items2(itemno number(3) primary key, itemname varchar2(20));&lt;/p&gt;  &lt;p&gt;Ø insert into items2 values(100 ,&#39;Wheat&#39;);&lt;/p&gt;  &lt;p&gt;Ø insert into items2 values(100 ,&#39;Rice&#39;);&lt;/p&gt;  &lt;h4&gt;Error Comes&lt;/h4&gt;  &lt;p&gt;unique constraint (SCOTT.CON_PK) violated&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Example 2&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Ø insert into item values(null ,&#39;Wheat&#39;);&lt;/p&gt;  &lt;h4&gt;Error Comes&lt;/h4&gt;  &lt;p&gt;mandatory (NOT NULL) column is missing or NULL during insert&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font color=&quot;#0000ff&quot;&gt;Foreign Key Constraint&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;This is used to establish relationship between 2 or more tables for inserting common or similar values into the related columns of the table.&lt;/p&gt;  &lt;p&gt;The table from which we are establishing relation is called child table. Because the related column in the child table depends on another table for its values.&lt;/p&gt;  &lt;p&gt;The table on which other tables are depending on the table, to which we are establishing a relation is called Mother table.&lt;/p&gt;  &lt;p&gt;If two tables are having this relationship then the system checks whether the new value that we are inserting is related column of child table exists in the mother table or not.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;There are five major restrictions related to foreign key&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The Mother table column related must have primary key&lt;/li&gt;    &lt;li&gt;We can not drop the mother table with out child table provided. &lt;/li&gt;    &lt;li&gt;The data type and the size of the related columns in both the tables must be same and name can be different&lt;/li&gt;    &lt;li&gt;We can establish relation to any number of tables&lt;/li&gt;    &lt;li&gt;We can not impose foreign key which are having values.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax : (Column Level)&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create Table &amp;lt;Table Name&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;(&amp;lt;column1&amp;gt;&amp;lt;data type&amp;gt;&amp;lt;size&amp;gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;References &amp;lt;Mother Table&amp;gt;(&amp;lt;column name&amp;gt;),&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&amp;lt;column2&amp;gt;&amp;lt;data type&amp;gt;&amp;lt;size&amp;gt;,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;--------------&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&amp;lt;columnN&amp;gt;&amp;lt;data type&amp;gt;&amp;lt;size&amp;gt;);&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Syntax : (Table Level)&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Create Table &amp;lt;Table Name&amp;gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;(&amp;lt;column1&amp;gt;&amp;lt;data type&amp;gt;&amp;lt;size&amp;gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&amp;lt;column2&amp;gt;&amp;lt;data type&amp;gt;&amp;lt;size&amp;gt;,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;--------------&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&amp;lt;columnN&amp;gt;&amp;lt;data type&amp;gt;&amp;lt;size&amp;gt;,&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;constraint &amp;lt;constraint Name&amp;gt; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;foreign key(child column) &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;References &amp;lt;Mother Table&amp;gt; (&amp;lt;column name&amp;gt;));&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Making relationships between two tables&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;1. First Create the Mother Table or Parent Table&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create table bankmast&lt;/p&gt;  &lt;p&gt;(accno number(3) primary key,&lt;/p&gt;  &lt;p&gt;accna varchar2(10) not null,&lt;/p&gt;  &lt;p&gt;balance number(8,2));&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;h4&gt;2. Next Create the Child Table &lt;/h4&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;create table banktrans&lt;/p&gt;  &lt;p&gt;(acno number(3) references bankmast(accno),&lt;/p&gt;  &lt;p&gt;trmode char(1),trdate date,&lt;/p&gt;  &lt;p&gt;amount number(8,2));&lt;/p&gt;  &lt;h5&gt;3. Next insert the values in to mother table first,&lt;/h5&gt;  &lt;p&gt;Ø insert into bankmast values(&amp;amp;accno,&#39;&amp;amp;accna&#39;,&amp;amp;bal);&lt;/p&gt;  &lt;p&gt;&lt;b&gt;select * from bankmast;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;ACCNO ACCNA BALANCE&lt;/p&gt;  &lt;p&gt;--------- ---------- -----------&lt;/p&gt;  &lt;p&gt;100 Priya 5000&lt;/p&gt;  &lt;h4&gt;101 Mahesh 6500&lt;/h4&gt;  &lt;p&gt;102 Saryu 4500&lt;/p&gt;  &lt;p&gt;103 Nandhini 7600&lt;/p&gt;  &lt;p&gt;&lt;b&gt;4. Next insert values into child table&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Ø insert into banktrans values(&amp;amp;acno,&#39;&amp;amp;trmode&#39;,&#39;&amp;amp;trdate&#39;,&amp;amp;amt);&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Ø insert into banktrans values(100,&#39;D&#39;,&#39;5-May-05&#39;,5000);&lt;/p&gt;  &lt;p&gt;Ø insert into banktrans values(102,&#39;W&#39;,&#39;15-Apr-05&#39;,2000);&lt;/p&gt;  &lt;p&gt;Ø insert into banktrans values(104,&#39;D&#39;,&#39;10-Feb-05&#39;,10000);&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ERROR : (since there is no such account number present in the mother table)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ORA-02291: integrity constraint (SCOTT.SYS_C00674) violated - parent key not found&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;SQL&amp;gt; select * from banktrans;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;ACNO T TRDATE AMOUNT&lt;/p&gt;  &lt;p&gt;--------- --- ------------- ------------&lt;/p&gt;  &lt;p&gt;100 d 05-MAY-05 5000&lt;/p&gt;  &lt;p&gt;102 w 01-JUN-05 2000&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Dropping Relationship Tables&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note :&lt;/b&gt;&lt;b&gt; &lt;/b&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;if you try to drop the mother table when child table is present, it will give an error message&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;drop table bankmast;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ERROR :&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;ORA-02449: unique/primary keys in table referenced by foreign keys&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;ol start=&quot;start&quot;&gt;   &lt;li&gt;To drop the mother table, First Drop the Child table and then drop the mother table &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;Drop table banktrans;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Drop table bankmast;&lt;/b&gt;&lt;/p&gt;  </description><link>http://naveen-plsql.blogspot.com/2011/03/constraints.html</link><author>noreply@blogger.com (Naveen)</author><thr:total>1</thr:total></item></channel></rss>