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

RMAN Different errors and their Solution

RMAN Different errors and their Solution Backup Fails with Invalid RECID Error: RMAN-20035, RMAN-6038 When you attempt a backup and receive the following error messages: RMAN-3014: Implicit resync of recovery catalog failed RMAN-6038: Recovery catalog package detected an error RMAN-20035: Invalid high RECID error It indicates the control file and the recovery catalog is not synchronized. RMAN detects that the control file currently in use is older than the control file previously used to resynchronize. Cause: This due to any of the scenario you are restore a backup controlfile through a non-oracle mechanism and then open the database through Resetlogs option or making a copy of control file through operating system utility and trying the restore on new system through RNAN. You do not use catalog so RMAN does not get any information regarding this process.  The recovery catalog indicates that the highest RECID is 100, but the control file indicates that the highe...

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_...

How to Monitor RMAN Backup through SQL Query

To Monitor RMAN you can use OEM or any other tools such as "Quest Backup Report for Oracle". You can also used following Views in oracle 9i to check the RMAN backup status: v$rman_configuration v$backup_set v$backup_piece v$backup_spfile v$session_longops Script to check RMAN Configuration: SELECT  name "PARAMETERS", value "RMAN CONFIGURATION" FROM  v$rman_configuration ORDER BY name; PARAMETERS RMAN CONFIGURATION BACKUP OPTIMIZATION ON CHANNEL DEVICE TYPE DISK FORMAT   'H:\ORABACK\%U' CONTROLFILE AUTOBACKUP ON CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO 'H:\ORABACK\%F' DEFAULT DEVICE TYPE TO DISK RETENTION POLICY TO RECOVERY WINDOW OF 30 DAYS Script to List RMAN Backup Piece: SELECT bs.recid, DECODE(   bp.status, 'A', 'Available', 'D', 'De...