May 10, 2003

A good naming convention for tables in a database

We're currently maintaining a product that was developped by a java team about a year and a half ago. Although, we tend to mostly be horrified by the code and sometimes the architecture, they did one thing really well I think and that is naming the tables in the database. I've never seen anyone or any project use their nomenclature so I'll share it here with everyone.

The basic idea is this

  • Every table name is prefixed by a three letters related to the name. So for example a table representing account would be ACC_ACCOUNT or a table reprensenting devices would be DEV_DEVICE

  • A table linking rows in two other tables is just made up of the prefixes and a LINK suffix. Imagine two tables TBA_TABLE_A and TBB_TABLE_B. Their link table would be TBA_TBB_LINK
  • every column name is prefixed with those same three letters, so you get ACC_NAME and DEV_BRAND for example

  • Columns which are part of a primary are further prefixed by PK_ in addition to the three letters. So you typically get PK_ACC_ID

  • If your column is a link to another column in an other table you prefix the name of the column in the other table with FK_. Imagine a contract table (CTR_CONTRACT) that is linked to an account through the account's primary key. In that first table, you would have a FK_ACC_ID column

  • Finally if your foreign keys make up your primary key (usually for a many-to-many relationship) you prefix with PK_FK_. Using the same example as above, you get for the link between tables A and B : PK_FK_TBA_ID and PK_FK_TBB_ID

What this gets you is

  • You (almost) always know what table a column belongs to in a query

  • You mostly don't need to to use table aliases in your queries

  • It's easy to build JOIN queries because you usually have WHERE clauses such as : PK_ACC_ID=FK_ACC_ID and such

  • It's easier for someone learning the schema to have a consistent nomenclature

Anyone else does things similar to this ? Better ideas ?

Posted by pgirolami76 at 10:20 AM | Comments (23) | TrackBack