In SQL, to insert NULL values into fields of a table, you can use the INSERT INTO statement. When inserting NULL values, you simply omit the value for the column or explicitly specify NULL as the value for the column.
Syntax for inserting NULL values:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, NULL, value3, ...);
Here are a couple of examples to illustrate:
Example 1: Inserting NULL into a specific column
Suppose you have a table named employees with columns id, name, and hire_date, and you want to insert a row where the hire_date is NULL:
INSERT INTO employees (id, name, hire_date)
VALUES (1, 'John Doe', NULL);
In this example, the hire_date column will receive a NULL value.
Example 2: Inserting NULL for multiple columns
If you want to insert NULL values for multiple columns:
INSERT INTO employees (id, name, hire_date)
VALUES (2, NULL, NULL);
Here, both name and hire_date will be set to NULL.
Important Notes:
- Make sure that the column allows
NULLvalues. If a column is defined with aNOT NULLconstraint, attempting to insertNULLwill result in an error. - If you're omitting a value for a column (and the column allows
NULL), the column will automatically getNULLunless the table has a default value defined for that column.
No comments:
Post a Comment