Foreign key with 3 primary keys

Asked

Viewed 243 times

1

Hi. I have this table with 3 primary keys and I need to reference it in another table. It’s like?

I was just going to put an int-type id in the monthly table to be the only primary key. But each monthly fee must be unique to a contract and have only one record per month/year, so the 3 as primary key.

In the simplest model, I would link the id that is primary key with the other table and create a foreign key, like this: id_mensalidade REFERENCES mensalidade(id). Right?

But how do I do it in this model?

Thanks, if it is not clear I can edit the post.

To using Mysql.

inserir a descrição da imagem aqui

  • You can create a whole type primary key for the table and additionally apply a Unique Constraint to the three existing fields.

  • I didn’t do this because I found the extra 4 bytes for each row of my table descent.

  • 1

    In the case of this table, because of the low volume of data, it seems to me to have 4 bytes more or less. More important would be the clarity and simplicity of working with the simple primary key. Composite keys in general are not very encouraged.

1 answer

2

To reference a composite primary key would be like this:

FOREIGN KEY (ax,bx,cx) REFERENCES tabela_origem(a,b,c)

Browser other questions tagged

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