举例

准备数据
2张表,一张水果表 fruit,一张是价格表 price
建表

  1. -- 价格 1
  2. create table price(
  3. id int primary key auto_increment,
  4. price double
  5. );
  6. -- 水果 n
  7. create table fruit(
  8. id int primary key auto_increment,
  9. name varchar(20) not null,
  10. price_id int,
  11. foreign key(price_id) references price(id)
  12. );
  13. insert into price values(1,2.30);
  14. insert into price values(2,3.50);
  15. insert into price values(4,null);
  16. insert into fruit values(1,'苹果',1);
  17. insert into fruit values(2,'橘子',2);
  18. insert into fruit values(3,'香蕉',null);
  19. -- 一种水果有一个价格 一个价格对应多种水果
  20. -- 价格 1 水果 n 水果将价格主键作为外键

fruit表
image-20220418113344935
price表
image-20220418113452055

什么是笛卡尔积现象

运行如下sql

  1. select * from fruit, price;

水果—->价格——>笛卡尔积结果映射关系:
image-20220418113713349

消除笛卡尔积

条件过滤笛卡尔积

  1. select * from fruit, price where fruit.price_id = price.id;

image-20220418113848895