user187580
user187580

Reputation: 2315

should I use mysql enum or tinyint for fields having values 1 and 0?

I use tinyint (1) for fields which contain either 1 or 0, for xample, is_active. I can use enum ('1','0') instead but not sure which cases I should use enum or tinyint. Any comments/suggetion?

Thanks

Js

Upvotes: 2

Views: 808

Answers (6)

the_void
the_void

Reputation: 5538

In your case both enum and tinyint will have the same size (i.e. 1 byte). However, the enum option is less flexible if your data changes. So use it if you're absolutely sure your data definition will not change (eg. what if you will you be adding a 'disabled' state in the future?).

If you decide to stick with the enum a definition like enum('active', 'not_active') is more practical.

Upvotes: 2

xil3
xil3

Reputation: 16439

Enum is good if you want to have specific values - for instance:

enum('y','n')

But if you just need a simple boolean, use tinyint(1).

Upvotes: 0

Sjoerd
Sjoerd

Reputation: 75598

An enum('active', 'inactive') would use the advantages of an enum better than enum(0, 1).

Upvotes: 0

Bozho
Bozho

Reputation: 597076

BOOL , BOOLEAN. These types are synonyms for TINYINT(1)

I think you should go for tinyint(1)

Upvotes: 1

LukeH
LukeH

Reputation: 269358

My personal preference would be to use TINYINT(1).

Upvotes: 0

Borealid
Borealid

Reputation: 98469

It depends on what the database engine chooses to do under the hood. If you're never going to grow beyond two options, I'd recommend tinyint - but I doubt it makes much of a difference.

Upvotes: 0

Related Questions