PDA

View Full Version : MySQL Good MySQL table structure



imagined
November 18th, 2009, 11:38 AM
This is what I have (simplified version)

USERS table
id
username
password

Each users has clients. The following table contains clients for all the users. userid column tells the app whose client it is.
CLIENTS table
id
userid (user client belongs to)
name
email

When a USER logs in, he sees a list of his CLIENTS.
SELECT * FROM CLIENTS WHERE USERID = 1234

The same goes for appointments, projects, etc.

Do you suggest I try another approach? For example, what about creating a table for each user so the user can have all of its clients information in a table. Or would that be complicating things.

Example:
table CLIENTS-1234
table CLIENTS-1235
table CLIENTS-1236

I'm sorry if this question is very basic. I'm going to start reading a book about just MySQL so I can make better, more informed MySQL database design decision.

blazes
November 18th, 2009, 01:33 PM
Your current layout is the correct (from a strict relational standpoint) way to do it. Creating a table for each user would lead to too many tables, too much repetitive data, and it would be easy to get things screwed up.

Voetsjoeba
November 19th, 2009, 05:30 AM
I concur with blazes; you definitely don't want to do the one-table-per-user thing. The way you have things set up now is the correct way. Rather than read a book on MySQL itself I suggest you read up on relational database design in general -- the database itself doesn't really matter that much. Of course though, keep in mind that if you're using MyISAM tables you don't get support for eg. foreign keys, which are pretty much the basic tool of relational design. You can still do it, but MySQL won't enforce your relationships.