| 1 |
create table purchase_order_v1 ( |
| 2 |
id int not null primary key, |
| 3 |
sequence_no int not null, |
| 4 |
user varchar(30), |
| 5 |
unique index (sequence_no) |
| 6 |
) engine = ndb; |
| 7 |
create table order_item_v1 ( |
| 8 |
id int not null auto_increment primary key, |
| 9 |
order_seq_no int not null, |
| 10 |
description varchar(40), |
| 11 |
quantity int not null, |
| 12 |
unit_cost decimal(6, 2), |
| 13 |
foreign key (order_seq_no) references purchase_order_v1 (sequence_no) |
| 14 |
on delete cascade on update cascade |
| 15 |
) engine = ndb; |
| 16 |
insert into purchase_order_v1 values (1,1,"jdd"); |
| 17 |
insert into order_item_v1 values(default, 1, "10mm hex nut", 1000, .04); |
| 18 |
update purchase_order_v1 set sequence_no = 2 where id = 1; |
| 19 |
select * from order_item_v1 ; |
| 20 |
id order_seq_no description quantity unit_cost |
| 21 |
1 2 10mm hex nut 1000 0.04 |
| 22 |
create table purchase_order_v2 ( |
| 23 |
id int not null primary key, |
| 24 |
sequence_no int as (id) stored unique, |
| 25 |
user varchar(30) |
| 26 |
) engine = ndb; |
| 27 |
create table order_item_v2 ( |
| 28 |
id int not null auto_increment primary key, |
| 29 |
order_seq_no int not null, |
| 30 |
description varchar(40), |
| 31 |
quantity int not null, |
| 32 |
unit_cost decimal(6, 2), |
| 33 |
foreign key (order_seq_no) references purchase_order_v2 (sequence_no) |
| 34 |
on delete cascade on update cascade |
| 35 |
) engine = ndb; |
| 36 |
insert into purchase_order_v2 (id, user) values (1, "jdd"); |
| 37 |
insert into order_item_v2 values(default, 1, "10mm hex nut", 1000, .04); |
| 38 |
select * from purchase_order_v2 ; |
| 39 |
id sequence_no user |
| 40 |
1 1 jdd |
| 41 |
select * from order_item_v2; |
| 42 |
id order_seq_no description quantity unit_cost |
| 43 |
1 1 10mm hex nut 1000 0.04 |
| 44 |
update purchase_order_v2 set id = 2 where id = 1; |
| 45 |
select * from order_item_v2 ; |
| 46 |
id order_seq_no description quantity unit_cost |
| 47 |
create table order_item_v3 ( |
| 48 |
id int not null auto_increment primary key, |
| 49 |
order_seq_no int not null, |
| 50 |
description varchar(40), |
| 51 |
quantity int not null, |
| 52 |
unit_cost decimal(6, 2), |
| 53 |
gcol int as (100 + order_seq_no) stored, |
| 54 |
foreign key (order_seq_no) references purchase_order_v1 (sequence_no) |
| 55 |
on delete cascade on update cascade |
| 56 |
) engine = ndb; |
| 57 |
insert into order_item_v3 values(default, 2, "10mm hex nut", 1000, .04, default); |
| 58 |
select * from order_item_v3; |
| 59 |
id order_seq_no description quantity unit_cost gcol |
| 60 |
1 2 10mm hex nut 1000 0.04 102 |
| 61 |
update purchase_order_v1 set sequence_no = 3 where id = 1; |
| 62 |
select * from order_item_v3; |
| 63 |
id order_seq_no description quantity unit_cost gcol |
| 64 |
1 3 10mm hex nut 1000 0.04 102 |
| 65 |
drop table order_item_v3; |
| 66 |
drop table order_item_v2; |
| 67 |
drop table order_item_v1; |
| 68 |
drop table purchase_order_v2; |
| 69 |
drop table purchase_order_v1; |