CREATE TABLE dept(
deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
dname VARCHAR(20) NOT NULL DEFAULT "",
loc VARCHAR(13) NOT NULL DEFAULT ""
);
INSERT INTO dept VALUES(10, 'ACCOUNTING', 'NEW YORK'),
(20, 'RESEARCH', 'DALLAS'),
(30, 'SALES', 'CHICAGO'),
(40, 'OPERATIONS', 'BOSTON');
SELECT * FROM dept;
-- 员工表
CREATE TABLE emp
(empno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
ename VARCHAR(20) NOT NULL DEFAULT "",
job VARCHAR(9) NOT NULL DEFAULT "",
mgr MEDIUMINT UNSIGNED ,
hiredate DATE NOT NULL,
sal DECIMAL(7,2) NOT NULL,
comm DECIMAL(7,2) ,
deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0
);
-- 添加测试数据
INSERT INTO emp VALUES(7369, 'SMITH', 'CLERK', 7902, '1990-12-17', 800.00,NULL , 20),
(7499, 'ALLEN', 'SALESMAN', 7698, '1991-2-20', 1600.00, 300.00, 30),
(7521, 'WARD', 'SALESMAN', 7698, '1991-2-22', 1250.00, 500.00, 30),
(7566, 'JONES', 'MANAGER', 7839, '1991-4-2', 2975.00,NULL,20),
(7654, 'MARTIN', 'SALESMAN', 7698, '1991-9-28',1250.00,1400.00,30),
(7698, 'BLAKE','MANAGER', 7839,'1991-5-1', 2850.00,NULL,30),
(7782, 'CLARK','MANAGER', 7839, '1991-6-9',2450.00,NULL,10),
(7788, 'SCOTT','ANALYST',7566, '1997-4-19',3000.00,NULL,20),
(7839, 'KING','PRESIDENT',NULL,'1991-11-17',5000.00,NULL,10),
(7844, 'TURNER', 'SALESMAN',7698, '1991-9-8', 1500.00, NULL,30),
(7900, 'JAMES','CLERK',7698, '1991-12-3',950.00,NULL,30),
(7902, 'FORD', 'ANALYST',7566,'1991-12-3',3000.00, NULL,20),
(7934,'MILLER','CLERK',7782,'1992-1-23', 1300.00, NULL,10);
SELECT * FROM emp;
-- 工资级别
#工资级别表
CREATE TABLE salgrade
(
grade MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
losal DECIMAL(17,2) NOT NULL,
hisal DECIMAL(17,2) NOT NULL
);
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);
SELECT * FROM salgrade;
SELECT * FROM dept;
SELECT * FROM emp;
-- 显示每个部门的平均工资和最低工资
select avg(sal),min(sal),deptno from emp group by deptno;
-- 显示每个部门的每种岗位的平均工资和最低工资
select avg(sal),min(sal),deptno,job from emp group by deptno,job;
select avg(sal),min(sal),deptno,job from emp group by job,deptno;
-- 显示平均工资低于2000的部门号和它的平均工资
select avg(sal),deptno from emp group by deptno having avg(sal)<2000
select avg(sal) as avg_sal,deptno from emp group by deptno having avg_sal<2000
-- 显示各个部门的平均工资
select avg(sal),deptno from emp group by deptno;
-- dual 压元表
select INSTR('CHENGSHENGJIE','JIE') from dual;
-- 转大写
select UCASE(ename) from emp;
-- 替换
select ename,REPLACE(job,'MANAGER','经理') from emp;
-- 取指定长度字符
select SUBSTRING(ename,1,2) from emp;
select concat(lcase(substring(ename,1,1)),substring(ename,2)) from emp;
-- 绝对值
select abs(-2) from dual;
-- 十进制转化为二进制
select bin(8) from dual;
-- 向上取整
select ceiling(1.8) from dual;
-- 向下取整
select floor(1.2) from dual;
-- 进制转换,10进制的8转化为2进制
select conv(8,10,2) from dual;
-- 保存小数点后几位
select format(87.569856,3) from dual;
-- 求最小值
select least(0.1,-1,6,89) from dual;
-- 随机数,rand(seed),当不给seed时,每次产生一个随机数,加上一个种子每次产生的随机数是一样的
select rand(2) from dual;