/*****************************************************************************
*
* Filename         - $Workfile:   bsm_log.sql         $
*
* Author           - Rob Cooke
*
* Project Name     - UltraTrak Core 
*
* Source Code
* Identifier       - Database 
*
* Design Spec
* Section          - 
*
* Contents         - Database script to create arch.bsm_data
*
* Language         - Oracle SQL script      
*
* Classification   - Commercial In Confidence
*
*          (c) Ultra Electronics Limited, 2007
*
*****************************************************************************/

PROMPT Creating table BSM_LOG;

CREATE TABLE bsm_log
(
   bsm_id                        NUMBER NOT NULL,
   bsm_log                       VARCHAR2(1500),
   log_time                      DATE,
   msg_type                      VARCHAR2(3) NOT NULL,
   rx_tx                         VARCHAR2(1) NOT NULL,
   flight_id                     NUMBER NOT NULL,
   flight_no                     CHAR(8),
   flight_date                   CHAR(5),
   row_create_time               date,
   row_date_time                 date,
   cuser                         varchar2(30),
   muser                         varchar2(30),
   mterm                         varchar2(64),
   source                        varchar2(8),
   to_be_archived                varchar2(1),
   appn_row_id                   number(18) not null,
   system_id                     number(4) default 0 not null
);

alter table bsm_log add ( constraint bsm_log_pk primary key (appn_row_id) using index tablespace &1._indexes);

ALTER TABLE bsm_log ADD CONSTRAINT bsm_log_uk1 UNIQUE (bsm_id) USING INDEX TABLESPACE &1._indexes;

ALTER TABLE bsm_log ADD CONSTRAINT bsm_log_fk_flight FOREIGN KEY (flight_id) REFERENCES flight (flight_id) ON DELETE CASCADE;
  
CREATE INDEX bsm_log_idx_flt ON bsm_log (flight_id) TABLESPACE &1._indexes;

CREATE INDEX bsm_log_idx_flt2 ON bsm_log (flight_no, flight_date) TABLESPACE &1._indexes;

CREATE INDEX bsm_log_idx_bsm ON bsm_log (bsm_id, log_time) TABLESPACE &1._indexes;

COMMENT ON TABLE BSM_LOG IS 'This table contains BSMs that are received by the system';

COMMENT ON COLUMN BSM_LOG.BSM_ID IS  'The identity of the BSM';
COMMENT ON COLUMN BSM_LOG.BSM_LOG IS  'The actual BSM received';
COMMENT ON COLUMN BSM_LOG.LOG_TIME IS  'The time the BSM was received';
COMMENT ON COLUMN BSM_LOG.MSG_TYPE IS  'The BSM type';
COMMENT ON COLUMN BSM_LOG.RX_TX IS  'Received or transmitted flag';
COMMENT ON COLUMN BSM_LOG.FLIGHT_ID IS  'Flight identifier';
COMMENT ON COLUMN BSM_LOG.FLIGHT_NO IS  'Flight number';
COMMENT ON COLUMN BSM_LOG.FLIGHT_DATE IS  'Flight date';
COMMENT ON COLUMN BSM_LOG.APPN_ROW_ID IS 'Unique sequence number for this row';
COMMENT ON COLUMN BSM_LOG.ROW_CREATE_TIME IS 'The time the row was created';
COMMENT ON COLUMN BSM_LOG.ROW_DATE_TIME IS 'The time the row was last modified';
COMMENT ON COLUMN BSM_LOG.CUSER IS 'The user who created the row';
COMMENT ON COLUMN BSM_LOG.MUSER IS 'The user who last modified the row';
COMMENT ON COLUMN BSM_LOG.MTERM IS 'The terminal from which the last modifcation to this row was made';
COMMENT ON COLUMN BSM_LOG.SYSTEM_ID IS 'The system identifier';
COMMENT ON COLUMN BSM_LOG.SOURCE IS 'The process that last amended this row ';
COMMENT ON COLUMN BSM_LOG.TO_BE_ARCHIVED IS 'Indicates that this row is to be archived or has been archived';
