1
To clarify how much can be "many foreign keys", I will explain the context:
In a system, I want to store by whom a certain record has been edited ( updatedBy
) or raised (createdBy
). This can happen by:
- A user, authenticated in the system;
- By the system, in an automatic action, for example.
The tables (which today are 8, but I imagine this situation in any system) seem something like:
_____________________________________________________
| ... | createdBy | createdAt | updatedBy | updatedAt |
| | | | | |
|_____|___________|___________|___________|___________|
I thought of three alternatives about as store the fields createdBy
and updatedBy
:
- Store the user name;
- Store some user identifier, not necessarily the primary key, and without creating foreign key;
- Store primary key by creating a foreign key.
The problems that I see in every alternative:
- User can change name, then data would become inconsistent;
- It does not seem correct to have an attribute that works as a foreign key but is not a foreign key, but I have no knowledge to elaborate this point and is the option that I am using now (in development environment);
- It seems the most correct way, but all tables would have connection with the table
Usuario
(including her with herself),createdBy
andupdatedBy
would be optional foreign keys.
And then we go to the questions:
- On Alternative 2: There is reason to say that it is bad use attributes that function as foreign keys but are not foreign keys?
- On Alternative 3: There is a performance problem in using many foreign keys?
If there is a better alternative that I haven’t mentioned, it will surely add to the answer.
If the database choice is relevant to the response (due to DBMS), I am using Mysql.
"Is it right to say that it is bad to use attributes that function as foreign keys but are not?" , are you sure you meant what you wrote? If they are not foreign keys it is completely wrong to qualify them as such. It is obvious that the use of foreign keys affects performance but you will have a more robust model and with coherent data, it remains for you to assess how important this is in your system.
– anonimo
These attributes, in practice, would not be FK, but queries would be made as for example
SELECT * FROM Usuario JOIN Roupa WHERE Usuario.username = Roupa.updatedBy
, That’s what I meant by "they work like foreign keys but they’re not"– Rafael Tavares
It does not seem to me a very common requirement that the user who created/modified a record needs to be saved (and what he deleted too, at least in many places this is only marked in the record, and not deleted effectively). Could you tell me why you need this? The alternative to my view would be that the schema itself already allows to have this information, depending on how it is modeled.
– Piovezan
I worked on a project that they solved that way, so it was the way I "learned". This type of information would also be displayed on client, in some situations, so I imagine you need to be in the comic book. I don’t understand what you mean by "the schema itself already allows me to have this information," so I don’t know if it would meet my need. If you have any reference you can share that I read :), or if you want to come up with an answer with this, I believe it is also valid as a "better alternative", as I mentioned at the end of the question.
– Rafael Tavares
I would say create an audit table, move these fields there and do FK for this audit table... At the end of the day, without a history, these camps are useless...
– Bruno Warmling
@Rafaeltavares I meant that the modeling itself already indicates which user has made the change (in another table, different from the one that has the record in question). But I don’t have an example at hand.
– Piovezan