You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

500 lines
16 KiB

branches/zip: Fast index creation: Remove the ROW_PREBUILT_OBSOLETE nonsense. Active transactions must not switch table or index definitions on the fly, for several reasons, including the following: * copied indexes do not carry any history or locking information; that is, rollbacks, read views, and record locking would be broken * huge potential for race conditions, inconsistent reads and writes, loss of data, and corruption Instead of trying to track down if the table was changed during a transaction, acquire appropriate locks that protect the creation and dropping of indexes. innodb-index.test: Test the locking of CREATE INDEX and DROP INDEX. Test that consistent reads work across dropped indexes. lock_rec_insert_check_and_lock(): Relax the lock_table_has() assertion. When inserting a record into an index, the table must be at least IX-locked. However, when an index is being created, an IS-lock on the table is sufficient. row_merge_lock_table(): Add the parameter enum lock_mode mode, which must be LOCK_X or LOCK_S. row_merge_drop_table(): Assert that n_mysql_handles_opened == 0. Unconditionally drop the table. ha_innobase::add_index(): Acquire an X or S lock on the table, as appropriate. After acquiring an X lock, assert that n_mysql_handles_opened == 1. Remove the comments about dropping tables in the background. ha_innobase::final_drop_index(): Acquire an X lock on the table. dict_table_t: Remove version_number, to_be_dropped, and prebuilts. ins_node_t: Remove table_version_number. enum lock_mode: Move the definition from lock0lock.h to lock0types.h. ROW_PREBUILT_OBSOLETE, row_update_prebuilt(), row_prebuilt_table_obsolete(): Remove. row_prebuilt_t: Remove the declaration from row0types.h. row_drop_table_for_mysql_no_commit(): Always print a warning if a table was added to the background drop queue.
18 years ago
branches/zip: Fast index creation: Remove the ROW_PREBUILT_OBSOLETE nonsense. Active transactions must not switch table or index definitions on the fly, for several reasons, including the following: * copied indexes do not carry any history or locking information; that is, rollbacks, read views, and record locking would be broken * huge potential for race conditions, inconsistent reads and writes, loss of data, and corruption Instead of trying to track down if the table was changed during a transaction, acquire appropriate locks that protect the creation and dropping of indexes. innodb-index.test: Test the locking of CREATE INDEX and DROP INDEX. Test that consistent reads work across dropped indexes. lock_rec_insert_check_and_lock(): Relax the lock_table_has() assertion. When inserting a record into an index, the table must be at least IX-locked. However, when an index is being created, an IS-lock on the table is sufficient. row_merge_lock_table(): Add the parameter enum lock_mode mode, which must be LOCK_X or LOCK_S. row_merge_drop_table(): Assert that n_mysql_handles_opened == 0. Unconditionally drop the table. ha_innobase::add_index(): Acquire an X or S lock on the table, as appropriate. After acquiring an X lock, assert that n_mysql_handles_opened == 1. Remove the comments about dropping tables in the background. ha_innobase::final_drop_index(): Acquire an X lock on the table. dict_table_t: Remove version_number, to_be_dropped, and prebuilts. ins_node_t: Remove table_version_number. enum lock_mode: Move the definition from lock0lock.h to lock0types.h. ROW_PREBUILT_OBSOLETE, row_update_prebuilt(), row_prebuilt_table_obsolete(): Remove. row_prebuilt_t: Remove the declaration from row0types.h. row_drop_table_for_mysql_no_commit(): Always print a warning if a table was added to the background drop queue.
18 years ago
branches/zip: Fast index creation: Remove the ROW_PREBUILT_OBSOLETE nonsense. Active transactions must not switch table or index definitions on the fly, for several reasons, including the following: * copied indexes do not carry any history or locking information; that is, rollbacks, read views, and record locking would be broken * huge potential for race conditions, inconsistent reads and writes, loss of data, and corruption Instead of trying to track down if the table was changed during a transaction, acquire appropriate locks that protect the creation and dropping of indexes. innodb-index.test: Test the locking of CREATE INDEX and DROP INDEX. Test that consistent reads work across dropped indexes. lock_rec_insert_check_and_lock(): Relax the lock_table_has() assertion. When inserting a record into an index, the table must be at least IX-locked. However, when an index is being created, an IS-lock on the table is sufficient. row_merge_lock_table(): Add the parameter enum lock_mode mode, which must be LOCK_X or LOCK_S. row_merge_drop_table(): Assert that n_mysql_handles_opened == 0. Unconditionally drop the table. ha_innobase::add_index(): Acquire an X or S lock on the table, as appropriate. After acquiring an X lock, assert that n_mysql_handles_opened == 1. Remove the comments about dropping tables in the background. ha_innobase::final_drop_index(): Acquire an X lock on the table. dict_table_t: Remove version_number, to_be_dropped, and prebuilts. ins_node_t: Remove table_version_number. enum lock_mode: Move the definition from lock0lock.h to lock0types.h. ROW_PREBUILT_OBSOLETE, row_update_prebuilt(), row_prebuilt_table_obsolete(): Remove. row_prebuilt_t: Remove the declaration from row0types.h. row_drop_table_for_mysql_no_commit(): Always print a warning if a table was added to the background drop queue.
18 years ago
branches/zip: Fast index creation: Remove the ROW_PREBUILT_OBSOLETE nonsense. Active transactions must not switch table or index definitions on the fly, for several reasons, including the following: * copied indexes do not carry any history or locking information; that is, rollbacks, read views, and record locking would be broken * huge potential for race conditions, inconsistent reads and writes, loss of data, and corruption Instead of trying to track down if the table was changed during a transaction, acquire appropriate locks that protect the creation and dropping of indexes. innodb-index.test: Test the locking of CREATE INDEX and DROP INDEX. Test that consistent reads work across dropped indexes. lock_rec_insert_check_and_lock(): Relax the lock_table_has() assertion. When inserting a record into an index, the table must be at least IX-locked. However, when an index is being created, an IS-lock on the table is sufficient. row_merge_lock_table(): Add the parameter enum lock_mode mode, which must be LOCK_X or LOCK_S. row_merge_drop_table(): Assert that n_mysql_handles_opened == 0. Unconditionally drop the table. ha_innobase::add_index(): Acquire an X or S lock on the table, as appropriate. After acquiring an X lock, assert that n_mysql_handles_opened == 1. Remove the comments about dropping tables in the background. ha_innobase::final_drop_index(): Acquire an X lock on the table. dict_table_t: Remove version_number, to_be_dropped, and prebuilts. ins_node_t: Remove table_version_number. enum lock_mode: Move the definition from lock0lock.h to lock0types.h. ROW_PREBUILT_OBSOLETE, row_update_prebuilt(), row_prebuilt_table_obsolete(): Remove. row_prebuilt_t: Remove the declaration from row0types.h. row_drop_table_for_mysql_no_commit(): Always print a warning if a table was added to the background drop queue.
18 years ago
  1. -- source include/have_innodb.inc
  2. create table t1(a int not null, b int, c char(10) not null, d varchar(20)) engine = innodb;
  3. insert into t1 values (5,5,'oo','oo'),(4,4,'tr','tr'),(3,4,'ad','ad'),(2,3,'ak','ak');
  4. commit;
  5. --error ER_DUP_KEYNAME
  6. alter table t1 add index b (b), add index b (b);
  7. --error ER_DUP_FIELDNAME
  8. alter table t1 add index (b,b);
  9. alter table t1 add index d2 (d);
  10. show create table t1;
  11. explain select * from t1 force index(d2) order by d;
  12. select * from t1 force index (d2) order by d;
  13. --error ER_DUP_ENTRY
  14. alter table t1 add unique index (b);
  15. show create table t1;
  16. alter table t1 add index (b);
  17. show create table t1;
  18. # Check how existing tables interfere with temporary tables.
  19. CREATE TABLE `t1#1`(a INT PRIMARY KEY) ENGINE=InnoDB;
  20. --error 156
  21. alter table t1 add unique index (c), add index (d);
  22. rename table `t1#1` to `t1#2`;
  23. --error 156
  24. alter table t1 add unique index (c), add index (d);
  25. drop table `t1#2`;
  26. alter table t1 add unique index (c), add index (d);
  27. show create table t1;
  28. explain select * from t1 force index(c) order by c;
  29. --error ER_REQUIRES_PRIMARY_KEY
  30. drop index c on t1;
  31. alter table t1 add primary key (a), drop index c;
  32. show create table t1;
  33. --error ER_MULTIPLE_PRI_KEY
  34. alter table t1 add primary key (c);
  35. --error ER_DUP_ENTRY
  36. alter table t1 drop primary key, add primary key (b);
  37. create unique index c on t1 (c);
  38. show create table t1;
  39. explain select * from t1 force index(c) order by c;
  40. select * from t1 force index(c) order by c;
  41. alter table t1 drop index b, add index (b);
  42. show create table t1;
  43. insert into t1 values(6,1,'ggg','ggg');
  44. select * from t1;
  45. select * from t1 force index(b) order by b;
  46. select * from t1 force index(c) order by c;
  47. select * from t1 force index(d) order by d;
  48. explain select * from t1 force index(b) order by b;
  49. explain select * from t1 force index(c) order by c;
  50. explain select * from t1 force index(d) order by d;
  51. show create table t1;
  52. drop table t1;
  53. create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
  54. insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,3,'ad','ad'),(4,4,'afe','afe');
  55. commit;
  56. alter table t1 add index (c(2));
  57. show create table t1;
  58. alter table t1 add unique index (d(10));
  59. show create table t1;
  60. insert into t1 values(5,1,'ggg','ggg');
  61. select * from t1;
  62. select * from t1 force index(c) order by c;
  63. select * from t1 force index(d) order by d;
  64. explain select * from t1 order by b;
  65. explain select * from t1 force index(c) order by c;
  66. explain select * from t1 force index(d) order by d;
  67. show create table t1;
  68. alter table t1 drop index d;
  69. insert into t1 values(8,9,'fff','fff');
  70. select * from t1;
  71. select * from t1 force index(c) order by c;
  72. explain select * from t1 order by b;
  73. explain select * from t1 force index(c) order by c;
  74. explain select * from t1 order by d;
  75. show create table t1;
  76. drop table t1;
  77. create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
  78. insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,2,'ad','ad'),(4,4,'afe','afe');
  79. commit;
  80. alter table t1 add unique index (b,c);
  81. insert into t1 values(8,9,'fff','fff');
  82. select * from t1;
  83. select * from t1 force index(b) order by b;
  84. explain select * from t1 force index(b) order by b;
  85. show create table t1;
  86. alter table t1 add index (b,c);
  87. insert into t1 values(11,11,'kkk','kkk');
  88. select * from t1;
  89. select * from t1 force index(b) order by b;
  90. explain select * from t1 force index(b) order by b;
  91. show create table t1;
  92. alter table t1 add unique index (c,d);
  93. insert into t1 values(13,13,'yyy','aaa');
  94. select * from t1;
  95. select * from t1 force index(b) order by b;
  96. select * from t1 force index(c) order by c;
  97. explain select * from t1 force index(b) order by b;
  98. explain select * from t1 force index(c) order by c;
  99. show create table t1;
  100. drop table t1;
  101. create table t1(a int not null, b int not null, c int, primary key (a), key (b)) engine = innodb;
  102. create table t3(a int not null, c int not null, d int, primary key (a), key (c)) engine = innodb;
  103. create table t4(a int not null, d int not null, e int, primary key (a), key (d)) engine = innodb;
  104. create table t2(a int not null, b int not null, c int not null, d int not null, e int,
  105. foreign key (b) references t1(b) on delete cascade,
  106. foreign key (c) references t3(c), foreign key (d) references t4(d))
  107. engine = innodb;
  108. --error ER_DROP_INDEX_FK
  109. alter table t1 drop index b;
  110. --error ER_DROP_INDEX_FK
  111. alter table t3 drop index c;
  112. --error ER_DROP_INDEX_FK
  113. alter table t4 drop index d;
  114. --error ER_DROP_INDEX_FK
  115. alter table t2 drop index b;
  116. --error ER_DROP_INDEX_FK
  117. alter table t2 drop index b, drop index c, drop index d;
  118. # Apparently, the following makes mysql_alter_table() drop index d.
  119. create unique index dc on t2 (d,c);
  120. create index dc on t1 (b,c);
  121. # This should preserve the foreign key constraints.
  122. alter table t2 add primary key (a);
  123. insert into t1 values (1,1,1);
  124. insert into t3 values (1,1,1);
  125. insert into t4 values (1,1,1);
  126. insert into t2 values (1,1,1,1,1);
  127. commit;
  128. alter table t4 add constraint dc foreign key (a) references t1(a);
  129. show create table t4;
  130. --replace_regex /'test\.#sql-[0-9a-f_]*'/'#sql-temporary'/
  131. # a foreign key 'test/dc' already exists
  132. --error ER_CANT_CREATE_TABLE
  133. alter table t3 add constraint dc foreign key (a) references t1(a);
  134. show create table t3;
  135. alter table t2 drop index b, add index (b);
  136. show create table t2;
  137. --error ER_ROW_IS_REFERENCED_2
  138. delete from t1;
  139. --error ER_CANT_DROP_FIELD_OR_KEY
  140. drop index dc on t4;
  141. # there is no foreign key dc on t3
  142. --replace_regex /'\.\/test\/#sql2-[0-9a-f-]*'/'#sql2-temporary'/
  143. --error ER_ERROR_ON_RENAME
  144. alter table t3 drop foreign key dc;
  145. alter table t4 drop foreign key dc;
  146. select * from t2;
  147. delete from t1;
  148. select * from t2;
  149. drop table t2,t4,t3,t1;
  150. -- let charset = utf8
  151. -- source include/innodb-index.inc
  152. create table t1(a int not null, b int) engine = innodb;
  153. insert into t1 values (1,1),(1,1),(1,1),(1,1);
  154. --error ER_DUP_ENTRY
  155. alter table t1 add unique index (a);
  156. --error ER_DUP_ENTRY
  157. alter table t1 add unique index (b);
  158. --error ER_DUP_ENTRY
  159. alter table t1 add unique index (a), add unique index(b);
  160. show create table t1;
  161. drop table t1;
  162. create table t1(a int not null, c int not null,b int, primary key(a), unique key(c), key(b)) engine = innodb;
  163. alter table t1 drop index c, drop index b;
  164. show create table t1;
  165. drop table t1;
  166. create table t1(a int not null, b int, primary key(a)) engine = innodb;
  167. alter table t1 add index (b);
  168. show create table t1;
  169. drop table t1;
  170. create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
  171. insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,3,'ac','ac'),(4,4,'afe','afe'),(5,4,'affe','affe');
  172. --error ER_DUP_ENTRY
  173. alter table t1 add unique index (b), add unique index (c), add unique index (d);
  174. --error ER_DUP_ENTRY
  175. alter table t1 add unique index (c), add unique index (b), add index (d);
  176. show create table t1;
  177. drop table t1;
  178. create table t1(a int not null, b int not null, c int, primary key (a), key(c)) engine=innodb;
  179. insert into t1 values (5,1,5),(4,2,4),(3,3,3),(2,4,2),(1,5,1);
  180. alter table t1 add unique index (b);
  181. insert into t1 values (10,20,20),(11,19,19),(12,18,18),(13,17,17);
  182. show create table t1;
  183. check table t1;
  184. explain select * from t1 force index(c) order by c;
  185. explain select * from t1 order by a;
  186. explain select * from t1 force index(b) order by b;
  187. select * from t1 order by a;
  188. select * from t1 force index(b) order by b;
  189. select * from t1 force index(c) order by c;
  190. drop table t1;
  191. create table t1(a int not null, b int not null) engine=innodb;
  192. insert into t1 values (1,1);
  193. alter table t1 add primary key(b);
  194. insert into t1 values (2,2);
  195. show create table t1;
  196. check table t1;
  197. select * from t1;
  198. explain select * from t1;
  199. explain select * from t1 order by a;
  200. explain select * from t1 order by b;
  201. checksum table t1;
  202. drop table t1;
  203. create table t1(a int not null) engine=innodb;
  204. insert into t1 values (1);
  205. alter table t1 add primary key(a);
  206. insert into t1 values (2);
  207. show create table t1;
  208. check table t1;
  209. commit;
  210. select * from t1;
  211. explain select * from t1;
  212. explain select * from t1 order by a;
  213. drop table t1;
  214. create table t2(d varchar(17) primary key) engine=innodb default charset=utf8;
  215. create table t3(a int primary key) engine=innodb;
  216. insert into t3 values(22),(44),(33),(55),(66);
  217. insert into t2 values ('jejdkrun87'),('adfd72nh9k'),
  218. ('adfdpplkeock'),('adfdijnmnb78k'),('adfdijn0loKNHJik');
  219. create table t1(a int, b blob, c text, d text not null)
  220. engine=innodb default charset = utf8;
  221. # r2667 The following test is disabled because MySQL behavior changed.
  222. # r2667 The test was added with this comment:
  223. # r2667
  224. # r2667 ------------------------------------------------------------------------
  225. # r2667 r1699 | marko | 2007-08-10 19:53:19 +0300 (Fri, 10 Aug 2007) | 5 lines
  226. # r2667
  227. # r2667 branches/zip: Add changes that accidentally omitted from r1698:
  228. # r2667
  229. # r2667 innodb-index.test, innodb-index.result: Add a test for creating
  230. # r2667 a PRIMARY KEY on a column that contains a NULL value.
  231. # r2667 ------------------------------------------------------------------------
  232. # r2667
  233. # r2667 but in BZR-r2667:
  234. # r2667 http://bazaar.launchpad.net/~mysql/mysql-server/mysql-5.1/revision/davi%40mysql.com-20080617141221-8yre8ys9j4uw3xx5?start_revid=joerg%40mysql.com-20080630105418-7qoe5ehomgrcdb89
  235. # r2667 MySQL changed the behavior to do full table copy when creating PRIMARY INDEX
  236. # r2667 on a non-NULL column instead of calling ::add_index() which would fail (and
  237. # r2667 this is what we were testing here). Before r2667 the code execution path was
  238. # r2667 like this (when adding PRIMARY INDEX on a non-NULL column with ALTER TABLE):
  239. # r2667
  240. # r2667 mysql_alter_table()
  241. # r2667 compare_tables() // would return ALTER_TABLE_INDEX_CHANGED
  242. # r2667 ::add_index() // would fail with "primary index cannot contain NULL"
  243. # r2667
  244. # r2667 after r2667 the code execution path is the following:
  245. # r2667
  246. # r2667 mysql_alter_table()
  247. # r2667 compare_tables() // returns ALTER_TABLE_DATA_CHANGED
  248. # r2667 full copy is done, without calling ::add_index()
  249. # r2667
  250. # r2667 To enable, remove "# r2667: " below.
  251. # r2667
  252. # r2667: insert into t1 values (null,null,null,'null');
  253. insert into t1
  254. select a,left(repeat(d,100*a),65535),repeat(d,20*a),d from t2,t3;
  255. drop table t2, t3;
  256. select count(*) from t1 where a=44;
  257. select a,
  258. length(b),b=left(repeat(d,100*a),65535),length(c),c=repeat(d,20*a),d from t1;
  259. # r2667: --error ER_PRIMARY_CANT_HAVE_NULL
  260. # r2667: alter table t1 add primary key (a), add key (b(20));
  261. # r2667: delete from t1 where d='null';
  262. --error ER_DUP_ENTRY
  263. alter table t1 add primary key (a), add key (b(20));
  264. delete from t1 where a%2;
  265. check table t1;
  266. alter table t1 add primary key (a,b(255),c(255)), add key (b(767));
  267. select count(*) from t1 where a=44;
  268. select a,
  269. length(b),b=left(repeat(d,100*a),65535),length(c),c=repeat(d,20*a),d from t1;
  270. show create table t1;
  271. check table t1;
  272. explain select * from t1 where b like 'adfd%';
  273. #
  274. # Test locking
  275. #
  276. create table t2(a int, b varchar(255), primary key(a,b)) engine=innodb;
  277. insert into t2 select a,left(b,255) from t1;
  278. drop table t1;
  279. rename table t2 to t1;
  280. connect (a,localhost,root,,);
  281. connect (b,localhost,root,,);
  282. connection a;
  283. begin;
  284. # Obtain an IX lock on the table
  285. select a from t1 limit 1 for update;
  286. connection b;
  287. # This would require an S lock on the table, conflicting with the IX lock.
  288. --error ER_LOCK_WAIT_TIMEOUT
  289. create index t1ba on t1 (b,a);
  290. connection a;
  291. commit;
  292. begin;
  293. # Obtain an IS lock on the table
  294. select a from t1 limit 1 lock in share mode;
  295. connection b;
  296. # This will require an S lock on the table. No conflict with the IS lock.
  297. create index t1ba on t1 (b,a);
  298. # This would require an X lock on the table, conflicting with the IS lock.
  299. --error ER_LOCK_WAIT_TIMEOUT
  300. drop index t1ba on t1;
  301. connection a;
  302. commit;
  303. explain select a from t1 order by b;
  304. --send
  305. select a,sleep(2+a/100) from t1 order by b limit 3;
  306. # The following DROP INDEX will succeed, altough the SELECT above has
  307. # opened a read view. However, during the execution of the SELECT,
  308. # MySQL should hold a table lock that should block the execution
  309. # of the DROP INDEX below.
  310. connection b;
  311. select sleep(1);
  312. drop index t1ba on t1;
  313. # After the index was dropped, subsequent SELECTs will use the same
  314. # read view, but they should not be accessing the dropped index any more.
  315. connection a;
  316. reap;
  317. explain select a from t1 order by b;
  318. select a from t1 order by b limit 3;
  319. commit;
  320. connection default;
  321. disconnect a;
  322. disconnect b;
  323. drop table t1;
  324. # Test creating a table that could lead to undo log overflow.
  325. # In the undo log, we write a 768-byte prefix (REC_MAX_INDEX_COL_LEN)
  326. # of each externally stored column that appears as a column prefix in an index.
  327. # For this test case, it would suffice to write 1 byte, though.
  328. create table t1(a blob,b blob,c blob,d blob,e blob,f blob,g blob,h blob,
  329. i blob,j blob,k blob,l blob,m blob,n blob,o blob,p blob,
  330. q blob,r blob,s blob,t blob,u blob)
  331. engine=innodb;
  332. create index t1a on t1 (a(1));
  333. create index t1b on t1 (b(1));
  334. create index t1c on t1 (c(1));
  335. create index t1d on t1 (d(1));
  336. create index t1e on t1 (e(1));
  337. create index t1f on t1 (f(1));
  338. create index t1g on t1 (g(1));
  339. create index t1h on t1 (h(1));
  340. create index t1i on t1 (i(1));
  341. create index t1j on t1 (j(1));
  342. create index t1k on t1 (k(1));
  343. create index t1l on t1 (l(1));
  344. create index t1m on t1 (m(1));
  345. create index t1n on t1 (n(1));
  346. create index t1o on t1 (o(1));
  347. create index t1p on t1 (p(1));
  348. create index t1q on t1 (q(1));
  349. create index t1r on t1 (r(1));
  350. create index t1s on t1 (s(1));
  351. create index t1t on t1 (t(1));
  352. --error 139
  353. create index t1u on t1 (u(1));
  354. --error 139
  355. create index t1ut on t1 (u(1), t(1));
  356. create index t1st on t1 (s(1), t(1));
  357. show create table t1;
  358. drop table t1;
  359. #
  360. # Test to check whether CREATE INDEX handles implicit foreign key
  361. # constraint modifications (Issue #70, Bug #38786)
  362. #
  363. SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
  364. SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
  365. CREATE TABLE t1(
  366. c1 BIGINT(12) NOT NULL,
  367. PRIMARY KEY (c1)
  368. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  369. CREATE TABLE t2(
  370. c1 BIGINT(16) NOT NULL,
  371. c2 BIGINT(12) NOT NULL,
  372. c3 BIGINT(12) NOT NULL,
  373. PRIMARY KEY (c1)
  374. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  375. ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
  376. FOREIGN KEY (c3) REFERENCES t1(c1);
  377. SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
  378. SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
  379. SHOW CREATE TABLE t2;
  380. CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
  381. SHOW CREATE TABLE t2;
  382. SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
  383. SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
  384. --error ER_NO_REFERENCED_ROW_2
  385. INSERT INTO t2 VALUES(0,0,0);
  386. INSERT INTO t1 VALUES(0);
  387. INSERT INTO t2 VALUES(0,0,0);
  388. DROP TABLE t2;
  389. CREATE TABLE t2(
  390. c1 BIGINT(16) NOT NULL,
  391. c2 BIGINT(12) NOT NULL,
  392. c3 BIGINT(12) NOT NULL,
  393. PRIMARY KEY (c1,c2,c3)
  394. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  395. ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
  396. FOREIGN KEY (c3) REFERENCES t1(c1);
  397. SHOW CREATE TABLE t2;
  398. CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
  399. SHOW CREATE TABLE t2;
  400. --error ER_NO_REFERENCED_ROW_2
  401. INSERT INTO t2 VALUES(0,0,1);
  402. INSERT INTO t2 VALUES(0,0,0);
  403. --error ER_ROW_IS_REFERENCED_2
  404. DELETE FROM t1;
  405. DELETE FROM t2;
  406. DROP TABLE t2;
  407. DROP TABLE t1;
  408. CREATE TABLE t1(
  409. c1 BIGINT(12) NOT NULL,
  410. c2 INT(4) NOT NULL,
  411. PRIMARY KEY (c2,c1)
  412. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  413. CREATE TABLE t2(
  414. c1 BIGINT(16) NOT NULL,
  415. c2 BIGINT(12) NOT NULL,
  416. c3 BIGINT(12) NOT NULL,
  417. PRIMARY KEY (c1)
  418. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  419. --replace_regex /'test\.#sql-[0-9a-f-]*_1'/'#sql-temporary'/
  420. --error ER_CANT_CREATE_TABLE
  421. ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
  422. FOREIGN KEY (c3,c2) REFERENCES t1(c1,c1);
  423. --replace_regex /'test\.#sql-[0-9a-f-]*_1'/'#sql-temporary'/
  424. --error ER_CANT_CREATE_TABLE
  425. ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
  426. FOREIGN KEY (c3,c2) REFERENCES t1(c1,c2);
  427. --replace_regex /'test\.#sql-[0-9a-f-]*_1'/'#sql-temporary'/
  428. --error ER_CANT_CREATE_TABLE
  429. ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
  430. FOREIGN KEY (c3,c2) REFERENCES t1(c2,c1);
  431. ALTER TABLE t1 MODIFY COLUMN c2 BIGINT(12) NOT NULL;
  432. --replace_regex /'test\.#sql-[0-9a-f-]*_1'/'#sql-temporary'/
  433. --error ER_CANT_CREATE_TABLE
  434. ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
  435. FOREIGN KEY (c3,c2) REFERENCES t1(c1,c2);
  436. ALTER TABLE t2 ADD CONSTRAINT fk_t2_ca
  437. FOREIGN KEY (c3,c2) REFERENCES t1(c2,c1);
  438. SHOW CREATE TABLE t1;
  439. SHOW CREATE TABLE t2;
  440. CREATE INDEX i_t2_c2_c1 ON t2(c2, c1);
  441. SHOW CREATE TABLE t2;
  442. CREATE INDEX i_t2_c3_c1_c2 ON t2(c3, c1, c2);
  443. SHOW CREATE TABLE t2;
  444. CREATE INDEX i_t2_c3_c2 ON t2(c3, c2);
  445. SHOW CREATE TABLE t2;
  446. DROP TABLE t2;
  447. DROP TABLE t1;