dbTalk Databases Forums  

Re: Referential integrity problem

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss Re: Referential integrity problem in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
rpresser
 
Posts: n/a

Default Re: Referential integrity problem - 02-01-2012 , 01:32 PM






I think I found an answer. Create a view that would return the illegal rows, cross joined to a table that has more than one row, and create a unique index on that view. The view cross joined to a multirow table would result in multiple rows for each illegal row; the unique index then prevents the row from being created.

Idea from this article:
http://www.sqlservercentral.com/arti...odeling/61529/

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Referential integrity problem - 02-01-2012 , 04:42 PM






rpresser (rpresser (AT) gmail (DOT) com) writes:
Quote:
I think I found an answer. Create a view that would return the illegal
rows, cross joined to a table that has more than one row, and create a
unique index on that view. The view cross joined to a multirow table
would result in multiple rows for each illegal row; the unique index
then prevents the row from being created.
Now, that's wild!

My gut feeling when I look at the problem is that there is something wrong
in the modelling, but I can't point my finger on it. Well, it seems crazy
that different items on an order have different shipping options, but I
guess names have been changed to protect the innocent. And just with
abstract names, it difficult to say whether the model is correct or not.

The solution was amazing nevertheless.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
  #3  
Old   
Gene Wirchenko
 
Posts: n/a

Default Re: Referential integrity problem - 02-01-2012 , 05:22 PM



On Wed, 01 Feb 2012 23:42:56 +0100, Erland Sommarskog
<esquel (AT) sommarskog (DOT) se> wrote:

Quote:
rpresser (rpresser (AT) gmail (DOT) com) writes:
I think I found an answer. Create a view that would return the illegal
rows, cross joined to a table that has more than one row, and create a
unique index on that view. The view cross joined to a multirow table
would result in multiple rows for each illegal row; the unique index
then prevents the row from being created.

Now, that's wild!

My gut feeling when I look at the problem is that there is something wrong
in the modelling, but I can't point my finger on it. Well, it seems crazy
I red flag on it, too, but I have not looked at it deeply.

Quote:
that different items on an order have different shipping options, but I
guess names have been changed to protect the innocent. And just with
abstract names, it difficult to say whether the model is correct or not.
Why would it be crazy? A heavy part might require a truck. Spare
gaskets might be delivered by the salesperson when visiting.

"And last on the order, we require a supply of gaskets. Actually,
we need those right away. We are running low. Sam Salesperson has an
appointment with us tomorrow morning. Can Sam bring them then?"

Quote:
The solution was amazing nevertheless.
Sincerely,

Gene Wirchenko

Reply With Quote
  #4  
Old   
rpresser
 
Posts: n/a

Default Re: Referential integrity problem - 02-01-2012 , 05:42 PM



The best part of the solution is that by putting a WHERE clause in the view, I can enforce it only on future orders and not have to worry about fixing previous orders. Can't do THAT with a foreign key ;-)

Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Referential integrity problem - 02-02-2012 , 03:22 AM



rpresser (rpresser (AT) gmail (DOT) com) writes:
Quote:
The best part of the solution is that by putting a WHERE clause in the
view, I can enforce it only on future orders and not have to worry about
fixing previous orders. Can't do THAT with a foreign key ;-)

A foreign can be applied WITH NOCHECK which means that SQL Server does not
check that the current data is correct. Not that I would ever condone it...


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.