I need a mysql table to have only n rows. Is it possible? There is solution for 1 row table but unable to find anything for n rows.
Asked
Active
Viewed 118 times
0
-
1Possible duplicate of [Can we limit the number of rows in a table in MySQL?](http://stackoverflow.com/questions/3155053/can-we-limit-the-number-of-rows-in-a-table-in-mysql) – JJJ Feb 19 '17 at 09:56
-
Possible duplicate http://stackoverflow.com/a/8048494/695787 – Jameson Feb 19 '17 at 09:58
1 Answers
1
If you really want to do that in mysql, you would have to write a trigger
which gets invoked whenever a row is inserted. (See https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html)
You would have to code your trigger so that it checks whether the number of rows has exceeded N, and if so, keep removing rows from the table until the number of rows is equal to or less than N. Of course the removal will need to be done according to some criteria that only you know, you have told us nothing about them. (Probably a timestamp, so that you can remove the oldest?)
Keep in mind that this will horrendously slow down insertions on your table.

Mike Nakis
- 56,297
- 11
- 110
- 142