"zorders" 4 Table Database example

Example ERD and basic commands to create tables

General ERD (Entity Relationship Diagram) aka Schema

zorders Database design

SQL code to generate additional tables

    USE STUusername;
    DROP TABLE IF EXISTS zproducts;  (just to start clean)

    CREATE TABLE zorders (
    /*  This is a comment */
    order_id INT UNSIGNED auto_increment PRIMARY KEY,
    date_ordered DATETIME,
    customer_id INT UNSIGNED,
    order_num VARCHAR(30),
    UNIQUE KEY(order_num)
    ) engine = InnoDB CHARSET utf8mb4 COLLATE = utf8mb4_unicode_ci;

    CREATE TABLE zcustomers (
    customer_id INT UNSIGNED auto_increment PRIMARY KEY,
    fname VARCHAR(30),
    lname VARCHAR(40),
    state varchar(3)
    ) engine = InnoDB CHARSET utf8mb4 COLLATE = utf8mb4_unicode_ci;

    CREATE TABLE zproducts (
    product_id INT UNSIGNED auto_increment PRIMARY KEY,
    prod_name VARCHAR(30),
    prod_desc VARCHAR(255),
    quantity INT,
    price DECIMAL(10,2),
    SKU INT UNSIGNED,
    UNIQUE KEY(SKU)
    ) engine = InnoDB CHARSET utf8mb4 COLLATE = utf8mb4_unicode_ci;

    CREATE TABLE zorders_products (
    product_id INT UNSIGNED,
    order_id INT UNSIGNED,
    UNIQUE KEY(product_id,order_id),
    saleprice DECIMAL(10,2),
    quantity INT
    ) engine = InnoDB CHARSET utf8mb4 COLLATE = utf8mb4_unicode_ci;

    ALTER TABLE zorders ADD FOREIGN KEY (customer_id) 
    REFERENCES zcustomers(customer_id);

    ALTER TABLE zorders_products ADD FOREIGN KEY (product_id) 
    REFERENCES zproducts(product_id);

    ALTER TABLE zorders_products ADD FOREIGN KEY (order_id) 
    REFERENCES zorders(order_id);

Possible Problems with above design

  1. inconsistent naming practices in zproducts
    • should be prod_price and prod_quantity etc.
  2. Would need additional tables to handle inventory properly
  3. May want to add a notes section for the zorders, or the zorders_products table
  4. zproducts should have a link to an image location
  5. the Foreign Keys (FKs) do NOT have an ON DELETE option, which could cause issues when deleting values from tables
  6. probably quite a few more

Do inserts for two more tables

    # This uses several built in MySQL functions to set dates
    INSERT INTO zorders (date_ordered) values
    (NOW()),
    (DATE_SUB(NOW(),INTERVAL 5 Day)),
    (DATE_ADD(NOW(),INTERVAL 1 month)),
    ('2018-09-23');

    INSERT INTO zproducts (prod_name,quantity,price)
    VALUES
    ('fusebox',23,23.95),
    ('conduit',123,3.95),
    ('monitor',34,112.95),
    ('electrical tape',345,2.97);

    # Note if you deleted product_id 1 from zproducts earlier, this will not work
    INSERT INTO zorders_products(order_id,product_id,quantity) VALUES
    (1,1,4),
    (1,3,2),
    (1,4,10),
    (2,4,4),
    (3,2,2),
    (3,4,25),
    (4,1,5),
    (4,2,3),
    (4,3,2),
    (4,4,55);

Query for Three tables

Rearranging the order of tables in the FROM line

    /* This adds the order_id so you can tell which order goes to which product
       and sorts it by date ordered. Note, adding zo.order_id to the ORDER BY
       will not do anything, as right now we only have ONE order per date. */

    SELECT zp.prod_name, zop.quantity, zo.date_ordered,zo.order_id
    FROM zorders zo JOIN zorders_products zop ON(zo.order_id = zop.order_id)
        JOIN zproducts zp ON(zp.product_id = zop.product_id)
    ORDER BY zo.date_ordered;

    /* You can usually change the order of tables in the FROM line and the
        query should work, especially if just using JOIN (INNER JOIN).
        When using LEFT JOIN or RIGHT JOIN  the order of tables DOES matter. */

    SELECT zp.prod_name, zop.quantity, zo.date_ordered,zo.order_id
    FROM zproducts zp JOIN zorders_products zop ON (zop.product_id = zp.product_id)
        JOIN zorders zo ON (zo.order_id = zop.order_id)
    ORDER BY zo.date_ordered, zo.order_id;

Adding Data to, and JOINing the Fourth Table

Updating zorders to have customers

    UPDATE zorders set customer_id = 1 WHERE order_id = 2;
    UPDATE zorders set customer_id = 2 WHERE order_id = 1;
    UPDATE zorders set customer_id = 4 WHERE order_id = 3;
    UPDATE zorders set customer_id = 4 WHERE order_id = 4;

    # Now re-run the above query, and you'll get your 10 expected results
    # As customer_id 3 has no orders, they do not show up at all as every JOIN in
    # the query has to match or the entire record is not shown

Additional Work

Add two more products

    INSERT INTO zproducts (prod_name,quantity,price)
    VALUES
    ('50Amp Breaker',22,12.99),
    ('Lightning Rod',54,34.55);

    # If we run the big query, none of these products show up, as they are not linked to an order

Add entry into zorders_products

    # the order of the fields is different in this example
    # product_id is first, then order_id, you can specify
    # which fields and what order. Any fields that are allowed to
    # be null can be omitted. As the PK (primary key) is set
    # auto_increment it can be left out from the INSERT.
    INSERT INTO zorders_products (product_id,order_id,quantity)
    VALUES (5,4,12);

    # Now there is a link between zorders (order_id 4) and zproducts (product_id 5)
    # The big query now shows 5 different items for order_id 4, including the 50Amp Breaker

Add another item to order 3 for zorders_products

    # adds product 6 (lightning rod) to order 3
    INSERT INTO zorders_products (product_id,order_id,quantity)
    VALUES (6,3,1);

    # run the big query above, and now you see that "lightning rod" shows up

Delete the item from Order 3

Additional SELECT queries

Page last updated on 20240829

HTML 5 Validation CSS 3 Validation Web Accessibility EValuator