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 ...
随机推荐
- Java虚拟机------JVM分析工具
主要介绍JVM的分析工具: jps jps:Java Virtual Machine Process Status Tool http://docs.oracle.com/javase/1.5.0/d ...
- 让MySql支持表情符号(MySQL中4字节utf8字符保存方法)
UTF-8编码有可能是两个.三个.四个字节.Emoji表情是4个字节,而MySQL的utf8编码最多3个字节,所以数据插不进去. 解决方案:将编码从utf8转换成utf8mb4. 1. 修改my.in ...
- thinkphp在linux下报mkdir()错误
这个主要是由于缓存文件的权限不够,一般我们git下来的文件时,这个runtime是没有下来的. 解决办法:进入到TP项目下:修改父级目录权限为0777即可linux: chmod -R 777 ./r ...
- jq-杂记
点击消失 <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"> </script& ...
- 正则表达式-使用说明Regular Expression How To (Perl, Python, etc)
notepad++ wiki about regular expression 正则表达式-使用说明Regular Expression How To (Perl, Python, etc) http ...
- 学习excel的使用技巧一空格替换为0
问题1 把excel表格中的空格 填充为0 方法1 选中CDE列 CRTL+F 查找空 替换为0 方法2 选中CDE列 CRTL+G 打开定位 点击条件定位 选择空值 点击确定 然后在 ...
- 16.Mongodb安装
Mongodb是由c++编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,其内容存储形式类似JSON对象,它的字段值可以包含其他文档.数组及文档数组,非常灵活. 1.相关链接: http ...
- python中的jion
on将列表或元组中的每个元素连接起来,举个例子: 1 a = ["hello", "world", "dlrb"] 2 a1 = " ...
- C# 设计模式-策略者模式(Strategy)
策略模式的用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换.策略模式使得算法可以在不影响到客户端的情况下发生变化. 策略模式是对算法的包装,是把使用算法的责任和 ...
- 简单ATM机功能实现及感想
感想: 在那一天下午气喘吁吁的上了六楼 在建民的课上 都要带电脑 第一次上这样的课,每一次都是个段子 ,这一次考试是学前考试,什么也不知道 ,但是通过百度, 发现JAVA有很多还都和C语言相似的地方 ...