mySQL constraints (Constraints): 1. Not NULL constraints NOT NULL constraints

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;

  

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325459215&siteId=291194637