Skip to main content

DEFAULT ON NULL Feature in Oracle 12c

DEFAULT ON NULL Feature in Oracle 12c


Prior to 12c, the DEFAULT for a column would not be applied if you explicitly or implicitly inserted a NULL into that column. The functionality has been limited with various restrictions. For example you are restricted from using a SEQUENCE object to supply a default value. If there is a need to insert or update the default value you have to either use DEFAULT keyword into the statement or leave the column out of the INSERT statement entirely. In fact adding a new column that permists NULL values with a dfault values was an offline operation.
SQL> create table TEST (id# number, name varchar2(20) default 'SPACE');
Table created.
SQL> insert into TEST values (1,NULL);
1 row created.
SQL> select id#, decode(name, NULL,'Null',name) from TEST;
 ID#       DECODE(NAME)
---------- ----------
         1 Null
1 row selected.
SQL> insert into TEST(id#) values (2);
1 row created.
SQL> select id#, decode(name,NULL,'A Null',name) from TEST;
 ID#       DECODE(DAT
---------- ----------
         1 Null
         2 SPACE
2 rows selected.
However, in oracle 12c these restrictionS and functionality limitations have been removed. It introduced a DEFAULT ON NULL clause. So now we can create a default column value not only when we use the DEFAULT keyword but also when we set the coulnm value explicitly to NULL.
SQL> create table TEST (id#, name varchar2(20) default on null 'SPACE');
Table created.
SQL> insert into TEST values (1, NULL);
1 row created.
SQL> select * from TEST;
 ID#    NAME
---------- ----------
         1 SPACE
SQL> insert into TEST (id#) values (2);
1 row created.
SQL> select * from TEST;
 ID#    NAME
---------- ----------
         1 SPACE
         2 SPACE
And now in oracle 12c, it is also possible to specify the CURRVAL and NEXTVAL sequence to create a default column value without using trigger as you are doing before oracle 12c.

Default Value from CURRVAL AND NEXTVAL
CREATE SEQUENCE seq1;
CREATE TABLE TEST (CUST_ID# NUMBER DEFAULT SEQ1.NEXTVAL, NAME VARCHAR2(30));
INSERT INTO TEST (NAME) VALUES ('SADHAN');
INSERT INTO TEST (ID#, NAME) VALUES (102, 'MUJAZ');
SQL> select * from TEST;
    CUST_ID#  NAME
----------    ------------------------------
       101    SADHAN
       102    MUJAZ
The following example shows how to use NEXTVAL for master table and CURRVAL for details or child table.
CREATE SEQUENCE cust_master_seq;
CREATE SEQUENCE cust_details_seq;
CREATE TABLE cust_master (id# NUMBER DEFAULT cust_master_seq.NEXTVAL, name VARCHAR2(30));
CREATE TABLE cust_details (id#  NUMBER DEFAULT cust_details_seq.NEXTVAL, cust_id NUMBER DEFAULT cust_master_seq.CURRVAL, name  VARCHAR2(30));
insert into cust_master (name) values ('SADHAN');
insert into cust_details (name) values ('SADHAN_RAWDA');
insert into cust_details (name) values ('SADHAN_OLAYA');
SQL> select * from cust_master;
  CUST_ID  NAME
---------- --------------------
         1 SADHAN
SQL> select * from cust_details;
    ID#   CUST_ID NAME
-----------   ------- ---------------------------------------
         1        101 SADHAN_RAWDA
         2        101 SADHAN_OLAYA

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