--da eseguire come utente DBCGO

-------------------------------------------------------------------------
--TABLES

-------------------------------------------------------------------------
ALTER TABLE DBCGO_ANAG_CLU_B
DROP COLUMN COD_MAR;

-------------------------------------------------------------------------
CREATE TABLE DBCGO_ANAG_CLU_MAR_B
(
  COD_CLU VARCHAR2(2) NOT NULL,
  COD_MAR VARCHAR2(2) NOT NULL
)
TABLESPACE DBCGO_DATA_SMALL;

ALTER TABLE DBCGO_ANAG_CLU_MAR_B ADD 
CONSTRAINT PK_DBCGO_ANAG_CLU_MAR_B
 PRIMARY KEY (COD_CLU,COD_MAR)
 USING INDEX 
 TABLESPACE DBCGO_IDX_SMALL
 ENABLE
 VALIDATE;
 
 
-------------------------------------------------------------------------

--VIEW
-------------------------------------------------------------------------

CREATE OR REPLACE VIEW DBM_ANAG_MAR (COD_MAR, DESC_MAR, TIPO_CLIENTE)
AS (
select COD_MAR, DESC_MAR, 'CO'
from DBM_ANAG_MAR_SYN
union
select COD_MAR, DESC_MAR, 'BU'
from DBCGO_ANAG_MAR_B);

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

CREATE OR REPLACE VIEW DBM_ANAG_CLU
(COD_CLU, DESC_CLU)
AS 
( 
select COD_CLU, DESC_CLU 
from DBM_ANAG_CLU_SYN 
union 
select COD_CLU, DESC_CLU 
from DBCGO_ANAG_CLU_B
);

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

CREATE OR REPLACE VIEW DBM_ANAG_CLU_MAR
(COD_CLU, DESC_CLU, COD_MAR, DESC_MAR, TIPO_CLIENTE)
AS 
( 
select a.COD_CLU, a.DESC_CLU, b.COD_MAR, b.DESC_MAR, 'CO'
from DBM_ANAG_CLU_SYN a, DBM_ANAG_MAR_SYN b
where a.cod_mar=b.cod_mar 
union 
select a.COD_CLU, a.DESC_CLU, c.COD_MAR, c.DESC_MAR , 'BU'
from DBCGO_ANAG_CLU_B a, DBCGO_ANAG_CLU_MAR_B b, DBCGO_ANAG_MAR_B c
where a.cod_clu=b.cod_clu
and c.cod_mar=b.cod_mar
);

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