0

I want to create a database structure that can store data about families (primary key, name and its members).

I came up with the idea of doing it using these two tables:

TABLE "family":
- id (INT)
- name (VARCHAR)
- members (?)

TABLE "members"
- id (INT)
- name (VARCHAR)

I would like to reference members of the family by the id in the members table. But since MySQL doesn't have arrays, how am I supposed to store multiple members in one column? Or are there better DB structures for this case?

Kenta1561

3
  • @GurwinderSingh What is a mapping table? Commented May 6, 2017 at 13:52
  • You can simply have a foreign key in the members table that points to the family table. Commented May 6, 2017 at 13:55
  • Can someone be in more than one family? Commented May 6, 2017 at 13:56

3 Answers 3

1

Don't try to store multiple value in a single cell as arrays. It'll be nightmare for you later when you have to search rows based on one of the value in those arrays or join table with that column and other operations of the sort.

You can create a separate mapping table for that to keeps the things normalized.

You can create a separate table family_members with, say, three columns:

id (auto increment), 
family_id (FK to family table), 
member_id (FK to members table)

Or have an extra column in the members table as FK to family table if there is one to many mapping.

The mapping table helps if there can be many to many mappings.

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

Comments

1

Create Foreign key to Member_Id in tblFamily table

TABLE "tblFamily":
- Fam_Id (INT)
- Family_Name (VARCHAR)
- Member_Id (INT)

Create Primary key to Member_Id in tblMember

TABLE "tblMembers"
- Member_Id (INT)
- Member_Name (VARCHAR)

Comments

0

You can try below structure :

Table "Family"
- FAMILY_ID INT (PK)
- FAMILY_NAME VARCHAR

Now as your family can have multiple members, so instead of storing member_id in family table, it will be good to store FAMILY_ID in MEMBERS table.

Table "Member"
- MEMBER_ID INT (PK)
- MEMBER_NAME VARCHAR
- FAMILY_ID INT (FK) REFERENCES FAMILY.FAMILY_ID

This way you will be able to store multiple members for a family and using join on these tables will be able to print all information about the family in single query.

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.