MySQL-最佳方法

时间:2019-02-17 13:36:46

标签: mysql

我在一个表中大约有500行,每天要根据JavaScript对象进行更新。大多数将已经存在于表中,并且具有与新数据相同的值。有些可能是javascript对象中的新功能。有些可能在表中,但不在对象中。 除了键和其他几个字段外,还有两个日期字段,“ dateCreated”和“ lastUpdated”

如果键(例如4567654)不存在,我要插入行,并将'dateCreated'设置为今天。

如果键存在,并且值相同,我想将“ lastUpdated”字段设置为今天。

如果确实存在,但值已更改,我想插入新行,但保留旧值。我正在考虑通过添加后缀(4567654_1)来修改现有密钥,或将旧行移至存档表。

我最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

在重复键上插入将触发更新触发器,因此也许您正在寻找类似的东西

drop table if exists t,t1;
create table t(id int primary key, val int, created_date datetime, lastLoaded datetime);
create table t1(archiveid  int auto_increment primary key,id int, val int, created_date datetime, lastLoaded datetime);
drop trigger if exists t;
delimiter $$
create trigger t before insert on t
for each row
begin
        if not exists(select 1 from t where id = new.id) then
            set new.created_date = '2019-01-01 01:00:00';
            set new.lastloaded = '2019-01-01 01:00:00';
            insert into debug_table(msg1) values (concat(new.id,': notfound:',new.lastloaded));
        end if;

end $$

drop trigger if exists t2;
delimiter $$
create trigger t2 before update on t
for each row
begin
        set new.lastloaded = '2019-01-01 08:00:00';
        insert into debug_table(msg1) values (concat(new.id,': before update:',new.lastloaded));
        if new.val  <> old.val then
            insert into t1(id,val,created_date,lastloaded) values (old.id,old.val,old.created_date,old.lastloaded);
        end if;
end $$
delimiter ;

truncate table debug_table;
insert into t(id,val) values
(1,10),(2,20),(3,30)
on duplicate key
update val = values(val);
commit;

select * from t;

insert into t(id,val) values
(1,10),(2,20),(3,35)
on duplicate key
update val = values(val);

select * from t;
+----+------+---------------------+---------------------+
| id | val  | created_date        | lastLoaded          |
+----+------+---------------------+---------------------+
|  1 |   10 | 2019-01-01 01:00:00 | 2019-01-01 08:00:00 |
|  2 |   20 | 2019-01-01 01:00:00 | 2019-01-01 08:00:00 |
|  3 |   35 | 2019-01-01 01:00:00 | 2019-01-01 08:00:00 |
+----+------+---------------------+---------------------+
3 rows in set (0.00 sec)
select * from t1;
+-----------+------+------+---------------------+---------------------+
| archiveid | id   | val  | created_date        | lastLoaded          |
+-----------+------+------+---------------------+---------------------+
|         1 |    3 |   30 | 2019-01-01 01:00:00 | 2019-01-01 01:00:00 |
+-----------+------+------+---------------------+---------------------+
1 row in set (0.00 sec)
select * from debug_table;
+----+--------------------------------------+
| id | msg1                                 |
+----+--------------------------------------+
|  1 | 1: notfound:2019-01-01 01:00:00      |
|  2 | 2: notfound:2019-01-01 01:00:00      |
|  3 | 3: notfound:2019-01-01 01:00:00      |
|  4 | 1: before update:2019-01-01 08:00:00 |
|  5 | 2: before update:2019-01-01 08:00:00 |
|  6 | 3: before update:2019-01-01 08:00:00 |
+----+--------------------------------------+
6 rows in set (0.00 sec)

您不需要debug_table,只需在其中调试...即可。