Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:20:19+00:00 2026-06-17T04:20:19+00:00

For example I have a table which is used for logging. So very old

  • 0

For example I have a table which is used for logging. So very old data is useless and there are no reasons to leave it in the table. I want create a trigger which will delete old rows if number of existing rows more than 10 for example. What I already have:

CREATE TABLE log (
    logId INT UNSIGNED NOT NULL AUTO_INCREMENT,
    firstLogin DATETIME NOT NULL,
    lastLogin DATETIME NOT NULL,
    fingerprint VARCHAR(64) CHARACTER SET BINARY,
    ip VARCHAR(24) NOT NULL,
    accountId INT UNSIGNED NOT NULL,
    FOREIGN KEY (accountId)
        REFERENCES accounts (accountId)
        ON UPDATE CASCADE ON DELETE CASCADE,
    PRIMARY KEY (logId)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DELIMITER |

CREATE TRIGGER logbeforeinsert BEFORE INSERT ON log
    FOR EACH ROW 
        BEGIN
            SET @rowcount = (SELECT COUNT(*) FROM log WHERE accountId = NEW.accountId);
            IF @rowcount > 9 THEN
                DELETE FROM log WHERE accountId = NEW.accountId LIMIT 1;
            END IF;
        END;
|

DELIMITER ; 

But with this trigger inserting stopped at all after number of rows had reached 10.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:20:20+00:00Added an answer on June 17, 2026 at 4:20 am

    Your trigger tries to write to the same table (DELETE is write access), that it is inserting into – this is not supported.

    As you have a BEFORE INSERT trigger, failure of the trigger means failure of the INSERT.

    You need either to trigger the delete from an operation, that does not write to log, or rethink your model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which defines what things another table can have, for example:
I have a table named 'Attendance' which is used to record student attendance time
I have a table in a database which is used for storing application configuration
I have a example with h:datatable which is used to open a new page
Hypothetical example: you have a table called HOUSES and you want to display each
I have a table which we can call decks. Decks has an id used
I currently have a table with all the data, using this data i want
Informix 11.70.TC4: I have an SQL dimension table which is used for looking up
I have a table of data which is part of a sample. I would
I have a mysql table in which there is a column e.g. called name.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.