Home > Database > Mysql Tutorial > body text

How can we apply NOT NULL constraint to a column of an existing MySQL table?

王林
Release: 2023-09-05 23:53:02
forward
665 people have browsed it

我们如何将 NOT NULL 约束应用于现有 MySQL 表的列?

#We can apply NOT NULL constraints to columns of existing MySQL tables with the help of ALTER TABLE statement.

Grammar

ALTER TABLE table_name MODIFY colum_name datatype NOT NULL; 
Copy after login

Example

mysql> Create table test123(ID INT, Date DATE);
Query OK, 0 rows affected (0.19 sec)

mysql> Describe test123;

+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| ID    | int(11) | YES  |     | NULL    |       |
| Date  | date    | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.04 sec)

mysql> ALTER TABLE test123 MODIFY ID INT NOT NULL;
Query OK, 0 rows affected (0.54 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> Describe test123;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| ID    | int(11) | NO   |     | NULL    |       |
| Date  | date    | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.06 sec)
Copy after login

The above is the detailed content of How can we apply NOT NULL constraint to a column of an existing MySQL table?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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