Difficulty: Easy
Correct Answer: Use the NULL keyword or a host variable with a suitable null indicator for the nullable columns in the INSERT statement.
Explanation:
Introduction / Context:
Handling nullable columns is a basic but important aspect of DB2 programming. Interviewers often ask how to insert rows when some columns are allowed to be null, because it tests both SQL syntax knowledge and understanding of host variable handling in embedded SQL environments such as COBOL or C programs.
Given Data / Assumptions:
Concept / Approach:
DB2 uses the concept of a null indicator for host variables and the NULL keyword in pure SQL. When coding interactive SQL, the NULL keyword can be placed directly in the VALUES list to indicate that the column has no value. In embedded SQL, a companion indicator variable specifies whether the host variable contains a real value or represents a null. Therefore, the correct option must mention the use of NULL or null indicators, not attempts to avoid nullable columns entirely.
Step-by-Step Solution:
Step 1: Consider a simple table EMP(ID, NAME, MIDDLE_NAME) where MIDDLE_NAME is nullable.
Step 2: An interactive SQL insert could be written as INSERT INTO EMP (ID, NAME, MIDDLE_NAME) VALUES (1001, 'RAVI KUMAR', NULL).
Step 3: In an embedded SQL program, you would declare a host variable for MIDDLE_NAME and an integer null indicator for it.
Step 4: Set the indicator to -1 when you want MIDDLE_NAME to be stored as null; set it to 0 when the host variable contains a real character value.
Step 5: Execute the INSERT statement; DB2 interprets the indicator and stores a null or the actual value as required.
Verification / Alternative check:
You can verify this behavior by inserting one row with a real value and another with NULL, then using a SELECT that checks for MIDDLE_NAME IS NULL. The row inserted with the NULL keyword or a negative indicator will be returned by that predicate, while the row with a real string will not, confirming that the technique correctly handles nullable columns.
Why Other Options Are Wrong:
Option B is wrong because nullable columns do not require a non null default in every INSERT; that would defeat the purpose of allowing nulls.
Option C is wrong because using a temporary table without nullable columns does not solve the fundamental requirement and adds unnecessary complexity.
Option D is wrong because DB2 fully supports inserting nulls directly; there is no need to rely on special utilities to populate nullable fields later.
Common Pitfalls:
A common mistake is forgetting to declare and use null indicator variables in embedded SQL programs, which can cause unexpected data values or SQL errors. Another pitfall is confusing empty strings with null values for character columns; an empty string is still a value, whereas null means no value is present. Developers should always check application logic to ensure they intentionally store null only when appropriate and not as a side effect of missing assignments.
Final Answer:
The correct technique is to use the NULL keyword or a host variable with a suitable null indicator for the nullable columns in the INSERT statement.
Discussion & Comments