MySQL5.7 database-table join & subquery & foreign key

Table join

  • When the column of the query result comes from multiple tables, you need to join the multiple tables into a large data set, and then select the appropriate column to return to mysql
  • At this time, the tables need to be connected

Internal connection

  • The inner join only selects records that match each other in the two tables
select * from1 inner join2 on1.=2.-- 显示学生的所有信息,但只显示班级名称
select s.*, c.name from students s inner join classes c on s.id=c.id;

-- 将班级名称显示在第一列
select c.name, s.* from students s inner join classes c on s.id=c.id;

-- 查询 有能够对应班级的学生以及班级信息,按照班级进行排序
select c.name, s.* from students s inner join classes c on s.cls_id = c.id order by c.name asc;

-- 当同一个班级时,按照学生的id进行从小到大排序
select c.name, s.* from students s inner join classes c on s.cls_id = c.id order by c.name asc, s.id asc;

Insert picture description here
Insert picture description here
Insert picture description here

Left connect

The result of the query is the data matched by the two tables, the data held in the left table, and the data that does not exist in the right table are filled with null

select * from1 left join2 on1.=2.-- students表左连接classes表 并查看班级为null的数据
select * from students s left join classes c on s.cls_id=c.id having s.cls_id is null;

-- 左连接 并且 查询 s.cls_id=1 并且 s.name="small-j" 的数据
select * from students s left join classes c on s.cls_id=c.id having s.cls_id=1 and s.name="small-j";

Insert picture description here

Right connection

The query result is the matched data of the two tables. The data held in the right table is filled with null for data that does not exist in the left table.

select * from 表1 right join 表2 on 表1.列 = 表2.列;

Subquery

In some cases, when performing a query, the required condition is the result of another select statement. At this time, a subquery is used

select * fromwhere(子查询语句)
-- 查询出students中身高最高的男生。显示名字和身高
select s.name, s.high from students s where high=(select max(high) from students) and gender="男";

-- 查询出高于平均身高的学生信息
select * from students where high>(select avg(high) from students);

-- 查询学生班级号cls_id能够对应的学生信息
select * from students where cls_id in (select id from students);

-- 查询最大年龄的女生的id
select * from students where id=(select max(id) from students where gender="女") and  gender="女";

Insert picture description here

Self-association

Simply understand that you can connect and query yourself

-- 查询广东省下的所有广东市
select * from cities c inner join provinces p on c.provinceid=p.provinceid having p.province="广东省";

-- 查询广东省下的所有广东市
-- 自关联
select * from areas a inner join areas b on a.id=b.pid having a.name="广东";

Insert picture description here
Insert picture description here

Foreign key

Introduction to foreign keys

  • MySQL's foreign key (foreing key) is a special field of the table. For two tables with an association relationship, the table where the primary key of the associated field is located is the primary table (parent table), and the table where the foreign key is located is the secondary table (child table).
  • Note: The primary key cannot contain null values, but null values ​​are allowed in the foreign key, that is, as long as each non-null value of the foreign key appears in the specified primary key, the content of the foreign key is correct.

Set foreign key constraints when creating a table

  • When creating a foreign key, you must delete the secondary table before deleting the primary table.
  • Create a slave table when the master table needs to exist.
  • The foreign key association of the secondary table must be the primary key of the primary table, and the types of the primary key and foreign key must be consistent.
[constraint 外键名] foreign key (字段名 [,字段名2, ...]) references <主表名> 主键列1 [, 主键列2, ...]
-- 创建班级表
create table classes(
    id int(4) not null primary key,
    name varchar(36)
);

-- 创建学生表
create table student(
    sid int(4) not null primary key,
    sname varchar(30),
    cid int(4) not null
);

-- 创建直接含有外键关系的学生表
create table student(
  	sid int(4) not null primary key,
  	sname varchar(30),
  	cid int(4) not null,
  	constraint pk_id foreign key (cid) references classes(id)
);

-- 通过alter来添加外键关系
alter table student add constraint pk_id foreign key (cid) references classes(id);

-- 删除外键约束
alter table student drop foreign key pk_id;

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_37662827/article/details/113056373