Saturday, February 5, 2011

Possible to set max rows in MySQL table?

Hello! I have a question about tables in MySQL.

I'm currently making a website where I have files for people to download. Now I want to make a table in MySQL that will store the latest downloads, like if someone downloads file1.zip then it will add a row in the table with the filename and date, so that I can show on the front page "Most recently downloaded files" or something like that.

And since I only need to display like 5-10 items I don't have to store more in the table. So what I want to know is if it's possible to set a max limit to how many rows there can be in a table. Like if it's set to 10 and there is 10 entries in the table, the next time it tries to add one it will automatically delete the oldest one, so there's always only 10 rows in the table.

  • Look at triggers to delete automatically the old rows.

    From Aif
  • This can be solved by using a trigger (MySQL 5 required) or setting up a cron job or something similar, that periodically checks the number of rows in the tables.

    From wvanbergen
  • I don't think you can set MySQL do it automatically (although my MySQL isn't exactly guru level, so I could be wrong there) so I think what you probably need to do is either create an INSERT trigger to DELETE the oldest row before you do the INSERT, or write your app so it always runs a DELETE prior to an INSERT.

    From gkrogers

0 comments:

Post a Comment