Deleting parent records with children

As mentioned earlier, you cannot create constraints by using automatically created columns in node and edge tables, and thus it is not possible to create foreign keys between these tables by using these columns. This means that you can delete a node, even if it is referenced in an edge table. For instance, the following code should work: the entry from the dbo.TwitterUser table with the UserId = 5 (@WienerSportklub) should be removed from the table, and two identical SELECT statements should return two different result sets, as shown in the following code:

BEGIN TRAN
SELECT t2.UserName
FROM dbo.TwitterUser t1, dbo.TwitterUser t2, dbo.Follows
WHERE MATCH (t1-(Follows)->t2) AND t1.UserName = '@MilosSQL';

DELETE dbo.TwitterUser WHERE UserId = 5;

SELECT t2.UserName
FROM dbo.TwitterUser t1, dbo.TwitterUser t2, dbo.Follows
WHERE MATCH (t1-(Follows)->t2) AND t1.UserName = '@MilosSQL';
ROLLBACK

When you execute it, however, you will see the following error message:

Msg 547, Level 16, State 0, Line 250
The DELETE statement conflicted with the REFERENCE constraint "FK_UserFollows_TwitterUser1". The conflict occurred in database "ASQLGraph", table "dbo.UserFollows", column 'UserId'.
The statement has been terminated.

The entry in the node table is protected by the junction table that you created at the beginning of the section in order to compare graph and normal tables. However, when you remove the foreign key, the code will work, as shown in the following code:

BEGIN TRAN
SELECT t2.UserName
FROM dbo.TwitterUser t1, dbo.TwitterUser t2, dbo.Follows
WHERE MATCH (t1-(Follows)->t2) AND t1.UserName = '@MilosSQL';

ALTER TABLE dbo.UserFollows DROP CONSTRAINT FK_UserFollows_TwitterUser1;
ALTER TABLE dbo.UserFollows DROP CONSTRAINT FK_UserFollows_TwitterUser2;

DELETE dbo.TwitterUser WHERE UserId = 5;

SELECT t2.UserName
FROM dbo.TwitterUser t1, dbo.TwitterUser t2, dbo.Follows
WHERE MATCH (t1-(Follows)->t2) AND t1.UserName = '@MilosSQL';
ROLLBACK

Now, the code is executed, and here is the output:

UserName
------------------
@DejanSarka
@sql_williamd
@tomaz_tsql
@WienerSportklub

UserName
------------------
@DejanSarka
@sql_williamd
@tomaz_tsql
NULL

You can see NULL in the second result set, since the entry is removed from the node table, but still exists in the edge table. To prevent this serious integrity issue, you will use a trigger, as shown in the following code:

CREATE TRIGGER dbo.TG4 ON dbo.TwitterUser
FOR DELETE
AS
BEGIN
IF (
EXISTS(SELECT 1 FROM deleted INNER JOIN dbo.Follows f ON f.$from_id = deleted.$node_id)
OR
EXISTS(SELECT 1 FROM deleted INNER JOIN dbo.Follows f ON f.$to_id = deleted.$node_id)
)
BEGIN
RAISERROR('Node cannot be deleted if it is referenced in an edge table',16,1);
ROLLBACK TRAN;
END
END

Now, execute the previous code:

BEGIN TRAN
SELECT t2.UserName
FROM dbo.TwitterUser t1, dbo.TwitterUser t2, dbo.Follows
WHERE MATCH (t1-(Follows)->t2) AND t1.UserName = '@MilosSQL';

ALTER TABLE dbo.UserFollows DROP CONSTRAINT FK_UserFollows_TwitterUser1;
ALTER TABLE dbo.UserFollows DROP CONSTRAINT FK_UserFollows_TwitterUser2;

DELETE dbo.TwitterUser WHERE UserId = 5;

SELECT t2.UserName
FROM dbo.TwitterUser t1, dbo.TwitterUser t2, dbo.Follows
WHERE MATCH (t1-(Follows)->t2) AND t1.UserName = '@MilosSQL';
ROLLBACK

The entry in the node table is now protected, as shown in the following code:

Msg 50000, Level 16, State 1, Procedure TG4, Line 11 [Batch Start Line 264]
Node cannot be deleted if it is referenced in an edge table
Msg 3609, Level 16, State 1, Line 271
The transaction ended in the trigger. The batch has been aborted.

Again, you had to create a trigger to protect data integrity. In relation tables, you can implement data integrity very easily and intuitively by using constraints and foreign keys. Sometimes, I get the impression that people who blame relational databases and are constantly look for speed often forget about the benefits of data integrity in the relational world!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.57.126