20140920
单表查询
-- 查询************************************************************************************************************
-- 使用scott用户登录
-- 查看emp表
desc emp
--查看所有数据
select * from emp;
--查看指定数据,按顺序写
select empno,ename from emp;
-- 去重复,如果ename和job都相同,则只显示一个
select distinct ename,job from emp;
-- 查询某项的其他项
select job,empno from emp where ename = 'SMITH';
-- 查询部门编号为 10 和 20 的所有员工
select * from emp where deptno = 10 or deptno = 20;
-- 查询部门标号不为30的其余所有员工
select * from emp where deptno != 30;
-- 查询薪水在2000-2500之间的所有员工
select * from emp where sal>2000 and sal<=2500;
-- 查询入职日期在1981年11月17日前的
select *from emp where hiredate<'17-11月 -81';
-- 查询名字中包括B K 的(模糊查询) %表示可以是任意内容
select * from emp where ename like '%B%K%';
-- 查询名字中第三个字母为O 的名字 _ 表示任意一个内容
select * from emp where ename like '__O%';
-- 查询号码中包含7369和1234的用户。
select * from rmp where empno in (7369,1234);
-- 查询没有上司的人。XXX is null
select * from emp where mgr is null;
-- 查询的时候使用别名显示菜单,其中as 可以省略
select empno as 编号,ename as 姓名,sal 薪水from emp where ename like '%K%';
-- 查询SMITH的年薪
select empno,ename,sal*12 from emp where ename = 'SMITH';
-- 查询以J开头并且 薪水大于500或者为MANEGER的用户
select empno,ename,sal from emp where ename like 'J%' and (sal>500 or JOB = 'MANAGER');
-- 将所有数据按照薪水的高低排序,如果遇到了相同的,就看主键的大小来排序
-- 从小到大
select * from emp order by sal;
select * from emp order by sal asc;
-- 从大到小
select * from emp order by sal desc;
-- 统计所有数据中的编号,姓名,年薪(包含奖金)
-- nvl(comm,0) 如果comm值为null,则赋予其 0 。
select empno,ename,(sal+nvl(comm,0))*13 年薪 from emp;
-- 更据上面的年薪的别名排序
select empno,ename,(sal+nvl(comm,0))*13 年薪 from emp order by 年薪 desc;
-- 分组函数 max,min,avg,sum,count ****************************
-- 查询出月薪最高的和月薪最低的员工的编号,姓名,月薪
select empno,ename,sal from emp where sal = (select max(sal) from emp) or sal = (select min(sal) from emp) ;
-- 计算所有员工的平均月薪
select sum(sal)/count(empno) from emp;
select avg(sal) from emp;
-- 显示每个部门的平均工资和最高工资
-- group by
-- 先查询出分组条件
select deptno 部门,avg(sal) 平均工资,max(sal) 最高工资 from emp group by deptno ;
-- oracle 另一种写法:
select avg(sal) 平均工资,max(sal) 最高工资 from emp group by deptno ;
-- 按照部门数字从小到大显示
select deptno 部门,avg(sal) 平均工资,max(sal) 最高工资 from emp group by deptno order by deptno;
-- 显示每个部门的每个岗位的平均工资和最低工资
select deptno 部门,job 岗位,avg(sal) 平均工资,max(sal) 最高工资 from emp group by deptno,job order by deptno;
-- 加个总数
select deptno 部门,job 岗位,avg(sal) 平均工资,max(sal) 最高工资,count(sal) 人数 from emp group by deptno,job order by deptno;
-- 显示部门平均工资低于2000的部门的平均工资(分组之后再写条件用having,且不可使用别名)
select deptno 部门,avg(sal) 平均工资 from emp group by deptno having avg(sal)<2000;
-- 不能按下面的方式写
select deptno 部门,avg(sal) 平均工资 from emp group by deptno having 平均工资<2000;
-- where 写在group by 前面,oeder by 写在最后
select deptno 部门,avg(sal) 平均工资 from emp where deptno!=30 group by deptno ;
select deptno 部门,avg(sal) 平均工资 from emp where deptno!=10 group by deptno having avg(sal)>2000;
select deptno 部门,avg(sal) 平均工资 from emp where deptno!=30 group by deptno having avg(sal)>2000 order by deptno;
多表查询
--***************多表查询**********************************************************************************************
--查询部门表
select * from dept;
-- 显示员工的名字和所在部门的名字,给表加一个别名(笛卡尔集)
select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno;
-- 只显示部门为10的
select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno and e.deptno = 10;
-- 显示每个员工的工资以及对应的级别
select e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal;
-- 显示员工名字,工资,所在部门名字,并且按部门号码排序
select e.ename,d.dname,e.sal from emp e,dept d where e.deptno = d.deptno order by e.deptno;
-- 查询SMITH的上司
select mgr,ename from emp where empno = (select mgr from emp where ename = 'SMITH');
-- 自链接查询:将一张表做为两张表查询
select e1.mgr,e2.ename from emp e1, emp e2 where e1.mgr = e2.empno and e1.ename = 'SMITH';
-- 查询部门10工作相同的员工信息
select e1.* from emp e1,emp e2 where e1.job = e2.job and e2.deptno = 10;
select * from emp where job in (select job from emp where deptno = 10);-- 多个结果用in
-- 显示工资比部门30最高的人高的员工信息
select * from emp where sal>(select max(sal) from emp where deptno = 30);
select * from emp where sal>all(select sal from emp where deptno = 30);
-- 查询部分数据
--伪列 ROWNUM,ROWID
select ROWNUM,ename from (select ROWNUM r,ename from emp where ROWNUM<=10) where r>5;
select * from (select ROWNUM r,ename from emp where ROWNUM<=10) where r>5;
-- 合并两次的结果
-- union 对于多个表的结果合并
select * from emp where job = 'SALESMAN' union select * from emp where deptno = 10;
select * from emp where job = 'SALESMAN'or deptno = 10;
-- union all 对于多个表的结果合并,保留重复
select * from emp where job = 'SALESMAN' union all select * from emp where deptno = 10;
-- 取交集
--intersect
select * from emp where job = 'SALESMAN' intersect select * from emp where sal>1500;
-- 得到在第一个结果集中存在但是第二个中不存在的数据
--minus
select * from emp where job = 'SALESMAN' minus select * from emp where sal>1500;
-- 字符串操作函数,数字操作函数,类型转换函数
--*******************************建表**************************************************************************
-- 用户表: BlogUsers
-- userID
-- userName
-- userPassword
-- userSex
-- userDate
-- userAge
-- userEmail
-- userPhoneNumber
create table BlogUsers(userID number (10) primary key not null,userName varchar2(20),userPassword varchar2(30));
-- 文章表 Articles
-- articleID
-- articleTitle
-- articleContent
-- articleTime
-- userID 发表者
create table Articles(articleID number (10) primary key not null,articleTitle varchar2(20),articleTime DATE,articleContent varchar2(4000),userID number(10));
insert into articles values(123,'name','1-1月 1981','text',1234);
insert into articles values(124,'name',TO_DATE('2014-9-22','yyyy-mm-dd'),'text',1234);
-- 调节显示效果
set linesize 100;
-- 建立引用
alter table ARTICLES add constraint USERID foreign key (USERID) references BLOGUSERS (USERID);
-- 删除用户,如果需要同时删除文章,则需要更改级联关系。C。。。
相关推荐
列举了常用的oracle数据库操作指令 适合oracle入门级人物
oracle数据库命令大全
这是我跟人总结的关于Oracle数据库的命令和一些心得,希望能给大家提供帮助
介绍Oracle数据库导出文本的方法,属于Oracle数据库的入门级应用。
Oracle数据库表空间巡检指令
呕心沥血两个礼拜,记录oracle学习点点滴滴 代码经过自己验证 是学习和复习不可多得的好东西 倾情奉献
oracle数据库数据导出.doc 文件中包含oracle数据库的imp和exp等操作和相关指令
关于oracle做恢复操作时启动数据库报错,通常是由于rman做了恢复操作导致的报错. 通过继续执行恢复指令而恢复数据库,成功启动数据库.
记录了Oracle中除了游标触发器及存储过程外所能使用到的各种语句命令,方便平时工作时直接复制使用,但推荐背下来,过分依赖复制粘贴,不是编程好习惯。
Oracle基本使用指令 一、ORACLE的启动和关闭 1、在单机环境下 要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下 ...Oracle数据库有哪几种启动方式? 上面的是部分内容摘要: 好不好,要不要你们说了算!
该文档详细的整合了oracle数据库备份,恢复的指令操作,并进行了详细的描述
在SQL plus下连接Oracle数据库时部分运行指令
描述了oracle在配置过程是的步骤以及一些指令的格式
在excel中编写表结构后,可以在首页生成对应数据库脚本,包括MySQL、oracle、sqlserver,然后把该脚本在数据库中运行就可以了,还包括首页生成目录结构,有助于数据库表文档的编写
手动启动和关闭Oralce服务,对Oracle数据库的服务进行操作
库、Oracle、Microsoft SQL Server、IBM DB2、Microsoft Excel 97-2000、文本文件、其他支持 ODBC 的 数据源。在数据的转换过程中,向导能根据源数据和目标数据具体类型,通过内置的转换规则提供数据类型 的默认映射...
1 检查Oracle的进程 2 查看数据库的实例: 3 查看数据库的日志模式,打开模式 4 查看数据库alert_SID.log 中的ora-的错误信息: 5 检查控制文件状态: 6 查询日志状态 7 查询数据状态: 8 查询表空间状态: 等等
可自动完成oracle的数据库数据导出,...5.对Oracle数据库在定时备份使用了Windows的计划任务,做到了只有需要备份时,程序才会启动,在备份完成后,程序会自动退出,直到操作系统的下一次调度,真正做到了节省系统资源
#include #include #import "C:Program FilesCommon FilesSystemADOmsado15.dll" no_namespace rename("EOF","adoEOF") using namespace std; #define BUFSIZE 64 int main() { //char buf[BUFSIZE];...
资源名称:Oracle性能优化求生指南内容简介:Oracle数据库的应用十分广泛,数据库性能的好坏直接影响应用程序能否快速响应用户指令。而Oracle数据库又比较复杂,数据库性能调优成为众多DBA最为头疼的问题。 本书...