0

I have a table with my user's information, including their address, city, country, phone number, website, social network links, etc... And they have the option to select which of those information they want displayed on their public page.

So instead of creating some boolean column in the database for each one of those values, I want to create a single integer column that would hold one number which indicates all of those options in binary-style.

For example if the first (binary) digit represents your address, the second is your country and the third is your Facebook page. You want to display your address and your country, but not your Facebook page, then that would equivalent to 1, 1 and 0, meaning I would store a 6 in the database.

In my code (PHP), I would display it like:

if($value & 4) { showAddress(); }
if($value & 2) { showCountry(); }
if($value & 1) { showFacebookPage(); }

My question, for consistency, readability, and collaboration purposes, is only this:

What is the name for this kind of value keeping? Or rather, what should be the name of this column in my database?

Digital Ninja
  • 3,415
  • 5
  • 26
  • 51
  • A more "databasey" way of recording this information would be a separate table containing multiple rows - one for each item to be displayed and each one linked to the original row. The problem with your current approach is that its completely opaque to the database - so you won't be able to use database tools to (efficiently) write arbitrary queries that want to make use of that information. – Damien_The_Unbeliever Apr 24 '15 at 07:49
  • And even in your question you demonstrate some of the problems with this approach - the whole knowledge of what those bits mean is application defined. You *intended* in your paragraph to reveal Address and Country but not Facebook, and so you stored 6 in your DB. But your code is going to show Country and Facebook and hide Address. – Damien_The_Unbeliever Apr 24 '15 at 07:55
  • You mean like a two-column table which would hold the `user_id` and the `column_name` to be displayed? I don't see how I would easily integrate that with the constructor for my user objects. In "my" way, I would just generically fetch an entire user and let the template show what it needs to show through conditions. In "your" way, my constructor should already know the end intention of the entire call in order to either use or ignore this new table, and it will also leave some properties of the object empty, and in the end I still need conditions in my template. – Digital Ninja Apr 24 '15 at 08:01
  • But the reason you won't find (much) discussion of the sort of solution you're looking at is that one of the first rules of database normalization is that columns only contain *one* value. Here you're making the column contain multiple values (via an encoding, but still) – Damien_The_Unbeliever Apr 24 '15 at 09:32

0 Answers0