4

I am setting up a MySQL database with multiple tables. Several of the tables will have fields with similar names that aren't necessarily for the same purpose.

For example, there's a users table that will have a name field, a category table with a name field and so on.

I've previously seen this setup up either with or without a preface to the field name, so in the above example using user_name, cat_name etc.

As these are all in separate tables, is there any benefit to structuring the database with or without this preface? I know that when using joins and calling the data through PHP you have to add a SELECT users.name AS username... to keep the fields from overwriting each other when using mysql_fetch_array. But i'm not sure if there's any efficiencies in using one method over the other?

1
  • If you were using a DBMS that supported "create domain", the kind of data management issue you are raising would best be addressed by whether user names and category names should be based on the same domain. And the answer depends on whether or not a user with name "XXX" and a category with name "XXX" are somehow referring to the same thing. The issue here is semantics, not format. Commented Apr 6, 2012 at 20:10

3 Answers 3

1

It depends on what your shop does or your preference. There is nothing about a prefix that will make this better. Personally I would just keep it as name since: Users.Name and Orders.Name and Products.Name all contain tuples with different object types.

At the end of the day you want to be consistent. If you prefer a cat_ and a user_ prefix just be consistent with your design and include this prefix for all object types. To me less is more.

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

Comments

0

It's really just a matter of preference. I personally prefer the approach of using just name.

One thing to watch out for though, if you're doing any SELECT * FROM ... queries (which you shouldn't be; always select fields explicitly), you may end up selecting the wrong data.

2 Comments

You don't get wrong data, you'll get an error that the name exists in two or more different tables, this is why you should alias and explicitly define each field you want returned.
yeah, i ran across this when I first started creating databases. It didn't give any errors, just when I combined tables the resulting array would overwrite the $query['name'] value with each instance of that variable name rather than distinguishing.
0

One disadvantage is if anyone is stupid enough to use natural joins (you can guess that I find this a poor practice but mysql does allow it so you need to consider if that will happen) you may end up joining on those fields with the same name by accident.

1 Comment

I agree on the bad practice (in a query that will be run over time). Natural joins and SELECT * are non-determinstic in the face of adding columns to a table. Baaaaad.

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.