You are on page 1of 1

42 Chapter 1: Creating

The solution is to split order_form into order_header and order_detail with


the repeating columns moved down into order_detail. The order_number col-
umn in order_detail is a foreign key pointing to the order_header table; this
makes order_detail a repeating child of order_header. The product_number col-
umn is part of the primary key to identify different detail rows that are part of
the same order.
CREATE TABLE order_header (
order_number INTEGER NOT NULL PRIMARY KEY,
client_name VARCHAR ( 100 ) NOT NULL,
shipping_address VARCHAR ( 1000 ) NOT NULL,
salesperson_name VARCHAR ( 100 ) NOT NULL,
salesperson_phone VARCHAR ( 100 ) NOT NULL,
salesperson_commission NUMERIC ( 6, 3 ) NOT NULL );

CREATE TABLE order_detail (


order_number INTEGER NOT NULL REFERENCES order_header,
product_number INTEGER NOT NULL,
product_description VARCHAR ( 100 ) NOT NULL,
requested_quantity INTEGER NOT NULL,
estimated_shipping_date DATE NOT NULL,
PRIMARY KEY ( order_number, product_number ) );
The number of order_detail rows in a single order is now truly variable with no
artificial maximum. Each order_detail row can be processed like any other in an
application program loop, and the number of rows can be easily counted.

1.16.2 Second Normal Form


Second Normal Form (2NF) eliminates any non-key column that only depends
on part of the primary key instead of the whole key. The order_detail table has a
two-column primary key (order_number and product_number), but the prod-
uct_description column only depends on product_number. This violates Second
Normal Form.
One problem here is redundancy: If a product description changes, it must
be changed in every order_detail row containing that value. Another problem is
theres no place to store a new product number and description until that prod-
uct is ordered.
The solution is to move product_description up into a new table, prod-
uct_catalog, which holds information about products separate from orders. The
order_detail table becomes product_order, and the product_number column
becomes a foreign key pointing to the new product_catalog table.
CREATE TABLE order_header (
order_number INTEGER NOT NULL PRIMARY KEY,
client_name VARCHAR ( 100 ) NOT NULL,
shipping_address VARCHAR ( 1000 ) NOT NULL,
salesperson_name VARCHAR ( 100 ) NOT NULL,
salesperson_phone VARCHAR ( 100 ) NOT NULL,
salesperson_commission NUMERIC ( 6, 3 ) NOT NULL );

CREATE TABLE product_catalog (


product_number INTEGER NOT NULL PRIMARY KEY,
product_description VARCHAR ( 100 ) NOT NULL );

CREATE TABLE product_order (


order_number INTEGER NOT NULL REFERENCES order_header,

You might also like