Database Design Guidelines: Naming & Data Types
Introduction
The quality of a database is determined by two pillars: clarity (how easy it is to read) and efficiency (how well it performs).
Naming Conventions ensure that the database is not a "black box" but a readable map for developers.
Data Types ensure that data is stored efficiently, preventing storage bloat and calculation errors.
Part I: Naming Conventions
Creating a language that everyone on the team speaks.
1. The "Musts" (Technical Constraints)
Violating these rules will typically cause syntax errors or break SQL queries.
Avoid Reserved Keywords: Never use SQL keywords (e.g.,
SELECT,TABLE,ORDER,USER). Quoting identifiers (e.g.,"Order") is messy and error-prone.No Special Characters or Spaces: Use only A-Z, 0-9, and underscores (_). Never use spaces, hyphens, or emojis.
Start with a Letter: Identifiers must begin with a letter, not a number or underscore.
Length Limits: Keep names under 64 characters to avoid truncation by database tools.
Uniqueness: Column names must be unique within a table; table names must be unique within a schema.
2. The "Shoulds" (Best Practices)
These are style choices, but consistency here is critical for long-term maintenance.
Case Strategy:
snake_caseRule: Always use lowercase letters separated by underscores.
Why: Many databases (like PostgreSQL) are case-insensitive by default. Using
CamelCasecan force you to use double quotes around every column name in your queries.Good:
first_name,customer_ordersBad:
FirstName,CustomerOrders,first-name
Pluralization: Consistency is Key
Recommendation: Use Plural for table names and Singular for column names. This matches conventions used by frameworks like Ruby on Rails and Django.
Example: A table named
users(plural) contains a column namedemail(singular).
Be Descriptive but Concise
Avoid over-abbreviation that obscures meaning.
Good:
birth_date,customer_idBad:
bd,cust_ident,c_id
3. Specific Object Guidelines
| Object | Convention | Example |
| Tables | Plural nouns, snake_case | customers, order_items |
| Columns | Singular attribute name, snake_case | email, billing_address |
| Primary Keys | Usually just id | id |
| Foreign Keys | Singular table name + _id | customer_id (links to customers table) |
| Booleans | Prefix with is_ or has_ | is_active, has_subscription |
| Dates | Suffix with _at (timestamp) or _date | created_at (time), birth_date (day) |
4. Anti-Patterns (What to Avoid)
Hungarian Notation: Do not prefix tables with
tbl_or columns withcol_. It is redundant.Bad:
tbl_usersGood:
users
Ambiguous Names: Avoid generic names like
data,info, orvalueunless unavoidable in a generic EAV structure.Reusing ID Names: Don't name a primary key simply
key. It is a reserved word in many SQL dialects.
Part II: Data Types
Choosing the right container for your data.
Using the correct data type optimizes Performance (speed of indexing/retrieval), Integrity (preventing bad data), and Storage (cost).
1. Numeric Types
Integers (
INT,BIGINT,TINYINT): Use for whole numbers.- Tip: Use
TINYINTfor small ranges (like status codes 0-255) andBIGINTfor massive IDs (like Twitter snowflakes).
- Tip: Use
Decimals (
DECIMAL,NUMERIC): Use for Money.- Critical: Never use
FLOATfor money.FLOATis approximate and leads to rounding errors (e.g., storing $19.99 might result in 19.99000001).
- Critical: Never use
Floats (
FLOAT,DOUBLE): Use for scientific calculations (e.g., latitude/longitude, physics simulations) where slight precision loss is acceptable for speed.
2. String (Text) Types
Varchar (
VARCHAR): Variable length text. The most common string type.- Usage: Names, emails, addresses.
VARCHAR(255)is standard.
- Usage: Names, emails, addresses.
Char (
CHAR): Fixed length text.- Usage: Only when data is always the same length, like a Country Code (
US,JP) or Currency Code (USD).
- Usage: Only when data is always the same length, like a Country Code (
Text (
TEXT,LONGTEXT): For large bodies of text.- Usage: Blog posts, product descriptions, comments.
3. Date & Time Types
Date (
DATE): Stores YYYY-MM-DD. Use for birthdays or holidays.Timestamp/Datetime: Stores YYYY-MM-DD HH:MM:SS. Use for audit logs (
created_at,updated_at).- Tip: Always store time in UTC (Universal Time) to avoid timezone headaches later.
4. Boolean Types
Boolean (
BOOLEAN/BIT): Stores simple True/False or 1/0 values.- Usage:
is_published,is_verified.
- Usage:
Summary: Data Type Cheat Sheet
| Data to Store | The "Bad" Choice | Why it's Bad | The Correct Choice |
| Phone Number | INTEGER | Drops leading zeros; prevents storing formatting (+ or -). | VARCHAR(15) |
| Zip Code | INTEGER | Drops leading zeros (e.g., Boston 02108 becomes 2108). | VARCHAR(10) |
| Price / Salary | FLOAT | Rounding errors cause money to "disappear". | DECIMAL(10,2) |
| User Age | VARCHAR | Takes extra space; prevents math (e.g., "Show users > 18"). | TINYINT or SMALLINT |
| Status (Yes/No) | VARCHAR(3) | Storing "Yes" takes bytes; slower to index. | BOOLEAN (1 bit) |
Final Checklist
Before finalizing a table, ask:
Naming: Is it
snake_case? Is it descriptive?Types: Am I using
DECIMALfor money? Am I usingVARCHARfor codes that look like numbers (zip/phone)?Keys: Does every table have a Primary Key (usually
id)?