All arguments seem pretty frail, always reduced to some form of "well this way makes it easier for ME to reason about!". I used to have opinions on this crap, and nowadays I've got way bigger fish to fry than worrying about table name plurality. As long as it's human-legible and consistent, who cares?
That's what the article is about..
Use whatever convention (per app/db/team), as long as it is consistent. Developers who use mixed singular and plural in the same db should be fired (into the sun).
The only mention of the word "consistency" was used to support the plural approach.
Nice guy, but a conditional recursively defined polymorphic class on a transaction server should be a crime. lol =3
And a relation is a set, hence plural.
2. It reads well everywhere else in the SQL query:
SELECT Employee.Name
, Manager.Name
FROM Users AS Employee
, Users AS Manager
WHERE Employee.ManagerID = Manager.ID
AND Employee.DateOfBirth IS NULL;
3. The name of the class you’ll store the data into is singular (User). You therefore have a mismatch, and in ORMs (e.g., Rails) they often automatically pluralize, with the predictable result of seeing tables with names like addresss.The class User represents a single row, not the entire table, hence singular. If the O/R mapper or some other tooling has issues with singular and plural, then I agree, it might not be worth fighting the tools.
4. Some relations are already plural. Say you have a class called UserFacts that store miscellaneous information about a user, like age and favorite color. What will you call the database table?
I think having the table and the class name both in plural would be fine. That also seems rare enough in practice that I would not let this dictate the decision. In the given example I would also tend to record the user facts as a list of them. A user fact is a key value pair associated with an user, the keys living in their own table. Having the keys implicit as column names will also make some queries unnecessarily complicated and as the number of facts grows, the table will become increasingly wide.
Also sometimes we have singular names for collections of things, then it is fine to have a singular table name, you can name your Trees table Forrest if that makes sense in the domain.
This is a very poor example, that case is literally in their unit tests file:
https://github.com/rails/rails/blob/b0c813bc7b61c71dd21ee3a6...
That test has been there over 18 years!
select sum(cat.length)
from cats as cat
That’s an interesting approach I haven’t seen before.(For me, I’m very much a singular it’s-a-datatype-not-a-collection person, but selecting from cat has always felt a bit awkward with that pattern.)
For me, it's in the same category as preferring generated primary keys were named <table-name>_id over just "id" - you may have a preference, but it's not got that much big picture importance.
Some DB engines won't let you use a keyword as identifiers for anything unless it's quoted, and then you either have to quote everything, or end up in a weird situation where some things are quoted and some aren't.
SELECT *
FROM customer
JOIN order USING (customer_id)
The general case is a named key.
For example we have a table "customer_data" at work and in our generated ORM code it's called "customer_datum".
The open-source singularization library is doing its job, but it sure made it difficult for me to global-search for that object name when I joined the company and didn't know the object was called that.
customers
customer_attributes
customers_labels
On one side you want to be consistent with the "customers" table you're joining to. On the other hand it reads weirdly to have an extra "s" in the middle of the table name.After you've got three or so words in a table name it really becomes inconsistent and you can't really guess what they're called when writing code.
There are solutions of course: whether to use the "s" on join tables could be a policy documented somewhere and everyone, including new employees, could be made aware of it. But it's a problem you don't have if you use singular table names.
It is better to be consistent but wrong, than inconsistent but correct.
Consistent and correct might seem ideal, but merely the fact that what is 'correct' is in the eye of the beholder most of the time, making it basically unobtainable.
But being consistent is at least something that is far less subjective.
than inconsistent but sometimes correct.
I try to avoid putting nouns on things when they could otherwise be inferred from their context of use. In this case we know it's a table. The possibility of multiple items being included is implied.
Singular/plural debate is driving us to name stuff in weird ways. This is why you should just go with the flow. Consider what a non technical person somewhere else in the business might call it. Aligning with this language has tremendous benefits. Trying to force purity into a natural language and communications context is how we make type systems and schemas that are indecipherable to the business.
* PostgreSQL system table names are singular e.g. "pg_class"
* Oracle system tables are plural e.g. "ALL_TABLES"
* MySQL is also plural e.g. "schemata".
I skimmed classic Codd paper "A relational model of data for large shared data banks" which gave birth to RDMS.
Codd uses singular nouns for example relation names: PART, PROJECT, COMMIT, supply, component, employee, jobhistory, salaryhistory. The only exception is "children" relation.
So that's one small argument in favour of singular approach :)
> select * from "user"
which is ugly
I like tabs, most pick spaces
I like mercurial, most pick git
But no one’s taking my vim!
chasil•6h ago
"For most SQL code, your safest bet is to never use any English language word as the name of a user-defined object...
"SQLite adds new keywords from time to time when it takes on new features. So to prevent your code from being broken by future enhancements, you should normally quote any identifier that is an English language word, even if you do not have to."
https://sqlite.org/lang_keywords.html
hvenev•6h ago
01HNNWZ0MV43FF•6h ago
phoyd•5h ago
<SQL language identifier> ::=<SQL language identifier start> [ { <underscore> | <SQL language identifier part> }... ]
<SQL language identifier start> ::= <simple Latin letter> <SQL language identifier part> ::= <simple Latin letter> | <digit>
So, using names with trailing underscore should always be safe.
isoprophlex•5h ago