Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store 60 Booleans in a MySQL Database?

I'm building a mobile App I use PHP & MySQL to write a backend - REST API.

If I have to store around 50-60 Boolean values in a table called "Reports"(users have to check things in a form) in my mobile app I store the values (0/1) in a simple array. In my MySql Table should I create a different column for each Boolean value or is it enough if I simply use a string or an Int to store it as a "number" like "110101110110111..."?

I get and put the data with JSON.

UPDATE 1: All I have to do is check if everything is 1, if one of them is 0 then that's a "problem". In 2 years this table will have around 15.000-20.000 rows, it has to be very fast and as space-saving as possible.

UPDATE 2: In terms of speed which solution is faster? Making separate columns vs store it in a string/binary type. What if I have to check which ones are the 0s? Is it a great solution if I store it as a "number" in one column and if it's not "111..111" then send it to the mobile app as JSON where I parse the value and analyse it on the user's device? Let's say I have to deal with 50K rows.

Thanks in advance.

like image 386
nethuszar Avatar asked Feb 14 '16 17:02

nethuszar


People also ask

How are Booleans stored in MySQL?

MySQL does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT). When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.

Can we store boolean in database?

PostgreSQL uses one byte for storing a boolean value in the database. The BOOLEAN can be abbreviated as BOOL . In standard SQL, a Boolean value can be TRUE , FALSE , or NULL . However, PostgreSQL is quite flexible when dealing with TRUE and FALSE values.

How do you store a boolean?

Instead, Boolean values are stored as integers 0 (false) and 1 (true). You can convert boolean to int in this way: int flag = (boolValue)?


2 Answers

A separate column per value is more flexible when it comes to searching.

A separate key/value table is more flexible if different rows have different collections of Boolean values.

And, if

  1. your list of Boolean values is more-or-less static
  2. all your rows have all those Boolean values
  3. your performance-critical search is to find rows in which any of the values are false

then using text strings like '1001010010' etc is a good way to store them. You can search like this

 WHERE flags <> '11111111'

to find the rows you need.

You could use a BINARY column with one bit per flag. But your table will be easier to use for casual queries and eyeball inspection if you use text. The space savings from using BINARY instead of CHAR won't be significant until you start storing many millions of rows.

edit It has to be said: every time I've built something like this with arrays of Boolean attributes, I've later been disappointed at how inflexible it turned out to be. For example, suppose it was a catalog of light bulbs. At the turn of the millennium, the Boolean flags might have been stuff like

screw base
halogen
mercury vapor
low voltage

Then, things change and I find myself needing more Boolean flags, like,

LED
CFL 
dimmable
Energy Star

etc. All of a sudden my data types aren't big enough to hold what I need them to hold. When I wrote "your list of Boolean values is more-or-less static" I meant that you don't reasonably expect to have something like the light-bulb characteristics change during the lifetime of your application.

So, a separate table of attributes might be a better solution. It would have these columns:

   item_id           fk to item table         -- pk
   attribute_id      attribute identifier     -- pk
   attribute_value   

This is ultimately flexible. You can just add new flags. You can add them to existing items, or to new items, at any time in the lifetime of your application. And, every item doesn't need the same collection of flags. You can write the "what items have any false attributes?" query like this:

 SELECT DISTINCT item_id FROM attribute_table WHERE attribute_value = 0

But, you have to be careful because the query "what items have missing attributes" is a lot harder to write.

like image 178
O. Jones Avatar answered Oct 16 '22 11:10

O. Jones


For your specific purpose, when any zero-flag is a problen (an exception) and most of entries (like 99%) will be "1111...1111", i dont see any reason to store them all. I would rather create a separate table that only stores unchecked flags. The table could look like: uncheked_flags (user_id, flag_id). In an other table you store your flag definitions: flags (flag_id, flag_name, flag_description).

Then your report is as simple as SELECT * FROM unchecked_flags.

Update - possible table definitions:

CREATE TABLE `flags` (
    `flag_id` TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `flag_name` VARCHAR(63) NOT NULL,
    `flag_description` TEXT NOT NULL,
    PRIMARY KEY (`flag_id`),
    UNIQUE INDEX `flag_name` (`flag_name`)
) ENGINE=InnoDB;

CREATE TABLE `uncheked_flags` (
    `user_id` MEDIUMINT(8) UNSIGNED NOT NULL,
    `flag_id` TINYINT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`user_id`, `flag_id`),
    INDEX `flag_id` (`flag_id`),
    CONSTRAINT `FK_uncheked_flags_flags` FOREIGN KEY (`flag_id`) REFERENCES `flags` (`flag_id`),
    CONSTRAINT `FK_uncheked_flags_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB;
like image 43
Paul Spiegel Avatar answered Oct 16 '22 11:10

Paul Spiegel