SQL NULL Values

SQL NULL Values

SQL NULL Values

In SQL, NULL is a special marker used to indicate that a data value does not exist in the database. NULL is not the same as an empty string (”) or zero (0). It is a value that is unknown, undefined or missing.

When a column is created, it may be defined as allowing NULL values, meaning that it can contain no data. If a column is not defined as allowing NULL values, then it must have a value assigned to it when a new row is added to the table.

Examples:

  1. Create a table called employees with two columns, id and name. The id column is defined as not allowing NULL values, while the name column is defined as allowing NULL values.
				
					CREATE TABLE employees (
    id INT NOT NULL,
    name VARCHAR(50)
);

				
			

In this example, the id column cannot contain NULL values, while the name column can contain NULL values.

  1. Insert a new record into the employees table with NULL value for the name column.
				
					INSERT INTO employees (id, name)
VALUES (1, NULL);

				
			

In this example, a new record is inserted into the employees table with the value 1 for the id column and a NULL value for the name column.

  1. Retrieve all records from the employees table where the name column is NULL.
				
					SELECT * FROM employees
WHERE name IS NULL;

				
			

In this example, the SELECT statement retrieves all records from the employees table where the name column contains NULL values.

By using NULL values, you can handle situations where data is unknown, undefined or missing in your database. It is important to be aware of NULL values when working with SQL, as they can affect the results of your queries and data manipulation.

Join To Get Our Newsletter
Spread the love