Overview
NOT Null could be a type of data constraint utilized in Structured Query Language (SQL) to guarantee that a column will never contain a Invalid esteem. It prevents clients from entering invalid information into a database table. NOT Null constraints are often utilized in conjunction with other data constraints such as Unique, Primary KEY, and Foreign KEY constraints. The NOT Null constraint can be indicated when a table is made or included later in an existing table.
The SQL NOT NULL Constraint
Not null is a constraint used in SQL that ensures a value is not empty. This is important for businesses and organizations that rely on their databases for day-to-day operations.
Take, for example, a retail store. If a customer purchases an item, the store must record it in its database. The store would use the Not Null constraint to ensure the purchased item is not left blank. With the constraint, the store could inadvertently create a record with a blank item, which would be accurate and lead to unexpected problems.
Syntax
CREATE TABLE table_name (
column1 datatype NOT NULL,
column2 datatype NOT NULL,
...
);
Example
CREATE TABLE employees (
id INT NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
salary DECIMAL(10,2) NOT NULL
);
Column Name | Data Type | Not Null |
---|---|---|
id | INT | Yes |
first_name | VARCHAR(50) | Yes |
last_name | VARCHAR(50) | Yes |
salary | DECIMAL(10,2) | Yes |
The NOT NULL constraint ensures that a column will never contain a NULL value. This constraint is specified when creating a table or can be added later. In the example above, the id, first_name, last_name, and salary columns will not accept NULL values. This means that if a user attempts to enter NULL values in any of these columns, the query will be rejected.
SQL NOT NULL on ALTER TABLE
The following statement can be used to set a column to NOT NULL on an ALTER TABLE statement:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype NOT NULL;
Example
Table Name: Customers
Column Name | Data Type | Not Null |
---|---|---|
Name | VARCHAR(50) | Yes |
ALTER TABLE Customers
MODIFY COLUMN Name VARCHAR(50) NOT NULL;
This code alters the Customers table by modifying the Name column. The data type of the Name column is set to VARCHAR(50), and the NOT NULL constraint is added, which means that the Name column must have a value in it.
Conclusion
In conclusion, Not Null is an important constraint used in SQL to ensure accurate data is recorded in databases. The retail store must use the Not Null constraint in its database to record customer purchases accurately. Without the constraint, the store could inadvertently create a record with a blank item, which would lead to unexpected problems.
Key takeaways
Quiz
1. What is the correct syntax for a NOT NULL constraint in SQL?
Answer: d. IS NOT NULL
2. What type of constraint is NOT NULL?
Answer: c. Check
3.Can a NOT NULL constraint be added to an existing column?
Answer: a. Yes
4. What is the result of attempting to insert a NULL value into a column with a NOT NULL constraint?
Answer: b. The value is rejected
Top Tutorials
Related Articles