declare
    object_exists exception;
    pragma exception_init (object_exists , -00955);
begin
  execute immediate
	'CREATE TABLE MD_CONTA (
        CONTA_ID NUMBER(7) NOT NULL,
        CVECONTA VARCHAR2(50),
        NOME VARCHAR2(100),
        TIPO VARCHAR2(1),
        CODIGOCONTABIL NUMBER(5),
        DOCASSOCIADO NUMBER(1),
		CANCELAMENTO NUMBER(1),
        STATUS NUMBER(1),
		ORIGEM VARCHAR2(2),
		REFERENCIA VARCHAR2(100),
        ACTIVO NUMBER(1),
        FECMODIF DATE,
        USUARIO_ID NUMBER(7),
        PRIMARY KEY (CONTA_ID)
    )';
	  
  execute immediate 
	'CREATE SEQUENCE MDCONTA_SEQ INCREMENT BY 1 START WITH 50 NOCACHE ORDER NOCYCLE';	
	
  exception when object_exists then null;
end;
/

declare
    object_exists exception;
    pragma exception_init (object_exists , -00955);
begin
  execute immediate
	'CREATE TABLE MD_TOTALIZADOR (
		TOTALIZADOR_ID NUMBER(12) NOT NULL,
		FECCORTE DATE,
		PUNTOVENTA_ID NUMBER(7),
		EMPRESA_ID NUMBER(7),
		CONTA_ID NUMBER(7),
		IMPORTE NUMBER(12,2),
        PRIMARY KEY (TOTALIZADOR_ID)
	)';
	  
  execute immediate 
	'CREATE SEQUENCE MDTOTALIZADOR_SEQ INCREMENT BY 1 START WITH 1 NOCACHE ORDER NOCYCLE';	
	
  exception when object_exists then null;
end;
/

declare
    object_exists exception;
    pragma exception_init (object_exists , -02275);
begin
	execute immediate 'ALTER TABLE MD_TOTALIZADOR ADD FOREIGN KEY (CONTA_ID) REFERENCES MD_CONTA (CONTA_ID)';
    exception when object_exists then null;
end;

