使用modify修改字段報錯如下:
mysql> alter table student modify name sname char(16);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sname char(16)' at line 1
經親測modify與change的區別在於修改字段名稱只能是用change,modify不能修改字段名稱
在網上看到很多人說change不能修改字段的類型,實際上是都可以的,只需要注意使用change修改字段類型的時候,即使不修改字段名稱但是也要把原名稱寫上,否則會報錯
mysql> alter table student change name name varchar(32) not null; Query OK, 15 rows affected (0.03 sec) Records: 15 Duplicates: 0 Warnings: 0
mysql> alter table student modify sname char(16); Query OK, 15 rows affected (0.16 sec) Records: 15 Duplicates: 0 Warnings: 0
修改字段名稱: (change)
注意修改字段名稱只能是用change,modify不能修改字段名稱。
mysql> alter table student change gender sex char(32) not null; Query OK, 15 rows affected (0.11 sec) Records: 15 Duplicates: 0 Warnings: 0