If users can often change their favorite colors / hobbies, I would use lookup tables, in my example I will call them decode tables. All relationships between user/hobbies and user/colors will be found in this decode table.
Since you can only have 1 marital status , this can easily handle this ratio from 1 to many.
Create a Marital_Status table with two fields, Id (pk) and Status(varchar(n)) A decode table decode not required to search for marital status .
Now I would recommend creating a table for storing colors and a table for hobbies . We did the marital status .
Hobbies HobbyId, Hobby Colors ColorId, Color
Whenever you need to add / remove a new hobby/color , do it in these decode tables.
It depends on whether you want to use 1 decode table for each relationship or a lot that is. Hobby_Decode and Color_Decode etc.
I will explain usage scenario 1.
Create your decoding table with the following fields ...
decode
Item_Type varchar(n) - We will click Hobby or Color in this field
UserId int - explaining itself, contains the user identifier for the search.
LookupId - will contain the identifier of either Hobby or Color
Let me create some sample data, and we will work on it.
Hobbies table data
| HobbyId | Hobby 1 Studying 2 Doing Drugs 3 Drinking
Colors table data
| ColorId | Color 1 Red 2 Blue
While we are on it, here is our user table.
Users
| UserId | Name 1 Marcin 2 CSharper
I like to drink, do drugs and red. You are a nerd, so you like to study and the color is Blue. In our extension table, we will add the following entries to represent this.
decode
| Item_Type| UserId | LookUpId 'Hobby' 2 2 'Hobby' 2 3 'Color' 2 1 'Hobby' 1 1 'Color' 1 2
Looking at this decoding table, we will not say anything. Once we join our decode table before colors/hobbies , this will be obvious.
If you want to see all my hobbies and my favorite colors, the request will look like this:
Note: this is SQL Server syntax, not mysql.
--Pull Hobbies Select u.Name, dH.Item_Type as 'Favorite', h.Hobby as 'Item' from User u inner join decode dH on dH.UserId = u.UserId and dH.Item_Type = 'Hobby' inner join Hobby h on h.HobbyId = dH.LookUpId where u.UserId = 2 --Union in Colors Union Select u.Name, dH.Item_Type as 'Favorite', h.Hobby 'Item' from User u inner join decode dC on dH.UserId = u.UserId and dH.Item_Type = 'Color' inner join Color c on c.ColorId = dH.LookUpId where u.UserId = 2
Your result will look like
| Name | Favorite | Item CSharper Hobby Drinking CSharper Hobby Doing Drugs CSharper Color Red
If it is configured in this way, then it is very easy to change / update your favorite hobbies and colors of people. The decode table will handle all this. It just requires a simple record or deletion of this table. And also in this way, the User can have an infinite number of favorite hobbies and colors, since this is the decoding table that controls this, and not the definition of the Users table.
Manipulating your sample request is a bit if we want to find all the Users who like the blue color and the request booze will look.
Select u.Name from User u inner join decode d on d.UserId = u.UserId inner join Hobby h on h.HobbyId = d.LookUpId and d.Item_Type = 'Hobby' inner join Color c on C.ColorId = d.LookUpId and d.Item_Type = 'Color' where h.Hobby = 'drinking' and c.Color = 'blue'
The implementation of such associations is quite acceptable.