开发者

Should I use 0/1 or True/False boolean? [duplicate]

This question already has answers here: Which MySQL data type to use for storing boolean values (13 answers) 开发者_JAVA技巧 Closed 7 years ago.

0/1 can be flexible and can add options such as "2, 3, 4" in the future.

Does TINYINT in MySQL take up more space than boolean?

Personally, I use 0 and 1 for everything.

You can answer this question in the context of regular programming (or MySQL, whichever you like).


Any good database system worth its salt will convert boolean to and from something appropriate for the database, and using an actual boolean type makes programming against it much nicer.


Boolean is more portable across different databases (e.g. PostgreSQL). Tinyint is more flexible, can be upgraded to something Enum-like.

The final choice should be yours. But anywhere except SQL I would definitely recommend explicit Booleans/Enums over "magic numbers", for clarity and type safety.


When I am absolutely sure that I will need true/false for a value, I will use the boolean type in my code. For other cases, often starting with 0/1 is convenient because 0 can act as a return code for success and any other positive or negative integers can define various error conditions.


If you're making a true/false decision in any language, if your language supports boolean, use it.

Using 0/1 to support the "extra numbers" probably means your field is not a decision, and may be or become multiple choices.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜