⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 foreign_key.out

📁 PostgreSQL7.4.6 for Linux
💻 OUT
📖 第 1 页 / 共 3 页
字号:
---- 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 "$1"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);-- 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)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 "$1"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)-- 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 "$1" 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 "$1" 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

⌨️ 快捷键说明

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