Joint foreign key between index field and a possible string

Asked

Viewed 288 times

2

I am creating an application where I thought of a possibility to insert all existing addresses in the system in a single table, both for users, customers, suppliers etc. the structure of my table then would look like this:

__id => CHAVE_PRIMÁRIA
__status
* idReferencia => CHAVE_EXTRANGEIRA_CONJUNTA (REF. GENÉRICA COM BASE NO txTabela)
* txTabela  => CHAVE_EXTRANGEIRA_CONJUNTA (STRING: 'CLIENTE','FORNECEDOR',etc)
txGenero
nmCep
txLogradouro
txComplemento
txBairro
txLocalidade
txUf
txIbge

Is there any way I can mount this foreign key using an existing string in the field txTabela? or will I have to keep these addresses on numerous different tables and significantly increase the size of my database without need? (Bs, this would also occur with contacts, documents and other types of generic values)

I am sure that if I do not use foreign keys I will be able to build the structure of my system completely, but with the keys the data loading improves..

1 answer

1


In your place, I would make an N to N association with a CHECK CONSTRAINT to check that only one of the Fks is filled:

CREATE TABLE ENDERECO 
(
    ENDERECO_ID INT PRIMARY KEY IDENTITY,
    STATUS VARCHAR(10),
    GENERO VARCHAR(10),
    CEP VARCHAR(8)
    LOGRADOURO VARCHAR(500),
    COMPLEMENTO VARCHAR(500),
    BAIRRO VARCHAR(100),
    LOCALIDADE VARCHAR(255),
    UF CHAR(2),
    IBGE NUMERIC(10)
);

CREATE TABLE ENDERECO_RELACOES (
    ENDERECO_RELACAO_ID INT PRIMARY KEY IDENTITY,
    ENDERECO_ID INT,
    CLIENTE_ID INT NULL,
    FORNECEDOR_ID INT NULL
    OUTRA_TABELA_ID INT NULL,
    CONSTRAINT ENDERECO_REL_ENDERECO_FK FOREIGN KEY (ENDERECO_ID) REFERENCES ENDERECO (ENDERECO_ID),
    CONSTRAINT ENDERECO_REL_FORNECEDOR_FK FOREIGN KEY (FORNECEDOR_ID) REFERENCES FORNECEDOR (FORNECEDOR_ID),
    CONSTRAINT ENDERECO_REL_CLIENTE_FK FOREIGN KEY (CLIENTE) REFERENCES CLIENTE (CLIENTE_ID),
    CONSTRAINT ENDERECO_REL_OUTRA_TABELA_FK FOREIGN KEY (OUTRA_TABELA_ID) REFERENCES OUTRA_TABELA (OUTRA_TABELA_ID)
);

ALTER TABLE ENDERECO_RELACOES 
ADD CONSTRAINT CheckFKs
CHECK (
(CASE WHEN CLIENTE_ID IS NOT NULL THEN 1 ELSE 0 END
    + CASE WHEN FORNECEDOR_ID IS NOT NULL THEN 1 ELSE 0 END
    + CASE WHEN OUTRA_TABELA_ID IS NOT NULL THEN 1 ELSE 0 END)
    = 1
);
  • Yeah, but by doing so, I’d raise a relationship. and as addresses are not records so flexible it’s okay for me to have a data-based link within the table... my intention is to reduce the number of repetitive tables based on associative columns from within the program

  • Anything other than this you are creating a reference fragility within your bank. Some Frameworks use discriminators, which are columns string indicating which table the key came from, but are weak associations, not checked by the database. I can give another much more complex answer, with conference by triggers, but I find a whim close to the answer provided.

  • yes agree, that could generate a weakness in the database, but for the type of data to be used, and the form of administration of what I want to build, I believe it is a "creative" option. in the case of the information that will be inserted they are already weak associations so there is no problem ;D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.