116

I've been reading a couple of questions/answers on StackOverflow trying to find the 'best', or should I say must accepted way, to name tables on a Database.

Most of the developers tend to name the tables depending on the language that requires the database (JAVA, .NET, PHP, etc). However I just feel this isn't right.

The way I've been naming tables till now is doing something like:

doctorsMain
doctorsProfiles
doctorsPatients
patientsMain
patientsProfiles
patientsAntecedents 

The things I'm concerned are:

  • Legibility
  • Quick identifying of the module the table is from (doctors||patients)
  • Easy to understand, to prevent confusions.

I would like to read any opinions regarding naming conventions. Thank you.

1
  • One thing to consider is what casing your frontend uses. With some technologies your JSON API properties will have the same casing as your ORM & DB which will cause mixed casing in your frontend code if they differ and you don't map the properties.
    – totymedli
    Apr 7, 2022 at 3:13

16 Answers 16

256

Being consistent is far more important than what particular scheme you use.

5
  • 29
    In other words, yes, well done, you've identified some consistent schemes. Get on with it!
    – Phil H
    Dec 10, 2009 at 14:40
  • 4
    +1 for the comment. Also, on a side note to the current naming scheme PascalCase is better, specially if you don't use Aliases for the SQL Queries. This way you can just use camelcase on the table column names and Pascal Case on the Table Names. Dec 11, 2009 at 1:58
  • 5
    Pascal/camel case for table names might lead to some issues. Every table will have a file in the filesystem, and some of them are case-insensitive (e.g. OSX).
    – ismriv
    Dec 18, 2013 at 11:52
  • 3
    @ismriv that's only a problem if you're inconsistent, if you're always referring to a tables/views/columns with the same consistent case you shouldn't have any problems, not being lazy when writing these names will gain you a big advantage later when reading. Using PascalCase for tables and camelCase for columns helps to identify the type of a name at glance and also mimics common Object-Oriented conventions.
    – TWiStErRob
    Dec 9, 2014 at 12:14
  • 1
    I totally agree that consistency is key, but this is an old question and for people using newer database platforms like Redshift and Snowflake that default to either all upper case or lower case identifiers, I'd add that it is quite important to think about your naming conventions right at the start. Opting to use a naming convention like Camel Case on these platforms can cause you some unnecessary headaches later on e.g. you will have to use quoted identifiers all the time and object name resolution might produce unexpected results. Mar 3, 2020 at 22:19
31

I typically use PascalCase and the entities are singular:

DoctorMain
DoctorProfile
DoctorPatient

It mimics the naming conventions for classes in my application keeping everything pretty neat, clean, consistent, and easy to understand for everybody.

6
  • 6
    It's also known as "CapitalCase".
    – corsiKa
    Nov 15, 2013 at 16:46
  • 11
    I've never heard it called "CapitalCase" in my 30 years experience. I have heard it called "camel case" the most and "Pascal case" has more recently been taken up. I came here to see why people seem to be using camel case for SQL when it has been mostly case-insensitive historically. I certainly don't want to fall behind the times.
    – Sinthia V
    Jul 27, 2017 at 18:30
  • 1
    @SinthiaV I don't know about others but in our case since we're using a JS ORM (regrettably) the options were 1) break convention by using camelCase in the db 2) break convention by using snake_case in JS 3) map camelCase in JS to snake_case in db. We decided without much initial thought to use camelCase in the db, and it hasn't been too bad, but quoting everything is a bit of a hassle.
    – Andy
    Mar 31, 2020 at 20:49
  • 4
    @SinthiaV as useless a comment this might be in context of the actual SO question, PascalCase is not the same as camelCase. PascalCase capitalizes the first letter of each word (crucially including the first) whereas camelCase only capitalizes the words after the first. Further reading: medium.com/better-programming/…
    – curiouser
    Apr 10, 2020 at 14:15
  • 3
    (Seven and a half years later...) In my college near Seattle the kids are taught by several professors that "CapitalCase" is the safer way to call it in the local businesses and that "PascalCase" is something you want to avoid saying if you don't want to sound too "old" during an interview.
    – Darren S
    Apr 13, 2021 at 5:33
19

Since the question is not specific to a particular platform or DB engine, I must say for maximum portability, you should always use lowercase table names.

/[a-z_][a-z0-9_]*/ is really the only pattern of names that seamlessly translates between different platforms. Lowercase alpha-numeric+underscore will always work consistently.

As mentioned elsewhere, relation (table) names should be singular: http://www.teamten.com/lawrence/programming/use-singular-nouns-for-database-table-names.html

1
  • I agree - underscore has least issues in comparing with Pascal or camel Casing. Specially when you namings travel to models, dto and frontend at the end.
    – Saulius
    Jun 8, 2017 at 11:25
16

Case insensitive nature of SQL supports Underscores_Scheme. Modern software however supports any kind of naming scheme. However sometimes some nasty bugs, errors or human factor can lead to UPPERCASINGEVERYTHING so that those, who selected both Pascal_Case and Underscore_Case scheme live with all their nerves in good place.

11

An aggregation of most of the above:

  • don't rely on case in the database
  • don't consider the case or separator part of the name - just the words
  • do use whatever separator or case is the standard for your language

Then you can easily translate (even automatically) names between environments.

But I'd add another consideration: you may find that there are other factors when you move from a class in your app to a table in your database: the database object has views, triggers, stored procs, indexes, constraints, etc - that also need names. So for example, you may find yourself only accessing tables via views that are typically just a simple "select * from foo". These may be identified as the table name with just a suffix of '_v' or you could put them in a different schema. The purpose for such a simple abstraction layer is that it can be expanded when necessary to allow changes in one environment to avoid impacting the other. This wouldn't break the above naming suggestions - just a few more things to account for.

10

I use underscores. I did an Oracle project some years ago, and it seemed that Oracle forced all my object names to upper case, which kind of blows any casing scheme. I am not really an Oracle guy, so maybe there was a way around this that I wasn't aware of, but it made me use underscores and I have never gone back.

1
  • 2
    In 11g and up, case seems to be preserved if the table name is wrapped in double quotes. Putting this here for future reference. I know with certainty Postgres does so as well, other db engines may not.
    – John O
    Dec 20, 2013 at 19:01
8

After reading a lot of other opinions I think it's very important to use the naming conventions of the language, consistency is more important than naming conventions only if you're (and will be) the only developer of the application. If you want readability (which is of huge importance) you better use the naming conventions for each language. In MySQL for example, I don't suggest using CamelCase since not all platforms are case sensitive. So here underscore goes better.

1
  • 2
    Absolutely agree!!! Especially with MySql when you run it on windows everything become from camelCase to camelcase. So having snake case is much better. Also about Modern software however supports any kind of naming scheme you have not any garanties that yop will write code for last version soft. Especially for database - version update is not trivial when you think about regression costs.
    – Cherry
    Feb 20, 2017 at 5:23
6

I tend to agree with the people who say it depends on the conventions of language you're using (e.g. PascalCase for C# and snake_case for Ruby).

Never camelCase, though.

2
  • 3
    Ada: Pascal_Case_With_Underscores
    – uetoyo
    Sep 29, 2014 at 9:21
  • 15
    This does not make sense when you want to access the same table by two different languages that have different naming conventions.
    – Daniel W.
    Feb 6, 2017 at 11:34
4

These are my five cents. I came to conclusion that if DBs from different vendors are used for one project there are two best ways:

  1. Use underscores.
  2. Use camel case with quotes.

The reason is that some databases will convert all characters to uppercase and some ones to lowercase. So, if you have myTable it will become either MYTABLE or mytable when you work with DB.

2
  • Postgres converts identifiers to lowercase. Which db does the contrary?
    – Kiruahxh
    Feb 19, 2021 at 22:38
  • 1
    @Kiruahxh Oracle
    – Neto Yo
    Dec 14, 2021 at 17:40
3

2 suggestions based on use cases:

  1. Singular table names.

Although I used to believe in pluralizing table names once, I found in practise that there is little to no benefit to it other than the human mind to think in terms of tables as collections.
When singularising the table names, you can silently add -table to the singular table name in your head, and then it all makes sense again.

SELECT username FROM UserTable 

Sounds more natural than

SELECT username FROM UsersTable 

But post-fixing every table with is just a waste.

The actual practical argumentation for singularising table names:

What is the plural of person: persons or people?
This is still ok.
But how do you like a table with postfix -status? Statuses?
That sucks, sorry. It is easy to inadvertently make a human mistake by singularizing the status table, but pluralizing the other tables.

  1. PascalCasing + Underscore convention.

Given table User, Role and a many-to-many table User_Role.
Considering underscore cased user_role is dubious when all table names are using underscore per default. Is user_role a table that contains user roles? In this case it is not, it is a join table.

When deciding on table name conventions I think it is useful to let go of personal preference and take into account the real practical considerations of real life problems in order to minimize dubious situations to occur.
As the many answers and opinions have indicated, whatever your personal opinion is, different people think differently, and you will not be the only person working on the database despite being the one who sets it up (unless you do, in which case you're only helping yourself).
Therefore it is useful to have practical argumentation (practical in the sense of, does it help my future co-workers to avoid dubious situations) when your past decision is being questioned.

1

Unfortunately there is no "best" answer to this question. As @David stated consistency is far more important than the naming convention.

1

Naming conventions exist within the scope of a language, and different languages have different naming conventions.

SQL is case-insensitive by default; so, snake_case is a widely used convention. SQL also supports delimited identifiers; so, mixed case in an option, like camelCase (Java, where fields == columns) or PascalCase (C#, where tables == classes and columns == fields). If your DB engine can't support the SQL standard, that's its problem. You can decide to live with that or choose another engine. (And why C# just had to be different is a point of aggravation for those of us who code in both.)

If you intend to ever only use one language in your services and applications, use the conventions of that language at all layers. Else, use the most widely used conventions of the language in the domain where that language is used.

1

C# approach

Singular/Plural

  • singular if your record in row contains just 1 value.
  • If it is array then go for plural. It would make perfect sense also when you foreach such element. E.g. your array column contains MostVisitedLocations: London, NewYork, Bratislava

then:

foreach(var mostVisitedLocation in MostVisitedLocations){
    //go through each array element
}

Casing

PascalCase for table names and camelCase for columns made the best sense to me. But in my case in .NET 5 when I had json objects saved in dbs with json object names in camelCase, System.Text.Json wasnt able to deserialise it to object. Because your model has to be public and public properties are PascalCase. So mapping table columns(camelCase) and json object names(camelCase) to these properties can result in error(because mapping is case sensitive). Btw with NeftonsoftJson this problem is not present.

So I ended app with:

Tables: App.Admin, App.Pricing, UserData.Account

Columns: Id, Price, IsOnline.

0

there's wide variability on how to separate words, so there you'll have to pick whatever you like better; but at the same time, it seems there's near consensus that the table name should be singular.

-2

lowercase and snake_case is the best practice

-2

I'm no expert, but I have qualms about the Underscore approach mentioned elsewhere. I was taught to use underscore to indicate subschemas, and I find it to be very useful. I also typically use Pascal Casing because I think it's easier to read:

Schema.SubSchema_TableName

What I have learned from experience is that each DB creator does whatever the hell they want. So, do what you like, just be consistent, write up a standards document for your DB. Indoctrinate your subordinates, start a cult, nothing is real!

Not the answer you're looking for? Browse other questions tagged or ask your own question.