简体   繁体   English

为 SQLite 创建触发器,当插入新记录时,删除旧记录

[英]Create trigger for SQLite, when new record is inserted, to delete old records

I have the table "log" in an SQLite database, where I write all my log files.我在 SQLite 数据库中有“日志”表,我在其中写入了所有日志文件。

However I want to pretend the database from getting to big - and the smartest way in doing this, is by using a trigger on the insert command - at least I think so...但是我想假装数据库变大 - 最聪明的方法是在插入命令上使用触发器 - 至少我是这么认为的......

When a new record is inserted, a trigger shall get fired, that deletes all records older than 10 days.当插入新记录时,将触发触发器,删除所有超过 10 天的记录。

Or...或者...

When a new record is inserted, a trigger shall get fired, that deletes all old records, which exceed a specific amount (for example 1000).当插入新记录时,将触发触发器,删除所有超过特定数量(例如 1000)的旧记录。

I need an example code.我需要一个示例代码。

Kind Regards, and thx.亲切的问候,和thx。

This will create an insert trigger that will delete anything with a create date that is more then 10 days old. 这将创建一个插入触发器,该触发器将删除创建日期超过10天的任何内容。

CREATE TRIGGER [TRIGGER_NAME] AFTER INSERT ON my_table
BEGIN
    DELETE FROM LOG WHERE DATE(CREATE_DATE) > DATE('now', '-10 days');
END

If you want to do something based on size like you were saying with 1000 rows you can do something like this. 如果你想做一些基于大小的事情就像你说1000行一样,你可以做这样的事情。

CREATE TRIGGER [TRIGGER_NAME] AFTER INSERT ON my_table
BEGIN
    DELETE FROM LOG WHERE ROW_NO NOT IN 
        (SELECT TOP 1000 ROW_NO FROM LOG ORDER BY CREATE_DATE DESC);
END

This will select the 1000 newest rows and delete anything that is not in that select statement. 这将选择1000个最新行并删除该select语句中没有的任何内容。

I have tried this approach:我试过这种方法:

CREATE TRIGGER [TRIGGER_NAME] AFTER INSERT ON my_table
BEGIN
    DELETE FROM LOG WHERE ROW_NO NOT IN 
        (SELECT TOP 1000 ROW_NO FROM LOG ORDER BY CREATE_DATE DESC);
END

and result is that i can not create a trigger due to valid syntax error that i get结果是由于我得到的有效语法错误,我无法创建触发器

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

相关问题 Postgresql:在插入带有时间条件的新记录时更新旧记录 - Postgresql: update old record when the new one is inserted with time condition MySQL触发器删除旧记录并插入新记录 - MySQL trigger to delete old record and insert new one 插入新记录时是否触发(可能是存储过程)替换文本? - Trigger (possibly stored procedure) to replace text when a new record is inserted? SQLite触发器删除激活触发器的记录 - SQLite trigger to delete record that activated the trigger SQLite 插入触发器如何根据两个值删除表中的旧记录 - SQLite Insert trigger how to delete old records in the table based on two values 如果旧记录与 SQL 中的新记录不同,如何创建仅更新记录的触发器 - How to create a trigger that will only update a record if old record is different from new in SQL 如何创建触发器以删除记录 - How to create a trigger to delete a record 为表创建触发器,每当插入或更新新记录并满足条件时,填充同表中的列 - Create a trigger for a table that fills a column in the same table whenever a new record is inserted or updated and meets the condition 将新记录添加到表中时,如何创建触发器以发送电子邮件 - How to create a trigger to send email when a new record is added in to the table 创建触发器以在添加新记录时更新日期 SQL 服务器 - Create a trigger to update date when a new record is added SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM