MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trig_frkey.inc
1 #################################################################
2 # This file include tests that address the foreign key cases of
3 # the following requirements since they are specific to innodb.
4 # Other test cases for these requirements are included in the
5 # triggers_*.inc files.
6 #################################################################
7 
8 --disable_abort_on_error
9 
10 # Section x.x.x.1
11 # Test case: Verifing that a trigger that activates a primary key results in
12 # the primary key acting correctly on the foreign key
13 let $message= Testcase x.x.x.1:;
14 --source include/show_msg.inc
15 
16 
17 --disable_warnings
18 DROP TABLE IF EXISTS t0, t1, t2;
19 --enable_warnings
20 
21 --replace_result $engine_type <engine_to_be_tested>
22 eval
23 CREATE TABLE t0 (col1 CHAR(50))
24 ENGINE = $engine_type;
25 --replace_result $engine_type <engine_to_be_tested>
26 eval
27 CREATE TABLE t1 (id INT NOT NULL, col1 CHAR(50), PRIMARY KEY (id))
28 ENGINE = $engine_type;
29 --replace_result $engine_type <engine_to_be_tested>
30 eval
31 CREATE TABLE t2 (id INT PRIMARY KEY, f_id INT,
32  INDEX par_ind (f_id), col1 CHAR(50),
33  FOREIGN KEY (f_id) REFERENCES t1(id) ON DELETE SET NULL)
34 ENGINE = $engine_type;
35 
36 INSERT INTO t1 VALUES (1,'Department A');
37 INSERT INTO t1 VALUES (2,'Department B');
38 INSERT INTO t1 VALUES (3,'Department C');
39 INSERT INTO t2 VALUES (1,2,'Emp 1');
40 INSERT INTO t2 VALUES (2,2,'Emp 2');
41 INSERT INTO t2 VALUES (3,2,'Emp 3');
42 
43 CREATE TRIGGER trig AFTER INSERT ON t0 FOR EACH ROW
44 DELETE FROM t1 WHERE col1 = new.col1;
45 
46 --sorted_result
47 SELECT * FROM t2;
48 LOCK TABLES t0 WRITE, t1 WRITE;
49 INSERT INTO t0 VALUES ('Department B');
50 UNLOCK TABLES;
51 --sorted_result
52 SELECT * FROM t2;
53 
54 # Cleanup
55 DROP TRIGGER trig;
56 DROP TABLE t2, t1;
57 
58 
59 #Section x.x.x.2
60 # Test case: Checking that triggers can be used as a way to address missing foreign
61 # key definition
62 let $message= Testcase x.x.x.2:;
63 --source include/show_msg.inc
64 
65 --disable_warnings
66 DROP TABLE IF EXISTS t1, t2;
67 --enable_warnings
68 
69 --replace_result $engine_type <engine_to_be_tested>
70 eval
71 CREATE TABLE t1 (id INT NOT NULL, col1 CHAR(50), PRIMARY KEY (id))
72 ENGINE = $engine_type;
73 --replace_result $engine_type <engine_to_be_tested>
74 eval
75 CREATE TABLE t2 (id INT PRIMARY KEY, f_id INT,
76  INDEX par_ind (f_id), col1 CHAR(50),
77  FOREIGN KEY (f_id) REFERENCES t1(id) ON UPDATE CASCADE)
78 ENGINE = $engine_type;
79 
80 INSERT INTO t1 VALUES (1,'Department A');
81 INSERT INTO t1 VALUES (2,'Department B');
82 INSERT INTO t1 VALUES (3,'Department C');
83 INSERT INTO t2 VALUES (1,2,'Emp 1');
84 INSERT INTO t2 VALUES (2,3,'Emp 2');
85 
86 --error ER_NO_REFERENCED_ROW_2
87 insert into t2 VALUES (3,4,'Emp 3');
88 
89 CREATE TRIGGER tr_t2 BEFORE INSERT ON t2 FOR EACH ROW
90 INSERT INTO t1 VALUES(new.f_id, CONCAT('New Department ', new.f_id));
91 
92 LOCK TABLES t1 WRITE, t2 WRITE;
93 INSERT INTO t2 VALUES (3,4,'Emp 3');
94 UNLOCK TABLES;
95 
96 --sorted_result
97 SELECT * FROM t1;
98 --sorted_result
99 SELECT * FROM t2;
100 
101 # Cleanup
102 DROP TRIGGER tr_t2;
103 DROP TABLE t2, t1, t0;
104 
105 
106 --echo
107 --echo Foreign Key tests disabled (bug 11472 - stored in trig_frkey2.test)
108 --echo -------------------------------------------------------------------