NYCPHP Meetup

NYPHP.org

[nycphp-talk] naming identifiers

Daniel Convissor danielc at analysisandsolutions.com
Mon Aug 31 22:26:26 EDT 2009


Folks:

Folks:

On Mon, Aug 31, 2009 at 06:06:22PM -0700, Kristina D. H. Anderson wrote:

> For instance imagine pulling out the contents of ten tables through ten 
> queries in one script, and all the ID fields are named, id.

So true.  Using "id" as the primary keys means you need to make aliases 
in queries over and over and over.  Then there's the possibility of 
having inconsistently named aliases for the same field in different 
queries, so it's hard to remember what you're dealing with inside your 
PHP code.  Most importantly, if you name everything "id," then you have 
to explicitly spell out your JOIN statements in an ON clause

SELECT product_name, vendor_name
FROM product
JOIN vendor ON (vendor.id = product.vendor_id)

When it's so much easier to take advantage of USING clause:

SELECT product_name, vendor_name
FROM product
JOIN vendor USING (vendor_id)



> The flipside of the argument is that it gets darn annoying always 
> forgetting whether or not in this table the ID is prodid or prod_id or 
> productid or whatever it may be.

And that's where a thorough naming standards come in.  Name tables 
singluar (person, pet, product, etc).  Name the primary key field 
<table_name>_id.  No guessing involved.

--Dan

-- 
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
            data intensive web and database programming
                http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409



More information about the talk mailing list