2

I want to build a small task-application with MYSQL and PHP. I want to assign tasks to single users and to multiple user groups. I was thinking to have a user_table:

userid  name
1       Peter
2       Hans

usergroup_table

usergroup_id name
1            Usergroup 1
2            Usergroup 2

usergroupmap_table (2 shared primary keys)

usergroup_id userid
1            1
1            2

tasks_table

taskid  taskname            due_date
1       Finish my exams     10-06-2016
2       Another task 2 do   15-06-2016

From this it is easy. Now I can think of 3 ways how to assign the user and usergroups to tasks. But I can't choose which one is best (Normalization??)

Solution 1

two seperate table, one table with single users, and the other with assigned groups: usertask_table (2 shared primary keys) && usergroup_table

taskid  userid
1       1
1       1

taskid  groupid
1       1
2       1

Solution 2

A combined table with either the userid 0, or groupid 0:

taskid  groupid  userid
1       1        0
2       1        0
2       0        1
2       0        2

Solution 3

A combined table with all assigned tasks, but with a varchar to define which group is selected

taskid  type  id
1       group 1
1       group 2
2       user  1      
2       user  2

I have no idea which solution gives me the most optimal design. I want to extend this application also with some other tables, where at the same way user and group permissions need to be assigned. Now i'm thinking about the 3th solution...

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user3278918
  • 45
  • 1
  • 7

1 Answers1

0

SOLUTION 1: Is good but then creates redundant tables which in my opinion are not only unnecessary, but also will require you querying 2 different tables each time you want to get all assigned tasks.

SOLUTION 2: Makes no sense to me at all.. Cause for you to determine if a task is assigned to a group or a user, you will have to check the values in each column, where one column has the task id of 0, then you check the task id in the other column. Unecessary load on your engine.

SOLUTION 3: To me is perfect - You only need to know what is in the type column to determine. Also you avoid disadvantages in having many tables as in the case of solution 1.

To futher improve solution 3, you can use numbers or booleans to differentiate between types (E.g. if 1, then it group, if 2 then its individual), just to reduce characters generated by the table / columns for your db

golf4sp
  • 129
  • 1
  • 1
  • 4