Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum('yes', 'no') vs tinyint -- which one to use?

What's the best practice for fields that hold true/false values?

Such columns can be defined as enum('yes','no') or as tinyint(1). Is one better/faster than the other?

Is it better to use enum('1','0') vs. enum('yes','no') (i.e., does it write 'yes' or 'no' as a string to every row so the database storage size gets bigger)?

like image 915
Yasin Ergul Avatar asked Nov 28 '10 14:11

Yasin Ergul


People also ask

What is the correct use of ENUM in MySQL?

An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time.

Is Tinyint the same as Boolean?

There is no difference between TINYINT(1) and Boolean. The keyword Bool or Boolean internally converts into TINYINT(1) or we can say Bool or Boolean are synonymous with TINYINT(1).

What happens when no value is inserted in an ENUM list?

DEFAULT: When a value is not specified in the column, the ENUM data type inserts the default value. In other words, if the INSERT statement does not provide a value for this column, then the default value will be inserted.

What is the use of Tinyint in MySQL?

TINYINT − A very small integer that can be signed or unsigned. If signed, the allowable range is from -128 to 127. If unsigned, the allowable range is from 0 to 255. You can specify a width of up to 4 digits.


2 Answers

avoid enum from this reasons

The bottom line is that ENUM has its place, but should be used sparingly. The model should enforce the constraints, not the database; the model should handle interpreting raw data into useful information for your views, not the database.

like image 175
Haim Evgi Avatar answered Oct 18 '22 18:10

Haim Evgi


BOOLEAN type is there for a reason. It is indeed a TINYINT(1) but since it's there, it must be the recommended way.

like image 39
Michal M Avatar answered Oct 18 '22 17:10

Michal M