• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the best data model in java to store check box value in database?

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I am working on a web application there is 6 checkbox field in my front end page

And I want to store then in my database

I have 3 datamodel in my mind
1)Create an Array and store that array in database using comma seprated values like...(value1,value2,value3....)


2)Increase number of columns in the main table and make those field as boolean type and store value as on off


3)Store all those checkBox Values in a seprate table using Foreign Key relations with Key_Id

What is best data model for this?

If there is any other good data model to deal with checkBox please share.

Thanks
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Booleans.
At least that's what I've always used.

To me a checkbox is a simple GUI that represents a boolean value.

As for where to store them, whether in the base table or some extension, I'd stick them in the base table until I had a good reason to shift them.
 
Tapas Chand
Ranch Hand
Posts: 583
8
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I have also gone for the 2nd approach most of the times.
I will just mention the drawbacks of 1st and 3rd approach which I have faced.

1st: Increases complexity at Java side. Need to play with String.split() and arrays
3rd: Increases complexity at DB side. Need join to fetch data

2nd approach does away with both the complexities.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also feel comfortable with storing those values as booleans

can someone explains what are the drawback of 3rd approach
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your design should be based on your requirements and not on what GUI components you choose to implement those requirements.

For example if you had one checkbox titled "Send me e-mails about our products" then it would be ridiculous to put that in a separate table from the rest of the user information. On the other hand if you had several checkboxes which might refer to pizza toppings or dessert options depending on what menu item was being ordered, then a separate table with an ID key would be just the thing.

And combining two or more data items into one database column is almost always the wrong thing to do.
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:On the other hand if you had several checkboxes which might refer to pizza toppings or dessert options depending on what menu item was being ordered, then a separate table with an ID key would be just the thing.


True.
That falls under the "good reason to shift them"...:)
To be honest, in that case there'd be a Toppings table which would drive the checkboxes displayed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic