如何通过练习精通MySQL数据库关联操作?

当然,我可以帮助你进行MySQL数据库的关联练习。请告诉我具体的问题或需求。

MySQL数据库的关联练习涉及多个表之间的数据查询和整合,以下是详细的步骤和小单元表格,帮助你理解如何进行多表关联查询:

如何通过练习精通MySQL数据库关联操作?

创建数据库和数据表

1、创建数据库

   CREATE DATABASE test;
   USE test;

2、创建DEPT表(部门表)

如何通过练习精通MySQL数据库关联操作?

   CREATE TABLE DEPT (
       DEPTNO INT(2) PRIMARY KEY,
       DNAME VARCHAR(14),
       LOC VARCHAR(13)
   );
   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');

3、创建EMP表(员工表)

   CREATE TABLE EMP (
       EMPNO INT(4) PRIMARY KEY,
       ENAME VARCHAR(10),
       JOB VARCHAR(9),
       MGR INT(4),
       HIREDATE DATE,
       SAL DECIMAL(7, 2),
       COMM DECIMAL(7, 2),
       DEPTNO INT(2),
       FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO)
   );
   INSERT INTO EMP VALUES (7369, 'SMITH', 'CLERK', 7902, '19801217', 800, NULL, 20);
   INSERT INTO EMP VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '1981220', 1600, 300, 30);
   INSERT INTO EMP VALUES (7521, 'WARD', 'SALESMAN', 7698, '1981222', 1250, 500, 30);
   INSERT INTO EMP VALUES (7566, 'MARTIN', 'SALESMAN', 7698, '1981228', 1250, 1400, 30);
   INSERT INTO EMP VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '1981315', 1500, 1400, 30);
   INSERT INTO EMP VALUES (7698, 'BLAKE', 'MANAGER', 7839, '1981115', 2850, NULL, 30);
   INSERT INTO EMP VALUES (7782, 'CLARK', 'MANAGER', 7839, '1981115', 2450, NULL, 10);
   INSERT INTO EMP VALUES (7788, 'SCOTT', 'ANALYST', 7566, '1982129', 3000, NULL, 20);
   INSERT INTO EMP VALUES (7839, 'KING', 'PRESIDENT', NULL, '19811117', 5000, NULL, 10);
   INSERT INTO EMP VALUES (7844, 'TURNER', 'SALESMAN', 7698, '198198', 1500, 0, 30);
   INSERT INTO EMP VALUES (7876, 'ADAMS', 'CLERK', 7788, '1983112', 1100, NULL, 20);
   INSERT INTO EMP VALUES (7900, 'JAMES', 'CLERK', 7698, '1981123', 950, NULL, 30);
   INSERT INTO EMP VALUES (7902, 'FORD', 'ANALYST', 7566, '1981123', 3000, NULL, 20);
   INSERT INTO EMP VALUES (7934, 'MILLER', 'CLERK', 7782, '1982123', 1300, NULL, 10);

单表查询练习

SQL语句 描述
SELECT * FROM EMP WHERE DEPTNO=30; 选择部门30中的所有员工;
SELECT ENAME,EMPNO,DEPTNO FROM EMP WHERE JOB='CLERK'; 列出所有办事员的姓名,编号和部门编号;
SELECT ENAME FROM EMP WHERE IFNULL(COMM,0) > SAL; 找出奖金高于工资的员工;
SELECT DISTINCT JOB FROM EMP; 收取不同的工作;
SELECT DISTINCT JOB FROM EMP WHERE COMM IS NOT NULL; 收取收取奖金的不同工作;
SELECT ENAME FROM EMP WHERE IFNULL(COMM,0) > SAL*0.6; 找出奖金高于工资的60%的员工;
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC = 'CHICAGO'); 在SALES部门工作的员工,假定不知道部门编号;
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC = 'DALLAS') AND JOB 'CLERK'; 部门10中所有经理和所有的办事员,以及部门20中所有不是办事员的员工;
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC = 'DALLAS') OR JOP = 'CLERK'; 部门10中所有经理和所有的办事员,以及部门20中所有不是办事员的员工;
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC LIKE '%S%'); 求部门名称中带字符’S’的所有员工的详细资料;
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC LIKE '%S%') AND SAL > 2000 AND JOB = 'SALESMAN'; 列出薪金超过2000的所有雇员的详细信息,并按部门号升序排列;
SELECT * FROM EMP WHERE YEAR(HIREDATE) 早于1982年受雇的所有雇员的详细信息;
SELECT * FROM EMP WHERE YEAR(HIREDATE) = (YEAR(CURRENT_DATE) 30); 早于受雇12年的所有雇员的详细信息;
SELECT * FROM EMP WHERE EXTRACT(MONTH FROM hiredate) = 12 AND extract(day from hiredate) = 31; 在十二月三十一日受雇的所有雇员的详细信息;
SELECT * FROM EMP WHERE DEPTNO = (SELECT DEPTNO FROM DEPT WHERE DNAME = 'SALES'); 在SALES部门工作的所有员工的详细信息;
SELECT * FROM EMP WHERE DEPTNO = (SELECT DEPTNO FROM DEPT WHERE DNAME = 'SALES') AND SAL > ALLAN.SAL; 薪金高于公司平均薪金的所有雇员的详细信息;
SELECT * FROM EMP WHERE DEPTNO = (SELECT DEPTNO FROM DEPT WHERE DNAME = 'SALES') AND SAL 薪金低于公司平均薪金的所有雇员的详细信息;
SELECT * FROM EMP WHERE DEPTNO = (SELECT DEPTNO FROM DEPT WHERE DNAME = 'SALES') AND SAL >= ALLAN.SAL; 薪金等于或高于公司平均薪金的所有雇员的详细信息;
SELECT * FROM EMP WHERE DEPTNO = (SELECT DEPTNO FROM DEPT WHERE DNAME LIKE '%S%'); 求部门名称中带字符'S'的所有员工的详细资料;
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC LIKE '%S%'); 求部门名称中带字符'S'的所有员工的详细资料;
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC LIKE '%S%') AND SAL > ALLAN.SAL; 求部门名称中带字符'S'且薪金高于公司平均薪金的所有雇员的详细资料;
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTno FROM DEpt where loc like '%S%') and SAL 求部门名称中带字符'S'且薪金低于公司平均薪金的所有雇员的详细资料;
SELECT * FROM EMP WHERE DEPTno IN (SELECT DEPTno FROM DEpt where loc like '%S%') and SAL >= Allan.sal; 求部门名称中带字符'S'且薪金等于或高于公司平均薪金的所有雇员的详细资料;
SELECT * FROM EMP WHERE DEPTno IN (SELECT DEPTno FROM Dept where loc like '%S%') and SAL >= ALLan.sal; 求部门名称中带字符'S'且薪金等于或高于公司平均薪金的所有雇员的详细资料;
SELECT * FROM EMP WHERE DEPTno IN (SELECT DEPTno FROM Dept where loc like '%S%') and SAL >= ALLan.sal; 求部门名称中带字符'S'且薪金等于或高于公司平均薪金的所有雇员的详细资料;
SELECT * FROM EMP WHERE DEPTno IN (SELECT DEPTno FROM Dept where loc like '%S%') and SAL >= ALLan.sal; 求部门名称中带字符'S'且薪金等于或高于公司平均薪金的所有雇员的详细资料;
SELECT * FROM EMP WHERE DEPTno IN (SELECT DEPTno FROM Dept where loc like '%S%') and SAL >= ALLan.sal; 求部门名称中带字符'S'且薪金等于或高于公司平均薪金的所有雇员的详细资料;
SELECT * FROM EMP WHERE DEPTno IN (SELECT DEPTno FROM Dept where loc like '%S%') and SAL >= ALLan.sal; 求部门名称中带字符'S'且薪金等于或高于公司平均薪金的所有雇员的详细资料;
SELECT * FROM EMP WHERE DEPTno IN (SELECT DEPTno FROM Dept where loc like '%S%') and SAL >= ALLan.sal; 求部门名称中带字符'S'且薪金等于或高于公司平均薪金的所有雇员的详细资料;

多表关联查询练习

SQL语句 描述
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTNO = d.DEPTNO AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURSE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTNO = d.DEPTNO AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURSE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTNO = d.DEPTNO AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURSE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTNO = d.DEPTNO AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURSE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTNO = d.DEPTNO AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURSE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTNO = d.DEPTNO AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURSE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURSE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURSE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURSE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURSE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURCE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURCE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURCE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURCE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURCE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURCE”的学生的姓名,课程名称及成绩;
SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e.JOB = 'SALESMAN'; 查询学生、课程、成绩表中选修“COURCE”的学生的姓名,课程名称及成绩;

| `SELECT e.ENAME, e.JOB, d.DNAME, d.LOC FROM EMP e, DEPT d WHERE e.DEPTno = d.DEPTno AND e

如何通过练习精通MySQL数据库关联操作?

原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1216127.html

本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。

(0)
未希新媒体运营
上一篇 2024-10-15 03:14
下一篇 2024-10-15 03:17

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

产品购买 QQ咨询 微信咨询 SEO优化
分享本页
返回顶部
云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购 >>点击进入