📄 fk_nonsps.out
字号:
ij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- now lets drop the foreign keys and try againdrop table f2;0 rows inserted/updated/deletedij> drop table f3;0 rows inserted/updated/deletedij> drop table f4;0 rows inserted/updated/deletedij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> select constraintname, referencecount from sys.sysconstraints c, sys.sysforeignkeys fk where fk.keyconstraintid = c.constraintid order by constraintname;CONSTRAINTNAME |REFERENCEC&--------------------------------------------------------------------------------------------------------------------------------------------PK1 |1 ij> alter table f drop constraint fk;0 rows inserted/updated/deletedij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- okalter table p1 drop constraint pk1;0 rows inserted/updated/deletedij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- we shouldn't be able to add an fk on p1 nowalter table f add constraint fk foreign key (x) references p1;ERROR X0Y41: Constraint 'FK' is invalid because the referenced table APP.P1 has no primary key. Either add a primary key to APP.P1 or explicitly specify the columns of a unique constraint that this foreign key references. ij> -- add the constraint and try againalter table p1 add constraint pk1 primary key (x);0 rows inserted/updated/deletedij> create table f2 (x int, constraint fk2 foreign key (x) references p1(x));0 rows inserted/updated/deletedij> create table f3 (x int, constraint fk3 foreign key (x) references p1(x));0 rows inserted/updated/deletedij> create table f4 (x int, constraint fk4 foreign key (x) references p1(x));0 rows inserted/updated/deletedij> -- drop constraintalter table f4 drop constraint fk4;0 rows inserted/updated/deletedij> alter table f3 drop constraint fk3;0 rows inserted/updated/deletedij> alter table f2 drop constraint fk2;0 rows inserted/updated/deletedij> alter table p1 drop constraint pk1;0 rows inserted/updated/deletedij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- all fks are gone, right?select constraintname from sys.sysconstraints c, sys.sysforeignkeys fk where fk.constraintid = c.constraintid order by constraintname;CONSTRAINTNAME --------------------------------------------------------------------------------------------------------------------------------ij> -- cleanup what we have done so fardrop table p1;0 rows inserted/updated/deletedij> drop table p2;0 rows inserted/updated/deletedij> drop table u1;0 rows inserted/updated/deletedij> drop table u2;0 rows inserted/updated/deletedij> drop table otherschema.p1;0 rows inserted/updated/deletedij> drop schema otherschema restrict;0 rows inserted/updated/deletedij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- will return dependencies for SPS metadata queries now created by default -- database is created.create table default_sysdepends_count(a int);0 rows inserted/updated/deletedij> insert into default_sysdepends_count select count(*) from sys.sysdepends;1 row inserted/updated/deletedij> select * from default_sysdepends_count;A -----------0 ij> -- -- now we are going to do some self referencing-- tests.-- create table selfref (p char(10) not null primary key, f char(10) references selfref);0 rows inserted/updated/deletedij> drop table selfref;0 rows inserted/updated/deletedij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- okcreate table selfref (p char(10) not null, f char(10) references selfref, constraint pk primary key (p));0 rows inserted/updated/deletedij> drop table selfref;0 rows inserted/updated/deletedij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- okcreate table selfref (p char(10) not null, f char(10), constraint f foreign key (f) references selfref(p), constraint pk primary key (p));0 rows inserted/updated/deletedij> -- should failalter table selfref drop constraint pk;ERROR X0Y25: Operation 'DROP CONSTRAINT' cannot be performed on object 'PK' because CONSTRAINT 'F' is dependent on that object.ij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- okalter table selfref drop constraint f;0 rows inserted/updated/deletedij> alter table selfref drop constraint pk;0 rows inserted/updated/deletedij> drop table selfref;0 rows inserted/updated/deletedij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- what if a pk references another pk? should just-- drop the direct references (nothing special, really)create table pr1(x int not null, constraint pkr1 primary key (x));0 rows inserted/updated/deletedij> create table pr2(x int not null, constraint pkr2 primary key(x), constraint fpkr2 foreign key (x) references pr1);0 rows inserted/updated/deletedij> create table pr3(x int not null, constraint pkr3 primary key(x), constraint fpkr3 foreign key (x) references pr2);0 rows inserted/updated/deletedij> select constraintname, referencecount from sys.sysconstraints order by constraintname;CONSTRAINTNAME |REFERENCEC&--------------------------------------------------------------------------------------------------------------------------------------------FPKR2 |0 FPKR3 |0 PK3 |0 PKR1 |1 PKR2 |1 PKR3 |0 ij> -- now drop constraint pkr1alter table pr2 drop constraint fpkr2;0 rows inserted/updated/deletedij> alter table pr1 drop constraint pkr1;0 rows inserted/updated/deletedij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- pkr1 and pfkr2 are goneselect constraintname, referencecount from sys.sysconstraints order by constraintname;CONSTRAINTNAME |REFERENCEC&--------------------------------------------------------------------------------------------------------------------------------------------FPKR3 |0 PK3 |0 PKR2 |1 PKR3 |0 ij> -- cleanupdrop table pr3;0 rows inserted/updated/deletedij> drop table pr2;0 rows inserted/updated/deletedij> drop table pr1;0 rows inserted/updated/deletedij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- should return 0, confirm no unexpected dependencies-- verify that all rows in sys.sysdepends got dropped -- apart from sps dependenciescreate table default_sysdepends_count2(a int);0 rows inserted/updated/deletedij> insert into default_sysdepends_count2 select count(*) from sys.sysdepends;1 row inserted/updated/deletedij> select default_sysdepends_count2.a - default_sysdepends_count.a from default_sysdepends_count2, default_sysdepends_count;1 -----------0 ij> -- dependencies and spsescreate table x (x int not null primary key, y int, constraint xfk foreign key (y) references x);0 rows inserted/updated/deletedij> create table y (x int, constraint yfk foreign key (x) references x);0 rows inserted/updated/deletedij> prepare ss as 'select * from x';ij> prepare si as 'insert into x values (1,1)';ij> prepare su as 'update x set x = x+1, y=y+1';ij> alter table x drop constraint xfk;0 rows inserted/updated/deletedij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> autocommit off;ij> -- drop the referenced fk, should force su to be recompiled-- since it no longer has to check the foreign key tablealter table y drop constraint yfk;0 rows inserted/updated/deletedij> commit;ij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> drop table y;0 rows inserted/updated/deletedij> commit;ij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- okdrop table x;0 rows inserted/updated/deletedij> remove ss;ij> remove si;ij> remove su;ij> drop table f3;0 rows inserted/updated/deletedij> drop table f2;0 rows inserted/updated/deletedij> drop table f;0 rows inserted/updated/deletedij> commit;ij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> -- verify that all rows in sys.sysdepends got dropped -- apart from sps dependencies-- Since, with beetle 5352; we create metadata SPS for network server at database bootup time-- so the dependencies for SPS are there.create table default_sysdepends_count3(a int);0 rows inserted/updated/deletedij> insert into default_sysdepends_count3 select count(*) from sys.sysdepends;1 row inserted/updated/deletedij> select default_sysdepends_count3.a - default_sysdepends_count.a from default_sysdepends_count3, default_sysdepends_count;1 -----------0 ij> -- ** insert fkdml.sqlautocommit on;ij> ---- DML and foreign keys--drop table s;ERROR 42Y55: 'DROP TABLE' cannot be performed on 'S' because it does not exist.ij> drop table f3;ERROR 42Y55: 'DROP TABLE' cannot be performed on 'F3' because it does not exist.ij> drop table f2;ERROR 42Y55: 'DROP TABLE' cannot be performed on 'F2' because it does not exist.ij> drop table f;ERROR 42Y55: 'DROP TABLE' cannot be performed on 'F' because it does not exist.ij> drop table p;ERROR 42Y55: 'DROP TABLE' cannot be performed on 'P' because it does not exist.ij> CALL WAIT_FOR_POST_COMMIT();0 rows inserted/updated/deletedij> create table p (x int not null, y int not null, constraint pk primary key (x,y));0 rows inserted/updated/deletedij> create table f (x int, y int, constraint fk foreign key (x,y) references p);0 rows inserted/updated/deletedij> insert into p values (1,1);1 row inserted/updated/deletedij> -- okinsert into f values (1,1);1 row inserted/updated/deletedij> -- failinsert into f values (2,1);ERROR 23503: INSERT on table 'F' caused a violation of foreign key constraint 'FK' for key (2,1). The statement has been rolled back.ij> insert into f values (1,2);ERROR 23503: INSERT on table 'F' caused a violation of foreign key constraint 'FK' for key (1,2). The statement has been rolled back.ij> -- nulls are okinsert into f values (1,null);1 row inserted/updated/deletedij> insert into f values (null,null);1 row inserted/updated/deletedij> insert into f values (1,null);1 row inserted/updated/deletedij> -- update on pk, failupdate p set x = 2;ERROR 23503: UPDATE on table 'P' caused a violation of foreign key constraint 'FK' for key (1,1). The statement has been rolled back.ij> update p set y = 2;ERROR 23503: UPDATE on table 'P' caused a violation of foreign key constraint 'FK' for key (1,1). The statement has been rolled back.ij> update p set x = 1, y = 2;ERROR 23503: UPDATE on table 'P' caused a violation of foreign key constraint 'FK' for key (1,1). The statement has been rolled back.ij> update p set x = 2, y = 1;ERROR 23503: UPDATE on table 'P' caused a violation of foreign key constraint 'FK' for key (1,1). The statement has been rolled back.ij> update p set x = 2, y = 2;ERROR 23503: UPDATE on table 'P' caused a violation of foreign key constraint 'FK' for key (1,1). The statement has been rolled back.ij> -- okupdate p set x = 1, y = 1;1 row inserted/updated/deletedij> -- delete pk, faildelete from p;ERROR 23503: DELETE on table 'P' caused a violation of foreign key constraint 'FK' for key (1,1). The statement has been rolled back.ij> -- delete fk, okdelete from f;4 rows inserted/updated/deletedij> insert into f values (1,1);1 row inserted/updated/deletedij> -- update fk, failupdate f set x = 2;ERROR 23503: UPDATE on table 'F' caused a violation of foreign key constraint 'FK' for key (2,1). The statement has been rolled back.ij> update f set y = 2;ERROR 23503: UPDATE on table 'F' caused a violation of foreign key constraint 'FK' for key (1,2). The statement has been rolled back.ij> update f set x = 1, y = 2;ERROR 23503: UPDATE on table 'F' caused a violation of foreign key constraint 'FK' for key (1,2). The statement has been rolled back.ij> update f set x = 2, y = 1;ERROR 23503: UPDATE on table 'F' caused a violation of foreign key constraint 'FK' for key (2,1). The statement has been rolled back.ij> -- update fk, okupdate f set x = 1, y = 1;1 row inserted/updated/deletedij> -- nulls okupdate f set x = null, y = 1;1 row inserted/updated/deletedij> update f set x = 1, y = null;1 row inserted/updated/deletedij> update f set x = null, y = null;1 row inserted/updated/deletedij> delete from f;1 row inserted/updated/deletedij> insert into f values (1,1);1 row inserted/updated/deleted
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -