Interview Questions   Tutorials   Discussions   Programs   

DB2 - What do you mean by NOT NULL WITH DEFAULT? When will you use it?




318
views
asked mar August 20, 2014 11:04 AM  

What do you mean by NOT NULL WITH DEFAULT? When will you use it?


           

2 Answers



 
answered By Mswami   0  
NOT NULL WITH DEFAULT: Is used for a column that does not allow null values, but provides a default value.
flag   
   add comment

 
answered By Mswami   0  
Not Null with Default:

When you insert a row into a table and omit the value of one or more columns, these columns can either be null (if the column is defined as nullable) or given a default value. If the column is defined as not nullable, the insert operation will fail unless a value has been provided for the column. DB2 has a defined default value for each of the DB2 data types, but you can explicitly provide a default value for any column. The default value is specified in the CREATE TABLE statement.

BONUS DECIMAL(7,2) NOT NULL WITH DEFAULT 1000.00

By defining your own default value, you can ensure that the column has been populated with appropriate values. The EMPLOYEE table definition shown below includes default values:

CREATE TABLE EMPLOYEE
 (
 ID     SMALLINT NOT NULL,
 NAME    VARCHAR(9) NOT NULL,
 DEPT    SMALLINT,
 HIREDATE  DATE NOT NULL WITH DEFAULT CURRENT_DATE,
 SALARY   DECIMAL(7,2) NOT NULL DEFAULT 10000.00
 );


In this example, all of the columns except the DEPT column require a value (NOT NULL). If no value is specified, the hire date is set to the current date in the system, and the salary is set to 10,000.00. The default value can either be a constant, or one of the special built-in functions. These functions can be very useful in setting column values based on the current environment in which the application is running.
flag   
   add comment

Your answer

Join with account you already have

FF

Preview


Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!

Alert