-- Drop all tables to make sure they do not exist
   DROP TABLE DEMO.PRODUCT;
   DROP TABLE DEMO.ORDERS;
   DROP TABLE DEMO.CUSTOMER;
   DROP TABLE DEMO.ITEMS;
   DROP TABLE DEMO.COPY_ITEMS;
-- Create Stogroup for tables
    CREATE STOGROUP DSN8G210
      VOLUMES (ABC005,DEF008)
      VCAT DSNCAT
      PASSWORD OSESAME;
-- Create Available Product table
   CREATE TABLE DEMO.PRODUCT
     (PROD_ID            CHAR(04)         NOT NULL
     ,PROD_DESC          VARCHAR(40)      NOT NULL WITH DEFAULT
     ,AMT_EACH           DEC(7,2)         NOT NULL WITH DEFAULT
     ,PRIMARY KEY (PROD_ID)
     )
      IN DSB8G210.CISC0001
         AUDIT NONE
      DATA CAPTURE CHANGES
   ;
-- Create Customer Address  table
   CREATE TABLE DEMO.CUSTOMER
     (CUST_ID            CHAR(04)         NOT NULL
     ,LAST_NM            CHAR(20)         NOT NULL WITH DEFAULT
     ,FIRST_NM           CHAR(10)         NOT NULL WITH DEFAULT
     ,ADDR_1             CHAR(32)         NOT NULL WITH DEFAULT
     ,ADDR_2             CHAR(32)         NOT NULL WITH DEFAULT
     ,CITY               CHAR(19)         NOT NULL WITH DEFAULT
     ,ST                 CHAR(03)         NOT NULL WITH DEFAULT
     ,ZIP                CHAR(10)         NOT NULL WITH DEFAULT
     ,SHIP_VIA           CHAR(10)         NOT NULL WITH DEFAULT
     ,PRIMARY KEY (CUST_ID)
     )
      IN DSB8G210.CISC0001
         AUDIT NONE
      DATA CAPTURE CHANGES
   ;
-- Create New Orders table
   CREATE TABLE DEMO.ORDERS
     (ORD_NO             INTEGER          NOT NULL
     ,CUST_ID            CHAR(04)         NOT NULL WITH DEFAULT
     ,DATE_ENTERED       DATE             NOT NULL WITH DEFAULT
     ,ORD_STATUS         CHAR(1)          NOT NULL WITH DEFAULT
     ,SHIP_VIA           CHAR(10)         NOT NULL WITH DEFAULT
     ,TYPE_CARD          CHAR(01)         NOT NULL WITH DEFAULT
     ,CARD_NO            CHAR(25)         NOT NULL WITH DEFAULT
     ,EXPIRES            CHAR(05)         NOT NULL WITH DEFAULT
     ,PRIMARY KEY (ORD_NO)
    )
      IN DSB8G210.CISC0002
         AUDIT NONE
      DATA CAPTURE CHANGES
    ;
-- Create Items on New orders tables
   CREATE TABLE DEMO.ITEMS
     (ORD_NO             INTEGER          NOT NULL
     ,ITEM_NO            SMALLINT         NOT NULL
     ,PROD_ID            CHAR(4)
     ,QTY_ORDERED        SMALLINT         NOT NULL WITH DEFAULT
     ,QTY_SHIPPED        SMALLINT         NOT NULL WITH DEFAULT
     ,AMT_EACH           DECIMAL(7,2)     NOT NULL WITH DEFAULT
     ,DATE_ENTERED       TIMESTAMP        NOT NULL WITH DEFAULT
     ,DATE_SHIPPED       DATE
     ,ITEM_NOTES         CHAR VARYING(560)
     ,PRIMARY KEY (ORD_NO ,ITEM_NO  )
     ,FOREIGN KEY FOR_KEY1
      ( PROD_ID )
       REFERENCES DEMO.PRODUCT ON DELETE SET NULL
      )
      IN DSB8G210.CISC0003
         AUDIT NONE
      DATA CAPTURE CHANGES
    ;
    COMMENT ON DEMO.ITEMS (ORD_NO IS 'ORDER NUMBER',
    ITEM_NO IS 'ITEM NUMBER', PROD_ID IS 'PRODUCT ID' )
   ;
    COMMENT ON TABLE DEMO.PRODUCT IS 'Available Product'
   ;
    COMMENT ON TABLE DEMO.ORDERS IS 'New Orders'
   ;
    COMMENT ON TABLE DEMO.ITEMS IS 'Items on Order'
   ;
    COMMENT ON TABLE DEMO.CUSTOMER IS 'Customer Address'
   ;
-- Create Index for table
   CREATE UNIQUE INDEX DEMO.INDEX_10121 ON DEMO.ITEMS
      (DATE_SHIPPED DESC, ORD_NO, ITEM_NO)
      USING STOGROUP DSN8G210
       PRIQTY 36
       ERASE NO
       SUBPAGES 8
       BUFFERPOOL BP1 CLOSE YES
       DSETPASS OSESAME
    ;
-- Alter table to add foriegn key
   ALTER TABLE DEMO.ITEMS
      FOREIGN KEY FOR_KEY2
       ( ORD_NO )
      REFERENCES DEMO.ORDERS
       ON DELETE CASCADE
   ;
-- Create a copy of the Items table
    CREATE TABLE DEMO.COPY_ITEMS
       LIKE DEMO.ITEMS
    IN DSB8G210.CISC0004
       AUDIT NONE
    DATA CAPTURE CHANGES
   ;
--
   commit;
--
-- Sample data for Order table
--
   INSERT INTO DEMO.ORDERS (ORD_NO ,CUST_ID ,DATE_ENTERED ,ORD_STATUS
              ,SHIP_VIA ,TYPE_CARD ,CARD_NO ,EXPIRES )
    VALUES (1 ,'0001' ,CURRENT DATE ,'S' ,'UPS' ,'M'
           ,'1123-0001-2345-9901' ,'01/99' )
   ;
   INSERT INTO DEMO.ORDERS (ORD_NO ,CUST_ID ,DATE_ENTERED ,ORD_STATUS
              ,SHIP_VIA ,TYPE_CARD ,CARD_NO ,EXPIRES )
    VALUES (2 ,'0002' ,CURRENT DATE ,'S' ,'FEX' ,'V'
           ,'0987-6543-2101-2345' ,'11/99' )
   ;
commit;
--
-- Sample data for Customer Table
--
   INSERT INTO DEMO.CUSTOMER (CUST_ID ,LAST_NM ,FIRST_NM ,ADDR_1
                            ,CITY ,ST ,ZIP )
    VALUES ( '0001' ,'Mouse' ,'Mickey' ,'2465 E. Bayshore Road'
            ,'Palo Alto' ,'CA' , '94303' )
   ;
   INSERT INTO DEMO.CUSTOMER (CUST_ID ,LAST_NM ,FIRST_NM ,ADDR_1
                            ,CITY ,ST ,ZIP )
    VALUES ( '0002' ,'Duck' ,'Donald' ,'650 E. Swedesford Road'
            ,'Wayne' ,'PA' , '19087-1675' )
   ;
   INSERT INTO DEMO.CUSTOMER (CUST_ID ,LAST_NM ,FIRST_NM ,ADDR_1
                            ,CITY ,ST ,ZIP )
    VALUES ( '0003' ,'Woodpecker' ,'Woody' ,'7612 Highway 60'
            ,'Cedarburg' ,'WI' , '53012' )
   ;
   INSERT INTO DEMO.CUSTOMER (CUST_ID ,LAST_NM ,FIRST_NM ,ADDR_1
                            ,CITY ,ST ,ZIP )
    VALUES ( '0004' ,'Bear' ,'Yogi' ,'7612 Highway 60'
            ,'Cedarburg' ,'WI' , '53012' )
   ;
commit;
