DEV Community

Akshay Khale
Akshay Khale

Posted on

A simple Database Suggestion

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 boolean field is_active they use activated_on (or deactivated_on depends on your business requirement) Date field with default value as NULL. They use the following queries to find users:

https://gist.github.com/akshaykhale1992/22448e265e646c5143ec2725d0358549#file-queries-sql

View on Gist

I am not saying this is the best approach but it surely help 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 :) :) :)

Originally Published on Medium: A simple Database Suggestion

Follow me on my social media profiles:
Twitter@TheAkshayKhale
Fb@TheAkshayKhale
Medium@TheAkshayKhale

GitHub: AkshayKhale1992

Discussion (0)