首页 > 数据库技术 > 详细

MYSQL表关系

时间:2019-07-09 23:45:13      阅读:110      评论:0      收藏:0      [点我收藏+]

表关系

一、外键--Foreign Key

?   作用:约束当前表的某列值必须取自于另一张表的主键列值

?   外键所在的列称之为“外键列”

?   外键所在的表称之为“外键表”或“子表”

?   被外键列所引用的表称之为“主表”或“主键表”

  语法:

  1.创建表的同时指定外键

  create table xxx(

  字段 类型,

  ...,

  constraint 外键名 foreign key(字段)

  references 主键表(主键列)

  )

 

-- 创建course表:id,cname,cduration
 create  table course(
    id int primary key auto_increment,
    cname varchar(30) not null,
    cduration int not null
 )
-- 创建teacher表:id,name,age,gender,hobby,course
-- course_id是外键,引用自course表的主键id
 create table teacher(
    id int primary key auto_increment,
    name varchar(30) not null,
    age int not null,
    gender varchar(2) not null,
    hobby varchar(50) not null,
    course_id int,
    -- 外键约束
    constraint fk_course_teacher
    foreign key(course_id) -- 设置外键
    references course(id) -- 参照主键(参照course的id值)参照谁就写谁
 )

?   2.对已有表增加外键

?   alter table 表名

?   add constraint 外键名

?   foreign key(字段)

?   references 主键表(主键)

-- 创建student表:id,name,age,gender,school,class_id,major_id
    create table student(
    id int primary key auto_increment,
    name varchar(30) not null,
    age int not null,
    gender varchar(2) not null,
    school varchar(100) not null,
    class_id int not null,
    major_id int not null
 );
-- 创建classinfo表:id,classname,status
    create table classinfo(
    id int primary key auto_increment,
    classname varchar(30) not null,
    status varchar(2) not null
    );
-- 更新student表结构,增加外键在class_id,引用子classinfo表的主键id
    alter table student
    add constraint fk_class_student
    foreign key(class)
    references classinfo(id)

  3.删除外键

  alter table 表名 drop foreign key 外键名;

  4.查看外键名

  show create table 表名;

二、级联操作

? 1.语法:

  alter table 表名

  add constraint 外键名

  foreign key(字段)

  references 主键表(主键)

  on delete 级联操作

  on update 级联操作

-- 为score表中的stu_id增加外键,并设置级联操作
    alter table score
    add constraint fk_student_score
    foreign key(stu_id)
    references student(id)
    on delete cascade
    on update cascade

? 2.级联操作取值

  (1)cascade

  数据级联删除、更新(主表有啥动作,子表跟着有啥动作)

  (2)restrict(默认)

  子表中有关联数据,那么主表中就不允许做删除、更新。

  (3)set null

  主表删除数据时子表中的相关数据会设置为null

三、表连接查询

? 1.交叉连接 - 笛卡尔积

? 查询teacher和course表中所有的数据

?   select * from teacher,course;

?   select name,courseid,course.id,cname from teacher,course where teacher.courseid=course.id;

? 2.内连接:在关联的两张表中,把满足条件的数据筛选出来

?   select 字段,... ...

?   from 表1

  inner join 表2

?  on 条件

?  inner join 表3

?  on 条件

-- 使用内连接查询teacher和course 表中的数据(姓名,年龄,课程名称,课时)
    select t.name,t.age,c.cname,c.cduration
    from teacher as t
    inner join course as c
    on t.course_id=c.id;
 -- 查询学员的姓名,年龄,所在班级名称,专业名称 并筛选出1902的学员
    select s.name,s.age,c.classname,m.m_name
    from student as s
    inner join classinfo as c
    on s.class_id=c.id
    inner join major as m
    on s.major_id=m.id
    where c.classname=1902;
-- 查询学员的姓名,毕业院校,所在班级,考试科目,考试成绩
    select s.name,s.school,c.classname,course.cname,sc.score
    from student as s
    inner join classinfo as c
    on s.class_id=c.id
    inner join score as sc
    on s.id=sc.stu_id
    inner join course
    on sc.course_id = course.id;

 

 

?

MYSQL表关系

原文:https://www.cnblogs.com/maplethefox/p/11161150.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!