By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,422 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,422 IT Pros & Developers. It's quick & easy.

best way to save multiple checkboxes value in a database

100+
P: 107
hey experts, please advise me what is the best way to save multiple checkboxes value in a database. I have 6 checkboxes and i came across storing the values in a single column of a table which i dont want to implement as the checkbox values may be used for search purpose in latter stage. Apart from that i also came across creating individual columns for each checkbox which i feel would really make my table large. Is there any other way that i can do the same in a professional way?
Feb 18 '09 #1
Share this Question
Share on Google+
3 Replies


Atli
Expert 5K+
P: 5,058
Hi.

That would depend on your data. But you are quite right not to want to store them all in a single column. That would violate the first normal form.

If the number of checkboxes is not going to change, and each row of the table should only contain a single value for each of them, the most efficient method would probably be to simply add a column for each of them.
Expand|Select|Wrap|Line Numbers
  1. +-----+-------+-------+-------+
  2. | ID  | Box1  | Box2  | Box3  |
  3. +-----+-------+-------+-------+
  4. | 1   | TRUE  | FALSE | NULL  |
  5. | 2   | TRUE  | NULL  | FALSE |
  6. | 3   | NULL  | TRUE  | FALSE |
  7. +-----+-------+-------+-------+
(If you feel this would clutter your original table, you could also simply put these columns in a separate table and set up a 1:N relationship between them)

But, if the checkboxes are dynamic, and you want to be able to add/remove boxes from the list, you may want to set up a N:M relationship, where each row in your table could have a value for each checkbox.
This would require a separate table to store the ceckboxes, and an intermediary table to store the values each row in table A has for each checkbox.
Like:
Expand|Select|Wrap|Line Numbers
  1. /* Your table */
  2. +-----+
  3. | AID | 
  4. +-----+ 
  5. | 1   |
  6. | 2   |
  7. | 3   | 
  8. +-----+
  9.  
  10. /* The table for the boxes */
  11. +-------+-------+
  12. | BoxID | Label |
  13. +-------+-------+
  14. | 1     | Box1  |
  15. | 2     | Box2  |
  16. | 3     | Box3  |
  17. +-------+-------+
  18.  
  19. /* The intermediary table 
  20.  * linking them together */
  21. +-----+-------+-------+
  22. | AID | BoxID | Value |
  23. +-----+-------+-------+
  24. | 1   | 1     | TRUE  |
  25. | 1   | 2     | FALSE |
  26. | 2   | 1     | TRUE  |
  27. | 2   | 3     | FALSE |
  28. | 3   | 2     | TRUE  |
  29. | 3   | 3     | FALSE |
  30. +-----+-------+-------+
See what I mean?
Feb 18 '09 #2

100+
P: 107
i m sure the 2nd option is gonna work for me because my checkboxes is not dynamic. Anyway thank you man!
Feb 19 '09 #3

P: n/a
Greatest response ever, thank you Atli
Nov 22 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.