简体   繁体   中英

Is it possible to set an Expiration Date/Time for a table in mysql?

I'm wondering if it is possible to set an "Expire" time or date when creating a table in mysql.

For example a table is created and will expire/auto-delete in 2 hours. Something to that effect. Google didn't give any answer so its probably not as simple as I'm hoping.

Need: I'm making a site with cart functionality. Users can add items to the cart if they are not signed in. This info is stored in a temp table that will then be migrated to a specific users table or orders once they log in. Well I was trying to find a way to remove the temporary table if say the user never signs in or completes an order and then leaves the site permanently. That way I wouldn't end up with a bunch of temporary tables cluttering things up.

You are doing it wrong! It's almost always an error when you change your schema dynamically. You should add and remove rows, not tables.

Put all your orders in the same table and just timestamp them. You can have a field with order status and remove non-completed orders with a DELETE ... WHERE status='not completed' AND timefield < DATE_SUB(NOW(), INTERVAL 2 HOUR) , or you can put temporary orders in another table than completed orders, but all temp orders should go to the same table.

Q: I'm wondering if it is possible to set an "Expire" time or date when creating a table in mysql.

A: It's easy to set an "added" time or date, then run a batch job to query any/all items older than your "expire" time and take the appropriate action. Perhaps delete. Perhaps e-mail a reminder. Perhaps generate a report and delete - whatever.

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