Skip to main content

Top-N Queries & Truncate Table Enhancement in Oracle 12c

Top-N Queries & Truncate Table Enhancement in Oracle 12c

In fact there is already various method exist to fetch TOP N query results in the previous release. In the oracle 12c retrieving TOP N query simplified and become easier with the new Oracle Database 12c includes support for the ANSI-standard FETCH FIRST/NEXT and OFFSET clauses, together called the row limiting clause. This clause enables you to easily retrieve the first N records from a result set so you can easily retrieve the result set.
The row limiting clause is simply added at the end of any SQL statement to fetch a specific set of records:
§         OFFSET provides a way to skip the N first rows in a result set before starting to return any rows
§         The FETCH clause limits the number of rows returned in the result set
§         For the result offset clause, the value of the integer literal must be equal to 0 (default if the clause is not given), or positive.
§         If it is larger than the number of rows in the underlying result set, no rows are returned.
§         For the fetch first clause, the value of the literal must be 1 or higher.
§         The literal can be omitted, in which case it defaults to 1. If the clause is omitted entirely, all rows (or those rows remaining if a result offset clause is also given) will be returned.
Suppose for Example: if you have employee table with huge set of records and you want to retrieve the first five records then you only need to FETCH FIRST N ROWS to the SQL query.
SQL> select owner, emp#, emp_name
from employee
order by owner, emp#
FETCH FIRST 5 ROWS ONLY;
Thus the row limiting clause is making it easier to do something easier than before. The above example returns the first five records. The use of ONLY clause limits the number of records exactly requested.
SQL> select owner, emp#, emp_name
from employee
order by owner, emp#
OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY;
We can achieve the same result with above query. The use of OFFSET clause, will skip the first five rows and get the next five rows from a result set.

Important point while using OFFSET clause:
  • If the offset is not specified it is assumed to be 0 and Negative values for the offset, row-count or percent are treated as 0.
  • Null values for offset, row-count or percent result in no rows being returned.
  • Fractional portions of offset, row-count or percent are truncated.
  • If the offset is greater than or equal to the total number of rows in the set, no rows are returned.
  • If the row-count or percent are greater than the total number of rows after the offset, all rows are returned.
  • The row limiting clause can not be used with the FOR UPDATE clause, CURRVAL and NEXTVAL sequence pseudo-columns or in an fast refresh materialized view.
Truncate Table Cascade
In the previous release, there was not a direct option available to truncate a master table while child table exist and having records.
TRUNCATE <table-name> CASCADE;
Now the truncate table with cascade option in 12c truncates the records in master as well as all referenced child table with an enabled ON DELETE constraint. This enhancement override the previous truncate table option where truncating all child records before truncating the master records.

Create master/detail table:
CREATE TABLE example_master
(
EMP# NUMBER(4) PRIMARY KEY,
DEPT# NUMBER(10)
)
CREATE TABLE example_child
(
EMP# NUMBER(4) PRIMARY KEY,
DEPT# NUMBER(10)
);

Add reference constraint:
ALTER TABLE example_child
ADD CONSTRAINT exp_child_fk FOREIGN KEY (dept#)
REFERENCE example_master ON DELETE CASCADE;

Add some Records:
Insert into example_master values (101, 10001);
Insert into example_master values (102, 10002);
……
Insert into example_detail values (101, 10001);
Insert into example_detail values (102, 10002);
……
Select * from example_master;
       EMP#       DEPT#
-------------     ------------------
                  101                                    1001
                  102                                    1002
Select * from example_detail;
       EMP#       DEPT#
-------------     ------------------
                  103                                    1001
                  104                                    1002

SQL> TRUNCATE TABLE example_master CASCADE;


Now you will not find any records in master as well as detail table. This option works also for partitioned table but only on table level. If you want to use this option on partition or sub-partition level it works only for reference partitioning.

Comments

Popular posts from this blog

Tablespace Management using Toad

You can view all of your tablespace information using toad tablespace screen from Database –> Administer –> Tablespaces Here each table describes different detailed information across all the tablespaces in database. If you open this screen from the toad DBA module then you are also able to see the “space history” and “IO history” tab along with Files, Free space, object, Fragmentation. With these tab you are able to able perform space monitoring and planning steps. These tab permits you check graphically space and IO usage over time. Through this way DBA are also able to estimate ‘Object size’ based on this estimation. From the below “alter tablespace” tab you can §          Modify the tablespace and datafile size. §          Add new datafile to the tablespace.

Import Excel Data into Oracle using Oracle form Button

Import Excel Data into Oracle using Oracle form Button 1. Create a button on the form with the name "IMPORT_EXCEL" and write a pl/sql on "WHEN BUTTON PRESSED" trigger. BEGIN IF :System.Cursor_Block<>'GL_DAILY_COMPOUND_HEADER' THEN   Go_Item('GL_DAILY_COMPOUND_HEADER.V_DATE'); END IF; IF :System.Mode = 'NORMAL' AND :System.Record_Status IN ('NEW','INSERT') THEN   IMPORT_EXCEL_PROC;   ---Form procedure ELSE  MESSAGE('Import allowed only for new entry!!!');  MESSAGE('Import allowed only for new entry!!!'); END IF; END; 2. Now write the procedure "IMPORT_EXCEL_PROC" into the program unit. Don't forget to create required table and folder for procedure IMPORT_EXCEL_PROC PROCEDURE IMPORT_EXCEL_PROC IS application    OLE2.Obj_Type; workbooks      OLE2.Obj_Type; workbook       OLE2.Obj_Type; worksheets      OLE2.Obj_Type; worksheet       OLE2.Obj_Type; cell      

Changing National Character Set AL16UTF16 to UTF8

Changing National Character Set AL16UTF16 to UTF8 The national character set is used for data that is stored in table columns of the types NCHAR, NVARCHAR2, and NCLOB.  In contrast, the database character set is used for data stored in table columns of the types CHAR, VARCHAR2 and CLOB. Like the database character set, the national character set is defined when the database is initially created and can usually no longer be changed, at least not easily or without involving quite a lot of work (export, recreate database, import). Except when creating the database, where the national character set is defined explicitly, it can change implicitly even when upgrading the database from Oracle8i to Oracle9i (or Oracle10g). You require SYSDBA authorization to change the national character set. Changing the national character set means changing an Oracle Dictionary entry, but no data is changed.  $sqlplus /nolog SQL> CONNECT / AS SYSDBA SQL> Select property_value fro