How to Design a Database Schema with an ER Diagram
Plan tables, keys and relationships with an ER diagram, import existing SQL, and export clean DDL for PostgreSQL, MySQL and SQLite with fewer mistakes.

How to Design a Database Schema with an ER Diagram
A database schema is one of the hardest things to change later. Columns spread into code, reports and integrations, and every rename becomes a migration. Spending an hour on a diagram before writing the first CREATE TABLE is one of the best investments in a project. This guide covers the basics of entity relationship design, keys and relationships, and how to move between dialects, using the Database ER Diagram Designer.
Entities, attributes and relationships
An entity is a thing you store many of: customers, products, orders. Each becomes a table. Attributes become columns, each with a type. Relationships describe how rows connect: a customer places many orders, an order contains many items, a product appears in many order items. An ER diagram draws tables as boxes and relationships as lines, often with crow's foot ends that show "one" and "many".
Choosing primary keys
Every table needs a primary key that uniquely identifies a row and never changes. Most teams use a surrogate key, an auto-incrementing integer or a UUID, rather than a natural key such as an email address, because emails change. Join tables that connect two entities, such as order_items, often use a composite primary key made of the two foreign keys, which also prevents duplicate pairs.
Foreign keys and on-delete rules
A foreign key column points at the primary key (or a unique column) of another table and lets the database refuse rows that point nowhere. Decide what happens when the parent row is deleted:
- CASCADE deletes the children too: right for order items when an order is removed.
- SET NULL keeps the child but clears the link: useful for optional relations.
- RESTRICT or NO ACTION blocks the delete while children exist: the safe default for important records.
Draw these decisions on the diagram so they are discussed, not discovered.
Normalisation without dogma
Normalisation means storing each fact once. If customer addresses appear in every order row, a change has to be made in many places. Moving them to their own table fixes that. Go too far, though, and simple screens need six joins. A good rule is to normalise first and denormalise deliberately for proven performance needs, writing down why.
Types and constraints
Pick types that describe the data: integer for counts, numeric(10,2) for money (never floating point), date or timestamp for times, text or varchar for strings, boolean for flags. Add NOT NULL wherever a value is required and UNIQUE for natural identifiers such as SKUs or emails. Constraints in the database are a last line of defence when application code has a bug.
Moving between PostgreSQL, MySQL and SQLite
The core SQL is shared, but details differ. Auto increment is serial or identity in PostgreSQL, AUTO_INCREMENT in MySQL and INTEGER PRIMARY KEY AUTOINCREMENT in SQLite. PostgreSQL has native uuid and jsonb types; MySQL stores UUIDs as CHAR(36) and uses JSON; SQLite has only a few storage classes and maps everything to INTEGER, TEXT, REAL, NUMERIC or BLOB. A designer that exports per dialect handles these translations, but always read the output, especially for defaults and time zones.
Starting from an existing database
For a legacy system, do not draw from memory. Export the schema (pg_dump with schema only, SHOW CREATE TABLE in MySQL, or .schema in SQLite), import it, and arrange the tables. Seeing the real relationships often reveals missing foreign keys, inconsistent naming or tables that nobody uses any more.
A checklist before you build
- Every table has a primary key.
- Every relationship has a foreign key with a chosen on-delete rule.
- Money uses numeric, not float.
- Required columns are NOT NULL, natural identifiers are UNIQUE.
- Names follow one convention, such as snake_case and singular or plural consistently.
- Created and updated timestamps exist where you will need an audit trail.
Limits
A diagram tool designs schemas; it does not connect to a live database, compare versions or write migrations for changes. Multi-column foreign keys, partitions, generated columns and database-specific types need manual work, and generated SQL should be reviewed and tested before it touches real data.



