First I need to point that I read Database columns type prefix but it's not the same issue.
A long time ago someone who I worked with told me that in project I took part, all columns need to have unique prefix.
For example for users
table I use prefix u_
so all columns are named u_id
, u_name
and so on. The same with all other tables for example for products
it will be p_
prefix.
The reason of that was easier SQL JOINS - all columns will have unique names if 2 or more tables would be join. To be honest I've used this suggestion so far but in fact I don't know if it is used by many of you or it's really so useful.
What's your opinion on that? Do you use such column naming or maybe you think this is completely unnecessary and waste of time? (when displaying data you need to use prefixes if you don't remove them using function or foreach)
EDIT
Just in case more explanation
Assume we have users table with fields id, name and address table with fields id, name, user_id
In case if this method is used if we want to get all fields we can do:
SELECT *
FROM users u
LEFT JOIN address a on u.u_id = a.a_user_id
And in case we don't use prefixes for columns we should use:
SELECT u.id AS `u_id`,
u.name AS `u_name`,
a.id AS `a_id`,
a.name AS `a_name`,
a.user_id
FROM users u
LEFT JOIN address a on u.id = a.user_id
assuming of course we want to use columns as names and not numeric indexes 0,1 and so on (for example in PHP)
EDIT2
It seems that I haven't explained enough what's the problem - in MySQL of course in both cases everything works just fine and that's not a problem.
However the problem is when I want to use data in programming language, for example PHP. If I use:
<?php
$db = new mysqli('localhost','root','','test_prefix');
$result = $db->query("SELECT * FROM `user` u LEFT JOIN `address` a ON u.id = a.user_id ");
while ($data = $result->fetch_array()) {
var_dump($data);
}
I get:
array(8) { [0]=> string(1) "1" ["id"]=> string(1) "1" 1=> string(5) "Frank" ["name"]=> string(3) "USA" [2]=> string(1) "1" [3]=> string(3) "USA" [4]=> string(1) "1" ["user_id"]=> string(1) "1" } array(8) { [0]=> string(1) "2" ["id"]=> string(1) "2" 1=> string(4) "John" ["name"]=> string(6) "Canada" [2]=> string(1) "2" [3]=> string(6) "Canada" [4]=> string(1) "2" ["user_id"]=> string(1) "2" }
Whereas result in PhpMyAdmin for that query look like this:
In PHP get all the data but I can access data using numerical indexes: $data[0]
, $data[1]
and that's not very convenient. I cannot use user name because in $data['name']
there's only address name, the same in id. If I used any of both: aliases for columns or prefixes for columns I would be able to use string indexes for accessing data for example $data['user_name']
to access User name and $data['address_name']
to access Address name.