Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It would be interesting to know which version of SQL Server you are using, because in SQL Server 2005 and 2008, the query plan shows all deletes against the clustered index are completed before the referential integrity checks are done. Constraint checking <em>is</em> deferred!</p> <p>The groupId values deleted are captured by an Eager Spool, after the delete operator. A sequence operator at the root of the plan then plays the second branch, which reads all rows from the spool and checks for other rows that reference the deleted rows using a semi-join. The last operation in the plan is an Assert, which will raise an error if referential integrity has been compromised.</p> <p>Reproduction script, using the data shown in the MPTT article referenced in the original question:</p> <pre><code> USE tempdb ; IF OBJECT_ID(N'dbo.Test', N'U') IS NOT NULL DROP TABLE dbo.Test ; CREATE TABLE dbo.Test ( groupId UNIQUEIDENTIFIER NOT NULL PRIMARY KEY NONCLUSTERED, parentId UNIQUEIDENTIFIER REFERENCES dbo.Test(groupId) NULL, name NVARCHAR(50) NOT NULL, lft INTEGER NOT NULL, rgt INTEGER NOT NULL, ) ; CREATE NONCLUSTERED INDEX nc1 ON dbo.Test (parentId) ; INSERT dbo.Test (groupId, parentId, name, lft, rgt) VALUES ({guid '00000000-0000-0000-0000-000000000000'}, NULL, N'Food', 1, 18), ({guid '99163693-B535-47A1-9F5B-E5F235CEB8E2'}, {guid '00000000-0000-0000-0000-000000000000'}, N'Fruit', 2, 11), ({guid '6A3FFED1-0230-4909-A7D4-DD9CBA674A1D'}, {guid '99163693-B535-47A1-9F5B-E5F235CEB8E2'}, N'Red', 3, 6), ({guid '2E7A25D8-8555-4FEC-A0D3-8B8E60501F6A'}, {guid '6A3FFED1-0230-4909-A7D4-DD9CBA674A1D'}, N'Cherry', 4, 5), ({guid '20613684-1081-4A42-AEAE-F21250D56703'}, {guid '99163693-B535-47A1-9F5B-E5F235CEB8E2'}, N'Yellow', 7, 10), ({guid 'B6D5A4AB-1A10-4069-842D-AAE09E73B3CD'}, {guid '20613684-1081-4A42-AEAE-F21250D56703'}, N'Banana', 8, 9), ({guid '174FF0C3-BAE0-4023-8819-EC89E080834C'}, {guid '00000000-0000-0000-0000-000000000000'}, N'Meat', 12, 17), ({guid '3CA5356E-343B-461C-8602-B9ECAEA1304D'}, {guid '174FF0C3-BAE0-4023-8819-EC89E080834C'}, N'Beef', 13, 14), ({guid '79664130-ACDF-48CE-A33D-7473153819E8'}, {guid '174FF0C3-BAE0-4023-8819-EC89E080834C'}, N'Pork', 15, 16) ; DELETE dbo.Test WHERE groupId > {guid '00000000-0000-0000-0000-000000000000'} ; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload