ORACLE数据库 常用命令和Sql常用语句
ORACLE
账号相关
如何获取表及权限
1.COPY表空间
backup scott
exp
登录管理员账号system
2.创建用户
create user han identified(认证) by mima default tablespace users(默认的表空间) quota(配额)10M on users;创建账号
分配权限
grant creatr session,create table,create view to 账号
grant connect to han; 赋予账号han登录权限
3.import the date引入数据
过程全选YES
imp
alter user scott account unlock; 解锁账号
对表的操作
建表
--新增2个字段
alter table stu
add(
stu_tel varchar2(11),--电话
stu_addr varchar2(50)--地址
);
alter table stu
modify (stuName varchar2(20),
stu_tel varchar2(8)
);
删1段alter table stu
drop column stuId;
删除多段alter table stu
drop (stu_tel,stu_addr);
增
insert into table_name values (值1, 值2,....)
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
改
update table_name set stuAge = 27 where stuno='1001';
update table_name set stuAge = 27,stuname='abc' where stuno='1002';
删
delete from emp3;
delete from stu where stuno='1003';
*查
select * from stu where stuno='1001';
select * from stu where stuno='1002';
查询所有列
select * from stu t
重复的只显示一条
select distinct stuno from stu;
select * from stu where stuno='1006';
select stuno,stuage from stu;
select distinct stuno from stu;
select * from stu where stuno ='1006' and stuage=29;
select * from stu order by stuage desc;
select * from stu order by stuage desc,stuno asc;
备份表
create table stuBAK as
select * from stu;
desc emp;描述EMP表有什么字段
一单条select语句(必须熟练!)
查询语句组成
1.
select * from emp; 查询emp表的所有的内容
select ename,sal*12 from emp;
select ename,sal*12 anuual_sal(anuual_sal表名可以随便写2个单词用下划线隔开 不然显示from不存在 不要占from的位置 简而言之 名字只能占一个 如果非得占2个 必须带上双一号“anuual sal”包住 默认引号内的是名字 而且显示是小写 可以认为 “”可以保持你输入时的状态 当然 中文也行) from emp;
select enamel||'哈哈(随便写字符串)' from emp; ||+'字符' 表示字符串拼接
select enamel||' 哈'哈(随便写字符串)' from emp; 是错误的 如果字符串里面有单引号 需要转义
select distinct deptno from emp; 重复的只显示一次
select distinect deptno,job from emp; 这个有重复的 这个不是表里自己和自己比 而是 2个表 deptno和job 2个表如果有重复的 只显示一次
2.from 那张表?
3.where 出现在单行语句 作为筛选条件优先执行
select ename,sal from emp where sal=800;
select ename,sal from emp where sal!=800;
select ename,sal from emp where sal>=800 and sal<=1500; 包含800和1500
select ename,sal from emp where sal between and 1500; 包含800和1500
select ename,sal,deptno from emp where sal not in (800,1500); 薪水不是800和1500的
select ename,sal,comm from emp where comm is null; 查出ename,sal,comm里面有null的表 之所以不用=因为 null是空值就是没值的意思
select ename,sal,comm from emp where comm is not null;
select ename,sal from emp where sal in (800,1500,2000); 选出sal=800或者1500或者2000的表
select ename,sal from emp where ename in ('SMITH','KING','JONES'); 选出'SMITH','KING','JONES'表
select ename,sal from emp where deptno=10 or sal>1500;
select ename,sal,deptno from emp where deptno=10 or sal>1500;
4.<--group by>(必须掌握)(需求 每个部门的平均薪资)
select avg(sal)from emp group by deptno;
select avg(sal)from emp group by deptno , job;
select deptno ,job,max(sal)from emp group by deptno , job;
select ename,max(sal)from emp;错误 组函数(一个都不能忘!)(多行输入 只有一行输出)
select ename from emp where sal=(select max(sal)from emp);
select ename ,max(sal) from emp group by deptno;错误 ename必须出现在 max()和group by 2个中一个,只有这样才唯一
select ename ,max(sal)from emp group by ename; 正确
select deptno,max(sal)from emp group by deptno;正确
5.having(需求每个部门平均薪资大于2000的有哪些 不能用where 因为where只能由于一条语句或者说一个表的筛选)
select avg(sal),deptno form emp group by deptno having avg(sal)>2000;
6.order by
select * from dept order by deptno desc; 降序排列
select empno,ename from emp; 升序
select empno,ename from emp order by asc; 升序
select empno,ename from emp where deptno!=10 order by empno asc; 部门标号不是10的 按升序排列的表
select ename,sal,deptno from emp order by deptno asc; 按部门编号升序
select ename,sal,deptno from emp order by sal asc;按薪水升序
select ename,sal,deptno from emp order by sal asc,ename desc; 薪水升序的情况下 名字倒序
select ename,sal*12 anuual_sal from emp where ename like '_A%' and sal>800 order by sal desc;
总结 1 select avg(sal),字段名 2 from 表 3 where 4 group by 5 having 6 order by
select avg(sal)from emp where sal>1200 group by deptno having avg(sal)>1500 order by avg(sal) desc;
二,子查询 (把他当成一张表!)
select ename ,max(sal) form emp;错误 因为这个max(sal)可以对应很多ename 不是一一对应关系
select ename,sal from emp where sal=(select max(sal)from emp);
需求 有哪些人的工资在平均工资之上
select ename,sal from emp where sal > (select avg(sal)from emp);
表连接
<--内连接>
<--写法1 常用>
select d1.dname , d1.loc, e.ename,e.mgr
from dept d1, emp e
where d1.deptno = e.deptno;
<--写法2 >
select d1.dname , d1.loc, e.ename,e.mgr
from dept d1
inner join emp e
on d1.deptno = e.deptno;
<--左外链接>
select d1.dname , d1.loc, e.ename,e.mgr
from dept d1
left join emp e
on d1.deptno = e.deptno;
<--右外链接>
select d1.dname , d1.loc, e.ename,e.mgr
from dept d1
right join emp e
on d1.deptno = e.deptno;
<--联合查询>
<--联合查询>
<--相同和合并>
select*from emp
union select * from emp;
<--直接相加>
select*from emp;
union all select *from emp2;
需求:按照部门分组后 每个部门挣钱最多的那个人
select ename,sal,deptno from emp where sal = (select max(sal)from emp group by deptno); 错误(单行 子查询 返回 多个值)
select ename,sal,deptno from emp where sal in (select max(sal)from emp) group by deptno;错误 (返回的是 符合3个值(3个部门最大工资)之一的所有可能 那么很有可能别的部门不是最大值的是别的部门的最大值也会选出来 结果就不对了!)
<--错误示范2个>
select ename,sal,deptno from emp where sal=(select max(sal)from emp) group by deptno;
select ename,sal,deptno from emp where sal in (select max(sal)from emp) group by deptno;
<--正确>
<--正确 每个部门挣钱最多的那个人>
1.(select max(sal)max_sal,deptno from emp group by deptno)t 命名这俩表是t join是连接
2.on (emp.sal=t.max_sal and emp.deptno = t.deptno); on是限制条件
select ename,sal from emp
join(select max(sal)max_sal,deptno from emp group by deptno)t
on (emp.sal=t.max_sal and emp.deptno = t.deptno);
<-- 需求每个部门平均薪水的等级(表连接 1先写出每个部门的平均薪水等级 2 和薪水等级那个表做连接)>
select deptno,avg_sal,grade from
(select deptno,avg(sal)avg_sal from emp group by deptno)t
join salgrade s
on (t.avg_sal between s.losal and s.hisal);
<--员工名字和经理人名字>
select empno,ename,mgr from emp;
select empno,ename,mgr from emp;
<-- 自连接 自身和自身链接 一个表看成2个一样表起2个别名做链接>
select e1.ename ,e2.ename from emp e1 ,emp e2 where e1.mgr=e2.empno;
<--92年老版本过滤筛选条件where里面写链接表的条件和过滤条件的写法,99年将连接条件和过滤条件分开写>
select ename ,dname,grade from emp e,dept d,salgrade s
where e.deptno=d.deptno and e.sal between s.losal and s.hisal and
job<>'clerk';
<--过滤和链接条件 99年的新写法 推荐这个写!>
<--等值链接>
<--cross join 交叉连接>
select ename ,dname from emp cross join dept;
<--92语法>
select ename,dname from emp,dept where emp.deptno =dept.deptno;
<--99年语法>
select ename,dname from emp join dept on (emp.deptno=dept.deptno);
<--非等值链接>
select ename,grade from emp e join salgrade s on (e.sal between s.losal and s.hisal);
<--3张表做链接>
select ename ,dname,grade from emp e
join dept d on (e.deptno=d.deptno)
join salgrade s on(e.sal between s.losal and s.hisal)
where ename not like '_A%';
<--自连接 用99新语法>
select e1.ename,e2.ename from emp e1 join emp e2 on(e1.mgr=e2.empno);
<--外链接 左外链接和右外链接 全外链接>
select e1.ename,e2.ename from emp e1 left join emp e2 on (e1.mgr=e2.empno);
select e1.ename,e2.ename from emp e1 right join emp e2 on (e1.mgr=e2.empno);
select ename , dname from emp e
join dept d
on(e.deptno = d.deptno);
select ename , dname from emp e
right outer join dept d
on(e.deptno = d.deptno);
<--全外链接>
select ename , dname from emp e
full join dept d
on(e.deptno=d.deptno);
<--每个部门平均薪水等级>
select deptno,avg_sal,grade from
(select deptno,avg(sal)avg_sal from emp group by deptno)t
join salgrade s1
on (t.avg_sal between s1.losal and s1.hisal);
<--每个人的薪水等级>
select deptno,ename,grade from emp
join salgrade s
on (emp.sal between s.losal and s.hisal);
<--每个部门(每个员工平均薪水等级的)平均薪水等级>
select deptno, avg(grade)from
(select deptno,ename,grade from emp
join salgrade s
on (emp.sal between s.losal and s.hisal))t
group by deptno;
<-- 那些雇员是经理?>
select ename from emp where empno in(select mgr from emp);
select ename from emp where empno in(select distinct mgr from emp);
select stuno num,stuage age from stu;
子查询
select deptno from emp where ename ='SCOTT';
in慎用 尤其结果大的
select * from emp where deptno in('10','20');
select * from emp where deptno not in('10','20');
select * from emp where( deptno='10' or deptno= '20') and sal>2000;
e1是emp的取名
select * from emp e1 where sal> (select avg(sal)from emp e2 where e1.deptno=e2.deptno);
select dname from dept where deptno where =(deptno from emp where sal>3000);
select dname from dept d where exists (select * from emp e where d.deptno = e.deptno and sal>3000);
<--不相关子查询>
select dname from dept where deptno =(select deptno from emp where sal>3000);
select dname from dept where deptno = (select deptno from emp where ename ='SMITH');
三,骚操作
select stuno "学号“stuage 年龄 from stu;
拼接字符
select 'test'||to_char(sysdate,'yyyy-mm-dd')from dual;
select 's'|| stuno "学 号“stuage 年龄 from stu;
select '测试','s'|| stuno "学 号“stuage 年龄 from stu;
select to_char(sysdate,'yyyy-mm-dd')from dual;
select to_char(sysdate,'yyyy"年"mm"月"dd"日 "hh24:mi:ss')from dual;
四,单行函数
select to_number('234')from dual;
select to_date('2017-09-09','yyyy-mm-dd')from dual;
select distinct stuno from stu;
五,聚合函数
5个聚合函数
所以数字类型的数字相加用和这个
(1)计数
select count(*)from emp;
(2)求和
select sum(empno) from emp;
(3)最小
select min(sal)from emp;
(4)最大
select max(sal)from emp;
(5)平均
select avg(sal)from emp;
分析函数(略)
截取字符函数
select substr('sdsfds',2) from dual;
select substr('sdsfds',2,4) from dual;
select substr('20170909120910',1,8) from dual;
NVL函数
如果是空 就打印后边的4
select nvl(null,4)from dual;
打印5
select nvl(5,4)from dual;
select nvl(comm,0)*1.1 from emp; 工作经验
上面为啥这么写呢?因为如果comm是null的话 *1.1不合适 所以 我们这么写
如果是null就改成0
判断字符是否一样 1一样 0不一样
select decode('男','女',1,0)from dual; 0
select decode('男','男',1,0)from dual; 1
select decode('男','女',1,'男',0,2)from dual;
select decode('男','女',1,'男',0,2)from dual;
select decode('男','女',1,'',0,2)from dual;
<分页查询>
伪列
<--分页>
<--伪列>
<rowid rownum>
;
<--先抓取5个 然后排序>
select sal from emp where rownum<6
order by sal desc;
<--收入前五>
select *from(select * from emp order by sal desc) where rownum<6;
select *from(select * from emp order by sal desc) where rownum<16;
<--先找到需要排序的表排序 2然后 对这个表进行rownum固化 3.对固化后的表加限定条件>
select * from (
select e.*,rownum rn from
(select * from emp order by sal desc)
e
)
where rn=5;
数据迁移
1、找到数据文件 执行select*from 表; 导出sql文件:点 绿色的按钮 导出查询结果 得到sql文件
2.建表 可以COPY→ 查看 sql(v) 里面有多余的部分 不要 只要建表操作
3.导入sql 新建命令窗口 @ 找到要导入的sql 文件
oracle 数据库编程语言 pl/sql 过程化语言和结构化语言的语言
存储过程
declare声明 变量 常量赋值 c_rate_iner number(7,2) comstant :=1.10; 变量 select ename,sal*c_rate_iner into v_name,v_rate from emp where empno='7788';
begin sql语句
exception 异常
end;
loop循环
DBMS_OUTPUT.PUT_LINE(''+变量)
if 条件 then
goto ming
else null;
end if
end loop;
<<ming>>
--变量类型 %type属性 他的意思可以根据别的表的数据类型定义自己的数据类型,
--表的某个字段数据类型 绑定 自己的数据类型,表的某个字段类型变成什么,他就跟着变成什么 >
例子
declare
--v_num 的数据类型是emp表下面的empno这个字段的数据类型
v_num emp.empno%type :=5566;
begin
dbms_output.put_line(v_num);
end;
--table数据类型 相当于java里面的数组 自定义类型 先声明一种类型 再用这个类型 声明一个变量
declare
--标准写法 type表明 我要自定义一个类型 type_table_emp_empno是类型名字 is table of 是一张表
--emp.empno%type表的类型是emp这个表的empno这个字段的类型(绑定) index by索引下标 binary_integer索引下标的类型
type type_table_emp_empno is table of emp.empno%type index by binary_integer;
--v_empnos 类型名 和定义好的数组类型
v_empnos type_table_emp_empno;
begin
v_empnos(0):=7369;
v_empnos(1):=7370;
v_empnos(2):=7860;
v_empnos(-1):=9999;
dbms_output.put_line(v_empnos(-1));
end;
--record类型 相当于java里面的类
declare
type type_record_dept is record
(
deptno dept.deptno%type,
dname dept.dname%type,
loc dept.loc%type
);
v_temp type_record_dept;
begin
v_temp.deptno:=50;
v_temp.dname:='aaaa';
v_temp.loc:='beijing';
dbms_output.put_line(v_temp.deptno||' '||v_temp.dname);
end;
--
使用 %rowtype属性 声明 record变量 (解决了原来的表dept改变了 record会跟着编号 解决了维护麻烦的问题)
declare
v_temp dept%rowtype;
begin
v_temp.deptno:=50;
v_temp.dname:='aaaa';
v_temp.loc:='beijing';
dbms_output.put_line(v_temp.deptno||' '||v_temp.dname);
end;
ORACLE数据库 常用命令和Sql常用语句的更多相关文章
- Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项)
Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项) A.cat B.concat C.join D.+ 解答:B
- Oracle数据库查看表空间sql语句
转: Oracle数据库查看表空间sql语句 2018-09-03 15:49:51 兰海泽 阅读数 6212 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出 ...
- Oracle数据库之开发PL/SQL子程序和包
Oracle数据库之开发PL/SQL子程序和包 PL/SQL块分为匿名块与命名块,命名块又包含子程序.包和触发器. 过程和函数统称为PL/SQL子程序,我们可以将商业逻辑.企业规则写成过程或函数保 ...
- Oracle数据库使用Analyze提升sql性能
Oracle数据库使用Analyze提升sql性能 如果你不使用analyze完成sql优化,将意味着:你拒绝使用数据库的查询优化器,也失去了使用优化连接的机会.假设你创建了一张拥有100万条记录的临 ...
- 54. Android中adb常用命令及应用常用目录
本文主要介绍adb常用命令及应用常用目录.1.adb常用命令adb devices列出所有连接的android设备.以下命令都是对单个devices而言,如果存在多个devices的话,下面的命令都需 ...
- Oracle数据库之FORALL与BULK COLLECT语句
Oracle数据库之FORALL与BULK COLLECT语句 我们再来看一下PL/SQL块的执行过程:当PL/SQL运行时引擎处理一块代码时,它使用PL/SQL引擎来执行过程化的代码,而将SQL语句 ...
- MySql常用命令集Mysql常用命令showdatabases;显示数据库createdatab
MySql 常用命令集 Mysql常用命令 show databases; 显示数据库 create database name; 创建数据库 use databasename; 选择数据库 drop ...
- 抓取oracle数据库耗费资源的sql语句
oracle数据库连接业务系统,而有些sql语句的执行严重影响了oracle的性能,就如同mysql的慢查询一样,mysql可以开启慢查询日志定位这些造成数据库性能下降的语句,而oracle同样可以做 ...
- Oracle数据库更新时间的SQL语句
---Oracle数据库更新时间字段数据时的sql语句---格式化时间插入update t_user u set u.name='pipi',u.modifytime=to_date('2015-10 ...
随机推荐
- C#存储过程 传入参数 传出参数 结果集
作者:卞功鑫 转载请保留:http://www.cnblogs.com/BinBinGo/p/6400928.html //1 连接字符串 string connectionString = &quo ...
- 10 sed命令的基本用法
grep灵活的关联正则表达式实现文件过滤,而sed是一个数据流的编辑器,或者说是一个行编辑器,它将文件一行一行的数据放入到内存中进行编辑,而awk是一个报告生成器,这个我们以后再说: 之前我们介绍过g ...
- cocos源码分析--LayerColor的绘制过程
1开始,先创建一个LayerColor Scene *scene=Scene::create(); director->runWithScene(scene); //目标 auto layer ...
- Android 接入X5WebView,让WebView加载更快;
X5内核,微信和QQ浏览器都在用的WebView: 官网地址:https://x5.tencent.com,详细的信息进官网了解: 这是官方的宣传语: 1) 速度快:相比系统webview的网页打开速 ...
- for循环中进行联网请求数据、for循环中进行异步数据操作,数据排序错乱问题解决;
for循环中进行联网请求数据,由于网络请求是异步的,第一个网络请求还没有回调,第二次第三次以及后续的网络请求又已经发出去了,有可能后续的网络请求会先回调:这时我们接收到的数据的排序就会错乱:怎么才能让 ...
- day9笔记整理,记忆
函数的使用:一 定义函数的三种形式 1.1 无参函数 1.2 有参函数 1.3 空函数二 调用函数的三种形式 2.1 语句形式 def foo(): print('from foo') ...
- redis的哨兵集群,redis-cluster
#主从同步redis主从优先1.保证数据安全,主从机器两份数据一主多从2.读写分离,缓解主库压力主redis,可读可写slave身份,只读 缺点1.手动主从切换假如主库挂了,得手动切换master ...
- 最全spring boot视频系列,你值得拥有
================================== 从零开始学Spring Boot视频 ================================== àSpringBoot ...
- gentoo moc audacious
最近想在 gentoo 上面听音乐, 以前用过 audacious,现在想换成 moc 试试看. emerge -av moc,结果安装完了以后,放 mp3 没有声音,把 USE 里面各种相关解码的都 ...
- uva-10132-排序
题意: 有很多文件,碎成了俩片,问,原来的文件是什么,如果有多个答案,任意一个答案就行,输入2N个字符串,拼接成N个文件. 直接排序,正确的答案一定是某个长度最短的和某个最长的连在一起. #inclu ...