简体   繁体   中英

flag record as delete instead of deleting the record from mysql table

i want to know how to proceed to flag records as deleted instead of physically deleting the record from the database. I never came across such a thing before. Is there any tutorial i can follow?

Easiest way is to add a new BOOL column to the table like enabled with a default value of 1 .

Then UPDATE table_name SET enabled = 0 when you would like to "delete" the row.

And filter out "deleted" rows in your queries like:

SELECT * FROM table_name WHERE ... AND enabled = 1

Use an extra boolean field in your table to denote if the entry is valid or not.

TableName(<Your Fields>,Valid)

Valid =1 , then it is present.

Valid =0, then it is deleted.

There's nothing intrinsic in MySQL to flag a record as deleted - but you can add your your own column to describe the state - you just need need to remember to exclude it from your queries. The complication arises when you try to insert a row with unique/primary keys matching your deleted record; you'll need to add a trigger to amend the state. Really a simpler option is to just delete the data in the first place (you don't say what you're trying to achieve by not deleting the row)

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