Debian Codegraph @

mysql-test/suite/sys_vars/t/foreign_key_checks_func.test

raw
1 ############## mysql-test\t\foreign_key_checks_func.test #####################
2 # #
3 # Variable Name: foreign_key_checks #
4 # Scope: SESSION #
5 # Access Type: Dynamic #
6 # Data Type: boolean #
7 # Default Value: NA #
8 # Range: NA #
9 # #
10 # #
11 # Creation Date: 2008-03-08 #
12 # Author: Rizwan #
13 # #
14 # Description: Test Cases of Dynamic System Variable foreign_key_checks #
15 # that checks the behavior of this variable #
16 # #
17 # Reference: http://dev.mysql.com/doc/refman/5.1/en/ #
18 # server-system-variables.html #
19 # #
20 ###############################################################################
21
22 --source include/have_innodb.inc
23
24 --echo '#--------------------FN_DYNVARS_032_01-------------------------#'
25 ####################################################################
26 # Check if setting foreign_key_checks is changed in new connection #
27 ####################################################################
28
29 SET @@session.foreign_key_checks = 0;
30 # con1 will be default connection from now on
31 connect (con1,localhost,root,,,,);
32 connection con1;
33 SELECT @@session.foreign_key_checks;
34 SET @@session.foreign_key_checks = 1;
35 connect (con2,localhost,root,,,,);
36 connection con2;
37 SELECT @@session.foreign_key_checks;
38 disconnect con2;
39
40 --echo '#--------------------FN_DYNVARS_032_02-------------------------#'
41 #################################################################
42 # Begin the functionality Testing of foreign_key_checks #
43 #################################################################
44
45 connection con1;
46
47 --disable_warnings
48 DROP TABLE IF EXISTS t1,t2;
49 --enable_warnings
50
51 CREATE TABLE t1(a INT PRIMARY KEY)ENGINE = INNODB;
52 CREATE TABLE t2(a INT PRIMARY KEY,b INT)ENGINE = INNODB;
53
54 ALTER TABLE t2
55 ADD CONSTRAINT fk FOREIGN KEY (b) REFERENCES t1 (a);
56
57 #===========================================================
58 --echo '---Check when foreign_key_checks is enabled---'
59 #===========================================================
60
61
62 SET @@session.foreign_key_checks = 1;
63
64 INSERT INTO t1 values (1),(2),(3);
65
66 INSERT INTO t2 values (10,1);
67 --Error ER_NO_REFERENCED_ROW_2
68 INSERT INTO t2 values (20,22);
69
70 #===========================================================
71 --echo '---Check when foreign_key_checks is disabled---'
72 #===========================================================
73
74 --Error ER_TRUNCATE_ILLEGAL_FK
75 TRUNCATE t1;
76
77 SET @@session.foreign_key_checks = 0;
78
79 TRUNCATE t1;
80 TRUNCATE t2;
81
82 INSERT INTO t1 values (1),(2),(3);
83
84 INSERT INTO t2 values (10,1);
85 INSERT INTO t2 values (20,4);
86
87 --echo 'try enabling foreign_key_checks again';
88 SET @@session.foreign_key_checks = 1;
89
90 UPDATE t2 SET b=4 where a=20;
91
92 #==============================================================================
93 --echo 'Check when foreign_key_checks is enabled and FK constraint is re-created'
94 #==============================================================================
95
96 SET @@session.foreign_key_checks = 0;
97 TRUNCATE t2;
98 TRUNCATE t1;
99
100 INSERT INTO t1 values (1),(2),(3);
101 INSERT INTO t2 values (10,1),(20,4);
102
103 ALTER TABLE t2 DROP FOREIGN KEY fk;
104
105 SET @@session.foreign_key_checks = 1;
106
107 # Test disabled as error description is different. The resulting description has
108 # difference in code #sql-xxx_2 where xxx is different for each run.
109 #--Error ER_NO_REFERENCED_ROW_2
110 #ALTER TABLE t2
111 #ADD CONSTRAINT fk FOREIGN KEY (b) REFERENCES t1 (a);
112
113 # delete all rows with incorrect reference
114 DELETE FROM t2 WHERE b not in (SELECT a from t1);
115
116 ALTER TABLE t2
117 ADD CONSTRAINT fk FOREIGN KEY (b) REFERENCES t1 (a);
118
119 INSERT INTO t2 values (20,2);
120
121 SELECT * from t2;
122
123 --disable_warnings
124 DROP TABLE IF EXISTS t2;
125 DROP TABLE IF EXISTS t1;
126 --enable_warnings
127
128 ##########################################################
129 # End of functionality Testing for foreign_key_checks #
130 ##########################################################
131