MySQL数据版本控制

发布于 2024-10-16 12:06:20 字数 984 浏览 2 评论 0原文

有没有办法将MySQL设置为每次更改一行时,然后使用原始数据创建到另一个表/数据库的行? (带时间戳)

如果是这样我该怎么做?

例如,

UPDATE `live_db`.`people`
SET    `live_db`.`people`.`name` = 'bob'
WHERE  `id` = 1;  

导致这种情况在更新之前发生:

INSERT INTO `changes_db`.`people`
SELECT *
FROM   `live_db`.`people`
WHERE  `live_db`.`people`.`id` = 1;  

如果您再次执行此操作,则会导致类似这样的结果:

`live_db`.`people`
+----+-------+---------------------+
| id | name  | created             |
+----+-------+---------------------+
| 1  | jones | 10:32:20 12/06/2010 |
+----+-------+---------------------+

`changes_db`.`people`
+----+-------+---------------------+
| id | name  | updated             |
+----+-------+---------------------+
| 1  | billy | 12:11:25 13/06/2010 |
| 1  | bob   | 03:01:54 14/06/2010 |
+----+-------+---------------------+

实时数据库需要在行上有一个创建的时间戳,并且更改数据库需要有一个时间戳实时数据库行已更新。 更改数据库也将没有主键和外键约束。

我正在使用 InnoDB 和 MySQL 5.1.49,但如果需要可以升级。

Is there any way to setup MySQL to every time a row is changed, then a row to another table/database is created with what the data was originally? (with time stamping)

If so how would I go about doing it?

E.g.

UPDATE `live_db`.`people`
SET    `live_db`.`people`.`name` = 'bob'
WHERE  `id` = 1;  

Causes this to happen before the update:

INSERT INTO `changes_db`.`people`
SELECT *
FROM   `live_db`.`people`
WHERE  `live_db`.`people`.`id` = 1;  

And if you did it again it would result in something like this:

`live_db`.`people`
+----+-------+---------------------+
| id | name  | created             |
+----+-------+---------------------+
| 1  | jones | 10:32:20 12/06/2010 |
+----+-------+---------------------+

`changes_db`.`people`
+----+-------+---------------------+
| id | name  | updated             |
+----+-------+---------------------+
| 1  | billy | 12:11:25 13/06/2010 |
| 1  | bob   | 03:01:54 14/06/2010 |
+----+-------+---------------------+

The live DB needs to have a created time stamp on the rows, and the changes DB needs to have a time stamp of when the live DB row was updated.
The changes DB will also have no primary keys and foreign key constraints.

I'm using InnoDB and MySQL 5.1.49 but can upgrade if required.

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

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

发布评论

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

评论(4

一世旳自豪 2024-10-23 12:06:20

使用触发器

MySQL 对触发器的支持从 MySQL 版本 5.0 开始。 2.

Use a Trigger

MySQL support for triggers started with MySQL version 5.0.2.

听,心雨的声音 2024-10-23 12:06:20

您可以创建一个触发器

DELIMITER \\
CREATE TRIGGER logtrigger BEFORE UPDATE ON live_db.people
FOR EACH ROW BEGIN
    INSERT INTO changes_db.people(id,name,updated) VALUES(OLD.id,OLD.name,now());
END;
\\

You can create a trigger:

DELIMITER \\
CREATE TRIGGER logtrigger BEFORE UPDATE ON live_db.people
FOR EACH ROW BEGIN
    INSERT INTO changes_db.people(id,name,updated) VALUES(OLD.id,OLD.name,now());
END;
\\
漫漫岁月 2024-10-23 12:06:20

这就是我最终做到的

DELIMITER |

# Create the log table
CREATE TABLE IF NOT EXISTS `DB_LOG`.`TABLE`
LIKE `DB`.`TABLE`|

# Remove any auto increment
ALTER TABLE `DB_LOG`.`TABLE` CHANGE `DB_LOG`.`TABLE`.`PK` `DB_LOG`.`TABLE`.`PK` INT UNSIGNED NOT NULL|
# Drop the primary keys
ALTER TABLE `DB_LOG`.`TABLE` DROP PRIMARY KEY|

#Create the trigger
DROP TRIGGER IF EXISTS `DB`.`update_TABLE`|
CREATE TRIGGER `DB`.`update_TABLE` BEFORE UPDATE ON `DB`.`TABLE` FOR EACH ROW
BEGIN
    INSERT INTO `DB_LOG`.`TABLE`
    SELECT `DB`.`TABLE`.*
    FROM `DB`.`TABLE`
    WHERE `DB`.`TABLE`.`PK` = NEW.`PK`;
END|

DELIMITER ;

This is how I ended up doing it

DELIMITER |

# Create the log table
CREATE TABLE IF NOT EXISTS `DB_LOG`.`TABLE`
LIKE `DB`.`TABLE`|

# Remove any auto increment
ALTER TABLE `DB_LOG`.`TABLE` CHANGE `DB_LOG`.`TABLE`.`PK` `DB_LOG`.`TABLE`.`PK` INT UNSIGNED NOT NULL|
# Drop the primary keys
ALTER TABLE `DB_LOG`.`TABLE` DROP PRIMARY KEY|

#Create the trigger
DROP TRIGGER IF EXISTS `DB`.`update_TABLE`|
CREATE TRIGGER `DB`.`update_TABLE` BEFORE UPDATE ON `DB`.`TABLE` FOR EACH ROW
BEGIN
    INSERT INTO `DB_LOG`.`TABLE`
    SELECT `DB`.`TABLE`.*
    FROM `DB`.`TABLE`
    WHERE `DB`.`TABLE`.`PK` = NEW.`PK`;
END|

DELIMITER ;
素染倾城色 2024-10-23 12:06:20

很抱歉对旧帖子发表评论,但我一直在寻找解决这个问题的方法!我想我会分享这个信息。

这完美地概述了一个解决方案:

http://www.hirmet.com /mysql-versioning-records-of-tables-using-triggers

Sorry to comment on an old post, but I was looking to solve this exact problem! Thought I would share this information.

This outlines a solution perfectly:

http://www.hirmet.com/mysql-versioning-records-of-tables-using-triggers

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