开发者

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

开发者 https://www.devze.com 2023-01-06 12:26 出处:网络
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

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


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.


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

I think you should go for tinyint(1)


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.


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


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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消