PHP

Creating the winestore with SQL

The CREATE TABLE SQL statements that build the winestore database are shown for reference in Example 3-1.

The statements in Example 3-1 are derived from the entity-relationship model in Figure 3-1, and the process of converting this model to CREATE TABLE statements is described in Appendix C. An electronic copy of the statements can be found in the winestore.database file used to create the winestore database earlier in this section.

Example 3-1. The complete winestore DDL statements
CREATE TABLE wine (
  wine_id int(5) DEFAULT '0' NOT NULL auto_increment,
  wine_name varchar(50) DEFAULT '' NOT NULL,
  winery_id int(4),
  type varchar(10) DEFAULT '' NOT NULL,
  year int(4) DEFAULT '0' NOT NULL,
  description blob,
  PRIMARY KEY (wine_id),
  KEY name (wine_name)
  KEY winery (winery_id)
);
CREATE TABLE winery (
  winery_id int(4) DEFAULT '0' NOT NULL auto_increment,
  winery_name varchar(100) DEFAULT '' NOT NULL,
  region_id int(4),
  description blob,
  phone varchar(15),
  fax varchar(15),
  PRIMARY KEY (winery_id),
  KEY name (winery_name)
  KEY region (region_id)
);
CREATE TABLE region (
  region_id int(4) DEFAULT '0' NOT NULL auto_increment,
  region_name varchar(100) DEFAULT '' NOT NULL,
  description blob,
  map mediumblob,
  PRIMARY KEY (region_id),
  KEY region (region_name)
);
CREATE TABLE customer (
  cust_id int(5) NOT NULL auto_increment,
  surname varchar(50) NOT NULL,
  firstname varchar(50) NOT NULL,
  initial char(1),
  title varchar(10),
  addressline1 varchar(50) NOT NULL,
  addressline2 varchar(50),
  addressline3 varchar(50),
  city varchar(20) NOT NULL,
  state varchar(20),
  zipcode varchar(5),
  country varchar(20),
  phone varchar(15),
  fax varchar(15),
  email varchar(30) NOT NULL,
  birth_date date(  ),
  salary int(7),
  PRIMARY KEY (cust_id),
  KEY names (surname,firstname)
);
CREATE TABLE users (
  cust_id int(4) DEFAULT '0' NOT NULL,
  user_name varchar(50) DEFAULT '' NOT NULL,
  password varchar(15) DEFAULT '' NOT NULL,
  PRIMARY KEY (user_name),
  KEY password (password)
);
CREATE TABLE grape_variety (
  variety_id int(3),
  variety_name varchar(20),
  PRIMARY KEY (variety_id),
  KEY var (variety)
);
CREATE TABLE inventory (
  wine_id int(5) DEFAULT '0' NOT NULL,
  inventory_id int(3) NOT NULL,
  on_hand int(5) NOT NULL,
  cost float(5,2) NOT NULL,
  case_cost float(5,2) NOT NULL,
  dateadded timestamp(12) DEFAULT NULL,
  PRIMARY KEY (wine_id,inventory_id)
);
CREATE TABLE orders (
  cust_id int(5) DEFAULT '0' NOT NULL,
  order_id int(5) DEFAULT '0' NOT NULL,
  date timestamp(12),
  discount float(3,1) DEFAULT '0.0',
  delivery float(4,2) DEFAULT '0.00',
  note varchar(120),
  PRIMARY KEY (cust_id,order_no)
);
CREATE TABLE items (
  cust_id int(5) DEFAULT '0' NOT NULL,
  order_id int(5) DEFAULT '0' NOT NULL,
  item_id int(3) DEFAULT '1' NOT NULL,
  wine_id int(4) DEFAULT '0' NOT NULL
  qty int(3),
  price float(5,2),
  date timestamp(12),
  PRIMARY KEY (cust_id,order_no,item_id)
);
CREATE TABLE wine_variety (
  wine_id int(5) DEFAULT '0' NOT NULL,
  variety_id int(3) DEFAULT '0' NOT NULL,
  id int(1) DEFAULT '0' NOT NULL,
  PRIMARY KEY (wine_id, variety_id)
);