Hello,
How can I delete old data with Mysql? For example, delete old data when data reaches 10 rows.
When insert new row, delete old data when data reaches 10 rows.
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 7, 2022, 1:23 AM EDT
use triggers
Uday DodiyaRank #239 · 10.3K reputationPosted Oct 7, 2022, 1:42 AM EDT
yes you can use trigger to do this,
do something like following
create trigger delete_old_rows after insert on your_table for each row begin DECLARE totalrows INT; set totalrows = ( select count(*) from your_table); if totalrows = 10 then Delete FROM your_table LIMIT 3; --here you define how many rows you have to delete end if; endVishal JoshiRank #377 · 6.7K reputationPosted Oct 7, 2022, 3:36 AM EDT
Hello
You can try below script code in Mysql database to delete records after insert 10 rows.
-- create CREATE TABLE EMPLOYEE ( empId INTEGER PRIMARY KEY, name TEXT NOT NULL, dept TEXT NOT NULL ); -- trigger CREATE TRIGGER InsTrigger AFTER INSERT ON EMPLOYEE FOR EACH ROW IF (select count(*) FROM EMPLOYEE) > 2 THEN DELETE FROM EMPLOYEE; END IF; -- insert INSERT INTO EMPLOYEE VALUES (0001, 'Clark', 'Sales'); INSERT INTO EMPLOYEE VALUES (0002, 'Dave', 'Accounting'); INSERT INTO EMPLOYEE VALUES (0003, 'Ava', 'Sales');