当前位置:网站首页>MySQL "invalid use of null value" solution

MySQL "invalid use of null value" solution

2022-07-07 13:51:00 Full stack programmer webmaster

1. Problem description

Because to change ”information” In the table ” Number ” Column is not empty , Use database query statements “alter table information modify ‘ Number ’ varchar(255) not null” You can change other columns , But it's just an error in the number column , The error message is Invalid use of NULL value.

Is very strange , Other columns can be queried by using this command , Why can't you check only the number column .

2. The reason for the error

The reason for this error may be the existing data ‘ Number ’ As a null, And not null The settings of conflict .( The reason found ).

I think it's because when I first created a new data table , The table structure of this column has been set , When making subsequent changes, it will conflict with the previous settings ( As for why it can't be changed here , I don't quite understand ), Because I set the structure attribute of the numbered column when creating a new table .

Since it cannot be set to non empty , So create a new column and set not null, You can succeed .

3. terms of settlement

1) Add new column , Set the structure properties of the column .

alter table information add column ‘yyy’ varchar(255) not null first; - Add a new column yyy To the first column .

2) Copy the contents of the wrong column into the new column and delete the wrong column

update information set yyy=’ Number ’; — take ’ Number ’ All values of the column are copied to yyy Column .

alter table information drop ‘ Number ’; — Delete the wrong column .

3) Modify the new column name to the wrong column name

alter table information change ‘yyy’ ‘ Number ’ varchar(255) not null;

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/113291.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071146403072.html