NOT NULL constraint NOT NULL constraint:
Mandatory columns cannot be NULL values, constraints enforce that fields always contain values. This means that you cannot insert a new record or update a record without adding a value to the field.
1. Create not null constraints on the Id and name columns when the "Persons" table is created :
create table Persons(
id int not NULL,
p_name varchar(20) not null,
deparment varchar(20),
address varchar(20),
telNum varchar(20)
)
DESC Persons;
result:
insert
insert into Persons(id,p_name,deparment,address,telNum) values('1','Zhang San','Marketing Department','X Road No.Y','110') SELECT * FROM Persons
result:
Insert a null value to see the effect:
3. Remove the not null constraint after the table is created (via the ALTER TABLE statement):
ALTER TABLE Persons MODIFY p_name varchar(20); DESC Persons;
4. Add a not null constraint after the table is created (via the ALTER TABLE statement):
ALTER TABLE Persons MODIFY p_name varchar(20) not null; DESC Persons;