触发器触发时,将表中的数据插入另一个表中

时间:2011-12-18 10:37:35

标签: mysql triggers

我有4张桌子:

  1. pacienti
  2. cabinete
  3. tmp
  4. vizite。

  5. DROP SCHEMA IF EXISTS tr;
    CREATE SCHEMA IF NOT EXISTS tr; 
    USE tr;
    CREATE TABLE IF NOT EXISTS pacienti
    ( 
       id_pac INT NOT NULL  AUTO_INCREMENT  PRIMARY KEY
       Nume VARCHAR(40) DEFAULT  'anonim',
       Prenume VARCHAR(40) DEFAULT  'anonim',
       UNIQUE (Nume,Prenume)
    ) ENGINE=INNODB;
    
    CREATE TABLE IF NOT EXISTS cabinete 
    (
       id_cab INT  NOT NULL  AUTO_INCREMENT PRIMARY KEY
       Denumire VARCHAR(40) DEFAULT  'nespecificat',
       UNIQUE (Denumire)
    ) ENGINE=INNODB;
    
    CREATE TABLE IF NOT EXISTS tmp 
    (
       id_tmp INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
       DataVizita VARCHAR(10),
       OraIntrare TIME,
       ComplDate DATETIME,
       NumePacient VARCHAR(40) DEFAULT 'anonim',
       PrenumePacient VARCHAR(40) DEFAULT 'anonim',
       NumeMedic VARCHAR(40) DEFAULT 'nespecificat',
       PrenumeMedic VARCHAR(40) DEFAULT 'nespecificat',
       Cabinet VARCHAR(30) DEFAULT 'nespecificat'
    );
    
    CREATE TABLE IF NOT EXISTS vizite 
    (
       id_viz INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
       Data_viz DATETIME,
       Medic_id INT(5) NOT NULL,
       Pacient_id INT(5) NOT NULL,
       Cabinet_id INT(5) NOT NULL, 
       FOREIGN KEY (`Medic_id`) REFERENCES `tr`.`medici`(`id_med`) 
       ON UPDATE RESTRICT ON DELETE CASCADE,
       FOREIGN KEY (`Pacient_id`) REFERENCES `tr`.`pacienti`(`id_pac`)
       ON UPDATE RESTRICT ON DELETE CASCADE,
       FOREIGN KEY (`Cabinet_id`) REFERENCES `tr`.`cabinete`(`id_cab`)
       ON UPDATE RESTRICT ON DELETE CASCADE
    ) ENGINE=INNODB;
    

    我有一个文件,我必须将数据导入tmp表。 文件(* .txt)包含下一个字段:

    DataVizita | OraIntrare | NumePacient | PrenumePacient | NumeMedic | PrenumeMedic | Cabinet
    --------------------------------------------------------------------------------------------
    22/04/2005 | 01:52:34   | Huruiala    |Carmen Alexandra| Garbeata  |  Panait      |   1
    05/12/2005 | 04:16:02   | Schornig    |     Petre      | Rusu      | Dragos Mihai |   2
    07/11/2004 | 11:24:27   | Graur       |Alexandra-Cris. | Soarece   | Dan-Cristian |   4
    07/05/2008 | 06:35:44   | Rudnitchi   |     Marian     | Galescu   |George Valentin|  5
     .
     .
     .
     etc…
    

    在我tmp表中进行导入之后,我必须做一些操作:

    1. 创建一个必须concat DataVizitaOraIntrare进入DATETIME字段的触发器(我通过创建一个将DataVizita转换为正确格式并将其连接起来的函数来完成OraIntrare并将结果放在ComplDate表格中的tmp

      这是我已经做过的事情并且有效:

      CREATE FUNCTION formdate(ziua VARCHAR(10), timpul TIME)RETURNS DATETIME       
      DERMINISTIC    
      RETURN 
       CONCAT (DATE_FORMAT(STR_TO_DATE(ziua, '%d/%m/%Y' ) , '%Y-%m-%d' ),' ',timpul);
      
      DELIMITER $$
      CREATE TRIGGER upd_date BEFORE INSERT ON tmp
      FOR EACH ROW
      BEGIN
            SET new.ComplDate = formdate(new.DataVizita,new.OraIntrare);
      END$$
      DELIMITER ;
      
    2. 我要做的第二件事是用pacienticabinete表填充此文件中的数据。

    3. 这个我需要它,因为我无法弄清楚如何做到这一点:(

      很抱歉这个长篇演讲,但我认为有人需要所有细节才能理解我的问题。

1 个答案:

答案 0 :(得分:0)

我意识到实际上非常简单:)。最终的触发器应如下所示:

DROP TRIGGER IF EXISTS upd_date;

DELIMITER $$

CREATE TRIGGER upd_date BEFORE INSERT ON tmp
FOR EACH ROW 
BEGIN

  SET new.ComplDate=formdate(new.DataVizita,new.OraIntrare);

  INSERT INTO pacienti(nume,prenume) VALUES(new.NumePacient,new.PrenumePacient)
  ON DUPLICATE KEY UPDATE nume=new.NumePacient, prenume=new.PrenumePacient;

  INSERT INTO cabinete(Denumire) VALUES(new.Cabinet)
  ON DUPLICATE KEY UPDATE Denumire=new.Cabinet;


END$$

DELIMITER ;

希望能帮助别人!