NYCPHP Meetup

NYPHP.org

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

Tim Lieberman tim_lists at o2group.com
Sat Sep 12 20:38:51 EDT 2009


In my experience, the most important thing is consistency.  Almost  
everything else is a matter of taste.

For instance, some folks like to name columns with the table name as a  
prefix on every column (except foreign keys):

create table a(
	a_id int,
	a_value varchar(64),
	a_created datetime
);

create table b (
	b_id int,
	a_id int references a.a_id
	b_value varchar(128),
	b_created datetime
);

This is a smart thing to do as every column, across all tables, has a  
unique name (unless it's a foreign key)

However, it can create a lot of typing, which can be annoying.

On the other hand, you can be more concise:

create table a(
	id int,
	value varchar(64)
	created datetime
);

create table b(
	id int,
	value varchar(64),
	created datetime
);

This saves some typing, but can create annoying ambiguity.  Join  
operations end up requiring more specific selection critera (SELECT  
a.title as a_title, b.title as b_title).

I used to use the former method almost exclusively.  However, as I  
started playing with various frameworks, I've switched to the latter  
as those I've worked with kind of expect it.  Probably because various  
_call() based magic ends up looking nicer in userland code.

If you have more specific considerations, feel free to get more  
specific.

-Tim




On Sep 12, 2009, at 8:17 PM, matt at atopia.net wrote:

> Does anyone have any good naming conventions for mysql databases and  
> tables and columns?  I'm developing a complex lamp project now, and  
> my normal convention doesn't seem to want to work too well for this  
> project - there are a few conflicts.
>
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/show_participation.php




More information about the talk mailing list