Say i have a datatype called "status" as a row in a mysql table. Status can only be a fixed number of strings, say "active", "inactive" and "pending". What datatype is best practice to use?
We can use varchar(<maximum_limit>) . The maximum limit that we can pass is 65535 bytes.
The MEDIUMTEXT data object is useful for storing larger text strings like white papers, books, and code backup. These data objects can be as large as 16 MB (expressed as 2^24 -1) or 16,777,215 characters and require 3 bytes of overhead storage.
Assuming you are using ASCII text, you will be able to store 65000 characters in a single row. If your paragraph is 1000 words with each word averaging 6 characters, that's 6000 characters. You will be able to store around 11 paragraphs. If you want to store more information, see MEDIUMTEXT or LONGTEXT .
The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.
Specifically for exactly that purpose is the ENUM type: http://dev.mysql.com/doc/refman/5.5/en/enum.html
CREATE TABLE ... (
status ENUM('active', 'inactive', 'pending')
...
);
If the set of statuses is fixed at development time you definitely want to use ENUM. Another case is when possible statuses can be added in runtime. In this case you want to use separated table to store them and foreign key to check that all statuses are valid.
Using simple string for this is a kind of bad practice. E.g. just a minor misspelling in status can break everything related to the particular row.
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