2

Lately I've been thinking about what's a better name for foreign keys, either user_id, or id_user style.

Personally I think id_user style is better for handling within database (db queries) where it's clear from the first three characters that the column is containing ID.

On the other hand I find user_id style more suitable e.g. with use of HTML autocomplete fields where there are two input fields; one name="user" for text, the other name="user_id" for his ID; which makes it easier to recognize that they belong to each other because they both start their name by defining what the content will be about.

What do you think?

4
  • I should use user.
    – S.Visser
    Apr 12, 2013 at 7:41
  • I would have to say this question is off topic. See FAQ
    – Class
    Apr 12, 2013 at 7:46
  • Different cms use different convention. Use what suits your best
    – silentboy
    Apr 12, 2013 at 8:05
  • @S.Visser: user is a reserved word and should not be used as column (or table) name.
    – user1822
    Apr 12, 2013 at 11:08

2 Answers 2

4

I prefer user_id but I think it doesn't really matter as long as you stay consistent and not mix them up so you keep your overview structured and readable.

0

As @randomizer correctly suggested, don't mix it, choose one - that is the stone rule.

I would suggest:

If you will name all columns with prefix e.g. user_columnName , do it with id too.

If I would use id_user I would avoid postfixing and named other self-columns without post/prefix.

I personally like using ID everywhere, where is some primary-ID and id_* elsewhere where it's referenced, because that way it is clear that ID is the primary id of the current table and id_*s are foreign keys to IDs in *s .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.