rpresser
2012-01-31 23:32:40 UTC
Here is a cut down structure:
CREATE TABLE Sites (
SiteID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
SiteName nvarchar(100) NOT NULL
)
CREATE TABLE Orders (
OrderID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
SiteID UNIQUEIDENTIFIER NOT NULL REFERENCES Sites (SiteID),
OrderNum INT NOT NULL
)
CREATE TABLE OrderItems (
OrderItemID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
OrderID UNIQUEIDENTIFIER NOT NULL REFERENCES Orders (OrderID),
ShippingOptionID UNIQUEIDENTIFIER NULL REFERENCES ShippingOptions (ShippingOptionID),
OrderItemQty INT NOT NULL,
OrderItemDescription NVARCHAR(100) NOT NULL
)
CREATE TABLE ShippingOptions (
ShippingOptionID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
SiteID UNIQUEIDENTIFIER NOT NULL REFERENCES Sites (SiteID),
ShippingOptionDescription NVARCHAR(80)
)
Note that OrderItems points to Orders, and also to ShippingOptions, each of which point to Sites. But it is still possible to have some inconsistency in the sense that an orderitem could point to an order of site X and also a shipping option of site Y.
My question is, is there any way to prevent it, without adding any fields? Is a trigger the right way to do it? Or a CHECK constraint using a UDF? Or something else I don't know about?
CREATE TABLE Sites (
SiteID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
SiteName nvarchar(100) NOT NULL
)
CREATE TABLE Orders (
OrderID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
SiteID UNIQUEIDENTIFIER NOT NULL REFERENCES Sites (SiteID),
OrderNum INT NOT NULL
)
CREATE TABLE OrderItems (
OrderItemID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
OrderID UNIQUEIDENTIFIER NOT NULL REFERENCES Orders (OrderID),
ShippingOptionID UNIQUEIDENTIFIER NULL REFERENCES ShippingOptions (ShippingOptionID),
OrderItemQty INT NOT NULL,
OrderItemDescription NVARCHAR(100) NOT NULL
)
CREATE TABLE ShippingOptions (
ShippingOptionID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
SiteID UNIQUEIDENTIFIER NOT NULL REFERENCES Sites (SiteID),
ShippingOptionDescription NVARCHAR(80)
)
Note that OrderItems points to Orders, and also to ShippingOptions, each of which point to Sites. But it is still possible to have some inconsistency in the sense that an orderitem could point to an order of site X and also a shipping option of site Y.
My question is, is there any way to prevent it, without adding any fields? Is a trigger the right way to do it? Or a CHECK constraint using a UDF? Or something else I don't know about?