在实际运用中,多对多也是十分常见的,比如一个订单可能包含多个产品,而每个商品有可能出现在多个订单中,在数据库中这样的情况就需要一张中间表来维护,下面通过一个案例来学习
1.新建三个数据表
其中订单表在上一票博客中已经创建过了(点这里跳转至上篇博客),下面展示中间表以及商品表的创建
create table tb_product(
id int(32) primary key auto_increment,
name varchar(32),
price double
);
insert into tb_product values('1','Java基础入门','44.5');
insert into tb_product values('2','Java Web程序开发入门','38.5');
insert into tb_product values('3','SSM框架整合实战','50');
create table tb_ordersitem(
id int(32) primary key auto_increment,
orders_id int(32),
product_id int(32),
foreign key(orders_id) references tb_orders(id),
foreign key(product_id) references tb_product(id)
);
insert into tb_ordersitem values('1','1','1');
insert in