简体   繁体   English

插入后MySQL触发器删除

[英]MySQL Trigger delete after insert

I am really new creating triggers, but I need something really easy but I haven't been able to figure out how to do it. 我确实是创建触发器的新手,但是我确实需要一些简单的方法,但是我一直无法弄清楚该如何做。

Basically I have a table this the following structure 基本上我有一个这样的表结构

Table Name orksegment
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| direction       | int(11)      | YES  |     | NULL    |                |
| duration        | bigint(20)   | NO   |     | NULL    |                |
| localEntryPoint | varchar(255) | YES  |     | NULL    |                |
| localParty      | varchar(255) | YES  |     | NULL    |                |
| loginString     | varchar(255) | YES  |     | NULL    |                |
| portName        | varchar(255) | YES  |     | NULL    |                |
| remoteParty     | varchar(255) | YES  |     | NULL    |                |
| sessionOffset   | bigint(20)   | NO   |     | NULL    |                |
| tapeOffset      | bigint(20)   | NO   |     | NULL    |                |
| timestamp       | datetime     | YES  |     | NULL    |                |
| port_id         | int(11)      | YES  | MUL | NULL    |                |
| session_id      | int(11)      | YES  | MUL | NULL    |                |
| tape_id         | int(11)      | YES  | MUL | NULL    |                |
| user_id         | int(11)      | YES  | MUL | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+

I want that if in the record that I just inserted the localParty is like '192%' the record be deleted. 我希望如果在我刚刚插入localParty的记录中像“ 192%”,则删除该记录。

I did something like this: 我做了这样的事情:

CREATE TRIGGER Delete_recordings_MoH
  AFTER INSERT
  ON  orksegment
  FOR EACH ROW 
 delete from orksegment where localparty like '192%';

But when I do this it looks like all the entries are getting deleted because I don't get any new records in the table. 但是,当我这样做时,似乎所有条目都被删除了,因为我在表中没有任何新记录。

Thank you 谢谢

You can create a trigger which before the insert looks for the new column value of localParty and insert the record if it is not like '192%' 您可以创建一个触发器,该触发器在插入之前查找localParty的新列值并插入记录(如果它不是'192%')

I think this code will work: 我认为这段代码可以工作:

DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON orksegment FOR EACH ROW
BEGIN
IF (NEW.localParty!='192%') THEN 
    INSERT INTO orksegment(all the column names) VALUES(New.columnA, New.ColumnB etc);
END IF
END
$$

Please let me know if this worked for you. 请让我知道这是否对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM