In mysql, you can modify the field to not null by adding a non-null constraint to the field using the ALTER TABLE statement. The syntax is "ALTER TABLE data table name CHANGE COLUMN field name field name data type NOT NULL;". The ALTER TABLE statement is used to modify the structure of the original table, and "NOT NULL" is the keyword to set a non-null constraint; after adding a non-null constraint to a field, its value cannot be null, otherwise the database system will report an error.
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
In mysql, if you want to change a field to not null, you can add a non-null constraint to the field.
Non-null constraint (NOT NULL) means that the value of the field cannot be empty. For fields that use non-null constraints, if the user does not specify a value when adding data, the database system will report an error.
There are two statements to add non-null constraints to fields:
CREATE TABLE statement
ALTER TABLE statement
But the CREATE TABLE statement is set when creating the table and does not meet the requirements; therefore, the ALTER TABLE statement is used here. Syntax:
ALTER TABLE 数据表名 CHANGE COLUMN 字段名 字段名 数据类型 NOT NULL;
Example: Modify the id field in the tb_students_score table to not null.
Let’s take a look at the table structure first:
DESC tb_students_score;
Add a non-null constraint to the id field
ALTER TABLE tb_students_score CHANGE COLUMN id id int(11) NOT NULL;
Looking at the table structure:
[Related recommendations:mysql video tutorial]
The above is the detailed content of How to change the field to not null in mysql. For more information, please follow other related articles on the PHP Chinese website!