During a recent browsing session on LinkedIn, I stumbled upon a poll that piqued my interest. The poll posed a simple yet divisive question: should table names in databases be singular or plural? I was genuinely surprised to see the online community so passionately engaged in this debate. So me being a person with an opinion decided to answer this question in this small text.
Plural table naming
A table is a collection of related data held in a table format within a database. It consists of columns and rows.
wikipedia
A single table should only consist of the same type of objects/events so the collection of it is plural. Let’s say we have an object user, so a collection of many “user” objects should suggest a name on its own and it should be called “users“.
Singular table naming
The only place where I saw singular table naming is then using ORM’s.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return f'<User {self.username}>'
if __name__ == '__main__':
app.run(debug=True)
The following example will create a table user in a database to make it users you should update this code by adding __tablename__ parameter
...
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
...
This would fix your table naming 🙂
Summary
As long as you are consistent in your project it doesn’t matter which one of the two you choose
… as long as it is plural 🙂