ALTER TABLE PLTM.CMPT_PL_TRACKMAP
 DROP PRIMARY KEY CASCADE;

DROP TABLE PLTM.CMPT_PL_TRACKMAP CASCADE CONSTRAINTS;

CREATE TABLE PLTM.CMPT_PL_TRACKMAP
(
  POSITION        NUMBER(2),
  COILID          VARCHAR2(25 BYTE),
  BEF_ES          NUMBER(1),
  ES              NUMBER(1),
  ENT_LOO         NUMBER(1),
  PL              NUMBER(1),
  INT_LOO         NUMBER(1),
  ST              NUMBER(1),
  EXI_LOO         NUMBER(1),
  RUN_SPEED_MIN   NUMBER(3,1),
  RUN_SPEED_MAX   NUMBER(3,1),
  WELD_SPEED_MIN  NUMBER(3,1),
  WELD_SPEED_MAX  NUMBER(3,1),
  TOC             DATE,
  TOM             DATE,
  MOP             VARCHAR2(60 BYTE)
)
TABLESPACE USERS
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          64K
            NEXT             1M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX PLTM.CMPT_PL_TRACKMAP_PK ON PLTM.CMPT_PL_TRACKMAP
(POSITION)
LOGGING
TABLESPACE USERS
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          64K
            NEXT             1M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;


CREATE OR REPLACE TRIGGER PLTM.INS_B_CMPT_PL_TRACKMAP
BEFORE INSERT ON PLTM.CMPT_PL_TRACKMAP
FOR EACH ROW
DECLARE
   Programm VARCHAR(60);
   user VARCHAR(30);
   session_id Number; 
BEGIN 
       SELECT userenv('SESSIONID') INTO session_id FROM dual;
       BEGIN
           SELECT program,username INTO programm,user FROM V$SESSION   WHERE audsid = session_id;
       EXCEPTION WHEN NO_DATA_FOUND THEN
           programm := 'UNKNOWN';
           user := 'UNKNOWN';
       WHEN OTHERS THEN
           If (session_id = 0) Then
               programm := 'JOB';
           Else
               programm := 'UNKNOWN';
           END IF;
       END;
       :new.toc := sysdate;
       :new.mop := substr( programm ,1 ,60);
END;
/


CREATE OR REPLACE TRIGGER PLTM.UPD_B_CMPT_PL_TRACKMAP
BEFORE UPDATE
ON PLTM.CMPT_PL_TRACKMAP
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
   Programm VARCHAR(60);
   user VARCHAR(30);
   session_id Number;
   nPosition  number; 
   szCoilID   VARCHAR2 (20);
BEGIN 
        szCoilID = SUBSTR(:NEW.COILID,1,12) + '-00'; 
        UPDATE RASTCMDB.CMPT_PL_TRACKMAP 
                SET COILID = szCoilID  WHERE POSITION = :NEW.POSITION ;
       SELECT userenv('SESSIONID') INTO session_id FROM dual;
       BEGIN
           SELECT program,username INTO programm,user FROM V$SESSION   WHERE audsid = session_id;
       EXCEPTION WHEN NO_DATA_FOUND THEN
           programm := 'UNKNOWN';
           user := 'UNKNOWN';
       WHEN OTHERS THEN
           If (session_id = 0) Then
               programm := 'JOB';
           Else
               programm := 'UNKNOWN';
           END IF;
       END;
       :new.tom := sysdate;
       :new.mop := substr( programm ,1 ,60);
       

      
END;
/


ALTER TABLE PLTM.CMPT_PL_TRACKMAP ADD (
  CONSTRAINT CMPT_PL_TRACKMAP_PK
  PRIMARY KEY
  (POSITION)
  USING INDEX PLTM.CMPT_PL_TRACKMAP_PK
  ENABLE VALIDATE);
