A friend told me that I should include the table name in the field name of the same table, and I\'m wondering why? And should it be like this? Example:
(Table)
Actually, there is a reason for that kind of naming, especially when it comes to fields, you're likely to join on. In MySQL at least, you can use the USING
keyword instead of ON
, then users u JOIN posts p ON p.user_id = u.id
becomes users u JOIN posts p USING(user_id)
which is cleaner IMO.
Regarding other types of fields, you may benefit when selecting *
, because you wouldn't have to specify the list of the fields you need and stay sure of which field comes from which table. But generally the usage SELECT *
is discouraged on performance and mainenance grounds, so I consider prefixing such fields with table name a bad practice, although it may differ from application to application.