Database Normalization – How to Normalize Database with Multiple Products to Single Transaction ID

normalization

How would you normalize this? I can't find a way to keep multiple products under a single SalesID
database

Best Answer

Might as well make my comment an answer so you can close it:

A Sales Order has many Sales Order Line Items. One table for Sales Orders, one for Sales Order Line Items with a foreign key to the former

create table sales_orders (
  id int primary key,
  order_date date,
  ...
);

create table order_items (
  order_id int references sales_orders(id),
  line_number smallint,
  unit_price decimal(19,4) not null,
  quantity smallint not null default 1,
  product_id int not null references products(id),
  ...

  primary key (order_id, line_number)
);