Home > Database > Mysql Tutorial > body text

How to delete not null restriction in mysql

WBOY
Release: 2022-04-06 15:25:15
Original
7208 people have browsed it

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;".

How to delete not null restriction in mysql

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

How to delete not null constraints in mysql

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  |       |
+-------+---------+------+-----+---------+-------+
Copy after login

Now , if we want to remove NOT NULL constraint, we can use ALTER TABLE statement as follows:

How to delete not null restriction in mysql

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!