Table Naming Dilemma: Singular vs. Plural Names
Others have given pretty good answers as far as "standards" go, but I just wanted to add this... Is it possible that "User" (or "Users") is not actually a full description of the data held in the table? Not that you should get too crazy with table names and specificity, but perhaps something like "Widget_Users" (where "Widget" is the name of your application or website) would be more appropriate.
Database table names: Plural or Singular
I always use plural for table names and singular for column names. Not that there's any real technical reason for it, that's just what I prefer.
Doesn't much matter, so long as you are consistent.
I.e.
+========+ +==========+
| Posts | | Users |
+--------+ +----------+
| idPost | |-> | idUser |
| Poster | <-| | Name |
+========+ +==========+
My reasoning for this is what happens when you write the actual query:
SELECT idPost, Name FROM Posts
INNER JOIN Users ON Poster = idUser
If you use singular, it looks like you're selecting from a post, rather than from the set of all posts, and joining to a single user, instead of all users.
Singular or plural database table names?
IMHO, Table names should be plural like Customers.
Class names should be singular like Customer if it maps to a row in Customers table.
Database tables naming, plural or singular
This question calls for a religious war.
I have no doubt it should be plural because...
- A table is a collection of rows.
- The SQL syntax becomes more natural -
SELECT * FROM Customers
instead ofSELECT * FROM Customer
. - The analogy to OOP - you have a class
Customer
and a list or other collection of customers calledCustomers
. SELECT * FROM Customers AS Customer WHERE Customer.FirstName = 'John'
-Customers
refers to the whole table whileCustomer
refers to the current row.
Negative things
One has to switch several times between singular and plural during the development. You may start with a conceptual model - for example an entity relationship model - where the natural choice is to name the entity Customer
. From this model you generate a database and must pluralize the name to get the Customers
table. Finally you pick your favourit O/R mapper and it has to singularize the name again to get a class named Customer
.
If you have to do this manually because the tool is lacking support (for example EntityFramework prior to .NET 4.0) it might be a reasonable choice to keep the table names singular but therfore get a class Customer
instead of Customers
without changing it by hand.
Table Naming Dilemma: Singular vs. Plural Names
Others have given pretty good answers as far as "standards" go, but I just wanted to add this... Is it possible that "User" (or "Users") is not actually a full description of the data held in the table? Not that you should get too crazy with table names and specificity, but perhaps something like "Widget_Users" (where "Widget" is the name of your application or website) would be more appropriate.
Table Naming: Underscore vs Camelcase? namespaces? Singular vs Plural?
Being consistent is far more important than what particular scheme you use.
Why do table names have to be plural in Laravel?
They don't have to be plural. To make things easy, by default, Eloquent assumes that, if you have a model User
, your database table for it will be users
. However, if you want to have another name for your table, you can specify custom tables for your models, by defining a table
property. For ex:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $table = 'user';
...
}
This will tell Eloquent to use the table user
on your database when working with the model User
.
Database, Table and Column Naming Conventions?
I recommend checking out Microsoft's SQL Server sample databases:
https://github.com/Microsoft/sql-server-samples/releases/tag/adventureworks
The AdventureWorks sample uses a very clear and consistent naming convention that uses schema names for the organization of database objects.
- Singular names for tables
- Singular names for columns
- Schema name for tables prefix (E.g.: SchemeName.TableName)
- Pascal casing (a.k.a. upper camel case)
Related Topics
How to Connect an Existing SQL Server Login to an Existing SQL Server Database User of Same Name
How Do We Implement an Is-A Relationship
Postgresql: Remove Attribute from JSON Column
SQL Get All Records Older Than 30 Days
Group Data by the Change of Grouping Column Value in Order
Group by and Count in Postgresql
How to Execute a Stored Procedure in a SQL Agent Job
Parse JSON into Oracle Table Using Pl/Sql
SQL Server - Check to See If Cast Is Possible
Database/SQL Tx - Detecting Commit or Rollback
How to Use a Postgresql Triggers to Store Changes (SQL Statements and Row Changes)
How to Drop Multiple Tables in Postgresql Using a Wildcard
How to Find Records That Are Not Joined
How to Get Column Attributes Query from Table Name Using Postgresql
SQL Server 2008 Query to Find Rows Containing Non-Alphanumeric Characters in a Column