A simple Database Suggestion
Akshay Khale
If you have been doing software development then you might have encountered a situation where you would want to deactivate a user account or mark some actions as done, where you might be using Boolean Data-type to store those actions.
E.g.
If you want to deactivate then you might have been setting boolean is_active
field to true
for active users and false
for in-active users.
Or
If you want to save notification status for an order then you might have been using email_status
as a boolean field with default value as false
which you change it to true
when the email status is shared with the customer.
The approach above is completely fair and it works but there is a problem in the above approach. It does not have a record of exactly when the user was deactivated
or exactly when the email_status
was sent to the customer.
For that, most of the developers follow a simple approach, instead of using a boolean field is_active
they use activated_on
(or deactivated_on
depends on your business requirement) Date with default value as NULL. They use following queries to find users:
/* Query to find all the Active Users
* (if you are using `deactivated_on` date field with default NULL
*/
SELECT *
FROM `users`
WHERE `deactivated_on` IS NULL
/* Query to find all the In-Active Users
* with the date when they were deactivated
*/
SELECT *
FROM `users`
WHERE `deactivated_on` IS NOT NULL
I am not saying this is the best approach but it surely helps you to make more sense out of the data in the Database and it completely depends on your business requirements.
That’s all folks!!!
Thank you for reading :) :) :)