What you are trying to do is make sure that the values ββinserted in one table exist in another table, that is, forcing a foreign key. So this will be:
CREATE TABLE RollingStocks ( ... CONSTRAINT Pk_RollingStocks Primary Key (Id), CONSTRAINT RollingStocks_CategoryId_FK (RollingStockCategoryId ) REFERENCES FreightWagonTypes (ID) );
Except that you want to apply a foreign key that refers to two tables. This is impossible to do.
You have several options. One could combine FreightWagonTypes and LocomotiveClasses into one table. If you need separate tables for other parts of the application, you can create a materialized view to force the use of a foreign key. Materialized views are similar to tables and can refer to foreign keys. This parameter will not work if the key values ββfor the two tables conflict.
Another option is to recognize that having two tables referenced by the candidates suggests that RollingStock may need to be split into two tables - or perhaps three: supertypes and two subtype tables, which are RollingStock and FreightWagons, Locomotives.
By the way, what about PassengerCoaches, GuardsWagons, and RestaurantCars?
APC
source share