📄 foreign_key.out
字号:
---- FOREIGN KEY---- MATCH FULL---- First test, check and cascade--CREATE TABLE PKTABLE ( ptest1 int PRIMARY KEY, ptest2 text );NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"CREATE TABLE FKTABLE ( ftest1 int REFERENCES PKTABLE MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, ftest2 int );-- Insert test data into PKTABLEINSERT INTO PKTABLE VALUES (1, 'Test1');INSERT INTO PKTABLE VALUES (2, 'Test2');INSERT INTO PKTABLE VALUES (3, 'Test3');INSERT INTO PKTABLE VALUES (4, 'Test4');INSERT INTO PKTABLE VALUES (5, 'Test5');-- Insert successful rows into FK TABLEINSERT INTO FKTABLE VALUES (1, 2);INSERT INTO FKTABLE VALUES (2, 3);INSERT INTO FKTABLE VALUES (3, 4);INSERT INTO FKTABLE VALUES (NULL, 1);-- Insert a failed row into FK TABLEINSERT INTO FKTABLE VALUES (100, 2);ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"DETAIL: Key (ftest1)=(100) is not present in table "pktable".-- Check FKTABLESELECT * FROM FKTABLE; ftest1 | ftest2 --------+-------- 1 | 2 2 | 3 3 | 4 | 1(4 rows)-- Delete a row from PK TABLEDELETE FROM PKTABLE WHERE ptest1=1;-- Check FKTABLE for removal of matched rowSELECT * FROM FKTABLE; ftest1 | ftest2 --------+-------- 2 | 3 3 | 4 | 1(3 rows)-- Update a row from PK TABLEUPDATE PKTABLE SET ptest1=1 WHERE ptest1=2;-- Check FKTABLE for update of matched rowSELECT * FROM FKTABLE; ftest1 | ftest2 --------+-------- 3 | 4 | 1 1 | 3(3 rows)DROP TABLE FKTABLE;DROP TABLE PKTABLE;---- check set NULL and table constraint on multiple columns--CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 text, PRIMARY KEY(ptest1, ptest2) );NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"CREATE TABLE FKTABLE ( ftest1 int, ftest2 int, ftest3 int, CONSTRAINT constrname FOREIGN KEY(ftest1, ftest2) REFERENCES PKTABLE MATCH FULL ON DELETE SET NULL ON UPDATE SET NULL);-- Test commentsCOMMENT ON CONSTRAINT constrname_wrong ON FKTABLE IS 'fk constraint comment';ERROR: constraint "constrname_wrong" for table "fktable" does not existCOMMENT ON CONSTRAINT constrname ON FKTABLE IS 'fk constraint comment';COMMENT ON CONSTRAINT constrname ON FKTABLE IS NULL;-- Insert test data into PKTABLEINSERT INTO PKTABLE VALUES (1, 2, 'Test1');INSERT INTO PKTABLE VALUES (1, 3, 'Test1-2');INSERT INTO PKTABLE VALUES (2, 4, 'Test2');INSERT INTO PKTABLE VALUES (3, 6, 'Test3');INSERT INTO PKTABLE VALUES (4, 8, 'Test4');INSERT INTO PKTABLE VALUES (5, 10, 'Test5');-- Insert successful rows into FK TABLEINSERT INTO FKTABLE VALUES (1, 2, 4);INSERT INTO FKTABLE VALUES (1, 3, 5);INSERT INTO FKTABLE VALUES (2, 4, 8);INSERT INTO FKTABLE VALUES (3, 6, 12);INSERT INTO FKTABLE VALUES (NULL, NULL, 0);-- Insert failed rows into FK TABLEINSERT INTO FKTABLE VALUES (100, 2, 4);ERROR: insert or update on table "fktable" violates foreign key constraint "constrname"DETAIL: Key (ftest1,ftest2)=(100,2) is not present in table "pktable".INSERT INTO FKTABLE VALUES (2, 2, 4);ERROR: insert or update on table "fktable" violates foreign key constraint "constrname"DETAIL: Key (ftest1,ftest2)=(2,2) is not present in table "pktable".INSERT INTO FKTABLE VALUES (NULL, 2, 4);ERROR: insert or update on table "fktable" violates foreign key constraint "constrname"DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.INSERT INTO FKTABLE VALUES (1, NULL, 4);ERROR: insert or update on table "fktable" violates foreign key constraint "constrname"DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.-- Check FKTABLESELECT * FROM FKTABLE; ftest1 | ftest2 | ftest3 --------+--------+-------- 1 | 2 | 4 1 | 3 | 5 2 | 4 | 8 3 | 6 | 12 | | 0(5 rows)-- Delete a row from PK TABLEDELETE FROM PKTABLE WHERE ptest1=1 and ptest2=2;-- Check FKTABLE for removal of matched rowSELECT * FROM FKTABLE; ftest1 | ftest2 | ftest3 --------+--------+-------- 1 | 3 | 5 2 | 4 | 8 3 | 6 | 12 | | 0 | | 4(5 rows)-- Delete another row from PK TABLEDELETE FROM PKTABLE WHERE ptest1=5 and ptest2=10;-- Check FKTABLE (should be no change)SELECT * FROM FKTABLE; ftest1 | ftest2 | ftest3 --------+--------+-------- 1 | 3 | 5 2 | 4 | 8 3 | 6 | 12 | | 0 | | 4(5 rows)-- Update a row from PK TABLEUPDATE PKTABLE SET ptest1=1 WHERE ptest1=2;-- Check FKTABLE for update of matched rowSELECT * FROM FKTABLE; ftest1 | ftest2 | ftest3 --------+--------+-------- 1 | 3 | 5 3 | 6 | 12 | | 0 | | 4 | | 8(5 rows)-- Try altering the column type where foreign keys are involvedALTER TABLE PKTABLE ALTER COLUMN ptest1 TYPE bigint;ALTER TABLE FKTABLE ALTER COLUMN ftest1 TYPE bigint;SELECT * FROM PKTABLE; ptest1 | ptest2 | ptest3 --------+--------+--------- 1 | 3 | Test1-2 3 | 6 | Test3 4 | 8 | Test4 1 | 4 | Test2(4 rows)SELECT * FROM FKTABLE; ftest1 | ftest2 | ftest3 --------+--------+-------- 1 | 3 | 5 3 | 6 | 12 | | 0 | | 4 | | 8(5 rows)DROP TABLE PKTABLE CASCADE;NOTICE: drop cascades to constraint constrname on table fktableDROP TABLE FKTABLE;---- check set default and table constraint on multiple columns--CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 text, PRIMARY KEY(ptest1, ptest2) );NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"CREATE TABLE FKTABLE ( ftest1 int DEFAULT -1, ftest2 int DEFAULT -2, ftest3 int, CONSTRAINT constrname2 FOREIGN KEY(ftest1, ftest2) REFERENCES PKTABLE MATCH FULL ON DELETE SET DEFAULT ON UPDATE SET DEFAULT);-- Insert a value in PKTABLE for defaultINSERT INTO PKTABLE VALUES (-1, -2, 'The Default!');-- Insert test data into PKTABLEINSERT INTO PKTABLE VALUES (1, 2, 'Test1');INSERT INTO PKTABLE VALUES (1, 3, 'Test1-2');INSERT INTO PKTABLE VALUES (2, 4, 'Test2');INSERT INTO PKTABLE VALUES (3, 6, 'Test3');INSERT INTO PKTABLE VALUES (4, 8, 'Test4');INSERT INTO PKTABLE VALUES (5, 10, 'Test5');-- Insert successful rows into FK TABLEINSERT INTO FKTABLE VALUES (1, 2, 4);INSERT INTO FKTABLE VALUES (1, 3, 5);INSERT INTO FKTABLE VALUES (2, 4, 8);INSERT INTO FKTABLE VALUES (3, 6, 12);INSERT INTO FKTABLE VALUES (NULL, NULL, 0);-- Insert failed rows into FK TABLEINSERT INTO FKTABLE VALUES (100, 2, 4);ERROR: insert or update on table "fktable" violates foreign key constraint "constrname2"DETAIL: Key (ftest1,ftest2)=(100,2) is not present in table "pktable".INSERT INTO FKTABLE VALUES (2, 2, 4);ERROR: insert or update on table "fktable" violates foreign key constraint "constrname2"DETAIL: Key (ftest1,ftest2)=(2,2) is not present in table "pktable".INSERT INTO FKTABLE VALUES (NULL, 2, 4);ERROR: insert or update on table "fktable" violates foreign key constraint "constrname2"DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.INSERT INTO FKTABLE VALUES (1, NULL, 4);ERROR: insert or update on table "fktable" violates foreign key constraint "constrname2"DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.-- Check FKTABLESELECT * FROM FKTABLE; ftest1 | ftest2 | ftest3 --------+--------+-------- 1 | 2 | 4 1 | 3 | 5 2 | 4 | 8 3 | 6 | 12 | | 0(5 rows)-- Delete a row from PK TABLEDELETE FROM PKTABLE WHERE ptest1=1 and ptest2=2;-- Check FKTABLE to check for removalSELECT * FROM FKTABLE; ftest1 | ftest2 | ftest3 --------+--------+-------- 1 | 3 | 5 2 | 4 | 8 3 | 6 | 12 | | 0 -1 | -2 | 4(5 rows)-- Delete another row from PK TABLEDELETE FROM PKTABLE WHERE ptest1=5 and ptest2=10;-- Check FKTABLE (should be no change)SELECT * FROM FKTABLE; ftest1 | ftest2 | ftest3 --------+--------+-------- 1 | 3 | 5 2 | 4 | 8 3 | 6 | 12 | | 0 -1 | -2 | 4(5 rows)-- Update a row from PK TABLEUPDATE PKTABLE SET ptest1=1 WHERE ptest1=2;-- Check FKTABLE for update of matched rowSELECT * FROM FKTABLE; ftest1 | ftest2 | ftest3 --------+--------+-------- 1 | 3 | 5 3 | 6 | 12 | | 0 -1 | -2 | 4 -1 | -2 | 8(5 rows)-- this should fail for lack of CASCADEDROP TABLE PKTABLE;NOTICE: constraint constrname2 on table fktable depends on table pktableERROR: cannot drop table pktable because other objects depend on itHINT: Use DROP ... CASCADE to drop the dependent objects too.DROP TABLE PKTABLE CASCADE;NOTICE: drop cascades to constraint constrname2 on table fktableDROP TABLE FKTABLE;---- First test, check with no on delete or on update--CREATE TABLE PKTABLE ( ptest1 int PRIMARY KEY, ptest2 text );NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"CREATE TABLE FKTABLE ( ftest1 int REFERENCES PKTABLE MATCH FULL, ftest2 int );-- Insert test data into PKTABLEINSERT INTO PKTABLE VALUES (1, 'Test1');INSERT INTO PKTABLE VALUES (2, 'Test2');INSERT INTO PKTABLE VALUES (3, 'Test3');INSERT INTO PKTABLE VALUES (4, 'Test4');INSERT INTO PKTABLE VALUES (5, 'Test5');-- Insert successful rows into FK TABLEINSERT INTO FKTABLE VALUES (1, 2);INSERT INTO FKTABLE VALUES (2, 3);INSERT INTO FKTABLE VALUES (3, 4);INSERT INTO FKTABLE VALUES (NULL, 1);-- Insert a failed row into FK TABLEINSERT INTO FKTABLE VALUES (100, 2);ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"DETAIL: Key (ftest1)=(100) is not present in table "pktable".-- Check FKTABLESELECT * FROM FKTABLE; ftest1 | ftest2 --------+-------- 1 | 2 2 | 3 3 | 4 | 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -