Re: [TriPUG] [mysql-144] Tripug: PHP Best Practices: SQL column prefixing

From: Simon
Sent on: Wednesday, August 18, 2010 9:11 PM
 >>> Database field names should not be prefixed with "field_" or "fld_" 
or, really, anything.
I've used column (field) prefixing to avoid ambiguity when tables with 
some common column names are joined and the data is being retrieved 
using the column names (mysql_fetch_assoc()­, mysql_fetch_object()­, etc). 
For example:

     select * from table1 left join table2 using(stocknum);

If table1 and table2 both have column 'id, what does $result['id'] 
actually refer to - table1.id or table2.id?
What if both are needed ?
(rhetorical questions :)

Prefixed column names (tbl1_id and tbl2_id) avoids the issue without 
getting into numeric index type retrieves (e.g. mysql_fetch_array() ) or 
having to code unique column alias' on each select, for example:

     select table1.id as tbl1_id, table2.id as tbl2_id from table1 left 
join table2 using(stocknum);

Simon....

On 8/16/[masked]:13 PM, Michael Kimsal wrote:
> Database field names should not be prefixed with "field_" or "fld_"
> or, really, anything.

Our Sponsors

  • TEKsystems

    Meeting space and refreshments

  • indieconf 2013

    $20 discount off indieconf 2013 - use code TRIANGLEPHP to save today!

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy