2 个数据库之间的 MySQL 同步和主键冲突

发布于 2024-11-30 23:43:34 字数 252 浏览 3 评论 0原文

我在开发和生产环境 MySQL 数据库方面遇到问题。我想合并 2 个数据库,但由于内容作者一直在向生产环境添加记录,因此大约有 20 个具有相同主键的重叠记录。

我使用 Navicat 进行数据同步,但它只是用我的开发服务器中的记录更新生产服务器上的记录。主键没有任何意义,我想做的是为这些记录提供新的主键。

通过 Navicat 可以实现吗?如果没有,手动进入数据库并更改主键会是一个坏主意吗?或者说这会影响MySQL的自增能力吗?

谢谢。

I have an issue with a development and production environment MySQL database. I want to merge the 2 databases, but since content authors have been adding records to the production environment, there are about 20 overlapping records with the same primary key.

I am using Navicat for my data sync, but it just updates the records on the production server with those from my development server. The primary keys don't mean anything, and what I would like to do is give those records new primary keys.

Is this possible through Navicat? If not, would it be a bad idea to manually go into the db and change the primary keys? Or does this affect the auto-increment ability of MySQL?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红ご颜醉 2024-12-07 23:43:34

我无法回答 Navicat 的功能,但在 MySQL 中,您可以轻松设置 auto_increment 值。如果是实时数据,我建议在事务中完成整个操作:

START TRANSACTION;
SELECT max(id)+1 INTO @new_id FROM mytable;
SET @range_start = [first ID you'd like to change];
SET @range_end = [last ID you'd like to change];
SET @offset = @new_id - @range_start;
UPDATE mytable SET id = id + @offset WHERE id BETWEEN @range_start AND @range_end;
SELECT @range_end + @offset + 1;
ALTER TABLE mytable AUTO_INCREMENT=[value returned by above statement];
COMMIT;

注意 - ALTER TABLE 语句需要一个常量而不是使用变量。

I can't answer for what Navicat is capable of, but in MySQL, you can easily set the auto_increment value. I recommend doing the whole thing in a transaction if it's live data:

START TRANSACTION;
SELECT max(id)+1 INTO @new_id FROM mytable;
SET @range_start = [first ID you'd like to change];
SET @range_end = [last ID you'd like to change];
SET @offset = @new_id - @range_start;
UPDATE mytable SET id = id + @offset WHERE id BETWEEN @range_start AND @range_end;
SELECT @range_end + @offset + 1;
ALTER TABLE mytable AUTO_INCREMENT=[value returned by above statement];
COMMIT;

Note - the ALTER TABLE statement requires a constant instead of the use of variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文