By default, columns can contain NULL values.
The NOT NULL constraint ensures a column cannot accept them, mandating that every field must have a value.
Thus, inserting or updating records requires specifying a value for this field.
The subsequent SQL guarantees that the “ID”, “LastName”, and “FirstName” columns will not allow NULL values upon creating the “Persons” table:
CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Age int ); |
To apply a NOT NULL constraint to the “Age” column after the creation of the “Persons” table, utilize the following SQL:
SQL Server / MS Access:
ALTER TABLE Persons ALTER COLUMN Age int NOT NULL; |
My SQL / Oracle (prior version 10G):
ALTER TABLE Persons MODIFY COLUMN Age int NOT NULL; |
Oracle 10G and later:
ALTER TABLE Persons MODIFY Age int NOT NULL; |