Use 1 field with foreign key to identify multiple tables

Asked

Viewed 22 times

1

What I would like is:

  • Create a log table
  • In the Logs table have a field ex: FK_ID, where this field would identify any data from any table. Because the table logs will have the logs of all tables in the system.
  • Because if that doesn’t work I would have to create a foreign key in the Logs table for each existing table in the system, and that would be several more fields added to the table, apart from the job of knowing which of the FK is filled.
  • A and it is not possible to have a FK of the log table in each table, as the same data can have multiple logs.

EX Pratico: Tab Client Tab Funcio Tab Usuario ID_CLIENTE ID_FUNCIONARIO ID_USUARIO

Tab Log ID_LOG FK_ID - Here would identify the belonging table and the specific data of the belonging table, it is possible?

The only way I could think was: Create the FK_ID field and insert the data code of the specific table Create the Fk_table field and insert the name of the table in which the data belongs

So I could do the search through the code and table name, but this I believe is more a gambiarra and not a real valid FK.

Could you help me? I’d appreciate it!

  • It is not clear what will save in the table logs, are system data errors related tables? You have to improve this question....

  • I believe that the concept of a foreign key is that the value of the field (or set of fields) uniquely identifies a row of another table, and not of possible multiple tables). The syntax is FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ] ie when creating the Constraint Foreign key you specify the referenced table.

  • Already managed to solve personal, with the issue of Foreign key being unique, I decided to create a code field and one to identify the table, thanks!

No answers

Browser other questions tagged

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