In mysql, you can use the "ALTER TABLE" statement to delete the "not null" non-null restriction from the columns of the specified table. You can use modify to modify the data type and constraints of the fields in the table. The syntax is "ALTER TABLE table name MODIFY field name INT NULL;".
The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.
We can use the ALTER TABLE statement to delete NOT NULL constraints from columns in existing tables.
modify is used to modify the data length, data type and field constraints of the fields in the table.
MySQL 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. This can be achieved with the CREATE TABLE or ALTER TABLE statement. Add the keyword NOT NULL as a qualifier after the definition of a column in the table to constrain the value of the column to not be empty.
For example, in the user information table, if the user name is not added, then this user information will be invalid. At this time, you can set a non-null constraint for the user name field.
Example
Suppose we have a table "test123" with a NOT NULL constraint on column "ID" as follows:
mysql> DESCRIBE test123; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | ID | int(11) | NO | | NULL | | | Date | date | YES | | NULL | | +-------+---------+------+-----+---------+-------+
Now , if we want to remove NOT NULL constraint, we can use ALTER TABLE statement as follows:
The above result set shows that NOT for column "ID" has been removed NULL constraints.
In the above query, the keyword NULL after the keyword MODIFY is optional. The following query will also produce the same results as above -
mysql> ALTER TABLE test123 MODIFY ID INT; Records: 0 Duplicates: 0 Warnings: 0
Recommended learning: mysql video tutorial
The above is the detailed content of How to delete not null restriction in mysql. For more information, please follow other related articles on the PHP Chinese website!