2011-08-03 48 views

回答

3

默認情況下,它允許您更改列,它會截斷比新長度長的字符串,並且會生成警告。

mysql> create table t (v varchar(20)); 
Query OK, 0 rows affected (0.06 sec) 

mysql> insert into t values ('123456789'); 
Query OK, 1 row affected (0.00 sec) 

mysql> alter table t modify column v varchar(10); 
Query OK, 1 row affected, 1 warning (0.04 sec) 
Records: 1 Duplicates: 0 Warnings: 1 

mysql> show warnings; 
+---------+------+----------------------------------------+ 
| Level | Code | Message        | 
+---------+------+----------------------------------------+ 
| Warning | 1265 | Data truncated for column 'v' at row 1 | 
+---------+------+----------------------------------------+ 
1 row in set (0.00 sec) 

mysql> select * from t; 
+------------+ 
| v   | 
+------------+ 
| 1234567890 | 
+------------+ 
1 row in set (0.00 sec) 

如果你有SQL modeSTRICT_ALL_TABLESSTRICT_TRANS_TABLES集,警告變成了錯誤,ALTER將失敗。

mysql> alter table t modify column v varchar(10); 
ERROR 1265 (01000): Data truncated for column 'v' at row 1 
+0

謝謝你這個優秀的解釋@Bill。我腦海中的所有問題都解決了。 :-) – Aufwind

+0

爲我工作:查詢確定,2919470行受影響,27062個警告(45分鐘41.31秒) –

相關問題