NYCPHP Meetup

NYPHP.org

[nycphp-talk] Database, table, and column naming schemes

Matt Juszczak matt at atopia.net
Sun Sep 13 11:56:58 EDT 2009


> Plus, if you're going to be consistent with that "mistake", then your naming 
> should be:
>
> customer_customer
> customer_account
> customer_account_type

I disagree.  I wasn't trying to create "customer" as a prefix.  I was 
simply renaming the tables based on the one:many relationships I have 
inside the tables.

account
account_type
customer

since customer stores an account_id, and account stores an account_type 
id, I could have picked customer to be the main level table, and just 
references out from there:

customer.account_id -> account.id ->
 	account.type_id -> account_type.id
 		-> ..... ->...
 			-> ...... -> ...

Really inefficient, but it was another method of "standardizing" (which to 
me, gives no clarity, because as I've explained in one of my other emails, 
starting at one top level will make things confusing.  And then what 
happens if some new table references customer.id as customer_id?  like an 
emails table?  Then I'm really confused:

email
email_customer
email_customer_account
email_customer_account_type

I know this is messed up, but it was my thinking for a while as I tried to 
create a "Perfect" relational standard.  And I had no many to many 
relationships at that point, so it was really easy to do, but very 
ineffective.

> One additional consideration, which was discussed last week, was not 
> entitling the index of all tables as "id", but rather adding the table name 
> to the id, such as:
>
> customer_id
> account_id
> account_type_id
>
> That makes sense to me because when you're dealing with a bunch of tables, a 
> variable named "id" might be used incorrectly OR overwritten.

This is what I've usually done in the past.

> For example, using:
>
> SELECT * FROM customer WHERE customer_id = '$customer_id'
>
> is just as easy to read as:
>
> SELECT * FROM customer WHERE id = '$id'
>
> But later in the code, after reading many different tables, what table does 
> "$id" relate to? It's not clear is it? But there is no confusion with 
> $customer_id is there?
>
> HTH's

Thanks for your input!

-Matt



More information about the talk mailing list