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)?
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.
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).
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.
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.
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.
BOOLEAN
type is there for a reason. It is indeed a TINYINT(1)
but since it's there, it must be the recommended way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With