foreign_key.out
来自「postgresql8.3.4源码,开源数据库」· OUT 代码 · 共 1,300 行 · 第 1/4 页
OUT
1,300 行
UPDATE PKTABLE set ptest2=10 where ptest2=4;-- Try to update something that should not set defaultUPDATE PKTABLE set ptest2=2 WHERE ptest2=3 and ptest1=1;-- Show PKTABLE and FKTABLESELECT * from PKTABLE; ptest1 | ptest2 | ptest3 | ptest4 --------+--------+--------+-------- 2 | 3 | 4 | test3 2 | -1 | 5 | test5 0 | 5 | 10 | test1 2 | 10 | 5 | test4 1 | 2 | 3 | test2(5 rows)SELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+-------- 2 | 3 | 4 | 1 | 2 | 3 | 2 2 | | 3 | 3 | 2 | 7 | 4 | 3 | 4 | 5 0 | -1 | | 1 2 | -1 | 5 | 1(7 rows)-- Try to delete something that should set nullDELETE FROM PKTABLE where ptest1=2 and ptest2=3 and ptest3=4;-- Show PKTABLE and FKTABLESELECT * from PKTABLE; ptest1 | ptest2 | ptest3 | ptest4 --------+--------+--------+-------- 2 | -1 | 5 | test5 0 | 5 | 10 | test1 2 | 10 | 5 | test4 1 | 2 | 3 | test2(4 rows)SELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+-------- | 2 | 3 | 2 2 | | 3 | 3 | 2 | 7 | 4 | 3 | 4 | 5 0 | -1 | | 1 2 | -1 | 5 | 1 | | | 1(7 rows)-- Try to delete something that should not set nullDELETE FROM PKTABLE where ptest2=5;-- Show PKTABLE and FKTABLESELECT * from PKTABLE; ptest1 | ptest2 | ptest3 | ptest4 --------+--------+--------+-------- 2 | -1 | 5 | test5 2 | 10 | 5 | test4 1 | 2 | 3 | test2(3 rows)SELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+-------- | 2 | 3 | 2 2 | | 3 | 3 | 2 | 7 | 4 | 3 | 4 | 5 0 | -1 | | 1 2 | -1 | 5 | 1 | | | 1(7 rows)DROP TABLE FKTABLE;DROP TABLE PKTABLE;CREATE TABLE PKTABLE (ptest1 int PRIMARY KEY);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"CREATE TABLE FKTABLE_FAIL1 ( ftest1 int, CONSTRAINT fkfail1 FOREIGN KEY (ftest2) REFERENCES PKTABLE);ERROR: column "ftest2" referenced in foreign key constraint does not existCREATE TABLE FKTABLE_FAIL2 ( ftest1 int, CONSTRAINT fkfail1 FOREIGN KEY (ftest1) REFERENCES PKTABLE(ptest2));ERROR: column "ptest2" referenced in foreign key constraint does not existDROP TABLE FKTABLE_FAIL1;ERROR: table "fktable_fail1" does not existDROP TABLE FKTABLE_FAIL2;ERROR: table "fktable_fail2" does not existDROP TABLE PKTABLE;-- Test for referencing column number smaller than referenced constraintCREATE TABLE PKTABLE (ptest1 int, ptest2 int, UNIQUE(ptest1, ptest2));NOTICE: CREATE TABLE / UNIQUE will create implicit index "pktable_ptest1_key" for table "pktable"CREATE TABLE FKTABLE_FAIL1 (ftest1 int REFERENCES pktable(ptest1));ERROR: there is no unique constraint matching given keys for referenced table "pktable"DROP TABLE FKTABLE_FAIL1;ERROR: table "fktable_fail1" does not existDROP TABLE PKTABLE;---- Tests for mismatched types---- Basic one column, two table setup CREATE TABLE PKTABLE (ptest1 int PRIMARY KEY);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"INSERT INTO PKTABLE VALUES(42);-- This next should fail, because int=inet does not existCREATE TABLE FKTABLE (ftest1 inet REFERENCES pktable);ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implementedDETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.-- This should also fail for the same reason, but here we-- give the column nameCREATE TABLE FKTABLE (ftest1 inet REFERENCES pktable(ptest1));ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implementedDETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.-- This should succeed, even though they are different types,-- because int=int8 exists and is a member of the integer opfamilyCREATE TABLE FKTABLE (ftest1 int8 REFERENCES pktable);-- Check it actually worksINSERT INTO FKTABLE VALUES(42); -- should succeedINSERT INTO FKTABLE VALUES(43); -- should failERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"DETAIL: Key (ftest1)=(43) is not present in table "pktable".UPDATE FKTABLE SET ftest1 = ftest1; -- should succeedUPDATE FKTABLE SET ftest1 = ftest1 + 1; -- should failERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"DETAIL: Key (ftest1)=(43) is not present in table "pktable".DROP TABLE FKTABLE;-- This should fail, because we'd have to cast numeric to int which is-- not an implicit coercion (or use numeric=numeric, but that's not part-- of the integer opfamily)CREATE TABLE FKTABLE (ftest1 numeric REFERENCES pktable);ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implementedDETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: numeric and integer.DROP TABLE PKTABLE;-- On the other hand, this should work because int implicitly promotes to-- numeric, and we allow promotion on the FK sideCREATE TABLE PKTABLE (ptest1 numeric PRIMARY KEY);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"INSERT INTO PKTABLE VALUES(42);CREATE TABLE FKTABLE (ftest1 int REFERENCES pktable);-- Check it actually worksINSERT INTO FKTABLE VALUES(42); -- should succeedINSERT INTO FKTABLE VALUES(43); -- should failERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"DETAIL: Key (ftest1)=(43) is not present in table "pktable".UPDATE FKTABLE SET ftest1 = ftest1; -- should succeedUPDATE FKTABLE SET ftest1 = ftest1 + 1; -- should failERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"DETAIL: Key (ftest1)=(43) is not present in table "pktable".DROP TABLE FKTABLE;DROP TABLE PKTABLE;-- Two columns, two tablesCREATE TABLE PKTABLE (ptest1 int, ptest2 inet, PRIMARY KEY(ptest1, ptest2));NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"-- This should fail, because we just chose really odd typesCREATE TABLE FKTABLE (ftest1 cidr, ftest2 timestamp, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable);ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implementedDETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.-- Again, so should this...CREATE TABLE FKTABLE (ftest1 cidr, ftest2 timestamp, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable(ptest1, ptest2));ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implementedDETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.-- This fails because we mixed up the column orderingCREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable);ERROR: foreign key constraint "fktable_ftest2_fkey" cannot be implementedDETAIL: Key columns "ftest2" and "ptest1" are of incompatible types: inet and integer.-- As does this...CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable(ptest1, ptest2));ERROR: foreign key constraint "fktable_ftest2_fkey" cannot be implementedDETAIL: Key columns "ftest2" and "ptest1" are of incompatible types: inet and integer.-- And again..CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable(ptest2, ptest1));ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implementedDETAIL: Key columns "ftest1" and "ptest2" are of incompatible types: integer and inet.-- This works...CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable(ptest2, ptest1));DROP TABLE FKTABLE;-- As does thisCREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable(ptest1, ptest2));DROP TABLE FKTABLE;DROP TABLE PKTABLE;-- Two columns, same table-- Make sure this still works...CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest3,ptest4) REFERENCES pktable(ptest1, ptest2));NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"DROP TABLE PKTABLE;-- And this, CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest3,ptest4) REFERENCES pktable);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"DROP TABLE PKTABLE;-- This shouldn't (mixed up columns)CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest3,ptest4) REFERENCES pktable(ptest2, ptest1));NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"ERROR: foreign key constraint "pktable_ptest3_fkey" cannot be implementedDETAIL: Key columns "ptest3" and "ptest2" are of incompatible types: integer and inet.-- Nor should this... (same reason, we have 4,3 referencing 1,2 which mismatches typesCREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest4,ptest3) REFERENCES pktable(ptest1, ptest2));NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"ERROR: foreign key constraint "pktable_ptest4_fkey" cannot be implementedDETAIL: Key columns "ptest4" and "ptest1" are of incompatible types: inet and integer.-- Not this one either... Same as the last one except we didn't defined the columns being referenced.CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest4,ptest3) REFERENCES pktable);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"ERROR: foreign key constraint "pktable_ptest4_fkey" cannot be implementedDETAIL: Key columns "ptest4" and "ptest1" are of incompatible types: inet and integer.---- Now some cases with inheritance-- Basic 2 table case: 1 column of matching types.create table pktable_base (base1 int not null);create table pktable (ptest1 int, primary key(base1), unique(base1, ptest1)) inherits (pktable_base);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"NOTICE: CREATE TABLE / UNIQUE will create implicit index "pktable_base1_key" for table "pktable"create table fktable (ftest1 int references pktable(base1));-- now some ins, upd, delinsert into pktable(base1) values (1);insert into pktable(base1) values (2);-- let's insert a non-existant fktable valueinsert into fktable(ftest1) values (3);ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"DETAIL: Key (ftest1)=(3) is not present in table "pktable".-- let's make a valid row for thatinsert into pktable(base1) values (3);insert into fktable(ftest1) values (3);-- let's try removing a row that should fail from pktabledelete from pktable where base1>2;ERROR: update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"DETAIL: Key (base1)=(3) is still referenced from table "fktable".-- okay, let's try updating all of the base1 values to *4-- which should fail.update pktable set base1=base1*4;ERROR: update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"DETAIL: Key (base1)=(3) is still referenced from table "fktable".-- okay, let's try an update that should work.update pktable set base1=base1*4 where base1<3;-- and a delete that should workdelete from pktable where base1>3;-- cleanupdrop table fktable;delete from pktable;-- Now 2 columns 2 tables, matching typescreate table fktable (ftest1 int, ftest2 int, foreign key(ftest1, ftest2) references pktable(base1, ptest1));-- now some ins, upd, delinsert into pktable(base1, ptest1) values (1, 1);insert into pktable(base1, ptest1) values (2, 2);-- let's insert a non-existant fktable valueinsert into fktable(ftest1, ftest2) values (3, 1);ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"DETAIL: Key (ftest1,ftest2)=(3,1) is not present in table "pktable".-- let's make a valid row for thatinsert into pktable(base1,ptest1) values (3, 1);insert into fktable(ftest1, ftest2) values (3, 1);-- let's try removing a row that should fail from pktabledelete from pktable where base1>2;ERROR: update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"DETAIL: Key (base1,ptest1)=(3,1) is still referenced from table "fktable".-- okay, let's try updating all of the base1 values to *4-- which should fail.update pktable set base1=base1*4;ERROR: update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"DETAIL: Key (base1,ptest1)=(3,1) is still referenced from table "fktable".-- okay, let's try an update that should work.update pktable set base1=base1*4 where base1<3;-- and a delete that should workdelete from pktable where base1>3;-- cleanupdrop table fktable;drop table pktable;drop table pktable_base;-- Now we'll do one all in 1 table with 2 columns of matching typescreate table pktable_base(base1 int not null, base2 int);create table pktable(ptest1 int, ptest2 int, primary key(base1, ptest1), foreign key(base2, ptest2) references pktable(base1, ptest1)) inherits (pktable_base);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"insert into pktable (base1, ptest1, base2, ptest2) values (1, 1, 1, 1);insert into pktable (base1, ptest1, base2, ptest2) values (2, 1, 1, 1);insert into pktable (base1, ptest1, base2, ptest2) values (2, 2, 2, 1);insert into pktable (base1, ptest1, base2, ptest2) values (1, 3, 2, 2);-- fails (3,2) isn't in base1, ptest1insert into pktable (base1, ptest1, base2, ptest2) values (2, 3, 3, 2);ERROR: insert or update on table "pktable" violates foreign key constraint "pktable_base2_fkey"DETAIL: Key (base2,ptest2)=(3,2) is not present in table "pktable".-- fails (2,2) is being referenceddelete from pktable where base1=2;ERROR: update or delete on table "pktable" violates foreign key constraint "pktable_base2_fkey" on table "pktable"DETAIL: Key (base1,ptest1)=(2,2) is still referenced from table "pktable".-- fails (1,1) is being referenced (twice)update pktable set base1=3 where base1=1;ERROR: update or delete on table "pktable" violates foreign key constraint "pktable_base2_fkey" on table "pktable"DETAIL: Key (base1,ptest1)=(1,1) is still referenced from table "pktable".-- this sequence of two deletes will work, since after the first there will be no (2,*) referencesdelete from pktable where base2=2;delete from pktable where base1=2;drop table pktable;drop table pktable_base;-- 2 columns (2 tables), mismatched typescreate table pktable_base(base1 int not null);create table pktable(ptest1 inet, primary key(base1, ptest1)) inherits (pktable_base);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"-- just generally bad types (with and without column references on the referenced table)create table fktable(ftest1 cidr, ftest2 int[], foreign key (ftest1, ftest2) references pktable);ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implementedDETAIL: Key columns "ftest1" and "base1" are of incompatible types: cidr and integer.create table fktable(ftest1 cidr, ftest2 int[], foreign key (ftest1, ftest2) references pktable(base1, ptest1));ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implementedDETAIL: Key columns "ftest1" and "base1" are of incompatible types: cidr and integer.-- let's mix up which columns reference whichcreate table fktable(ftest1 int, ftest2 inet, foreign key(ftest2, ftest1) references pktable);ERROR: foreign key constraint "fktable_ftest2_fkey" cannot be implementedDETAIL: Key columns "ftest2" and "base1" are of incompatible types: inet and integer.create table fktable(ftest1 int, ftest2 inet, foreign key(ftest2, ftest1) references pktable(base1, ptest1));ERROR: foreign key constraint "fktable_ftest2_fkey" cannot be implementedDETAIL: Key columns "ftest2" and "base1" are of incompatible types: inet and integer.create table fktable(ftest1 int, ftest2 inet, foreign key(ftest1, ftest2) references pktable(ptest1, base1));ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implementedDETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: integer and inet.drop table pktable;drop table pktable_base;-- 2 columns (1 table), mismatched typescreate table pktable_base(base1 int not null, base2 int);create table pktable(ptest1 inet, ptest2 inet[], primary key(base1, ptest1), foreign key(base2, ptest2) references pktable(base1, ptest1)) inherits (pktable_base);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"ERROR: foreign key constraint "pktable_base2_fkey" cannot be implementedDETAIL: Key columns "ptest2" and "ptest1" are of incompatible types: inet[] and inet.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?