Skip to content
Advertisement

manage reference table with foreign keys on new incoming data in other table

I have a reference table (in Postgres) and multiple other tables which can reference data in/from this table. It would make sense to use foreign keys to manage this, but I was wondering how one could manage updating the entries in the reference table in the best way when new values come in.
I guess it could be done through the coding that handles the data ingestion and check and insert new values in the reference and after that add the data to the other table. This doesn’t seem ideal somehow to me, so I wondered what the best practice is in handling this situation. As there is management of deletion present in SQL I wondered if the opposite might also be available for instance? (not to prevent insertion, but sort of cascading of the values?)

Example reference table;

table with sales to companies:

Now data can come in including rows like;

Ideally I could insert this with one query and update the reference table with the new company name so that it gets an id that will be used in the sales table.

Advertisement

Answer

You can first check if the record in the reference table exists ..

And in case it does you can insert the sales normally making reference to the company table. If it does not, you can use a CTE to insert the new company and get its id, so that you can outside the CTE insert the new sales record ..

Testing script to add a new company

If you want to ignore records that violate the foreign key constraint, check this answer by @a_horse_with_no_name.

Edit: Using anonymous code blocks including checks and inserts

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement