/*ORDER 502297*/      
      /*
      ACTION is CREATE Table CUSTOMERSPERCLUSTER
      */
CREATE SEQUENCE SEQ_CUSTOMERSPERCLUSTER
START WITH 1
INCREMENT BY 1;

CREATE TABLE CUSTOMERSPERCLUSTER (
       CUSTOMERSPERCLUSTER_ID NUMBER NOT NULL,
       CLUSTER_ID           NUMBER NOT NULL,
       CUSTOMER_ID          NUMBER NOT NULL,
       SYSTEMUSER_ID        NUMBER NOT NULL,
       TIMESTAMP            DATE NOT NULL
);

ALTER TABLE CUSTOMERSPERCLUSTER
       ADD  ( CONSTRAINT XPKCUSTOMERSPERCLUSTER PRIMARY KEY (
              CUSTOMERSPERCLUSTER_ID) ) ;

CREATE UNIQUE INDEX XAK1CUSTOMERSPERCLUSTER ON CUSTOMERSPERCLUSTER
(
       CLUSTER_ID                     ,
       CUSTOMER_ID                    
);

CREATE INDEX XIF1CUSTOMERSPERCLUSTER ON CUSTOMERSPERCLUSTER
(
       CLUSTER_ID                     ASC
);

CREATE INDEX XIF2CUSTOMERSPERCLUSTER ON CUSTOMERSPERCLUSTER
(
       CUSTOMER_ID                    ASC
);


ALTER TABLE CUSTOMERSPERCLUSTER
       ADD  ( CONSTRAINT CUSTOMERSPERCLUSTER_CUSTOMER_I
              FOREIGN KEY (CUSTOMER_ID)
                             REFERENCES CUSTOMER ) ;


ALTER TABLE CUSTOMERSPERCLUSTER
       ADD  ( CONSTRAINT CUSTOMERSPERCLUSTER_CLUSTER_ID
              FOREIGN KEY (CLUSTER_ID)
                             REFERENCES CUSTOMERCLUSTER ) ;

----------------------------------------------------------------

INSERT INTO CUSTOMERSPERCLUSTER
  (CUSTOMERSPERCLUSTER_ID,
   CLUSTER_ID,
   CUSTOMER_ID,
   SYSTEMUSER_ID,
   TIMESTAMP)
  (SELECT SEQ_CUSTOMERSPERCLUSTER.NEXTVAL,
          C.CLUSTER_ID,
          C.CUSTOMER_ID,
          getminsystemuser(),
          sysdate
     FROM CUSTOMER C
    WHERE C.CLUSTER_ID is not null) ;



