MahmoudAdelAli's avatar

Best practice for naming database tables

Hi , i named all tables with the the default names like

users
reports
clients

but i what if the name consists of two parts like financials reports or file fees , the name 'll be

financials_reports 
file_fee

so that 'll be confused between the relation tables so what's the best practice ?

0 likes
5 replies
OussamaMater's avatar

You are already following the standards, what's confusing about it?

You always need to keep in mind 3 things:

  • Tables names should be in plural (because it is less likely to conflict with reserved keywords).
  • Use snake_case (which you are already doing).
  • An intermediate table's name should be composed by the names of the referenced tables, and respects alphabetical order, example:
users
    id - integer
    name - string
 
roles
    id - integer
    name - string
 
role_user # notice here that it is singular and alphabetically ordered
    user_id - integer
    role_id - integer

And for sure it comes down to preferences, I just stated what's commonly used among developers.

1 like
MahmoudAdelAli's avatar

@OussamaMater Thank , the confuse here about the relationship tables like

role_user # notice here that it is singular and alphabetically ordered
    user_id - integer
    role_id - integer

and the two part table like

user_documents
	id
	name
	description
	etc ....
OussamaMater's avatar
Level 37

@MahmoudAdelAli you just join the tables names, make sure they're singular, and order them alphabetically.

Explanation:

  1. users => user && roles => role (I made sure they are singulars).
  2. first letter for users => U, first letter for roles => R (get the first letter of each name).
  3. R comes before U therefore the name of the table => role_user

This is it for intermediate tables, if it's not intermediate and is composed by two words, just make sure it's readable for you.

1 like

Please or to participate in this conversation.