有两个表 一个为temp 一个为 date 我想要在temp中按id删除一条记录时把 这条记录插入到date表中 在Navicat Lite中定义触发器 希望大虾指教一下不胜感激 在线等

解决方案 »

  1.   

    mysql> create table `temp` (
        ->  id int primary key,
        ->  col     int
        -> );
    Query OK, 0 rows affected (0.27 sec)mysql> create table `date`
        -> (
        ->  id      int,
        ->  col int
        -> );
    Query OK, 0 rows affected (0.13 sec)mysql> delimiter //
    mysql>
    mysql> create trigger trg_temp_bd before delete ON `temp`
        ->   FOR EACH ROW BEGIN
        ->     INSERT INTO `date` values(old.id,old.col);
        ->   END;
        ->
        -> //
    Query OK, 0 rows affected (0.11 sec)mysql> delimiter ;
    mysql> insert into `temp` values
        -> (1,100),
        -> (2,200),
        -> (3,300),
        -> (4,400);
    Query OK, 4 rows affected (0.16 sec)
    Records: 4  Duplicates: 0  Warnings: 0mysql> select * from `temp` ;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |  100 |
    |  2 |  200 |
    |  3 |  300 |
    |  4 |  400 |
    +----+------+
    4 rows in set (0.00 sec)mysql> select * from `date`;
    Empty set (0.03 sec)mysql>
    mysql> delete from `temp` where id=2 or id=3;
    Query OK, 2 rows affected (0.11 sec)mysql> select * from `temp` ;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |  100 |
    |  4 |  400 |
    +----+------+
    2 rows in set (0.00 sec)mysql> select * from `date`;
    +------+------+
    | id   | col  |
    +------+------+
    |    2 |  200 |
    |    3 |  300 |
    +------+------+
    2 rows in set (0.00 sec)mysql>