alter table adquirente add CONSTRAINT fk_adquirente_trans_imovel FOREIGN KEY(idn_transimovel) REFERENCES trans_imovel(idn_transimovel)

alter TABLE aidf add CONSTRAINT ck_data_doct_aidf CHECK(num_doct_fina >= num_doct_inic);
alter TABLE aidf add CONSTRAINT idx_pk_aidf PRIMARY KEY(idn_aidf);
alter TABLE aidf add CONSTRAINT fk_aidf_mode FOREIGN KEY(idn_modelo) REFERENCES modelo(idn_modelo);
alter TABLE aidf add CONSTRAINT fk_aidf_seri FOREIGN KEY(idn_serie) REFERENCES serie(idn_serie);
alter TABLE aidf add CONSTRAINT fk_aidf_pess FOREIGN KEY(idn_empr) REFERENCES empresa(idn_empr);
alter TABLE aidf add CONSTRAINT IDX_UK_AIDF_EMP_NUM UNIQUE(NUM_AIDF,IDN_EMPR,NUM_DOCT_INIC);
CREATE INDEX idx_aidf_empr ON aidf(idn_empr);

alter TABLE aliquota_simples_nacional add  CONSTRAINT ck_data_fim_simp_naci check(data_fim >= data_inic OR data_fim is null);
alter TABLE aliquota_simples_nacional add  CONSTRAINT idx_pk_aliq_situ PRIMARY KEY(idn_aliq_simp_naci);
alter TABLE aliquota_simples_nacional add  CONSTRAINT idx_uk_aliq_situ UNIQUE(valor_aliq,data_inic);

alter table atividade add   CONSTRAINT idx_pk_ativ PRIMARY KEY(idn_atvd);
alter table atividade add   CONSTRAINT fk_ativ_grup FOREIGN KEY(idn_grup_atvd) REFERENCES grupo_atividade(idn_grup_atvd);

alter table atividade_declarante add  CONSTRAINT ck_indc_atvd_dcrd check(ind_atvd = 1/*Principal*/ OR ind_atvd = 2/*Secundaria*/);
alter table atividade_declarante add  CONSTRAINT idx_pk_empr_dcrd_ativ PRIMARY KEY(idn_ativ_decl);
alter table atividade_declarante add  CONSTRAINT fk_empr_dcrd_ativ_ativ FOREIGN KEY(idn_atvd) REFERENCES atividade(idn_atvd);
alter table atividade_declarante add  CONSTRAINT fk_empr_dcrd_ativ_empr FOREIGN KEY(idn_declarante) REFERENCES declarante(idn_declarante);

alter TABLE cliente_fornecedor add  CONSTRAINT idx_pk_clie_forn PRIMARY KEY(idn_cliente);
alter TABLE cliente_fornecedor add  CONSTRAINT idx_uk_clie_forn UNIQUE(insc_muni,cnpj,cpf);
alter TABLE cliente_fornecedor add  CONSTRAINT fk_clie_forn_ende FOREIGN KEY(idn_ender) REFERENCES endereco(idn_ender);
alter TABLE cliente_fornecedor add  CONSTRAINT fk_clie_empr FOREIGN KEY(idn_empr) REFERENCES empresa(idn_empr);

alter TABLE compensacao_guia add  CONSTRAINT idx_pk_cops_cred_guia PRIMARY KEY(idn_compens_guia);
alter TABLE compensacao_guia add  CONSTRAINT fk_cops_cred_guia_moti FOREIGN KEY(idn_cred_decl) REFERENCES credito_declarante(idn_cred_decl);
alter TABLE compensacao_guia add  CONSTRAINT fk_cops_cred_guia_guia FOREIGN KEY(idn_guia) REFERENCES guia_iss_proprio(idn_guia);

alter TABLE controle_atu add  PRIMARY KEY (IDN_CONTROLE_ATU);


alter TABLE credito_declarante add  CONSTRAINT idx_pk_cred_empr PRIMARY KEY(idn_cred_decl);
alter TABLE credito_declarante add  CONSTRAINT fk_cred_empr_empr FOREIGN KEY(idn_declarante) REFERENCES declarante(idn_declarante);
alter TABLE credito_declarante add  CONSTRAINT fk_cred_empr_moti FOREIGN KEY(idn_moti_comp) REFERENCES motivo_compensacao(idn_moti_comp);


alter TABLE DATA_ENVIO add CONSTRAINT idx_pk_data_envio PRIMARY KEY(idn_data_envio);
alter TABLE DATA_ENVIO add CONSTRAINT fk_controle_alt_declarante FOREIGN KEY(idn_declarante) REFERENCES declarante(idn_declarante);

alter table declarante add  CONSTRAINT ck_scdd_libl_empr_dcr CHECK(ind_scdd_libl = 1/*Sim*/ OR ind_scdd_libl = 2/*Não*/);
alter table declarante add  CONSTRAINT ck_ince_cult_empr_dcr CHECK(ind_ince_cult = 1/*Sim*/ OR ind_ince_cult = 2/*Nao*/);
alter table declarante add  CONSTRAINT ck_subs_tribu_empr_dcr CHECK(ind_subs_tribu = 1 OR ind_subs_tribu = 2 OR ind_subs_tribu = 3);
alter table declarante add CONSTRAINT idx_pk_empr_dcr PRIMARY KEY(idn_declarante);
alter table declarante add CONSTRAINT idx_uk_empr_dcr UNIQUE(cnpj,insc_muni,cpf);
alter table declarante add CONSTRAINT fk_empr_dcr_sies FOREIGN KEY(idn_situ_esp) REFERENCES situ_especial(idn_situ_esp);
alter table declarante add  CONSTRAINT fk_empr FOREIGN KEY(idn_empr) REFERENCES empresa(idn_empr);


alter TABLE deducao add   CONSTRAINT idx_pk_dedu PRIMARY KEY(idn_dedu);
alter TABLE deducao add   CONSTRAINT fk_dedu_nota_dedu FOREIGN KEY(idn_nota_dedu) references nota_deducao(idn_nota_dedu);
alter TABLE deducao add   CONSTRAINT fk_dedu_nota_emit FOREIGN KEY(idn_serv_prest) references servico_prestado(idn_serv_prest);

alter TABLE deducao_guia add CONSTRAINT IDX_PK_COPS_DEDU_GUIA PRIMARY KEY(IDN_DEDU_GUIA);
alter TABLE deducao_guia add CONSTRAINT FK_COPS_DEDU_GUIA_SERV FOREIGN KEY(IDN_SERV_TOMA) REFERENCES SERVICO_TOMADO(IDN_SERV_TOMA);
alter TABLE deducao_guia add CONSTRAINT FK_COPS_DEDU_GUIA_GUIA FOREIGN KEY(IDN_GUIA) REFERENCES GUIA_ISS_PROPRIO(IDN_GUIA);

alter TABLE DEDUCAO_SERVICO_TOMADO add CONSTRAINT IDX_PK_DEDU_TOMA PRIMARY KEY(IDN_DEDU);
alter TABLE DEDUCAO_SERVICO_TOMADO add CONSTRAINT FK_DEDU_TOMA_SERV_TOMA FOREIGN KEY(IDN_SERV_TOMA) REFERENCES SERVICO_TOMADO(IDN_SERV_TOMA);
alter TABLE DEDUCAO_SERVICO_TOMADO add CONSTRAINT FK_DEDU_TOMA_NOTA_EMIT FOREIGN KEY(IDN_SERV_PREST) REFERENCES SERVICO_PRESTADO(IDN_SERV_PREST);

alter table evento add  CONSTRAINT idx_pk_even PRIMARY KEY(idn_evento);

alter table feriado add CONSTRAINT ck_tipo_feri CHECK(tip_feri = 1 OR tip_feri = 2 OR tip_feri = 3);
alter table feriado add CONSTRAINT idx_pk_feri PRIMARY KEY(data_feri);

alter TABLE guia_iss_proprio add CONSTRAINT idx_pk_guia_prop PRIMARY KEY(idn_guia);
alter TABLE guia_iss_proprio add CONSTRAINT ck_sit_guia CHECK(sit_guia >=1 AND sit_guia<=6);
alter TABLE guia_iss_proprio add CONSTRAINT fk_guia_prop_pess FOREIGN KEY(idn_empr_guia) REFERENCES declarante(idn_declarante);
alter TABLE guia_iss_proprio add CONSTRAINT uk_num_decl UNIQUE (num_guia, idn_empr_guia);
CREATE INDEX idx_fk_guia_prop_pess ON guia_iss_proprio(idn_empr_guia);


alter TABLE guia_iss_retido add CONSTRAINT idx_pk_guia_retd PRIMARY KEY(idn_guia);
alter TABLE guia_iss_retido add CONSTRAINT ck_sit_guia_retd CHECK(sit_guia >=1 AND sit_guia<=6);
alter TABLE guia_iss_retido add CONSTRAINT fk_guia_retd_toma FOREIGN KEY(idn_declarante) REFERENCES declarante(idn_declarante);
CREATE INDEX idx_fk_guia_retd_pres ON guia_iss_retido(idn_declarante);

alter TABLE guia_nota_proprio add  CONSTRAINT idx_pk_guia_nota_prop PRIMARY KEY(idn_guia_nota_proprio);
alter TABLE guia_nota_proprio add  CONSTRAINT fk_guia_nota_prop_guia FOREIGN KEY(idn_guia) REFERENCES guia_iss_proprio(idn_guia);
alter TABLE guia_nota_proprio add  CONSTRAINT fk_guia_nota_prop_evnt FOREIGN KEY(idn_serv_prest) REFERENCES servico_prestado(idn_serv_prest);

alter TABLE guia_nota_retido add  CONSTRAINT idx_pk_guia_nota_retd PRIMARY KEY(idn_guia_nota_retido);
alter TABLE guia_nota_retido add  CONSTRAINT fk_guia_nota_retd_guia FOREIGN KEY(idn_guia) REFERENCES guia_iss_retido(idn_guia);
alter TABLE guia_nota_retido add  CONSTRAINT fk_guia_evnt_retd_evnt FOREIGN KEY(idn_serv_toma) REFERENCES servico_tomado(idn_serv_toma);

alter TABLE guia_profissional add CONSTRAINT idx_pk_guia_prof PRIMARY KEY(idn_guia_prof);
alter TABLE guia_profissional add   CONSTRAINT fk_guia_prof_guia FOREIGN KEY(idn_guia) REFERENCES guia_iss_proprio(idn_guia);
alter TABLE guia_profissional add   CONSTRAINT fk_guia_prof_prof FOREIGN KEY(idn_prof_libe) REFERENCES profissional_liberal(idn_prof_libe);

alter  TABLE incentivo_guia add CONSTRAINT idx_guia_ince_prop PRIMARY KEY(idn_incent_guia);
alter  TABLE incentivo_guia add CONSTRAINT fk_guia_ince_prop_guia FOREIGN KEY(idn_guia) REFERENCES guia_iss_proprio(idn_guia);
alter  TABLE incentivo_guia add CONSTRAINT fk_guia_ince_prop_perd_ince FOREIGN KEY(idn_ince) REFERENCES incentivo_cultural(idn_ince);

alter table mensagem add PRIMARY KEY (IDN_MSG);

alter  TABLE motivo_cancelamento add   CONSTRAINT idx_pk_moti_canc PRIMARY KEY(idn_moti_canc);
alter  TABLE motivo_cancelamento add    CONSTRAINT idx_uk_moti_canc UNIQUE(desc_moti_canc);

alter TABLE motivo_compensacao add  CONSTRAINT idx_pk_moti PRIMARY KEY(idn_moti_comp);
alter TABLE motivo_compensacao add   CONSTRAINT idx_uk_moti UNIQUE(desc_moti_comp);

alter TABLE natureza_transimovel add  CONSTRAINT idx_pk_natu_trans PRIMARY KEY(idn_natu_trans);
alter TABLE natureza_transimovel add   CONSTRAINT idx_uk_natu_trans UNIQUE(desc_natu_trans);

alter TABLE nf_cancelada add  CONSTRAINT idx_pk_cancl PRIMARY KEY(idn_canc);
alter TABLE nf_cancelada add   CONSTRAINT fk_cancl_moti FOREIGN KEY(idn_moti_canc) REFERENCES motivo_cancelamento(idn_moti_canc);

alter TABLE nota_deducao add  CONSTRAINT idx_pk_nota_dedu PRIMARY KEY(idn_nota_dedu);
alter TABLE nota_deducao add   CONSTRAINT fk_nota_dedu_pess_pres FOREIGN KEY(idn_declarante_pres) REFERENCES declarante(idn_declarante);
alter TABLE nota_deducao add   CONSTRAINT fk_nota_dedu_pess_toma FOREIGN KEY(idn_cliente) REFERENCES cliente_fornecedor(idn_cliente);

alter table obra add  CONSTRAINT idx_pk_obra PRIMARY KEY(idn_obra);

alter TABLE parametro_calculo add   CONSTRAINT idx_pk_indi_mone PRIMARY KEY(data_ref,tip_param);

alter TABLE retencao_obrigatoria add   CONSTRAINT idx_pk_ret_obr PRIMARY KEY(cnpj, tip_retencao);

alter  TABLE servico_prestado add  CONSTRAINT idx_pk_emit PRIMARY KEY(idn_serv_prest);
alter  TABLE servico_prestado add   CONSTRAINT fk_empr_pres FOREIGN KEY(idn_declarante) REFERENCES declarante(idn_declarante);
alter  TABLE servico_prestado add CONSTRAINT fk_emit_tipo_negc FOREIGN KEY(idn_tipo_negc) REFERENCES tipo_negocio(idn_tipo_negc);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_natu_oper FOREIGN KEY(idn_natu_oper) REFERENCES natureza_operacao(idn_natu_oper);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_regi_espe_trbt FOREIGN KEY(idn_regi_espe_trbt) REFERENCES regime_especial_tributacao(idn_regi_espe_trbt);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_mode FOREIGN KEY(idn_modelo) REFERENCES modelo(idn_modelo);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_seri FOREIGN KEY(idn_serie) REFERENCES serie(idn_serie);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_atvd FOREIGN KEY(idn_atvd) REFERENCES atividade(idn_atvd);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_list_serv FOREIGN KEY(idn_list_serv) REFERENCES lista_servico(idn_list_serv);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_cida_pres FOREIGN KEY(idn_cidade_pres) REFERENCES cidade(idn_cidade);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_cida_inci FOREIGN KEY(idn_cidade_inci) REFERENCES cidade(idn_cidade);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_pess_toma FOREIGN KEY(idn_cliente) REFERENCES cliente_fornecedor(idn_cliente);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_pess_prep FOREIGN KEY(idn_intermed) REFERENCES cliente_fornecedor(idn_cliente);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_situ_espe FOREIGN KEY(idn_situ_esp) REFERENCES situ_especial(idn_situ_esp);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_obra FOREIGN KEY(idn_obra) REFERENCES obra(idn_obra);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_evnt FOREIGN KEY(idn_evento) REFERENCES evento(idn_evento);
alter  TABLE servico_prestado add   CONSTRAINT fk_emit_canc FOREIGN KEY(idn_canc) REFERENCES nf_cancelada(idn_canc);
CREATE INDEX IDX_DEC_DATA_EMIS ON SERVICO_PRESTADO(IDN_DECLARANTE,DATA_EMIS);
CREATE INDEX IDX_DEC_DATA_COMP ON SERVICO_PRESTADO(IDN_DECLARANTE,DATA_COMP);

alter table servico_tomado add CONSTRAINT idx_pk_rece PRIMARY KEY(idn_serv_toma);
alter table servico_tomado add CONSTRAINT fk_rece_pess_pres FOREIGN KEY(idn_declarante) REFERENCES declarante(idn_declarante);
alter table servico_tomado add CONSTRAINT fk_rece_pess_toma FOREIGN KEY(idn_cliente) REFERENCES cliente_fornecedor(idn_cliente);
alter table servico_tomado add CONSTRAINT fk_rece_moti_rete FOREIGN KEY(idn_motv_nao_retc) REFERENCES motivo_nao_retencao(idn_motv_nao_retc);
alter table servico_tomado add CONSTRAINT fk_rece_regr_espe FOREIGN KEY(idn_situ_espe_resp) REFERENCES situ_especial_respons(idn_situ_resp);
alter table servico_tomado add CONSTRAINT fk_rece_mode FOREIGN KEY(idn_modelo) REFERENCES modelo(idn_modelo);
alter table servico_tomado add CONSTRAINT fk_rece_seri FOREIGN KEY(idn_serie) REFERENCES serie(idn_serie);
alter table servico_tomado add CONSTRAINT fk_rece_cida FOREIGN KEY(idn_cidade_pres) REFERENCES cidade(idn_cidade);
alter table servico_tomado add CONSTRAINT fk_rece_pess_prep FOREIGN KEY(idn_intermed) REFERENCES cliente_fornecedor(idn_cliente);
alter table servico_tomado add CONSTRAINT fk_rece_evnt FOREIGN KEY(idn_evento) REFERENCES evento(idn_evento);
alter table servico_tomado add CONSTRAINT fk_rece_cida_inci FOREIGN KEY(idn_cidade_inci) REFERENCES cidade(idn_cidade);
CREATE INDEX IDX_DEC_DATA_RECE ON SERVICO_TOMADO(IDN_DECLARANTE,DATA_RECE);


alter table trans_imovel add CONSTRAINT FK_TRANS_DEC FOREIGN KEY(IDN_DECLARANTE) REFERENCES DECLARANTE(IDN_DECLARANTE);
