錯誤1:[22001][1138] Data truncation: Invalid use of NULL value
原因:
如果你xxx表已有數據,你是無法新增自增列的,需要中轉一下。因爲自增列需要是key。
解決:
- 第一步,給xxx表加上自增列,不要設置自增屬性,否則會失敗。
- 第二步:執行以下sql給xxx的自增列賦初值。
with cte as (
select row_number() over (order by createTime) as incre_id, <表中主鍵ID> from area_code
) update xxx ac set incre_id = (select incre_id from cte where <表中主鍵ID> = ac.<表中主鍵ID>) where incre_id is null;
-
第三步:設置自增列的額外的屬性吧。例如:主鍵、非空等屬性。
-
ok了,應該沒有問題了
== 注意,以上是在mysql8.0版本上操作的==