Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
6 tables ordering system model - Design and test in SQL
More MySQL commands example from a Well House Consultants training course
More on More MySQL commands [link]

This example is described in the following article(s):
   • MySQL - table design and initial testing example - [link]

Source code: shopper Module: S157

# Demonstration of six tables for a simple
# ordering system ...

# For the shop: store, aisle, product tables
# For the customer: customer, request, item tables

# Note - all fields start with same letter in each table
# Note - all tables have a unique id field
# Note - all tables first letter in unique
# Note - all table names are singular

# Note - sample data includes 2 records at each level so
# that we can test the dynamics but keep it as simple as poss

drop database if exists shopper;
create database shopper;
use shopper;

# Store, Aisle, Products

create table store (
        sid int primary key not null auto_increment,
        s_name text);
insert into store (sid, s_name) values
        (301,"The Emporium"),
        (302,"Cleaning made Easy");

create table aisle (
        aid int primary key not null auto_increment,
        a_sid int,
        a_name text);
insert into aisle (aid, a_sid, a_name) values
        (201, 301, "African Products"),
        (202, 301, "Chinese Products"),
        (203, 302, "Products for Bathrooms"),
        (204, 302, "Products for Outhouses");

create table product (
        pid int primary key not null auto_increment,
        p_aid int,
        p_name text,
        p_price int);
# To add later - purchase cost, VAT, etc
# Potential further tables - assoc. products,
# stock levels, shipments due etc.
insert into product (pid, p_aid, p_name, p_price) values
        (101, 201, "Rhino Horn", 100000),
        (102, 201, "Biltong", 500),
        (103, 202, "Sweet and Sour Soup", 130),
        (104, 202, "Flied Lice", 120),
        (105, 203, "Squeegee", 287),
        (106, 204, "Spiderweb", 5);

select * from (store join aisle on sid = a_sid) join
        product on aid = p_aid ;

# ------------------------------------------------

create table customer (
        cid int primary key not null auto_increment,
        c_name text,
        c_addy text );
insert into customer (cid, c_name, c_addy) values
        (601, "Jean James", "501 Tight Street"),
        (602, "Joe Public", "10 Market Place");

create table request (
        rid int primary key not null auto_increment,
        r_cid int);
# To add - date placed, status, shipped, etc
insert into request (rid, r_cid) values
        (501,601),(502,602),(503,601) ;

create table item (
        iid int primary key not null auto_increment,
        i_rid int,
        i_pid int,
        i_count int);
# Assuming unit sales - not half a pound of potatoes
insert into item (iid, i_rid, i_pid, i_count) values
        (401, 501, 101, 3),
        (402, 502, 102, 1),
        (403, 502, 101, 1),
        (404, 503, 103, 1),
        (405, 503, 104, 2),
        (406, 503, 106, 15);
# Make sure (at this stage) data is clean
# Some orders with one item and some with several
# Some products not on any order

# Should be able to formulate queries as follows:

# Product listings
# Customer listings
# Order listings
# Order pricing
# Products that have not been ordered
# Products by popularity (income)
# Products by popularity (quantity)
# Products by popularity (number of orders they are on)

# But first ... join all tables - "The ultimate join"

select c_name,p_name,i_count,p_price,a_name,s_name from
        (((( customer
        join request on cid = r_cid )
        join item on rid=i_rid)
        join product on pid = i_pid)
        join aisle on aid = p_aid)
        join store on sid = a_sid;

# Better structured - see as follows

select c_name,p_name,i_count,p_price,a_name,s_name from
         ((store
         join aisle on sid = a_sid)
         join product on aid = p_aid)
         join
         ((customer
         join request on cid = r_cid )
         join item on rid=i_rid)
         on pid = i_pid ;

# And here are some of the others!
# Look for all the orphan records (never-ordered products)

select s_name,a_name,p_name,i_count from
         ((store
         join aisle on sid = a_sid)
         join product on aid = p_aid)
         left join item on pid=i_pid where i_count is NULL;

# Order pricing - firstly make up the order (i.e. order by)
# Then group items by order id - BEWARE - [[example - i_count]]
# some fields appear CORRUPTED as only first value reported

select c_name,rid,i_count,sum(i_count) as n_items,
        sum(i_count*p_price) as req_total from

         (((customer
         join request on cid = r_cid )
         join item on rid=i_rid)
         join product on i_pid = pid)
         group by rid
         order by rid;

# Order value taken grouped by product sorted by value

select p_name,p_price/100 as price,sum(i_count) as count,
        sum(p_price*i_count/100) as total
        from product left join item on pid = i_pid
        group by p_name
        order by total desc;

# Dump out tables for completeness

select * from store;
select * from aisle;
select * from product;
select * from customer;
select * from request;
select * from item;
Learn about this subject
This module and example are covered on our public MySQL course. If you have a group of three or more trainees who need to learn the subject, we can also arrange a private or on site course for you.

Books covering this topic
Yes. We have over 700 books in our library. Books covering are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "More MySQL commands" training module. You'll find a description of the topic and some other closely related examples on the "More MySQL commands" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Web site author
This web site is written and maintained by Well House Consultants.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb