Is it wrong to use every table (redundant) relation to the company table to simplify my SQL queries?
Yes, absolutely, because that would mean updating every redundant relationship when updating a customer relationship with a company or company division - and if you miss such an update, you will now have a database full of redundant data. This is bad denormalization.
If you just want to simplify your SQL, consider using views to โwrapโ parent data. Here is the view that pulls company_id into the contract, by attaching to the client:
create view contract_customer as select a.*, b.contract_id, b.company_id from contract a join customer b on (a.customer_id = b.customer_id);
This connection is simple, but why repeat it again and again? Write it once, and then use the view in other queries.
Many (but not all) RDBMSs can even optimize the connection if you do not put any columns from the client in the selection list or where a view-based query suggestion is made if you do contract.customer_id restriction of the referential integrity of the foreign key on client.customer_id. (In the absence of such a restriction, the union cannot be omitted, since then there will be a .customer_id contract that does not exist in the client. Since you will never want this, you will add a foreign key constraint.)
Using a view provides what you want, without the overhead of time to update the child tables, without the additional cost of creating more detailed rows, adding a redundant column (and it really starts to matter when you have many rows, because the wider the row, the fewer lines can immediately fit into the memory), and most importantly, without the possibility of inconsistent data when the parent is updated, and the children are not.
tpdi
source share