-- 創建雇員表:empCREATE TABLE emp( empno INT, -- 員工編號 ename VARCHAR(50), -- 員工姓名 job VARCHAR(50), -- 員工工作 mgr INT, -- 領導編號 hiredate DATE, -- 入職日期 sa...
-- 創建雇員表:emp
CREATE TABLE emp(
empno INT, -- 員工編號
ename VARCHAR(50), -- 員工姓名
job VARCHAR(50), -- 員工工作
mgr INT, -- 領導編號
hiredate DATE, -- 入職日期
sal DECIMAL(7,2), -- 月薪
comm DECIMAL(7,2), -- 獎金
deptno INT -- 部分編號
) ;
-- 添加數據
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,'MARTIN','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,'1987-04-19',3000,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-05-23',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,'1982-01-23',1300,NULL,10);
-- 創建學生信息表:stu
CREATE TABLE stu(
sid CHAR(6), -- 學生學號
sname VARCHAR(50), -- 學生姓名
age INT, -- 學生年齡
gender VARCHAR(50) -- 學生性別
);
-- 添加數據
INSERT INTO stu VALUES('S_1001', 'liuYi', 35, 'male');
INSERT INTO stu VALUES('S_1002', 'chenEr', 15, 'female');
INSERT INTO stu VALUES('S_1003', 'zhangSan', 95, 'male');
INSERT INTO stu VALUES('S_1004', 'liSi', 65, 'female');
INSERT INTO stu VALUES('S_1005', 'wangWu', 55, 'male');
INSERT INTO stu VALUES('S_1006', 'zhaoLiu', 75, 'female');
INSERT INTO stu VALUES('S_1007', 'sunQi', 25, 'male');
INSERT INTO stu VALUES('S_1008', 'zhouBa', 45, 'female');
INSERT INTO stu VALUES('S_1009', 'wuJiu', 85, 'male');
INSERT INTO stu VALUES('S_1010', 'zhengShi', 50, 'female');
INSERT INTO stu VALUES('S_1011', 'xxx', NULL, NULL);
-- 創建部分表:dept
CREATE TABLE dept(
deptno INT, -- 部分編碼
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');
-- 查詢所有列
SELECT * FROM stu;
-- 查詢指定列
SELECT sname,age FROM stu;
-- 查詢性別為女,並且年齡為50的記錄
SELECT * FROM stu WHERE gender='female' AND age=50;
-- 查詢學號為S_1001,或者姓名為liSi的記錄
SELECT * FROM stu WHERE sid='s_1001' OR sname='lisi';
-- 查詢學號為S_1001,S_1002,S_1003的記錄
SELECT * FROM stu WHERE sid IN ('s_1001','s_1002','s_1003' );
-- 查詢學號不是S_1001,S_1002,S_1003的記錄
SELECT * FROM stu WHERE sid NOT IN ('s_1001','s_1002','s_1003' );
-- 查詢年齡為null的記錄
SELECT * FROM stu WHERE age IS NULL;
-- 查詢年齡在20到40之間的學生記錄
SELECT * FROM stu WHERE age BETWEEN 20 AND 40; -- 或者
SELECT * FROM stu WHERE age>=20 AND age<=40;
-- 查詢性別非男的學生記錄
SELECT * FROM stu WHERE gender !='male'; -- 或者
SELECT * FROM stu WHERE gender <>'male';
-- 查詢姓名不為null的學生記錄
SELECT * FROM stu WHERE sname IS NOT NULL; -- 或者
SELECT * FROM stu WHERE NOT sname IS NULL;
-- 查詢姓名由5個字母構成的學生記錄
SELECT * FROM stu WHERE sname LIKE '_____';
-- 查詢姓名由5個字母構成,並且第5個字母為“i”的學生記錄
SELECT * FROM stu WHERE sname LIKE '____i';
-- 查詢姓名以“z”開頭的學生記錄
SELECT * FROM stu WHERE sname LIKE 'z%';
-- 查詢姓名中第2個字母為“i”的學生記錄
SELECT * FROM stu WHERE sname LIKE '_i%';
-- 查詢姓名中包含“a”字母的學生記錄
SELECT * FROM stu WHERE sname LIKE '%a%';
-- 去除重覆記錄
SELECT DISTINCT * FROM stu;
-- 查詢所有列
SELECT * FROM emp;
-- 查看雇員的月薪與佣金之和
SELECT *,sal+comm FROM emp; -- 或者
SELECT *,sal+IFNULL(comm,0) FROM emp;
-- 給列名添加別名
SELECT *, sal+IFNULL(comm,0) AS total FROM emp; -- 或者
SELECT *,sal+IFNULL(comm,0) total FROM emp;
-- 查詢所有學生記錄,按年齡升序排序
SELECT * FROM stu ORDER BY age;
SELECT * FROM stu ORDER BY age ASC;
-- 查詢所有學生記錄,按年齡降序排序
SELECT * FROM stu ORDER BY age DESC;
-- 查詢所有雇員,按月薪降序排序,如果月薪相同時,按編號升序排序
SELECT * FROM emp ORDER BY sal DESC,empno ASC;
-- 查詢emp表中記錄數
SELECT COUNT(*) AS cnt FROM emp;
-- 查詢emp表中有佣金的人數
SELECT COUNT(comm) AS cnt FROM emp;
-- 查詢emp表中月薪大於2500的人數
SELECT COUNT(*) AS cnt FROM emp WHERE sal > 2500;
-- 統計月薪與佣金之和大於2500元的人數
SELECT COUNT(*) AS cnt FROM emp WHERE sal+IFNULL(comm,0) > 2500;
-- 查詢有佣金的人數,以及有領導的人數
SELECT COUNT(comm), COUNT(mgr) FROM emp;
-- 查詢所有雇員月薪和
SELECT SUM(sal) FROM emp;
-- 查詢所有雇員月薪和,以及所有雇員佣金和
SELECT SUM(sal),SUM(comm) FROM emp;
-- 查詢所有雇員月薪+佣金和
SELECT SUM(sal+IFNULL(comm,0)) FROM emp;
-- 統計所有員工平均工資
SELECT AVG(sal) FROM emp;
-- 查詢最高工資和最低工資
SELECT MAX(sal),MIN(sal) FROM emp;
-- 查詢每個部門的部門編號和每個部門的工資和
SELECT deptno, SUM(sal) FROM emp GROUP BY deptno;
-- 查詢每個部門的部門編號以及每個部門的人數
SELECT deptno, COUNT(*) AS cnt FROM emp GROUP BY deptno;
-- 查詢每個部門的部門編號以及每個部門工資大於1500的人數
SELECT deptno, COUNT(*) FROM emp WHERE sal>1500 GROUP BY deptno;
-- 查詢工資總和大於9000的部門編號以及工資和
SELECT deptno, SUM(sal) FROM emp GROUP BY deptno HAVING SUM(sal) > 9000;
--
-- 查詢所有列
SELECT * FROM dept;
-- 創建學生信息表 student
CREATE TABLE student(
sid INT, -- 學號
sname VARCHAR(20), -- 姓名
age INT, -- 年齡
scores INT, -- 成績
gender VARCHAR(6), -- 性別
birthday DATE -- 出生日期
);
-- 添加數據
INSERT INTO student(sid,sname,age,scores,gender) VALUES (0001,'張華',23,49,'女');
INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0007,'孫萌',20,59,'女','1997-02-24');
INSERT INTO student(sid,sname,age,scores,gender) VALUES (0002,'李四',28,69,'男');
INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0009,'吳笑清',22,68,'女','1994-11-02');
INSERT INTO student(sid,sname,age,scores,gender) VALUES (0003,'王小花',26,29,'女');
INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0004,'趙六',22,85,'男','1990-12-17');
INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0005,'錢雨',21,87,'女','1992-03-30');
INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0006,'李四',25,83,'男','1996-05-13');
INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0008,'周董',27,99,'男','1995-07-23');
INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (00010,'鄭源',27,79,'男','1994-08-23');
-- 查詢所有列
SELECT * FROM student;
-- 刪除表 student
DROP TABLE student;
-- 去除重覆記錄
SELECT DISTINCT * FROM student;
-- 顯示mydb下的所有表
USE mydb; SHOW TABLES;