Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-08-24 23:53:04
forward
1240 people have browsed it

当我们使用 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
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!