当前位置: 首页 > news >正文

Java基础学完,继续深耕(0506)SQL--多表关系

多表关系

一对多(多对一)

一对一

多对多

一对多

场景:部门与员工的关系 (一个部门下有多个员工)。

实现:在数据库表中多的一方,添加字段,来关联一的一方的主键。

现在只是在语法上关联了,但是还需要添加外键约束实现真正关联

可以在创建表时 或 表结构创建完成后,为字段添加外键约束。 具体语法如下:

-- 添加外键约束(为emp表中的dept_id添加外键关联dept表中的id
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);

一对一

案例: 用户 与 身份证信息 的关系

关系: 一对一关系,多用于单表拆分,将一张表的基础字段放在一张表中,其他字段放在另一张表中,以提升操作效率

实现: 在任意一方加入外键,关联另外一方的主键,并且设置外键为唯一的(UNIQUE)

-- ==============一对一=====================

create table tb_user(
    id int unsigned  primary key auto_increment comment 'ID',
    name varchar(10) not null comment '姓名',
    gender tinyint unsigned not null comment '性别, 1   2 ',
    phone char(11) comment '手机号',
    degree varchar(10) comment '学历'
) comment '用户信息表';
insert into tb_user values (1,'白眉鹰王',1,'18812340001','初中'),
                        (2,'青翼蝠王',1,'18812340002','大专'),
                        (3,'金毛狮王',1,'18812340003','初中'),
                        (4,'紫衫龙王',2,'18812340004','硕士');
create table tb_user_card(
    id int unsigned  primary key auto_increment comment 'ID',
    nationality varchar(10) not null comment '民族',
    birthday date not null comment '生日',
    idcard char(18) not null comment '身份证号',
    issued varchar(20) not null comment '签发机关',
    expire_begin date not null comment '有效期限-开始',
    expire_end date comment '有效期限-结束',
    user_id int unsigned not null unique comment '用户ID',
    constraint fk_user_id foreign key (user_id) references tb_user(id)
) comment '用户信息表';
insert into tb_user_card values (1,'','1960-11-06','100000100000100001','朝阳区公安局','2000-06-10',null,1),
        (2,'','1971-11-06','100000100000100002','静安区公安局','2005-06-10','2025-06-10',2),
        (3,'','1963-11-06','100000100000100003','昌平区公安局','2006-06-10',null,3),
        (4,'','1980-11-06','100000100000100004','海淀区公安局','2008-06-10','2028-06-10',4);

多对多

案例: 学生 与 课程的关系

关系: 一个学生可以选修多门课程,一门课程也可以供多个学生选择

实现: 建立第三张中间表,中间表至少包含两个外键,分别关联两方主键

--  ==============多对多==================

create table tb_student(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '姓名',
    no varchar(10) comment '学号'
) comment '学生表';
insert into tb_student(name, no) values ('黛绮丝', '2000100101'),('谢逊', '2000100102'),('殷天正', '2000100103'),('韦一笑', '2000100104');
create table tb_course(
   id int auto_increment primary key comment '主键ID',
   name varchar(10) comment '课程名称'
) comment '课程表';
insert into tb_course (name) values ('Java'), ('PHP'), ('MySQL') , ('Hadoop');
create table tb_student_course(
   id int auto_increment comment '主键' primary key,
   student_id int not null comment '学生ID',
   course_id  int not null comment '课程ID',
   constraint fk_courseid foreign key (course_id) references tb_course (id),
   constraint fk_studentid foreign key (student_id) references tb_student (id)
)comment '学生课程中间表';

相关文章:

  • 【Hive入门】Hive行级安全:基于Apache Ranger的细粒度访问控制深度解析
  • ShardingJdbc-水平分库
  • 【ArcGISPro】属性规则--属性联动
  • 【JsonCpp、Muduo、C++11】JsonCpp库、Muduo库、C++11异步操作
  • 根据蓝牙名称自动匹配对应 UI
  • BigemapPro小技巧:调整线方向及延长线
  • IAA-Net:一种实孔径扫描雷达迭代自适应角超分辨成像方法——论文阅读
  • 游戏引擎学习第260天:在性能分析器中实现钻取功能
  • mac m2 安装 hbase
  • 依图科技C++后端开发面试题及参考答案
  • 网站主机控制面板深度解析:cPanel、Plesk 及其他主流选择
  • 2、Kafka Replica机制与ISR、HW、LEO、AR、OSR详解
  • MySQL 中 EXISTS (SELECT 1 FROM ...) 的用法详解
  • Kotlin 中 List 和 MutableList 的区别
  • VS2022 Qt配置Qxlsx
  • Oracle02-安装
  • Kotlin空安全解决Android NPE问题
  • 13.Spring boot中使用Actuator 监控
  • 链表的面试题3找出中间节点
  • git “分离头指针”(detached HEAD) 状态。
  • 经济日报:落实落细更加积极的财政政策
  • 丁薛祥在学习《习近平经济文选》第一卷专题研讨班上强调:深入学习贯彻习近平经济思想,加强党中央对经济工作的集中统一领导
  • 默茨在德国联邦议院第一轮投票中未能当选总理
  • 中年人多活动有助预防阿尔茨海默病
  • “五一”假期余额不足两天,多地景点已约满、限流
  • 德国巴斯夫:关税政策加剧全球市场不确定性,间接影响已显现