views:

859

answers:

5

Do any versions of SQL Server support deferrable constraints (DC)?

Since about version 8.0, Oracle has supported deferrable constraints - constraints that are only evaluated when you commit a statement group, not when you insert or update individual tables. Deferrable constraints differ from just disabling/enabling constraints, in that the constraints are still active - they are just evaluated later (when the batch is committed).

The benefit of DC is that they allow updates that individually would be illegal to be evaluated that cummulatively result in a valid end state. An example is creating circular references in a table between two rows where each row requires a value to exist. No individual insert statement would pass the constraint - but the group can.

To clarify my goal, I am looking to port an ORM implementation in C# to SQLServer - unfortunately the implementation relies on Oracle DC to avoid computing insert/update/delete orders amongst rows.

+3  A: 

So far SQL Server does not support them. What is the problem you are solving?

AlexKuznetsov
We have an ORM layer in our business system that takes advantage of DC in Oracle that we may want to port to SQL Server. Unfortuntely, DC doesn't seem to be supported which complicates the effort to port the implementation. In particular, the network of changes records will need to be processed in a very particular (and difficult to compute order) to avoid violating any RI constraints. Just looking for a way to avoid having to do this.
LBushkin
I wrote a small post about it some time ago. Basically you save draft rows in all your tables, than you mark them as finished, at which point RI turns on. Google up"Mimicking Deferrable Constraints With Persisted Computed Columns. "
AlexKuznetsov
+1  A: 

Apparently not.

I found about five different blog posts all saying SQLServer (in various versions) does not support Deferrable Constraints.

On the other hand, I also found a post which attempts to mimic this feature by using "persisted computed columns," (scroll to the last entry), but caveat emptor

Matthew Jones
the link appears to be broken?
Jeffrey Kemp
A: 

Are you basically asking a variant of this question?

Aaron Alton
Not exactly - although it is related. We have an ORM layer in our business system that takes advantage of DC in Oracle that we may want to port to SQL Server. Unfortuntely, DC doesn't seem to be supported which complicates the effort to port the implementation. In particular, the network of changes records will need to be processed in a very particular (and difficult to compute order) to avoid violating any RI constraints. Just looking for a way to avoid having to do this.
LBushkin
+1  A: 

It sounds like the problem you have is that SQL does not support what Date and Darwen call 'multiple assignment'. Standard SQL's response to this was 'deferrable constraints', which SQL Server does not support. A SQL Server FK or CHECK constraint can be flagged with NOCHECK but its not quite the same. For more details see MSDN: ALTER TABLE (Transact-SQL).

onedaywhen
+1  A: 

OT: There are IMHO quite a few things SQL Server does not support, but would make sense in an enterprise environment:

  • Deferrable constraints as mentioned here
  • MARS: Just why do you need to set an option for something entirely natural?
  • CASCADE DELETE constraints: SQL Server does only allow one single cascadation path for a given CASCADE DELETE constraint. Again, I don't see a reason why it shouldn't be allowed to cascade on deletion through multiple possible paths: In the end, at the time it really is executed, there will always be only one path being actually used, so why is this restriction?
  • Prevention of parallel transactions on a single ADO.NET connection.
  • Forcing of every command executed on a connection that has a transaction to be executed within this transaction.
  • When creating a UNIQUE index, NULL is treated as if it was an actual value, and allowed to appear only once in the index. SQL's notion of NULL as an "unknown value" would, however, indicate, that NULL values be ignored altogether when creating the index...

All these little things make many of the referential integrity and transactional features you would expect from a full-sized RDBMS nearly useless in SQL Server. For example, since deferrable constraints are not supported, the notion of a "transaction" as an externally consistent Unit Of Work is partly negated, the only viable solution - except fro some dirty workarounds - being to not define referential integrity constraints at all. I would expect, the natural behavior of a transaction be that you can work inside it in the way and order of operations you like, and the system will make sure it is consistent at the time you commit it. Similar problems arise from the restriction, that a referential integrity constraint with ON DELETE CASCADE may only be defined in a way that only one single constraint can lead to the cascaded deletion of an object. This really doesn't fit most real-world scenarios.

Mirko Klemm