<strong> </strong><pre name="code" class="html">SELECT comm,NVL(comm,0) AS m FROM emp1 SELECT MAX(sal)AS 最大工资,MIN(sal)AS 最小工资,SUM(sal)AS 总工资,COUNT(*)AS 总人数,AVG(sal)AS 平均工资 FROM emp1 SELECT COUNT(DISTINCT JOB) FROM emp; 1、SQL中select子句分组 group by 2、sql中的连接查询 笛卡尔积:【A(m,n),B(x,y)】============》A*B=[m*x,n+y]:行相乘,列相加 交叉连接:select * FROM A CROSS JOIN B-------------------------------------没有意义,理论要求 自然连接:SELECT * FROM emp NATURAL JOIN dept------------------------------不常用【要求连接的两个表有相同的列名】 等值连接:SELECT * FROM EMP e ,DEPT d WHERE e.DEPTNO=d.DEPTNO--------------常用【无需要求】 内连接:inner join on A,B 外连接: left outer join ---------------left join right outer join ---------------right join 全连接: full join 3、子查询 4、集合查询 集合运算操作符 UNION:(并集)返回两个集合去掉重复值的所有的记录 UNION ALL:(并集)返回两个集合去掉重复值的所有的记录 INTERSECT:(交集)返回两个集合的所有记录,重复的只取一次 MINUS:(差集)返回属于第一个集合,但不属于第二个集合的所有 记录 集合运算中各个集合必须有相同的列数,且类型一致,集合运算的结 果将采用第一个集合的表头作为最终的表头,order by必须放在每个 集合后 函数: 1、日期函数 SELECT SYSDATE d FROM dual;--取当前日期 SELECT trunc(SYSDATE) d FROM dual;--日期截断函数 SELECT to_char(SYSDATE,'yyyy') d FROM dual; --日期转换为字符 SELECT to_char(SYSDATE,'yy') d FROM dual; --日期转换为字符 SELECT to_char(SYSDATE,'year') d FROM dual; --日期转换为字符 SELECT to_char(SYSDATE,'mm') d FROM dual; --日期转换为字符 SELECT to_char(SYSDATE,'mon') d FROM dual; --日期转换为字符 SELECT to_char(SYSDATE,'d') d FROM dual; --日期转换为字符 SELECT to_char(SYSDATE,'dd') d FROM dual; --日期转换为字符 SELECT to_char(SYSDATE,'ddd') d FROM dual; --日期转换为字符 SELECT to_char(SYSDATE,'q') d FROM dual; --日期转换为字符 SELECT to_char(SYSDATE,'yyyy-mm-dd') d FROM dual--日期转换为字符 SELECT to_char(SYSDATE,'day') d FROM dual--日期转换为字符 SELECT to_char(SYSDATE-1)d FROM dual;--1天前 SELECT to_char(SYSDATE-1/24,'yyyy-mm-dd HH24:MI:SS')d FROM dual;--1小时前 SELECT to_char(SYSDATE-1/1440,'yyyy-mm-dd HH24:MI:SS')d FROM dual;--1分钟前 SELECT add_months(SYSDATE,-1)d FROM dual;--1月前 SELECT add_months(SYSDATE,1)d FROM dual;--1月后 SELECT add_months(SYSDATE,12)d FROM dual;--1年后 SELECT months_between(SYSDATE,SYSDATE+100) d FROM dual; SELECT next_day(SYSDATE,'星期三')d FROM dual; SELECT last_day(add_months(SYSDATE,-1))d FROM dual; 2、字符函数 SELECT LENGTH('aa张aaa')s FROM dual; SELECT chr(97)s FROM dual; SELECT chr(65)s FROM dual; SELECT chr(48)s FROM dual; SELECT '2'||chr(32)||'4's FROM dual; -------------------------------- SELECT ASCII('唐')s FROM dual; ------------------------------------------------ SELECT INSTR('12abcdeabf234ab6abfffab','ab',4,30)s FROM dual; SELECT RPAD('abc',6,'-') s FROM dual; SELECT RTRIM(' 2 34 ')s FROM dual; SELECT REPLACE('2 34 ',' ','')s FROM dual; 3、其他函数 SELECT DECODE(1,2,3,4,5,6,7,8,9,9999)N FROM DUAL; SELECT empno, ename, DECODE(JOB,'CLERK','CC','SALESMAN','SS','MANAGER','MM','ANALYST','AA','PRESIDENT','PP','KK') JOB, mgr, hiredate, nvl(sal,0) sal, comm, deptno FROM emp SELECT empno, ename, CASE WHEN sal>1000 AND sal<2000 THEN 'A' WHEN sal BETWEEN 2000 AND 3000 THEN 'B' WHEN sal>=3000 AND sal<5000 THEN 'C' ELSE 'N' END SAL, mgr, hiredate, JOB, comm, deptno FROM emp SELECT empno, ename, CASE JOB WHEN 'CLERK'THEN 'CC1' WHEN 'SALESMAN'THEN 'S1S' WHEN 'MANAGER'THEN 'M1M'WHEN 'ANALYST'THEN 'A1A'WHEN 'PRESIDENT'THEN 'P1P' ELSE 'KK' END JOB, mgr, hiredate,NVL(sal,0) sal,comm,deptno FROM emp -------------------------------------------------------------------------------------------------------------------- 子查询: 嵌套在SELECT子句中的select; 嵌套最多不超过255层 子查询必须用括弧分割--------------------------* 所有的子查询效率都比较低,一般可以转化为连接查询 子查询的分类: 按照嵌入的位置可以分为以下几种: select from where 按照返回结果分: 单行单列 单行多列 多行多列 多行单列 SELECT e.empno,------------------------------------5 e.ename, (SELECT ename ------------------------------4 FROM emp --------------------------------2 WHERE empno=e.mgr-----------------------3 )dd FROM emp e;----------------------------------------1 --请利用子查询检索每个员工的领导名称,要求显示结果为: 员工号 员工姓名 领导姓名 SELECT e1.empno 员工号,e1.ename 员工姓名,e2.ename 领导姓名 FROM emp e1,emp e2 WHERE e1.mgr=e2.empno(+) SELECT *FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM emp)ttt)tt)t SELECT * FROM emp WHERE sal>( SELECT sal FROM emp WHERE empno=7369) update delete insert ------------------------------------ commit rollback savepoint ----------------------------------- oracle的安装和卸载 oracle的配置 oracle的SQL语法 oracle的对象 表 视图 索引 同义词 序列 存储过程 触发器 函数 PLSQL语法 游标 动态SQL ----------------------- ORACLE的数据管理 备份和还原 导入和导出
1、SQL的标准:ANSI和ISO2、SQL的分类:DDL(数据定义语句) CREATE、ALTER、DROP、TRUNCATE DML(数据操作语句) SELECT、DELETE、UPDATE、INSERT DCL(数据控制语句) GRANT、REVOKE TCL(事物控制语句) COMMIT、ROLLBACK、SAVEPOINT3、范式:数据库设计的理论规范 第一范式(1NF) 第二范式(2NF):在1范式基础上,所有非主关键字直接依赖主关键字 第三范式(3NF):在2范式基础上,消除了传递依赖【所有非主键字不能互相依赖】 第四范式(4NF) 第五范式(5NF) 第六范式(6NF) 大部分数据库设计为3范式即可4、数据库的专业术语 数据库(DB),存储数据的仓库,数据必须按照规定的格式存放,例如表格的形式 数据库管理系统(DBMS),是一个软件,必须在操作系统之上才能运行,该软件提供了一系列的标准供应用程序【数据库客户端】访问,例如sql语句。 目前DBMS可分为:关系型数据库管理系统(RDBMS),典型数据库有【ORACLE、mysql、sqlserver、db2】 对象关系型数据库管理系统(ORDBMS),例如【oracle】 数据库管理员(DBA):拥有一定专业知识,且通过数据库认证的技术人员。对于oracle而言,有三个级别的认证:OCA、OCP、OCM 数据库系统(DBS),是一个包含DBA、DB、DBMS的整体5、ORACLE数据库的安装和卸载 services.msc6、ORACLE服务端和客户端的配置 监听器 服务名7、oracle客户端的使用 sqlplus sql-developer pl/sql developer8、SQL基础语法讲解 A、select * from 表 where 条件 having 表达式 group by 表达式 order by 表达式 b、操作符 算术运算:+、-、*、/ 关系运算:>、<、<>、= 逻辑运算:NOT、OR、AND SQL操作符:between and和 not between and in 和 not in like 和 not like is null 和 is not null c、表达式:由运算符和标识符【列名】、函数组成的算式 d、函数: oralce系统函数 时间类:sysdate、add_months() 算数类:trunc 字符类:concat或者|| 其它:dbms_random.value(min,max) oracle自定义函数 9、伪表【dual】的作用 友好列【as】的作用
select * from(select rownum --------------------------------------------------------------------------- select * from(select rownum empno ,ename,job from student) where no>2 select * from (select * from scott.emp order by rownum desc) where rownum <=1 --------------------------------------------------------------------------- select * from emp where rownum<=( select trunc(count(*)/2) from emp ) --二,小数位进一 select * from emp where rownum<=( select ceil(count(*)/2) from emp ) ------------------------------------------------------------- select * from (select rownum num ,emp.* from emp ) where num <=(select count(*)/2 from emp) ----------------------------------------------------------------------------- select * from(select emp.*,rownum rm from emp)t where mod(t.rm,2)=0; ---------------------------------------------------------------------------- --分页查询 SELECT * FROM (SELECT e.*,ROWNUM rm FROM emp e) WHERE MOD(rm,2)=1 ------------------------------------------------------------- SELECT * FROM (SELECT t.*,ROWNUM rm FROM (SELECT * FROM emp ORDER BY sal)t )tt WHERE tt.rm>2 AND tt.rm<6; --------------------------------------------------------------------------------------------------------------------- --查询奇数行 SELECT * FROM (SELECT e.*,ROWNUM rm FROM emp e) WHERE MOD(rm,2)=1; -------------------------------------------------------------------------------------------------------------- --查询前面的一半内容 select * from emp where rownum<=( select ceil(count(*)/2) from emp ) ------------------------------------------------------------------------------------------------------------------------------ --分页查询,理解其中的意思含义 SELECT * FROM (SELECT t.*,ROWNUM rm FROM (SELECT * FROM emp ORDER BY sal)t )tt WHERE tt.rm>2 AND tt.rm<6; ------------------------------------------------------------------------------------------------------------------------------ --分页查询 SELECT * FROM (SELECT ROWNUM NO,e.* FROM (SELECT * FROM emp ORDER BY sal DESC) e WHERE ROWNUM<5) WHERE NO>=3 ---------------------------------------------------------------------------------------------------------------------------------- --员工表中每个部门的员工数和部门no SELECT deptno,COUNT(*) FROM emp GROUP BY deptno; ------------------------------------------------------------------------------------------------------------------------------- --找出工资比Jones多的员工 SELECT * FROM emp WHERE sal>=(SELECT sal FROM emp WHERE LOWER(ename)='jones'); -------------------------------------------------------------------------------------------- --找出平均工资最高的两种职位 SELECT * FROM (SELECT AVG(sal) s,job FROM emp GROUP BY job ORDER BY s DESC) WHERE ROWNUM<3; --------------------------------------------------------------------------------------------- --找出部门不再20,且比部门20中任何一个人工资都高的员工姓名和部门名称 SELECT * FROM emp e,dept d WHERE e.deptno<>20 AND e.sal>(SELECT MAX(sal) FROM emp WHERE deptno=20) AND e.deptno=d.deptno; SELECT * FROM emp WHERE emp.deptno<>20 AND emp.sal>(SELECT MAX(sal) FROM emp WHERE deptno=20) ----------------------------------------------- SELECT * FROM emp; SELECT * FROM dept; ------------------------------------------------ --得到平均工资大雨2000的工作职种 SELECT job FROM emp GROUP BY job HAVING AVG(sal)>2000; ------------------------------------------------------- --分部门得到工资大于2000的所有员工的平均工资,并且平均工资还要大于2500 --SELECT AVG(sal) FROM emp GROUP BY deptno --SELECT * FROM emp WHERE AVG(sal)>(SELECT AVG(sal) FROM emp GROUP BY deptno) SELECT deptno,AVG(sal) FROM emp WHERE sal>2000 AND sal<2500 GROUP BY deptno ---------------------------------------------------------------------------------------- --得到每个月工资总数最少的部门编号,部门名称,部门位置 SELECT * FROM dept WHERE deptno= ( SELECT deptno FROM (SELECT deptno,SUM(sal) FROM emp GROUP BY deptno ORDER BY SUM(sal))e WHERE ROWNUM=1 ); -------------------------------------------------------------------------------------------------- --批量插入语法 INSERT INTO 表 values(1,2,3,4) INSERT INTO 表 SELECT * FROM 表------------------------oracle INSERT INTO 表 VALUES(1,2,3,4,),(2,3,4,5,),(5,6,7,8) --mysql SELECT * FROM 表 LIMIT 2,6; --mysql -------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------- CREATE TABLE emp2 AS SELECT empno,ename,hiredate ,sal FROM emp; ------------------------------------------------------------------------------------------------------------------------------- SELECT e.*,ROWID FROM emp2 e ORDER BY 1 DELETE FROM emp2 WHERE ROWID='aaaaadfdafasdssssasdf' ------------------------------------------------------ CREATE TABLE t2 AS SELECT * FROM emp SELECT * FROM t2; ----------------------------------------------------------------------- CREATE TABLE t1 AS SELECT * FROM emp; SELECT * FROM t1; --批量插入 INSERT INTO t1 SELECT * FROM t1------------------------oracle CREATE TABLE t3 AS SELECT DISTINCT * FROM t1; SELECT * FROM t3 TRUNCATE TABLE t1 DROP TABLE t3 INSERT INTO t1 SELECT * FROM t3; SELECT * FROM t1 ---------------------------------------------------- --方法2 DELETE FROM tb1 WHERE ROWID IN (SELECT a.rowid FROM tbl a,tbl b WHERE a.rowid>b.rowid AND a.coll=b.coll AND a.col2=b.col2) ------------------------------------------------------------------------------------------------------------- --删除只剩下一行数据 SELECT * FROM t1; dELECT * FROM t1 WHERE ROWID IN (SELECT MAX(ROWID) rid FROM t1 t11 GROUP BY e1.empno,e1.ename,e1.job,e.mgr,e1.hiredate,e1.sal,e1.comm,e1.deptno); ----------------------------------------------------------------------------------- ALTER TABLE emp6 DISABLE contraint sys_; --禁用约束 ALTER TABLE emp6 ENABLE CONSTRAINT sys_;
--高级查询 --随机返回5条记录 SELECT * FROM (SELECT ename,job FROM emp ORDER BY dbms_random.value()) WHERE ROWNUM<6 --确定一年内的天数 SELECT add_months (TRUNC(SYSDATE,'y'),12)-TRUNC(SYSDATE,'y') FROM dual; --得到2014/1/1 SELECT TRUNC(SYSDATE,'y') FROM DUAL;