Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Complex Foreign Key to cover integrity check over graph of tables

$
0
0

I have the following tables structure:

Site:
==========
Id (PK)
Name


Location:
==========
Id (PK, UQ1)
SiteId (UQ1)
Name


Order:
==========
Id (PK)
SiteId

OrderLine:
==========
Id (PK)
OrderId
LocationId

Is it possible to create foreign key from OrderLine.LocationId to Location table, that will not allow to insert a Location from a Site other than Order's Site ?
I know that this task can be implemented using triggers, but is it possible to implement it with foreign key constraint ?

I mean something like OrderLine.LocationId + OrderLine.Order.SiteId -> Location.Id + Location.SiteId

To reference the Location.Id + Location.SiteId combination I can create the Unique Index over this fields combination, but have no idea how to use the OrderLine.Order.SiteId in the FK...



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>