3

i want to insert multiple selected checkbox value in one colunm in MS Sql server 2005/08 Eg. checkboxes are:

Cricket Football Painting

Table structure like:

    Id | name     | Hobbies
   ----|----------|--------------------------
    1  | Atish    | cricket,football,painting
    2  | Swapnil  | football, painting

can you help me?

1
  • 2
    Why? Sounds like a non-optimal database design to me. I would have thought separate columns would make more sense or having several related tables. Please provide further details. Commented Aug 31, 2011 at 11:26

2 Answers 2

3

There is something called the third normal form: use it :)

Basically this means for you, you need to separate the hobbies from that table, create a seperate table with the possible hobbies and create a lookup-table between the people and the hobbies.

The dirty way would be to define a seperator and insert smth like cricktet|football|painting to the column: but I would really not suggest to do that!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks MADmap Can I didnt Do it in the same table. Please suggest me if you know. it would be very kind of you...
0

You can make three tables

[Names]
Name_ID (int)
Name (varchar)

[Hobbies]
Hobby_ID
Hobby (varchar)

[Hobbies_Names]
Name_ID (foreign key)
Hobby_ID (foregin key)

So, if Atish, who has Name_ID = 1, and hobbies cricket,football,painting, which have Hobby_ID: 1, 2 and 3, your Hobbies_Names table would look like this:

    Name_Id | Hobby_ID
   ---------|----------|
    1       | 1
    1       | 2
    1       | 3

Then you could make a SQL query that selects all Hobbies, where Name_ID = 1 for example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.