One to many relationship on the same table

不问归期 提交于 2020-01-13 11:23:11

问题


Here is the situation:-

I have a table called Users. This contains user data for students and tutors as most of the data required is the same.

Having completed the system I am now told that the client would like to be able to assign students to tutors.

Is there a legitimate/ clean way I can create a one to many relationship within a single table, perhaps via a link table?

I've tried to think this through but whatever solution I come up with seems messy.

I would be grateful for any input.

Thanks

Phill


回答1:


Have you tried the following approach?

Make a new table, for example TutorStudent (choose a more appropriate name if needed). It should have two columns:

  • Tutor_ID
  • Student_ID

Both columns shall be the (composite) primary key, each column will be a foreign key to your Users table User_ID (I assume this is what you have).

So, if you have a tutor named Newton that has two students, Tesla and Edison, your Users table will have something like this:

  • User_ID, Name
  • 1, Newton
  • 2, Tesla
  • 3, Edison

and your TutorStudent table will have following values:

  • Tutor_ID, Student_ID
  • 1, 2
  • 1, 3

Relatively simple and doesn't require any modifications to your existing table.

Do take care when deleting users - use the delete cascade feature of your database system or do some maintenance work afterwards so your TutorStudent table doesn't go stale when updating/removing your users.




回答2:


My ideal for the same situation

Example: one book have many category:

Basic solution: book table has recorded book information category table has recored category information ex: 100 documents book_category_relation table has single book (book_id) has category(category_id) 1 book may be have 100 category_id

Ideal solution: First calculate total your category: ex 100 document. Each category equal value 1 bit: max 31 bit so 100 category we have ceil floor(100%31) = 4 groups

category_id = 1  :  1 (1%31) <=>  000000001  group 0 = floor(1/31)
category_id = 2  :  2 (2%31)<=>  000000010 group 0 = floor(2/31)
category_id = 3  :  4 (3%31)<=>  000000100 group 0 = floor(3/31)
category_id = 4   : 8(4%31)<=>  000001000 group 0 = floor(4/31)

...........................
category_id = 31:  2^31(31%31) <=>1000..000 group 0 if moduler 31 equal zero so number group = (31/31 -1)=0;
category_id = 32: 1(32%31) <=> 0000000001 group 1 = floor(32/31)
category_id = 33: 2(33%31) <=> 0000000010 group 1 = floor(33/31)

Ok now we add 4 fields in design book table (group_0,group_1,group_2,group_3) with int(11) unsigned and add index that fields

if book has category id = n so we can the following calculate formula:

bit code = (n%31 ==0)?31: (n%31)
number group field = (n%31==0)?(n/31 -1):floor(n/31)

ex: book in category_id = 100 so:

bit code = (100%31) =7 <=>2^7 = 128, 
group = floor(100%31) = 3 <=> in group_3

so if you need query all book in category_id = 100, query string is:

SELECT * FROM book WHERE group_3&128

Note: MySQL not index working if bitwise in where. But you can check in this link: Bitwise operations and indexes



来源:https://stackoverflow.com/questions/25501698/one-to-many-relationship-on-the-same-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!