foreign_key.out

来自「postgresql8.3.4源码,开源数据库」· OUT 代码 · 共 1,300 行 · 第 1/4 页

OUT
1,300
字号
ERROR:  update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"DETAIL:  Key (ptest1)=(2) is still referenced from table "fktable".-- Update a row from PK TABLE (should succeed)UPDATE PKTABLE SET ptest1=0 WHERE ptest1=4;-- Check PKTABLE for updatesSELECT * FROM PKTABLE; ptest1 | ptest2 --------+--------      1 | Test1      2 | Test2      3 | Test3      0 | Test4(4 rows)DROP TABLE FKTABLE;DROP TABLE PKTABLE;-- MATCH unspecified-- Base test restricting update/deleteCREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"CREATE TABLE FKTABLE ( ftest1 int, ftest2 int, ftest3 int, ftest4 int,  CONSTRAINT constrname3			FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE);-- Insert Primary Key valuesINSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');-- Insert Foreign Key valuesINSERT INTO FKTABLE VALUES (1, 2, 3, 1); INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);-- Insert a failed valuesINSERT INTO FKTABLE VALUES (1, 2, 7, 6);ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname3"DETAIL:  Key (ftest1,ftest2,ftest3)=(1,2,7) is not present in table "pktable".-- Show FKTABLESELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+--------      1 |      2 |      3 |      1        |      2 |      3 |      2      2 |        |      3 |      3        |      2 |      7 |      4        |      3 |      4 |      5(5 rows)-- Try to update something that should failUPDATE PKTABLE set ptest2=5 where ptest2=2;ERROR:  update or delete on table "pktable" violates foreign key constraint "constrname3" on table "fktable"DETAIL:  Key (ptest1,ptest2,ptest3)=(1,2,3) is still referenced from table "fktable".-- Try to update something that should succeedUPDATE PKTABLE set ptest1=1 WHERE ptest2=3;-- Try to delete something that should failDELETE FROM PKTABLE where ptest1=1 and ptest2=2 and ptest3=3;ERROR:  update or delete on table "pktable" violates foreign key constraint "constrname3" on table "fktable"DETAIL:  Key (ptest1,ptest2,ptest3)=(1,2,3) is still referenced from table "fktable".-- Try to delete something that should workDELETE FROM PKTABLE where ptest1=2;-- Show PKTABLE and FKTABLESELECT * from PKTABLE; ptest1 | ptest2 | ptest3 | ptest4 --------+--------+--------+--------      1 |      2 |      3 | test1      1 |      3 |      3 | test2      1 |      3 |      4 | test3(3 rows)SELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+--------      1 |      2 |      3 |      1        |      2 |      3 |      2      2 |        |      3 |      3        |      2 |      7 |      4        |      3 |      4 |      5(5 rows)DROP TABLE FKTABLE;DROP TABLE PKTABLE;-- cascade update/deleteCREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"CREATE TABLE FKTABLE ( ftest1 int, ftest2 int, ftest3 int, ftest4 int,  CONSTRAINT constrname3			FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE			ON DELETE CASCADE ON UPDATE CASCADE);-- Insert Primary Key valuesINSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');-- Insert Foreign Key valuesINSERT INTO FKTABLE VALUES (1, 2, 3, 1); INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);-- Insert a failed valuesINSERT INTO FKTABLE VALUES (1, 2, 7, 6);ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname3"DETAIL:  Key (ftest1,ftest2,ftest3)=(1,2,7) is not present in table "pktable".-- Show FKTABLESELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+--------      1 |      2 |      3 |      1        |      2 |      3 |      2      2 |        |      3 |      3        |      2 |      7 |      4        |      3 |      4 |      5(5 rows)-- Try to update something that will cascadeUPDATE PKTABLE set ptest2=5 where ptest2=2;-- Try to update something that should not cascadeUPDATE PKTABLE set ptest1=1 WHERE ptest2=3;-- Show PKTABLE and FKTABLESELECT * from PKTABLE; ptest1 | ptest2 | ptest3 | ptest4 --------+--------+--------+--------      2 |      4 |      5 | test4      1 |      5 |      3 | test1      1 |      3 |      3 | test2      1 |      3 |      4 | test3(4 rows)SELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+--------        |      2 |      3 |      2      2 |        |      3 |      3        |      2 |      7 |      4        |      3 |      4 |      5      1 |      5 |      3 |      1(5 rows)-- Try to delete something that should cascadeDELETE FROM PKTABLE where ptest1=1 and ptest2=5 and ptest3=3;-- Show PKTABLE and FKTABLESELECT * from PKTABLE; ptest1 | ptest2 | ptest3 | ptest4 --------+--------+--------+--------      2 |      4 |      5 | test4      1 |      3 |      3 | test2      1 |      3 |      4 | test3(3 rows)SELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+--------        |      2 |      3 |      2      2 |        |      3 |      3        |      2 |      7 |      4        |      3 |      4 |      5(4 rows)-- Try to delete something that should not have a cascadeDELETE FROM PKTABLE where ptest1=2;-- Show PKTABLE and FKTABLESELECT * from PKTABLE; ptest1 | ptest2 | ptest3 | ptest4 --------+--------+--------+--------      1 |      3 |      3 | test2      1 |      3 |      4 | test3(2 rows)SELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+--------        |      2 |      3 |      2      2 |        |      3 |      3        |      2 |      7 |      4        |      3 |      4 |      5(4 rows)DROP TABLE FKTABLE;DROP TABLE PKTABLE;-- set null update / set default deleteCREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"CREATE TABLE FKTABLE ( ftest1 int DEFAULT 0, ftest2 int, ftest3 int, ftest4 int,  CONSTRAINT constrname3			FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE			ON DELETE SET DEFAULT ON UPDATE SET NULL);-- Insert Primary Key valuesINSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');-- Insert Foreign Key valuesINSERT INTO FKTABLE VALUES (1, 2, 3, 1); INSERT INTO FKTABLE VALUES (2, 3, 4, 1); INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);-- Insert a failed valuesINSERT INTO FKTABLE VALUES (1, 2, 7, 6);ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname3"DETAIL:  Key (ftest1,ftest2,ftest3)=(1,2,7) is not present in table "pktable".-- Show FKTABLESELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+--------      1 |      2 |      3 |      1      2 |      3 |      4 |      1        |      2 |      3 |      2      2 |        |      3 |      3        |      2 |      7 |      4        |      3 |      4 |      5(6 rows)-- Try to update something that will set nullUPDATE PKTABLE set ptest2=5 where ptest2=2;-- Try to update something that should not set nullUPDATE 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 |      4 |      5 | test4      1 |      5 |      3 | test1      1 |      2 |      3 | test2(4 rows)SELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+--------      2 |      3 |      4 |      1        |      2 |      3 |      2      2 |        |      3 |      3        |      2 |      7 |      4        |      3 |      4 |      5      1 |        |      3 |      1(6 rows)-- Try to delete something that should set defaultDELETE FROM PKTABLE where ptest1=2 and ptest2=3 and ptest3=4;-- Show PKTABLE and FKTABLESELECT * from PKTABLE; ptest1 | ptest2 | ptest3 | ptest4 --------+--------+--------+--------      2 |      4 |      5 | test4      1 |      5 |      3 | test1      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      1 |        |      3 |      1      0 |        |        |      1(6 rows)-- Try to delete something that should not set defaultDELETE FROM PKTABLE where ptest2=5;-- Show PKTABLE and FKTABLESELECT * from PKTABLE; ptest1 | ptest2 | ptest3 | ptest4 --------+--------+--------+--------      2 |      4 |      5 | test4      1 |      2 |      3 | test2(2 rows)SELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+--------        |      2 |      3 |      2      2 |        |      3 |      3        |      2 |      7 |      4        |      3 |      4 |      5      1 |        |      3 |      1      0 |        |        |      1(6 rows)DROP TABLE FKTABLE;DROP TABLE PKTABLE;-- set default update / set null deleteCREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"CREATE TABLE FKTABLE ( ftest1 int DEFAULT 0, ftest2 int DEFAULT -1, ftest3 int, ftest4 int,  CONSTRAINT constrname3			FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE			ON DELETE SET NULL ON UPDATE SET DEFAULT);-- Insert Primary Key valuesINSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');INSERT INTO PKTABLE VALUES (2, -1, 5, 'test5');-- Insert Foreign Key valuesINSERT INTO FKTABLE VALUES (1, 2, 3, 1); INSERT INTO FKTABLE VALUES (2, 3, 4, 1); INSERT INTO FKTABLE VALUES (2, 4, 5, 1);INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);-- Insert a failed valuesINSERT INTO FKTABLE VALUES (1, 2, 7, 6);ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname3"DETAIL:  Key (ftest1,ftest2,ftest3)=(1,2,7) is not present in table "pktable".-- Show FKTABLESELECT * from FKTABLE; ftest1 | ftest2 | ftest3 | ftest4 --------+--------+--------+--------      1 |      2 |      3 |      1      2 |      3 |      4 |      1      2 |      4 |      5 |      1        |      2 |      3 |      2      2 |        |      3 |      3        |      2 |      7 |      4        |      3 |      4 |      5(7 rows)-- Try to update something that will failUPDATE PKTABLE set ptest2=5 where ptest2=2;ERROR:  insert or update on table "fktable" violates foreign key constraint "constrname3"DETAIL:  Key (ftest1,ftest2,ftest3)=(1,-1,3) is not present in table "pktable".-- Try to update something that will set defaultUPDATE PKTABLE set ptest1=0, ptest2=5, ptest3=10 where ptest2=2;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?