当前位置:网站首页>Multi table query of MySQL - multi table relationship and related exercises
Multi table query of MySQL - multi table relationship and related exercises
2022-07-03 13:19:00 【Levi Bebe】
1. Multi table relation
MySQL The relationship between multiple tables can be summarized as bit : one-on-one 、 One to many or many to one 、 Many to many .
1.1 One to many foreign key constraints
1.1.1 The creation of
1.1.2 Insertion of foreign key constraints 、 Delete operation
Insert : Insert data that conforms to the format randomly in the main table , The secondary table needs to rely on the primary key column of the primary table .
Delete : When the data of the master table is dependent on the slave table , Both master and slave tables have primary keys , The primary table cannot delete the related primary key , Otherwise, you can delete ; The data from the table can be deleted at will .
1.1.3 Delete foreign key constraint key
1.2 Many to many foreign key constraints
1.3 Multi-table query
1.3.1 Cross link query
The content source of Cartesian product :
1.3.2 Internal connection query
1.3.3 External connection query
1.3.4 Multi table joint query
1.3.5 Self association query
2. Case study
Create table :
-- establish test1 database
CREATE DATABASE test1;
-- Choose to use test1 database
USE test1
-- Create department table
CREATE TABLE dept(
deptno INT PRIMARY KEY, -- Department number
dname VARCHAR(20), -- Department name
loc VARCHAR(20) -- Department Address
)
INSERT INTO dept VALUES (10,'accounting','new york');
INSERT INTO dept VALUES (20,'research','dallas');
INSERT INTO dept VALUES (30,'sales','chicago');
INSERT INTO dept VALUES (40,'operations','boston');
-- Create an employee table
CREATE TABLE emp(
empno INT PRIMARY KEY, -- Employee number
ename VARCHAR(10), -- Employee name
job VARCHAR(9), -- Staff work
mgr INT, -- Employee index leader number
hiredate date, -- Entry time
sal DOUBLE, -- Wages
comm DOUBLE, -- Bonus
deptno INT -- Corresponding dept Table foreign key
);
-- Add the main foreign key relationship between department and employee
ALTER TABLE emp ADD CONSTRAINT FOREIGN KEY emp(deptno) REFERENCES dept (deptno);
INSERT INTO emp VALUES (7369,'smith','clerk',7902,'1980-12-17',800,null,20);
INSERT INTO emp VALUES (7499,'allen','salesman',7698,'1981-02-20',1600,300,30);
INSERT INTO emp VALUES (7521,'ward','salesman',7698,'1981-02-22',1250,500,30);
INSERT INTO emp VALUES (7566,'jones','manager',7839,'1981-04-02',2975,null,20);
INSERT INTO emp VALUES (7654,'marthin','salesman',7698,'1981-09-28',1250,1400,30);
INSERT INTO emp VALUES (7698,'blake','manager',7839,'1981-05-01',2850,null,30);
INSERT INTO emp VALUES (7782,'clark','manager',7839,'1981-06-09',2450,null,10);
INSERT INTO emp VALUES (7788,'scott','analyst',7566,'1981-07-03',5000,null,20);
INSERT INTO emp VALUES (7839,'king','president',null,'1981-11-17',5000,null,10);
INSERT INTO emp VALUES (7844,'turner','salesman',7698,'1981-09-08',1500,0,30);
INSERT INTO emp VALUES (7876,'adams','clerk',7788,'1987-07-13',1100,null,20);
INSERT INTO emp VALUES (7900,'james','clerk',7698,'1981-12-03',950,null,30);
INSERT INTO emp VALUES (7902,'ford','analyst',7566,'1981-12-03',3000,null,20);
INSERT INTO emp VALUES (7934,'miller','clerk',7782,'1981-01-23',1300,null,10);
-- Create a pay scale
CREATE TABLE salgrade(
grade INT, -- Grade
losal DOUBLE, -- minimum wage
hisal double -- Maximum wage
);
INSERT INTO salgrade VALUES (1,700,1200);
INSERT INTO salgrade VALUES (2,1201,1400);
INSERT INTO salgrade VALUES (3,1401,2000);
INSERT INTO salgrade VALUES (4,2001,3000);
INSERT INTO salgrade VALUES (5,3001,9999);
practice :
-- practice
-- 1、 Return the name of the Department that owns the employee 、 Department number
SELECT DISTINCT a.dname,a.deptno FROM dept a join emp b on a.deptno=b.deptno;
-- 2、 The salary level is redundant smith Staff level
SELECT * FROM emp WHERE sal > (SELECT sal FROM emp WHERE ename = 'smith');
-- 3、 Returns the names of the employee and the manager
SELECT a.ename,b.ename FROM emp a join emp b on a.empno=b.mgr;
-- 4、 Return the name of the employee and his / her manager whose employment date is earlier than that of his / her manager .
SELECT a.ename,a.hiredate,b.ename,b.hiredate FROM emp a join emp b on a.empno=b.mgr and a.hiredate > b.hiredate;
-- 5、 Return the name of the employee and the name of his / her department .
SELECT a.ename,b.dname FROM emp a join dept b on a.deptno = b.deptno;
-- 6、 Return to work clerk The name of the employee working and the name of the Department .
SELECT a.ename,a.job,b.dname FROM emp a join dept b on a.deptno=b.deptno and job = 'clerk';
-- 7. Returns the department number and its minimum wage
SELECT deptno,MIN(sal) FROM emp GROUP BY deptno;
-- 8. Return the names of all employees in the sales department .
SELECT a.ename,b.dname FROM emp a join dept b on a.deptno=b.deptno and b.dname='sales';
-- 9. Return to employees who earn more than the average wage
SELECT ename,sal FROM emp WHERE sal > (SELECT AVG(sal) FROM emp);
-- 10、 Return to scott Employees who do the same job .
SELECT * FROM emp WHERE job = (SELECT job FROM emp WHERE ename = 'scott');
-- 11、 Return above 30 Employee information about the salary level of department employees ;
SELECT * FROM emp WHERE sal > ALL(SELECT sal FROM emp WHERE deptno=30);
-- 12、 Return to the employee's job and the minimum wage for the job ;
SELECT job,MIN(sal) FROM emp GROUP BY job;
-- 13、 Calculate the employee's annual salary , And in order of annual salary
SELECT ename,(sal * 12 + ifnull(comm,0)) as yeas_sal FROM emp ORDER BY yeas_sal DESC;
-- 14、 Return the salary of the employee whose salary is at the fourth level
SELECT * FROM emp
WHERE sal BETWEEN (SELECT losal FROM salgrade WHERE grade = 4) AND (SELECT hisal FROM salgrade WHERE grade = 4);
-- 15、 Return the name of the employee whose salary is level II 、 Department location
-- Here, three tables are combined into Cartesian product , Then use the condition to delete
SELECT
*
FROM dept a
JOIN emp b on a.deptno=b.deptno
JOIN salgrade c on grade = 2 and b.sal >= c.losal and b.sal <= c.hisal;
--------------------
SELECT
*
FROM dept a , emp b ,salgrade c
WHERE a.deptno=b.deptno
AND grade = 2 and b.sal >= c.losal and b.sal <= c.hisal;
3. Reference resources
https://www.bilibili.com/video/BV1iF411z7Pu?p=82&spm_id_from=pageDriver
边栏推荐
- Logback 日志框架
- Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
- json序列化时案例总结
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
- [Exercice 5] [principe de la base de données]
- 剑指 Offer 17. 打印从1到最大的n位数
- Create a dojo progress bar programmatically: Dojo ProgressBar
- Loan calculator my pressure is high
- 这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
- 2022-02-10 introduction to the design of incluxdb storage engine TSM
猜你喜欢
道路建设问题
Finite State Machine FSM
高效能人士的七个习惯
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]
【数据库原理及应用教程(第4版|微课版)陈志泊】【第六章习题】
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
Annotation and reflection
regular expression
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
人身变声器的原理
随机推荐
Task5: multi type emotion analysis
[exercise 5] [Database Principle]
Cadre de logback
PostgreSQL installation
2022-02-13 plan for next week
Flink SQL knows why (19): the transformation between table and datastream (with source code)
mysql更新时条件为一查询
MySQL_ JDBC
C graphical tutorial (Fourth Edition)_ Chapter 17 generic: genericsamplep315
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
Kotlin - 改良装饰者模式
Flink SQL knows why (7): haven't you even seen the ETL and group AGG scenarios that are most suitable for Flink SQL?
Seven habits of highly effective people
OpenHarmony应用开发之ETS开发方式中的Image组件
Mysql database basic operation - regular expression
Sword finger offer 12 Path in matrix
对业务的一些思考
Logback 日志框架
Dojo tutorials:getting started with deferrals source code and example execution summary
Useful blog links