Wednesday, 14 September 2011

Oracle: Get the size of a CLOB column

Assume the table structure is as below:

Table Name: Table_A
Column        Datatype
XMLID           NUMBER
XMLDATA      CLOB

Sample Records/Data:
XMLID   XMLDATA
123        Some text1 stored in CLOB
135        Some text2 stored in CLOB
  

1. If output of size of CLOB column needed in bytes:
SQL>  SELECT XMLID, DBMS_LOB.GETLENGTH(XMLDATA) AS SIZE_IN_BYTES FROM TABLE_A;

Output:
XMLID      SIZE_IN_BYTES
--------      -----------------------
123          25
135          25


2. If output of size of CLOB column needed in kilo bytes (KB) by rounding it off to two decimal places:
SQL>  SELECT XMLID, ROUND(DBMS_LOB.GETLENGTH(XMLDATA) / 1024, 2) AS SIZE_IN_KB FROM TABLE_A;
 
Output:
XMLID      SIZE_IN_KB
--------      --------------------
123          .02
135          .02

Friday, 26 August 2011

Oracle: List all foreign key constraints for a table

In SQL below, replace the <TABLE_NAME> with the table name for which you want to list out all the referential integrity constraints:

 SELECT
    AC.CONSTRAINT_NAME,
    ACC.COLUMN_NAME,
    AR.TABLE_NAME,
    ARC.COLUMN_NAME
FROM ALL_CONSTRAINTS AC,
    ALL_CONSTRAINTS AR,
    ALL_CONS_COLUMNS ACC,
    ALL_CONS_COLUMNS ARC
WHERE AC.CONSTRAINT_TYPE = 'R'
    AND AC.R_OWNER = AR.OWNER
    AND AC.R_CONSTRAINT_NAME = AR.CONSTRAINT_NAME
    AND AC.CONSTRAINT_NAME = ACC.CONSTRAINT_NAME
    AND AC.OWNER = ACC.OWNER
    AND AR.CONSTRAINT_NAME = ARC.CONSTRAINT_NAME
    AND AR.OWNER = ARC.OWNER
    AND AC.TABLE_NAME = UPPER('<TABLE_NAME>');