Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store status flag value in the table (char or int)

i need to have a column called status in the table. this status column can contain -> activated , expired, deactivated. so i am wondering why cant i create an integer type column called status and store them as 0, 1, 2 (expired, activated, deactivated). but we are asked to do these kind of things using. CHAR instead of Int. so it will be, status Char(2) and to store as 0, 1, 2.

why should i use Char as the column type instead of Integer is my question?

like image 477
dev1234 Avatar asked Nov 13 '13 05:11

dev1234


1 Answers

For a flag field I will usually use CHAR(1) so that I have somewhat meaningful but internal values such as 'A', 'E', 'D' - I do not use sequential integers values in such a case as they reveal nothing about the information!

These flag values - even though thy are "more readable" are still mapped via constants such that no "magic values" appear in code. (Unfortunately they must still appear in Views and whatnot.)

Take care when combining flags, however: if a flag tries to represent too much (e.g. more than a small discreet set of values) then it probably is too complicated and needs to be split in multiple columns/relationships.

like image 196
user2864740 Avatar answered Sep 24 '22 13:09

user2864740