1,多对多:

image.png

  1. # 商品表
  2. create table tb_goods
  3. (
  4. id int primary key auto_increment,
  5. payment DECIMAL(10, 2),
  6. payment_type varchar(30),
  7. status int
  8. );
  9. # 订单表
  10. create table tb_order
  11. (
  12. id int primary key auto_increment,
  13. title varchar(32),
  14. price DECIMAL(8, 2)
  15. );
  16. # 中间表
  17. create table tb_other_goods (
  18. orther_id int,
  19. goods_id int,
  20. primary key (orther_id,goods_id),
  21. # 两个外键对应两个主键,实现多个一对多的关系,就是多对多;
  22. foreign key (orther_id) references tb_goods(id),
  23. foreign key (goods_id) references tb_order(id)
  24. );

2,一对多:

  1. # 用户表
  2. create table tb_user
  3. (
  4. id int primary key auto_increment,
  5. photo varchar(200),
  6. nickname varchar(32),
  7. age int,
  8. gender char(1)
  9. );
  10. drop table tb_user_desc;
  11. # 用户详情表
  12. create table tb_user_desc
  13. (
  14. id int primary key auto_increment,
  15. city varchar(20),
  16. edu varchar(10),
  17. income DECIMAL(8, 2),
  18. status varchar(10),
  19. # 关键字作为字段名要加 飘号:` `
  20. `desc` varchar(1000),
  21. # 主键作为外键对应主表的主键,就形成一对一的关系;
  22. foreign key (id) references tb_user (id)
  23. );
  24. #

3,一对一:

4,自己对自己:(属于一对一)