Select*fromtest2;+------+--------+|ID |Name | +------+--------+|1 |Gaurav||NULL|Rahu"/> Select*fromtest2;+------+--------+|ID |Name | +------+--------+|1 |Gaurav||NULL|Rahu">

Home >Database >Mysql Tutorial >What happens when we use the ALTER TABLE statement to apply a NOT NULL constraint on a column that contains NULL values?

What happens when we use the ALTER TABLE statement to apply a NOT NULL constraint on a column that contains NULL values?

WBOY
WBOYforward
2023-08-24 23:53:041294browse

当我们使用 ALTER TABLE 语句对包含 NULL 值的列应用 NOT NULL 约束时会发生什么?

In this case, MySQL will return an error message that the data for the column is truncated. Here is an example to demonstrate it -

Example

Suppose we have a table "test2" where row 2 contains NULL value in the "ID" column. Now, if we try to declare the column ID as NOT NULL, then MySQL will return an error as shown below -

mysql> Select * from test2;
+------+--------+
| ID   | Name   |
+------+--------+
| 1    | Gaurav |
| NULL | Rahul  |
+------+--------+
2 rows in set (0.00 sec)
mysql> ALTER TABLE TEST2 MODIFY ID INT NOT NULL;
ERROR 1265 (01000): Data truncated for column 'ID' at row 2

The above is the detailed content of What happens when we use the ALTER TABLE statement to apply a NOT NULL constraint on a column that contains NULL values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete