| 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 |
|
| 23 |
--echo '#--------------------FN_DYNVARS_032_01-------------------------#' |
| 24 |
#################################################################### |
| 25 |
# Check if setting foreign_key_checks is changed in new connection # |
| 26 |
#################################################################### |
| 27 |
|
| 28 |
SET @@session.foreign_key_checks = 0; |
| 29 |
# con1 will be default connection from now on |
| 30 |
--echo 'connect (con1,localhost,root,,,,)' |
| 31 |
connect (con1,localhost,root,,,,); |
| 32 |
--echo 'connection con1' |
| 33 |
connection con1; |
| 34 |
SELECT @@session.foreign_key_checks; |
| 35 |
SET @@session.foreign_key_checks = 1; |
| 36 |
--echo 'connect (con2,localhost,root,,,,)' |
| 37 |
connect (con2,localhost,root,,,,); |
| 38 |
--echo 'connection con2' |
| 39 |
connection con2; |
| 40 |
SELECT @@session.foreign_key_checks; |
| 41 |
disconnect con2; |
| 42 |
|
| 43 |
--echo '#--------------------FN_DYNVARS_032_02-------------------------#' |
| 44 |
################################################################# |
| 45 |
# Begin the functionality Testing of foreign_key_checks # |
| 46 |
################################################################# |
| 47 |
|
| 48 |
--echo 'connection con1' |
| 49 |
connection con1; |
| 50 |
|
| 51 |
--disable_warnings |
| 52 |
DROP TABLE IF EXISTS t1,t2; |
| 53 |
--enable_warnings |
| 54 |
|
| 55 |
CREATE TABLE t1(a INT PRIMARY KEY)ENGINE = INNODB; |
| 56 |
CREATE TABLE t2(a INT PRIMARY KEY,b INT)ENGINE = INNODB; |
| 57 |
|
| 58 |
ALTER TABLE t2 |
| 59 |
ADD CONSTRAINT fk FOREIGN KEY (b) REFERENCES t1 (a); |
| 60 |
|
| 61 |
#=========================================================== |
| 62 |
--echo '---Check when foreign_key_checks is enabled---' |
| 63 |
#=========================================================== |
| 64 |
|
| 65 |
|
| 66 |
SET @@session.foreign_key_checks = 1; |
| 67 |
|
| 68 |
INSERT INTO t1 values (1),(2),(3); |
| 69 |
|
| 70 |
INSERT INTO t2 values (10,1); |
| 71 |
--Error ER_NO_REFERENCED_ROW_2 |
| 72 |
INSERT INTO t2 values (20,22); |
| 73 |
|
| 74 |
#=========================================================== |
| 75 |
--echo '---Check when foreign_key_checks is disabled---' |
| 76 |
#=========================================================== |
| 77 |
|
| 78 |
--Error ER_TRUNCATE_ILLEGAL_FK |
| 79 |
TRUNCATE t1; |
| 80 |
|
| 81 |
SET @@session.foreign_key_checks = 0; |
| 82 |
|
| 83 |
TRUNCATE t1; |
| 84 |
TRUNCATE t2; |
| 85 |
|
| 86 |
INSERT INTO t1 values (1),(2),(3); |
| 87 |
|
| 88 |
INSERT INTO t2 values (10,1); |
| 89 |
INSERT INTO t2 values (20,4); |
| 90 |
|
| 91 |
--echo 'try enabling foreign_key_checks again'; |
| 92 |
SET @@session.foreign_key_checks = 1; |
| 93 |
|
| 94 |
UPDATE t2 SET b=4 where a=20; |
| 95 |
|
| 96 |
#============================================================================== |
| 97 |
--echo 'Check when foreign_key_checks is enabled and FK constraint is re-created' |
| 98 |
#============================================================================== |
| 99 |
|
| 100 |
SET @@session.foreign_key_checks = 0; |
| 101 |
TRUNCATE t2; |
| 102 |
TRUNCATE t1; |
| 103 |
|
| 104 |
INSERT INTO t1 values (1),(2),(3); |
| 105 |
INSERT INTO t2 values (10,1),(20,4); |
| 106 |
|
| 107 |
ALTER TABLE t2 DROP FOREIGN KEY fk; |
| 108 |
|
| 109 |
SET @@session.foreign_key_checks = 1; |
| 110 |
|
| 111 |
# Test disabled as error description is different. The resulting description has |
| 112 |
# difference in code #sql-xxx_2 where xxx is different for each run. |
| 113 |
#--Error ER_NO_REFERENCED_ROW_2 |
| 114 |
#ALTER TABLE t2 |
| 115 |
#ADD CONSTRAINT fk FOREIGN KEY (b) REFERENCES t1 (a); |
| 116 |
|
| 117 |
# delete all rows with incorrect reference |
| 118 |
DELETE FROM t2 WHERE b not in (SELECT a from t1); |
| 119 |
|
| 120 |
ALTER TABLE t2 |
| 121 |
ADD CONSTRAINT fk FOREIGN KEY (b) REFERENCES t1 (a); |
| 122 |
|
| 123 |
INSERT INTO t2 values (20,2); |
| 124 |
|
| 125 |
SELECT * from t2; |
| 126 |
|
| 127 |
--disable_warnings |
| 128 |
DROP TABLE IF EXISTS t2; |
| 129 |
DROP TABLE IF EXISTS t1; |
| 130 |
--enable_warnings |
| 131 |
|
| 132 |
########################################################## |
| 133 |
# End of functionality Testing for foreign_key_checks # |
| 134 |
########################################################## |
| 135 |
|