Logical Database Design: Keys and Normalization
For developers who know SQL tables and want to settle the structure before writing any DDL; keys and the first three normal forms are worked through on one order form.
On this page
You know what the application has to store, and you want tables that won't need restructuring once they hold data. Logical database design settles that structure before you choose a database engine.
It names the entities and their attributes, the key that identifies each row, and the relationships between the entities. Then it normalizes the entities so that each fact is stored once.
Conceptual, logical and physical design
Three models sit between a business conversation and a running database. Each one adds detail to the one before it.
| Model | What it holds |
|---|---|
| Conceptual | the main things and how they relate, such as "a customer places orders" |
| Logical | entities, attributes, keys and relationships, in normal form |
| Physical | tables, typed columns, constraints and indexes for one engine |
Here is a customer with their orders at each level:
Keys belong in the logical model, because the normal forms are defined by what depends on a key. A logical model may also carry general types, such as text, number or date. The engine's own types, such as varchar(100), belong in the physical model, with indexes, storage and collation.
Entities and attributes
A shop takes orders on a form. Three orders arrive like this:
Order 101 Alice Smith [email protected] Shoes 60.00, T-shirt 15.00
Order 102 Alice Smith [email protected] Shoes 60.00
Order 103 Ben Jones [email protected] T-shirt 15.00
Each line mixes facts about four things: the order, the customer, the products, and the link between an order and its products. Logical design gives each of those things its own entity.
An entity has one attribute for each fact, such as the customer's email. An entity becomes a table in the physical model, and an attribute becomes a column.
Normalization in one pass
Normalization splits the data until every fact is stored once. Each of the first three normal forms removes one kind of repetition. Apply them in order: split the 1NF table for 2NF, then the 2NF tables for 3NF.
First normal form (1NF)
First normal form allows only one value in each attribute. Order 101 holds two products, so in 1NF each product gets a row of its own:
| order_no | customer_name | product_name | price | |
|---|---|---|---|---|
| 101 | Alice Smith | [email protected] | Shoes | 60.00 |
| 101 | Alice Smith | [email protected] | T-shirt | 15.00 |
| 102 | Alice Smith | [email protected] | Shoes | 60.00 |
| 103 | Ben Jones | [email protected] | T-shirt | 15.00 |
No single attribute identifies a row here: order 101 has two rows, and Shoes appears on two orders. Together, order_no and product_name identify a row, so they form the key of this table. A key made of two or more attributes is a composite key.
Second normal form (2NF)
Second normal form requires every attribute outside the key to depend on the whole key. customer_name and email depend on order_no alone: one order_no gives one customer_name and one email.
price depends on product_name alone. So move customer_name and email to an orders table, and price to a products table.
orders keeps one row per order:
| order_no | customer_name | |
|---|---|---|
| 101 | Alice Smith | [email protected] |
| 102 | Alice Smith | [email protected] |
| 103 | Ben Jones | [email protected] |
products keeps one row per product:
| product_name | price |
|---|---|
| Shoes | 60.00 |
| T-shirt | 15.00 |
order_lines keeps one row for each product on an order:
| order_no | product_name |
|---|---|
| 101 | Shoes |
| 101 | T-shirt |
| 102 | Shoes |
| 103 | T-shirt |
The price of Shoes is now stored once, not twice. order_lines holds only its key, because no fact depends on an order and a product together.
This design assumes that each product has one price. If an order must keep the price at the time of sale, that sale price depends on the order and the product together. Then the price belongs in order_lines.
Third normal form (3NF)
Third normal form moves out the attributes that depend on the key only through another attribute outside the key. In orders, the email identifies the customer, so customer_name depends on the email rather than on the order. Alice's name is therefore still stored twice.
customers keeps each customer once:
| customer_name | |
|---|---|
| [email protected] | Alice Smith |
| [email protected] | Ben Jones |
orders keeps order_no and the email. The email now points at a row of customers. To change Alice's name, you now update one row.
Every remaining attribute depends on the key of its own table, and on nothing else.
The forms beyond the third, such as Boyce-Codd, handle cases that don't occur in the shop's orders. For those, see Database Normalization.
Choosing the primary keys
After normalization, each table has a key made of business data: order_no, product_name, email, and order_no with product_name in order_lines. Each of these keys is a candidate key. A candidate key identifies one row, and every one of its attributes is needed for that.
The primary key is the candidate key that you choose to identify rows. Other tables use the primary key to refer to those rows.
A natural key comes from the data, such as an email. A surrogate key is a generated value, usually a number, added only to identify the row.
When the values of a natural key never change, keep it as the primary key. When they can change, add a surrogate key:
| Table | Primary key | Kind | Why |
|---|---|---|---|
| orders | order_no | natural | the shop never changes an order number |
| customers | customer_id | surrogate | a customer can change their email |
| products | product_id | surrogate | a product can be renamed |
| order_lines | order_no, product_id | composite of two foreign keys | a line is one product on one order |
With email as the key, a customer's new address would have to change in every one of their orders. With customer_id as the key, the new address changes one row of customers.
A table that refers to another table holds the other table's primary key as a foreign key. orders now holds customer_id in place of the email, and order_lines holds product_id in place of product_name.
Relationships and cardinality
Each foreign key is one side of a relationship. The table that holds the foreign key is the child, and the table it points to is the parent. For each relationship, you decide three things.
Cardinality says how many children one parent can have. Optionality says whether a child needs a parent. A relationship is identifying when the parent's key is part of the child's primary key.
The order model decides them like this:
| Relationship | Cardinality | Child needs a parent | Identifying |
|---|---|---|---|
| customers to orders | one to zero or more | yes | no |
| orders to order_lines | one to one or more | yes | yes |
| products to order_lines | one to zero or more | yes | yes |
The finished model, in the crow's foot notation that an entity relationship diagram uses:
Orders and products have a many-to-many relationship: an order holds several products, and a product appears on several orders. order_lines is a junction table: it splits this relationship into two one-to-many relationships, one foreign key each.
If a child needs a parent, declare the child's foreign key NOT NULL. An order line can't exist without its order, because order_no is part of the order line's primary key. The relationship from customers to orders isn't identifying, because an order is identified by its own order_no.
No foreign key can enforce "one or more lines". The foreign key of order_lines makes each line point at an existing order, but the database still accepts an order with no lines. Check that rule outside the foreign keys, for example in the application.
From logical model to physical schema
In the physical model, each relationship becomes a FOREIGN KEY constraint. email and product_name are candidate keys but not primary keys, so they become UNIQUE constraints. These statements ran unchanged on PostgreSQL 18, MySQL 9.1 and SQLite 3.50:
CREATE TABLE customers (
customer_id int PRIMARY KEY,
customer_name varchar(100) NOT NULL,
email varchar(100) NOT NULL UNIQUE
);
CREATE TABLE products (
product_id int PRIMARY KEY,
product_name varchar(100) NOT NULL UNIQUE,
price decimal(8,2) NOT NULL
);
CREATE TABLE orders (
order_no int PRIMARY KEY,
customer_id int NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);
CREATE TABLE order_lines (
order_no int,
product_id int,
PRIMARY KEY (order_no, product_id),
FOREIGN KEY (order_no) REFERENCES orders (order_no),
FOREIGN KEY (product_id) REFERENCES products (product_id)
);
On SQLite, run PRAGMA foreign_keys = ON on each connection. Without it, SQLite doesn't check the foreign keys. SQLite's int PRIMARY KEY columns accept NULL unless they are also declared NOT NULL.
Logical design in DbSchema
In DbSchema Architect, a logical design is a model with no database connected. In these steps, you change only the model file. To change a database, generate the physical model, connect it to the database, and create the schema there.
- Choose File → New Model, then Design without Database.
- Choose Logical Design and press Start Design.
- Right-click the diagram and choose Create Entity. Add the attributes in the Attributes tab, and tick Primary Key for the key.
- Drag an attribute of the child, such as customer id of orders, onto the key of the parent.
- In the New Relationship dialog, choose Identifying or Non-Identifying, Mandatory or Optional, and the cardinality.
- Choose Model → Generate Physical Model, set Target DBMS, check the Model Conversion Rules and press Convert.
The New Relationship dialog holds the three decisions of a relationship:
Two dictionaries under Model → Dictionaries apply on every conversion. The Naming Dictionary turns customer id into customer_id, and the Conversion Dictionary maps each logical data type to a type of the target database.
Download DbSchema at https://dbschema.com/download.html, start a logical model, and draw the four entities of the order form with their keys and relationships. A new installation starts with a 15-day trial of Architect, the edition that has logical design. The free Community Edition covers connecting to a database, reverse-engineering its schema, and the diagrams.
Model the structure before you pick an engine
DbSchema keeps entities, identifiers and relationships in one model file, validates it, and converts it into the physical schema for your target database.