如何更改已在数据库中注册的 id 列类型
How to change id column type already registered in the database
我正在尝试将我的 Id 列类型从 int 更改为 bigInt,因为记录太大,如果我尝试存储更多记录,我会收到错误消息,但我不确定是否安全像这样更改 table:
alter table the_table
alter the_column type bigint;
我正在使用 Java Springboot 创建实体和 Hibernate 以实现持久性。由于数据库中有非常重要的记录,以安全方式解决此问题的最佳方法是什么?
MySQL 的正确 SQL 代码为:
ALTER TABLE the_table MODIFY COLUMN the_column BIGINT;
大的tables需要一些时间(因为需要复制数据);如果你想在 table 上不中断,你可以尝试使用 percona 工具 (https://www.percona.com/doc/percona-toolkit/3.0/pt-online-schema-change.html) 中的 pt-online-schema-change
命令,或者如果你想更安全地做到这一点(但使用数据中断)
RENAME TABLE the_table TO the_table_old
CREATE TABLE the_table LIKE the_table_old
REPLACE INTO the_table select * from the_table_old limit 0,1000
REPLACE INTO the_table select * from the_table_old limit 1000,1000
REPLACE INTO the_table select * from the_table_old limit 2000,1000
DROP TABLE the_table_old
但是回答你的问题 - 这是在 table 上进行更改的安全方法;这可能需要一些时间,如果您只是 运行 更改 table
,table 将被锁定为 updates/inserts
为什么你不能这样做?
ALTER TABLE tableName ALTER COLUMN ID bigint
我想先在测试环境中尝试一下,但这对我来说总是有效的
我正在尝试将我的 Id 列类型从 int 更改为 bigInt,因为记录太大,如果我尝试存储更多记录,我会收到错误消息,但我不确定是否安全像这样更改 table:
alter table the_table
alter the_column type bigint;
我正在使用 Java Springboot 创建实体和 Hibernate 以实现持久性。由于数据库中有非常重要的记录,以安全方式解决此问题的最佳方法是什么?
MySQL 的正确 SQL 代码为:
ALTER TABLE the_table MODIFY COLUMN the_column BIGINT;
大的tables需要一些时间(因为需要复制数据);如果你想在 table 上不中断,你可以尝试使用 percona 工具 (https://www.percona.com/doc/percona-toolkit/3.0/pt-online-schema-change.html) 中的 pt-online-schema-change
命令,或者如果你想更安全地做到这一点(但使用数据中断)
RENAME TABLE the_table TO the_table_old
CREATE TABLE the_table LIKE the_table_old
REPLACE INTO the_table select * from the_table_old limit 0,1000
REPLACE INTO the_table select * from the_table_old limit 1000,1000
REPLACE INTO the_table select * from the_table_old limit 2000,1000
DROP TABLE the_table_old
但是回答你的问题 - 这是在 table 上进行更改的安全方法;这可能需要一些时间,如果您只是 运行 更改 table
,table 将被锁定为 updates/inserts为什么你不能这样做?
ALTER TABLE tableName ALTER COLUMN ID bigint
我想先在测试环境中尝试一下,但这对我来说总是有效的