· 8 years ago · Jan 21, 2018, 11:52 AM
1drop database if exists x;
2create database x character set utf8 collate utf8_unicode_ci;
3use x;
4
5create table a (
6 id int unsigned not null auto_increment, primary key(id),
7 name_a char(32) not null default ''
8) engine=InnoDB;
9
10create table b (
11 id int unsigned not null auto_increment, primary key(id),
12 ref int unsigned not null,
13 name_b char(32) not null default '',
14 foreign key(ref) references a (id) on update cascade on delete cascade
15) engine=InnoDB;
16
17mysql> show create table b;
18+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
19| Table | Create Table |
20+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
21| b | CREATE TABLE `b` (
22 `id` int(10) unsigned NOT NULL auto_increment,
23 `ref` int(10) unsigned NOT NULL,
24 `name_b` char(32) collate utf8_unicode_ci NOT NULL default '',
25 PRIMARY KEY (`id`),
26 KEY `ref` (`ref`),
27 CONSTRAINT `b_ibfk_1` FOREIGN KEY (`ref`) REFERENCES `a` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
28) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
29+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
301 row in set (0.00 sec)
31
32mysql> alter table b drop foreign key ref;
33ERROR 1025 (HY000): Error on rename of './x/b' to './x/#sql2-da7-8' (errno: 152)
34mysql> alter table b drop foreign key b_ibfk_1;
35Query OK, 0 rows affected (1.51 sec)
36Records: 0 Duplicates: 0 Warnings: 0
37
38mysql>