68

How should I name my Tables when creating a new database?

Singular: Client or Plural: Clients?

  • I once had a co-worker who insisted that table names be singular and view names be plural. – René Nyffenegger Feb 23 '12 at 08:59
  • 2
    There are other schools of thought. 1) Use verbs that will allow one to express queries in natural language e.g. person NAMED 'fred' EARNS 20,000 (where the uppercase names are the tables). 2) use the enterprise's name for the set e.g. PERSONNEL, PAYROLL, ORG_CHART, etc. – onedaywhen Feb 23 '12 at 09:10
  • 3
    Possible cross site duplicate: http://stackoverflow.com/questions/338156/table-naming-dilemma-singular-vs-plural-names?rq=1 – Ciro Santilli OurBigBook.com Jan 24 '14 at 14:11

9 Answers9

70

Up to you. Just be consistent though.

Personally I prefer singular based on what each *row" stores: Order, Product, User, Item, etc.

This matches my modelling (via Object Role Modelling) where I use singular entities/types.

Edit:

One reason is that plural fails when you have link tables:
Orders, Products would give OrderProducts or OrdersProducts. Neither sounds correct

Or history tables (of course you can use schemas for this):
Orders -> OrdersHistory or (no!) OrdersHistories? Wouldn't Order-> OrderHistory be better?

gbn
  • 69,809
  • 8
  • 163
  • 243
  • 4
    Should it always boils down to a Personal choice? What if there were 2 people working in one database design, then one might name tables as plural and the other as singular. Is there no valid reasoning as to why use Singular or Plural? – John Isaiah Carmona Feb 23 '12 at 08:23
  • 2
    @JohnIsaiahCarmona: added a reason – gbn Feb 23 '12 at 08:28
  • 5
    I agree about using singular as being the most sensible. This doesn't seem to be the popular opinion, if you look around at similar questions here and on SO, etc. Lots of people seem to take a programmerly view of tables as collections which should therefore have plural names. I think that maybe ORMs might be starting to break people of this (bad) habit. If your tables have plural names to begin with it makes it hard to distinguish parent and child navigation properties and to distinguish instance and collection objects for a table. – Joel Brown Feb 23 '12 at 12:47
  • 5
    Another reason in favour of Singular is if you have a rule that the PK is named after tablename, for example TablenameID or TablenameCode or tablename_id. With plural table names, you end up with Orders.OrdersID (which doesn't look right) or with Orders.OrderID where you use plural for table names but change to singular for column prefixes. – ypercubeᵀᴹ Feb 27 '12 at 10:10
  • Another point along the same lines. – Jack Douglas Jul 21 '14 at 19:40
  • @ypercube There is a way to get around that issue. One can use the AS statement like so:

    SELECT * FROM Orders AS Order where Order.OrderID = 0

    – Abel Melquiades Callejo Dec 04 '15 at 14:51
  • @AbelMelquiadesCallejo Yeah but more like SELECT * FROM Orders AS O where O.OrderID = 0 ;. You can't use a reserved keyword as an alias. – ypercubeᵀᴹ Dec 04 '15 at 17:27
  • 1
    "Just be consistent though." - the most important part – Holly Sep 11 '19 at 15:24
13

Concerning singular versus plural table names, the subject seems to be controversial, but it shouldn't be.

While a table is a collection of multiple records, a table is named after the definition of the one type of record that it contains. If a table was allowed to have a different name than that of the type of record that it contains, you could give the table a plural name, so that you could for example have an Employees table containing multiple Employee records. But the designer of SQL did not provide for separate names for tables and record types.

Things work out more logically for object oriented programs that use the data, if the name of a record type (and by extension the table name) is kept singular, as it will correspond with the name of the class you would use to describe one record.

If you then want to identify a collection in the program, you can use a plural, or better, use an appropriate modifier, such as EmployeeList or EmployeeArray.

There is also a problem with irregular plurals for automatic code generation and programmers who have different language backgrounds or ideas about the formation of plurals in a program.

The English language is not a good and proper programming language, and trying to make database and program statements conform to English because it sounds better to read one of those statements is a mistake.

bobs
  • 381
  • 6
  • 16
Bruce Patin
  • 231
  • 2
  • 2
12

"order" is a reserved word. "orders" is not

"user" is a reserved word. "users" is not

"session" is a reserved word. "sessions" is not

"result" is a reserved word. "results" is not

"relative" is a reserved word. "relatives" is not

...

Those seem like common words that might go in line-of-business database. Plural words seem to be less common as key words than singular words. Therefore, it might be beneficial to use plural table names so as to avoid conflict with SQL key words.

99% of PostgreSQL reserved keywords are singular (except references), so if you use plural table names, you're more likely to avoid using a reserved keyword (such as user)

97% of SQL-92 reserved keywords are also singular (exceptions: constraints, diagnostics, names, references, rows, values)

Neil McGuigan
  • 8,423
  • 4
  • 39
  • 56
  • 4
    This is too close for clarity. Just stay away from reserved words, singular or plural. That really helps when debugging error messages that use plurals of reserved words interchangeably. Ideally pick words from the domain of the application to make it more relevant to use/user. – Emacs User Jul 18 '15 at 00:00
  • 2
    what does "too close for clarity" mean? – Neil McGuigan Jul 18 '15 at 00:09
  • 2
    It means a needless higher overhead deciphering error messages. For example, order by and orders in syntax error messages. Or trying to debug user and users in authentication error messages. – Emacs User Jul 18 '15 at 20:04
  • 2
    IMO PurchaseOrder, PortalUser, UserSession are better than just Order, User, Session so singular might just do fine in this scenario – John Jai May 23 '19 at 19:52
7

Just as @gbn's answer I think this is most a matter of preferences and just like him I recommend that any choice you made, apply it everywhere (in that DB at least). Consistency is worth it.

My preference, however is that a plural sounds better in SELECT statements :

SELECT Id, Name, Status 
FROM   Persons
WHERE  Status <> 5  --5 meaning deleted

I mean in this case, at least, there are several persons in the table and several of them are returned to the client.

Andrei Rînea
  • 728
  • 8
  • 14
  • 6
  • 1 though technically the plural of Person is People and this is one reason I use singular. Some ORM's will auto create the tables for you and you get weird scenarios like this where linguistically the naming is not logical.
  • – Mr.Brownstone Jan 06 '18 at 03:14