Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Shon Levi
Shon Levi
6,036 Points

Help with understand DATABASE structure

Hey you all, I just finished some of the php&mysql courses here...

I understand almost everything but I can't find out something about how to build and use database with many users.

Let's assume I have table for family name: ID, first name, last name, age, relationship, description -> for each family member

Everything works fine for 1 user - but if I will add register and login system - how can I manage the same tables for different users? It's like every user need it's personal table - what's sound really bad.

I came with this idea - to add another column of "user_id" - then to display all the rows that user_id = to the user id in users table - But what about id displayed? some user will get - 56 57 60 63 64 - for example - I want 1 2 3 4 5 for every user - like it's his own table.

BRIEFLY - how we manage many users in databases??

2 Answers

Not sure if I really understand. You said:

"Everything works fine for 1 user - but if I will add register and login system - how can I manage the same tables for different users? It's like every user need it's personal table - what's sound really bad.

I came with this idea - to add another column of "user_id" - then to display all the rows that user_id = to the user id in users table - But what about id displayed? some user will get - 56 57 60 63 64 - for example - I want 1 2 3 4 5 for every user - like it's his own table."

Agreed, you certainly don't want, and don't need, a separate table for each user!

What it sounds like you need are the two tables you mentioned: Users and Families. The fields in the Users table will be userID, loginName, . . ., and most importantly familyID. The fields in the Families table will be familyID, first name, last name, age, relationship, description. All the records in this Families table for any one family will have the same familyID. You join the two tables on familyID (the PK of the Families table to the FK of the Users table).

Now, if you want the family members in each family to have a sequential number 1, 2, 3, 4, etc., you solve that issue separately. One way is to add a seqNum field to the Families table, and as each family member is added to a family increment the seqNum field by 1. So the first family member would have a seqNum of 1, the next a seqNum of 2, etc. Then, when you query, you can include seqNum in the output and get the numbers you want. Note that seqNum is not a user id of any sort!

To really make this work you need to make sure the combination of familyID and seqNum in the Families table is unique for each record. It would also be a good idea to automate incrementing seqNum on an INSERT. But that's beyond the basics of initial design.

Steven Parker
Steven Parker
231,261 Points

Are the "users" the same thing as "family members"? In that case you don't need a new table for each user, just a new row. The table might look like this:

CREATE TABLE users
    ( ID            INTEGER
    , first_name    VARCHAR(32)
    , last_name     VARCHAR(32)
    , age           INTEGER
    , description   VARCHAR(200)
    );

For the relationships between users, that would be a many-to-many association, so for that you might have a "junction table". I'll call it relationships:

CREATE TABLE relationships
    ( user1         INTEGER
    , user2         INTEGER
    , type          VARCHAR(32)
    );

So some sample data for a couple named John and Mary Smith, and their children Jim and Jane:

INSERT INTO users (ID, first_name, last_name, age, description) VALUES
    (1, 'John', 'Smith', 35, 'Johns info'),
    (2, 'Mary', 'Smith', 32, 'Marys info'),
    (3, 'Jim',  'Smith', 16, 'Jims info'),
    (4, 'Jane', 'Smith', 14, 'Janes info');

INSERT INTO relationships (user1, user2, type) VALUES
    (1, 2, 'wife'),     (2, 1, 'husband'),  (3, 1, 'father'), (4, 1, 'father'),       
    (1, 3, 'son'),      (2, 3, 'son'),      (3, 2, 'mother'), (4, 2, 'mother'),       
    (1, 4, 'daughter'), (2, 4, 'daughter'), (3, 4, 'sister'), (4, 3, 'brother');

And a sample query for "Who is(are) Jim's sibling(s)?" might look like:

SELECT u2.first_name, u2.last_name, type AS relation FROM users u1
JOIN relationships r ON u1.id = r.user1
JOIN users u2 ON r.user2 = u2.id
WHERE u1.first_name = 'Jim' and type IN ('brother','sister');

And a fancier sample query for "Who are John's relatives?":

SELECT u2.first_name || ' ' || u2.last_name ||  ' is ' || 
       u1.first_name || ' ' || u1.last_name || '''s ' || type  AS relatives
FROM users u1
JOIN relationships r on u1.id = r.user1
JOIN users u2 on r.user2 = u2.id
WHERE u1.first_name = 'John';

The user ID's are different for each person, but normally there won't be any need to expose them. If you just want to sequentially number the results of a certain query, there's usually a method for that built into the database (but the methods differ in each type of database).