Jeff Davidson
Jeff Davidson

Reputation: 1929

DB storage of values

I'm just looking to get different people's opinions because for some reason I'm not looking in the right places but what do most people use or better yet what is the most accepted away of storing something in a database with either an active value or inactive value. I was thinking of having a database table for statuses and the first one would be Active and the other Inactive and then I have an form when creating a new content page or something and have a select dropdown of values of active(1) or inactive(2) and store 1 or 2 as the status_id for that content page.

Is that the best way or is there a better way?

Upvotes: 0

Views: 66

Answers (3)

Marius Burz
Marius Burz

Reputation: 4645

It depends on the database you use and what data types it supports. If it has a built-in type for Boolean values I would use that, otherwise I would use a char(1) with a check constraint that checks for the allowed values (0/1 or n/y) because it's faster than any other type out there and it does the job.

In case you have multiple boolean flags (that are related, but not necessary) and don't need support for NULL (which is a third state) on a per flag basis I would go for a numeric column with bit flags if your database supports that (1=true and 0=false, bit one for flag one, bit two for the second flag and so on).
Mysql has very good support for bit operations, see here.

Upvotes: 1

Abdul Manaf
Abdul Manaf

Reputation: 4888

you can use boolean as well as enum('Active','Inactive') for the same

Upvotes: 1

David Weitz
David Weitz

Reputation: 461

I usually just use a Boolean. 0 is Inactive and 1 is active.

Upvotes: 3

Related Questions