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?

asked Apr 12, 2013 at 7:38
4
  • I should use user. Commented Apr 12, 2013 at 7:41
  • I would have to say this question is off topic. See FAQ Commented Apr 12, 2013 at 7:46
  • Different cms use different convention. Use what suits your best Commented Apr 12, 2013 at 8:05
  • @S.Visser: user is a reserved word and should not be used as column (or table) name. Commented 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.

answered Apr 12, 2013 at 7:43
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 .

answered Sep 20, 2015 at 14:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.