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?
In this case, MySQL will return an error message that the data for the column is truncated. Here is an example to demonstrate it -
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!