| 1 |
create table emp ( |
| 2 |
id int primary key auto_increment, |
| 3 |
name varchar(256) not null, |
| 4 |
manager int, |
| 5 |
index(manager)) engine = ndb; |
| 6 |
alter table emp add constraint fkname foreign key (manager) references emp(id) on update restrict on delete cascade; |
| 7 |
insert into emp values |
| 8 |
(1, 'larry', null) |
| 9 |
,(2, 'eddie', 1) |
| 10 |
,(3, 'tomas', 2) |
| 11 |
,(4, 'ricky', 2) |
| 12 |
,(5, 'edwin', 2) |
| 13 |
,(6, 'bockie', 3) |
| 14 |
,(7, 'jonas', 6) |
| 15 |
,(8, 'magnus', 6) |
| 16 |
,(9, 'frasse', 6) |
| 17 |
,(10, 'samuel', 7) |
| 18 |
,(11, 'jonatan', 7) |
| 19 |
,(12, 'wille', 8); |
| 20 |
select * from emp order by 1; |
| 21 |
id name manager |
| 22 |
1 larry NULL |
| 23 |
2 eddie 1 |
| 24 |
3 tomas 2 |
| 25 |
4 ricky 2 |
| 26 |
5 edwin 2 |
| 27 |
6 bockie 3 |
| 28 |
7 jonas 6 |
| 29 |
8 magnus 6 |
| 30 |
9 frasse 6 |
| 31 |
10 samuel 7 |
| 32 |
11 jonatan 7 |
| 33 |
12 wille 8 |
| 34 |
begin; |
| 35 |
delete from emp where id=6; |
| 36 |
select * from emp order by 1; |
| 37 |
id name manager |
| 38 |
1 larry NULL |
| 39 |
2 eddie 1 |
| 40 |
3 tomas 2 |
| 41 |
4 ricky 2 |
| 42 |
5 edwin 2 |
| 43 |
rollback; |
| 44 |
select * from emp order by 1; |
| 45 |
id name manager |
| 46 |
1 larry NULL |
| 47 |
2 eddie 1 |
| 48 |
3 tomas 2 |
| 49 |
4 ricky 2 |
| 50 |
5 edwin 2 |
| 51 |
6 bockie 3 |
| 52 |
7 jonas 6 |
| 53 |
8 magnus 6 |
| 54 |
9 frasse 6 |
| 55 |
10 samuel 7 |
| 56 |
11 jonatan 7 |
| 57 |
12 wille 8 |
| 58 |
alter table emp drop foreign key fkname; |
| 59 |
drop table emp; |
| 60 |
create table tree ( |
| 61 |
id int primary key, |
| 62 |
value varchar(256) not null, |
| 63 |
parent int null, |
| 64 |
left_child int null, |
| 65 |
right_child int null, |
| 66 |
index (parent), |
| 67 |
unique (left_child), |
| 68 |
unique (right_child)) engine = ndb; |
| 69 |
insert into tree values (0, '3-root', null, 1,2); |
| 70 |
insert into tree values (1, '1-inner', 0, 3,4); |
| 71 |
insert into tree values (2, '5-inner', 0, 5,6); |
| 72 |
insert into tree values (3, '0-leaf', 1, null, null); |
| 73 |
insert into tree values (4, '2-leaf', 1, null, null); |
| 74 |
insert into tree values (5, '4-leaf', 2, null, null); |
| 75 |
insert into tree values (6, '6-leaf', 2, null, null); |
| 76 |
select * from tree order by 1; |
| 77 |
id value parent left_child right_child |
| 78 |
0 3-root NULL 1 2 |
| 79 |
1 1-inner 0 3 4 |
| 80 |
2 5-inner 0 5 6 |
| 81 |
3 0-leaf 1 NULL NULL |
| 82 |
4 2-leaf 1 NULL NULL |
| 83 |
5 4-leaf 2 NULL NULL |
| 84 |
6 6-leaf 2 NULL NULL |
| 85 |
alter table tree add constraint fk_parent foreign key (parent) references tree(id) on update restrict on delete cascade; |
| 86 |
alter table tree add constraint fk_left foreign key (left_child) references tree(id) on update restrict on delete cascade; |
| 87 |
begin; |
| 88 |
delete from tree where value = '5-inner'; |
| 89 |
select * from tree order by 1; |
| 90 |
id value parent left_child right_child |
| 91 |
0 3-root NULL 1 2 |
| 92 |
1 1-inner 0 3 4 |
| 93 |
3 0-leaf 1 NULL NULL |
| 94 |
4 2-leaf 1 NULL NULL |
| 95 |
rollback; |
| 96 |
begin; |
| 97 |
delete from tree where value = '4-leaf'; |
| 98 |
select * from tree order by 1; |
| 99 |
id value parent left_child right_child |
| 100 |
0 3-root NULL 1 2 |
| 101 |
1 1-inner 0 3 4 |
| 102 |
3 0-leaf 1 NULL NULL |
| 103 |
4 2-leaf 1 NULL NULL |
| 104 |
rollback; |
| 105 |
begin; |
| 106 |
delete from tree where value = '3-root'; |
| 107 |
select * from tree order by 1; |
| 108 |
id value parent left_child right_child |
| 109 |
commit; |
| 110 |
drop table tree; |