如何在mysql中将bigint更改为int,并且表具有外键
我最近注意到我的一个数据库中有几个 bigint 类型字段可以替换为“int”类型。问题是数据库已经在线并正在使用,而且还有外键在起作用,所以当我尝试更改离线数据库中的数据类型时,mysql 不会让我这样做并产生错误消息:“#1025 - 重命名 (...) 时出错。
那么如何在保持外键满意的同时更改这些字段呢? (并且不会清除现有的数据库!)
I recently noticed that there are several bigint type fields in one of my databases that could be replaced with type "int". The problem is that the database is already online and in use and also that there are foreign keys in action, so when I try changing the data type in the offline DB, mysql won't let me and produces the error message: "#1025 - Error on rename of (...)".
So how can I change these fields while keeping the foreign keys happy? (and not wiping out the existing DB!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要列中没有任何不适合较小 INT 数据类型的数据,您就可以将列从 BIGINT 更改为 INT。
更改表 mytable
修改列 mycolumn INT(11);
You can alter a column from BIGINT to INT as long as there is not any data in the column that would not fit into the smaller INT datatype.
ALTER TABLE mytable
MODIFY COLUMN mycolumn INT(11);
您可以尝试:
DROP
和CREATE
)。You could try:
DROP
andCREATE
).当字段保存数据时,您无法减小字段的大小。您只能扩展它们。
您可以做的是导出所有数据、禁用键、清空表、更改字段大小和字段大小。再次导入数据。
You can't reduce the size of field when it holds data. You can only expand them.
What you can do is export all the data, disable the keys, empty the tables, change the field sizes & import the data again.