简体   繁体   中英

setting max number of entries per userid in MySQL table

Say I have the following table

//table user_update
update_id   |   userid   |   update_message   |   timestamp

Is there a way to set a maximum number of entries per userid? I want to make it so after a user types say 5 updates, any more updates entered after that deletes the oldest update. I know how to do this through PHP, just curious if there's any way to do this through MySQL only.

That is actually possible. but it is questionable if you really want to make that effort.

Read a little about 'triggers'. You can use a trigger to start an action when certain conditions are met. This way you can trigger a delete action on every insert action on that table. Then all that is left is a condition that up to five entries are kept.

The only way I can think of doing this database-side would be to use a TRIGGER on the table. Maybe something like this:

CREATE TRIGGER check_for_too_many_rows
AFTER INSERT ON User_Update
FOR EACH ROW BEGIN
   DO SOME LOGIC TO CHECK THE COUNT AND DELETE IF MORE THAN 5...
END;

Here is some additional information:

http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM