📄 foreign_key.out
字号:
(4 rows)-- Check PKTABLESELECT * FROM PKTABLE; ptest1 | ptest2 --------+-------- 1 | Test1 2 | Test2 3 | Test3 4 | Test4 5 | Test5(5 rows)-- Delete a row from PK TABLE (should fail)DELETE FROM PKTABLE WHERE ptest1=1;ERROR: update or delete on "pktable" violates foreign key constraint "fktable_ftest1_fkey" on "fktable"DETAIL: Key (ptest1)=(1) is still referenced from table "fktable".-- Delete a row from PK TABLE (should succeed)DELETE FROM PKTABLE WHERE ptest1=5;-- Check PKTABLE for deletesSELECT * FROM PKTABLE; ptest1 | ptest2 --------+-------- 1 | Test1 2 | Test2 3 | Test3 4 | Test4(4 rows)-- Update a row from PK TABLE (should fail)UPDATE PKTABLE SET ptest1=0 WHERE ptest1=2;ERROR: update or delete on "pktable" violates foreign key constraint "fktable_ftest1_fkey" on "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 "pktable" violates foreign key constraint "constrname3" on "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 "pktable" violates foreign key constraint "constrname3" on "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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -