Is there any easy way to limit the size of a MySQL table to a given number of rows, so that once that limit is reached, when a new row is inserted, the oldest is automatically deleted?
Thanks,
Rich
Thanks,
Rich
Yep, I'd just put the limit in the stored procedure. Something like :
DELETE FROM table t WHERE
t.ID not in (SELECT ID from table t2 ORDER BY t2.date DESC LIMIT 1000)
It's been a while since I've played with mysql, but any time you need to make updates to multiple records, those updates must be made sequentially without any other intervening updates taking place between them, and the database would be in an inconsistent state if, say, the server crashed after one of the updates had been committed but before the second one had, then you have an "atomicity" requirement. These are handled by doing the updates in the context of a transaction:... But I was concerned that two users making entries at the same time might cause problems if two steps were required (one to delete the oldest entry, and one to insert the new one)....
I can't quote you the code to use off the top of my head, but couldn't you just limit the database size and instead of a delete/add sequence, merely update the oldest record with the new stuff?
1=Taxes stink.
2=I like taxes.
3=You're a nut.
4=I like nuts.
5=You stink.
1=So do you.
It's been a while since I've played with mysql, but any time you need to make updates to multiple records, those updates must be made sequentially without any other intervening updates taking place between them, and the database would be in an inconsistent state if, say, the server crashed after one of the updates had been committed but before the second one had, then you have an "atomicity" requirement. These are handled by doing the updates in the context of a transaction:
-harry
Using pseudocode:Thanks. When I tried that, it deleted the newest entry. Most probably an error on my part, as I haven't had the uninterrupted time to sit down and do this without being distracted.
-Rich
CREATE TABLE `post` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`post` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO post (post) VALUES ('Hello!');
INSERT INTO post (post) VALUES ('Hello!');
INSERT INTO post (post) VALUES ('Hello!');
INSERT INTO post (post) VALUES ('Hello!');
INSERT INTO post (post) VALUES ('Hello!');
DELETE FROM post ORDER BY id LIMIT 1;
INSERT INTO post(post) VALUES ('New post!!!');
There are lots of things you could do Richard from triggers to stored procedures to other trickery.
The simplest thing to do would be to do this:
add fives posts:Code:CREATE TABLE `post` ( `id` int(11) NOT NULL AUTO_INCREMENT, `post` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
When you add a new post do this (you could wrap it into a transaction):Code:INSERT INTO post (post) VALUES ('Hello!'); INSERT INTO post (post) VALUES ('Hello!'); INSERT INTO post (post) VALUES ('Hello!'); INSERT INTO post (post) VALUES ('Hello!'); INSERT INTO post (post) VALUES ('Hello!');
The thing Aunt Peggy suggested would work but then you need to roll through and update the ID on every row which is a lot more action happening on the DB and more code for you to worry about.Code:DELETE FROM post ORDER BY id LIMIT 1; INSERT INTO post(post) VALUES ('New post!!!');
That iframe concept needs to go.
You have a ton of screel real estate on each page but the actual content that matters is forced into a tiny box that requires you scroll.