create table booking(
o_id bigint(20) not null primary key,
o_user int(10) not null,
o_time varchar(50) not null,
o_state varchar(10) not null,
o_delivery varchar(50),
o_ems varchar(20),
o_number int(20),
o_total float(6,2) not null,
foreign key(o_user) references user(u_id)
)engine=InnoDB charset=utf8;
create table information(
i_id int(10) not null auto_increment primary key,
i_orderid bigint(20) not null,
i_name varchar(50) not null,
i_number int(10) not null,
i_price float(6,2) not null,
foreign key(i_orderid) references booking(o_id) //外键
)engine=InnoDB charset=utf8;
4 个解决方案
#1
create table information(
i_id int(10) not null auto_increment primary key,
i_orderid bigint(20) not null,
i_name varchar(50) not null,
i_number int(10) not null,
i_price float(6,2) not null,
foreign key(i_orderid) references booking(o_id) ON DELETE CASCADE //外键
)engine=InnoDB charset=utf8;
i_id int(10) not null auto_increment primary key,
i_orderid bigint(20) not null,
i_name varchar(50) not null,
i_number int(10) not null,
i_price float(6,2) not null,
foreign key(i_orderid) references booking(o_id) ON DELETE CASCADE //外键
)engine=InnoDB charset=utf8;
#2
create table booking(
o_id bigint(20) not null primary key,
o_user int(10) not null,
o_time varchar(50) not null,
o_state varchar(10) not null,
o_delivery varchar(50),
o_ems varchar(20),
o_number int(20),
o_total float(6,2) not null
)engine=InnoDB charset=utf8;
create table information(
i_id int(10) not null auto_increment primary key,
i_orderid bigint(20) not null,
i_name varchar(50) not null,
i_number int(10) not null,
i_price float(6,2) not null,
CONSTRAINT `i_orderid` FOREIGN KEY (`i_id`) REFERENCES `booking` (`o_id`) ON DELETE CASCADE ON UPDATE CASCADE
)engine=InnoDB charset=utf8;
o_id bigint(20) not null primary key,
o_user int(10) not null,
o_time varchar(50) not null,
o_state varchar(10) not null,
o_delivery varchar(50),
o_ems varchar(20),
o_number int(20),
o_total float(6,2) not null
)engine=InnoDB charset=utf8;
create table information(
i_id int(10) not null auto_increment primary key,
i_orderid bigint(20) not null,
i_name varchar(50) not null,
i_number int(10) not null,
i_price float(6,2) not null,
CONSTRAINT `i_orderid` FOREIGN KEY (`i_id`) REFERENCES `booking` (`o_id`) ON DELETE CASCADE ON UPDATE CASCADE
)engine=InnoDB charset=utf8;
#3
如果不想重建 可以这样
1.找出原来建立好的外键约束
从这个脚本的constraint_name就是对应的外键约束名字啦
2.删除这个约束
alter table information drop foreign key information_ibfk_1换成你查找出来的外键名字
3.添加新的外键带级联删除的约束
1.找出原来建立好的外键约束
SELECT
ke.referenced_table_name parent,
ke.table_name child,
ke.REFERENCED_COLUMN_NAME parent_column,
ke.column_name child_column,
ke.constraint_name
FROM
information_schema.KEY_COLUMN_USAGE ke
WHERE
ke.referenced_table_name IS NOT NULL
and ke.referenced_table_name = 'booking'
and ke.REFERENCED_COLUMN_NAME = 'o_id'
and ke.REFERENCED_TABLE_SCHEMA = '你的数据库名字'
ORDER BY ke.referenced_table_name;
从这个脚本的constraint_name就是对应的外键约束名字啦
2.删除这个约束
alter table information drop foreign key information_ibfk_1换成你查找出来的外键名字
3.添加新的外键带级联删除的约束
--先给i_orderid字段加索引
alter table information add index ind_inf(i_orderid);
--然后添加约束
alter table information add constraint FK_book_inf foreign key(i_orderid) references booking(o_id) on delete cascade
#4
delete booking,information from booking inner join information on o_id=i_orderid where o_id=10 ;
不修改表的基础上用上述SQL语句操作。
#1
create table information(
i_id int(10) not null auto_increment primary key,
i_orderid bigint(20) not null,
i_name varchar(50) not null,
i_number int(10) not null,
i_price float(6,2) not null,
foreign key(i_orderid) references booking(o_id) ON DELETE CASCADE //外键
)engine=InnoDB charset=utf8;
i_id int(10) not null auto_increment primary key,
i_orderid bigint(20) not null,
i_name varchar(50) not null,
i_number int(10) not null,
i_price float(6,2) not null,
foreign key(i_orderid) references booking(o_id) ON DELETE CASCADE //外键
)engine=InnoDB charset=utf8;
#2
create table booking(
o_id bigint(20) not null primary key,
o_user int(10) not null,
o_time varchar(50) not null,
o_state varchar(10) not null,
o_delivery varchar(50),
o_ems varchar(20),
o_number int(20),
o_total float(6,2) not null
)engine=InnoDB charset=utf8;
create table information(
i_id int(10) not null auto_increment primary key,
i_orderid bigint(20) not null,
i_name varchar(50) not null,
i_number int(10) not null,
i_price float(6,2) not null,
CONSTRAINT `i_orderid` FOREIGN KEY (`i_id`) REFERENCES `booking` (`o_id`) ON DELETE CASCADE ON UPDATE CASCADE
)engine=InnoDB charset=utf8;
o_id bigint(20) not null primary key,
o_user int(10) not null,
o_time varchar(50) not null,
o_state varchar(10) not null,
o_delivery varchar(50),
o_ems varchar(20),
o_number int(20),
o_total float(6,2) not null
)engine=InnoDB charset=utf8;
create table information(
i_id int(10) not null auto_increment primary key,
i_orderid bigint(20) not null,
i_name varchar(50) not null,
i_number int(10) not null,
i_price float(6,2) not null,
CONSTRAINT `i_orderid` FOREIGN KEY (`i_id`) REFERENCES `booking` (`o_id`) ON DELETE CASCADE ON UPDATE CASCADE
)engine=InnoDB charset=utf8;
#3
如果不想重建 可以这样
1.找出原来建立好的外键约束
从这个脚本的constraint_name就是对应的外键约束名字啦
2.删除这个约束
alter table information drop foreign key information_ibfk_1换成你查找出来的外键名字
3.添加新的外键带级联删除的约束
1.找出原来建立好的外键约束
SELECT
ke.referenced_table_name parent,
ke.table_name child,
ke.REFERENCED_COLUMN_NAME parent_column,
ke.column_name child_column,
ke.constraint_name
FROM
information_schema.KEY_COLUMN_USAGE ke
WHERE
ke.referenced_table_name IS NOT NULL
and ke.referenced_table_name = 'booking'
and ke.REFERENCED_COLUMN_NAME = 'o_id'
and ke.REFERENCED_TABLE_SCHEMA = '你的数据库名字'
ORDER BY ke.referenced_table_name;
从这个脚本的constraint_name就是对应的外键约束名字啦
2.删除这个约束
alter table information drop foreign key information_ibfk_1换成你查找出来的外键名字
3.添加新的外键带级联删除的约束
--先给i_orderid字段加索引
alter table information add index ind_inf(i_orderid);
--然后添加约束
alter table information add constraint FK_book_inf foreign key(i_orderid) references booking(o_id) on delete cascade
#4
delete booking,information from booking inner join information on o_id=i_orderid where o_id=10 ;
不修改表的基础上用上述SQL语句操作。