Dataphor SQL RAC (Relational Application Companion)


A site of hope for those looking for a true relational database system

Thursday, January 22, 2009

Multiple cascade paths to the same table

This example is based on the thread:

microsoft.public.sqlserver.programming
Jan 21, 2009
'JET Referential Integrity superior than SQL'
http://tinyurl.com/d58ubl 

Sql Server can't deal with more than 1 cascade to a table. Some nonsense about
cycles. I guess if two paths are involved they call it a bi-cycle  It simply is
a poor system for relationally modeling a business problem. Simply replace it 
with Dataphor and use sql server to store the data. Sql server doesn't screw
up data storage  Here's how simple this example is with Dataphor (and sql server
as the backend).

The tables will be persisted in sql server but Dataphor will (mercifully) perform all
the integrity checks.

create table users
{
id:Integer,
username:String tags{Storage.Length='50'},
key{id},
key{username}
};

create table table2
{
id:Integer,
user1:String tags{Storage.Length='50'}, 
user2:String tags{Storage.Length='50'}, 
user3:String tags{Storage.Length='50'} ,
key{id},
reference user1username {user1} references users {username}
update cascade delete cascade,
reference user2username {user2} references users {username}
update cascade delete cascade,
reference user3username {user3} references users {username}
update cascade delete cascade
};

users:= table{
              row{1 id,'john' username},
              row{2,'steve'},
              row{3,'judy'},
              row{4,'larry'},
              row{5,'bill'},
              row{6,'rita'}};

This row would violate the user3username constraint.
insert row{12 id,'john' user1,'rita' user2, 'stan' user3} into table2;              
//Error: The table users does not have a row with username "stan".

table2:=table{
              row{10 id,'john' user1,'steve' user2, 'judy' user3},
              row{11,'larry','rita', 'bill'},
              row{13,'larry','steve', 'steve'},
              row{14,'judy','steve', 'john'},
              row{15,'bill','judy','rita'},
              row{16,'steve','bill', 'judy'},
              row{17,'john','bill','john'}};
              
select table2;
id user1 user2 user3 
-- ----- ----- ----- 
10 john  steve judy  
11 larry rita  bill  
13 larry steve steve 
14 judy  steve john  
15 bill  judy  rita  
16 steve bill  judy  
17 john  bill  john  

Check the cascading updates. In table table2 all entries
of 'john' are replaced with 'paul'.

update users set {username:='paul'} where username='john';

select table2;
id user1 user2 user3 
-- ----- ----- ----- 
10 paul  steve judy  
11 larry rita  bill  
13 larry steve steve 
14 judy  steve paul  
15 bill  judy  rita  
16 steve bill  judy  
17 paul  bill  paul  

Check the cascading deletes. Any row in table2 with an entry
of 'steve' is deleted.

delete users where username='steve';

select table2;
id user1 user2 user3 
-- ----- ----- ----- 
11 larry rita  bill  
15 bill  judy  rita  
17 paul  bill  paul  

Dataphor is open source, get a copy here:
http://databaseconsultinggroup.com/downloads/

About Me

My photo
Phoenix, Arizona, United States