Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bits vs Char - What's the best way to store 3 mutually exclusive flags?

I've got a table which is set to keep track of various items. Among other properties, items can be either A, B, or C, each mutually exclusive of the rest. Is it best practice to store this information as a character, or as 3 sets of bits (isA isB, isC, etc), or some other method? I could understand using the character if I would possibly need more data types in the future, however it also makes sense to me that using bit datatypes would consume smaller amounts of storage. Or am I overanalyzing this and will the difference be so miniscule as to not even matter?

like image 212
Kupo Avatar asked Feb 23 '23 21:02

Kupo


1 Answers

Or am I overanalyzing this and will the difference be so miniscule as to not even matter?

A little bit, yes.

But you must understand that there's a crucial difference between your design proposals: having a char column will make exclusive exception work. Having IsX fields (alone) will not. Explained: by having IsA and IsB columns, you can, potentially, have both set to true in the same record, unless you use another mechanism to prevent that (trigger, check constraint, etc.)

Additionally, having a new column every time a new value is possible is not good DB design.

like image 184
Adriano Carneiro Avatar answered Apr 08 '23 07:04

Adriano Carneiro