SQL NOT NULL Constraint

SQL NOT NULL Constraint

SQL NOT NULL Constraint

The NOT NULL constraint in SQL ensures that a column cannot have a NULL value. It enforces that every row must have a value for the specified column, ensuring data integrity.


Key Points

  1. Mandatory Values:

    • Prevents inserting or updating rows with a NULL value for the column.
  2. Column-Level Constraint:

    • Applied to individual columns when creating or altering a table.
  3. Default Behavior:

    • If not explicitly set, columns can be accepted NULL by default in most databases.

Syntax

1. Define NOT NULL During Table Creation

CREATE TABLE table_name ( column1 DataType NOT NULL, column2 DataType );

2. Add NOT NULL to an Existing Column

ALTER TABLE table_name MODIFY column_name DataType NOT NULL; -- MySQL
ALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL; -- PostgreSQL, SQL Server

3. Remove NOT NULL Constraint

ALTER TABLE table_name MODIFY column_name DataType; -- MySQL
ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULL; -- PostgreSQL, SQL Server

Examples

1. Create a Table with NOT NULL Constraint

CREATE TABLE Employees ( EmployeeID INT NOT NULL, Name VARCHAR(50) NOT NULL, DepartmentID INT );
  • EmployeeID and Name must have a value in every row.
  • DepartmentID can accept NULL.

2. Insert Values into a NOT NULL Column

INSERT INTO Employees (EmployeeID, Name, DepartmentID) VALUES (1, 'Alice', NULL); -- Valid, as DepartmentID allows NULL INSERT INTO Employees (EmployeeID, Name) VALUES (NULL, 'Bob'); -- Error: EmployeeID cannot be NULL

3. Adding NOT NULL to an Existing Column

If a column is already created without a NOT NULL constraint, it can be added later:

ALTER TABLE Employees ALTER COLUMN Name SET NOT NULL; -- PostgreSQL, SQL Server

Use Cases

  1. Mandatory Fields:

    • For columns like ID, Name, or Email, which must always have a value.
  2. Data Consistency:

    • Prevents accidental null values that could cause errors in queries or applications.
  3. Primary Key Columns:

    • Primary keys inherently have the NOT NULL constraint since they must uniquely identify rows.

Best Practices

  1. Identify Critical Columns:

    • Apply NOT NULL to columns where null values would be illogical (e.g., identifiers, dates, or names).
  2. Default Values:

    • Consider combining NOT NULL with a DEFAULT value to avoid NULL errors during inserts.
    CREATE TABLE Orders ( OrderID INT NOT NULL, OrderDate DATE NOT NULL DEFAULT CURRENT_DATE );
  3. Check for Existing NULLs:

    • Before adding a NOT NULL constraint to an existing column, ensure no rows currently contain NULL.
    SELECT * FROM Employees WHERE Name IS NULL;
  4. Test Updates and Inserts:

    • Validate changes to avoid conflicts with existing queries or applications.

Comparison with DEFAULT and NULL Constraints

FeatureNOT NULLDEFAULTNULL
Prevents NULL ValuesYesNoNo
Requires ValueYesNo (uses default)No (accepts NULL)
Suitable for Primary KeysYesNoNo

Conclusion

The NOT NULL constraint is an essential tool for enforcing data integrity in SQL. By ensuring that certain columns cannot have NULL values, it helps prevent incomplete or invalid data from being stored in the database.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close