简体   繁体   English

INSERT 后删除空行的触发器

[英]Trigger to delete blank rows after INSERT

I have inserted a set of data into a database, now I want to delete all the rows with blank values.我已将一组数据插入数据库,现在我想删除所有具有空白值的行。 How can I do this?我怎样才能做到这一点? Can this be done using triggers?这可以使用触发器来完成吗?

Example:例子:

table BOOKS contains author_name , title , price .表 BOOKS 包含author_nametitleprice

After inserting data, I want to delete all the rows with empty values in the author_name column.插入数据后,我想删除author_name列中所有值为空的行。 Here's what I've written so far:这是我到目前为止所写的内容:

CREATE TRIGGER trigger1
  AFTER INSERT
  ON  BOOKS
  FOR EACH ROW 
  BEGIN
  DELETE FROM BOOKS WHERE author_name = ''  
END;

This is not working :(这不起作用:(

The issue is that you've got BEGIN…END around a statement not delimited with a ;问题是你在一个没有用;分隔的语句周围有BEGIN…END ; . . If you put the missing ;如果你把丢失的; , however, you will need to introduce a meta delimiter for the entire definition, because, if you don't do that, the inner ; ,但是,您需要为整个定义引入分隔符,因为如果不这样做,内部; will break it.会打破它。

So, put a ;所以,放一个; at the end of the DELETE statement and introduce a DELIMITER before the trigger definition:DELETE语句的末尾并在触发器定义之前引入DELIMITER

DELIMITER $$
CREATE TRIGGER trigger1
  AFTER INSERT
  ON  BOOKS
  FOR EACH ROW 
BEGIN
  DELETE FROM BOOKS WHERE author_name = '';
END
$$

Alternatively, since the body contains only one statement, you can simply remove the BEGIN…END :或者,由于正文仅包含一个语句,您可以简单地删除BEGIN…END

CREATE TRIGGER trigger1
  AFTER INSERT
  ON  BOOKS
  FOR EACH ROW 
DELETE FROM BOOKS WHERE author_name = '';

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

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