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;

转载于:https://www.cnblogs.com/PoeticalJustice/p/7608575.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/454927.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

光荣之路测试开发面试linux考题之四:性能命令

Hi,大家好我是tom,I am back.今天要给大家讲讲linux系统一些性能相关命令。 1.fdisk 磁盘管理 是一个强大的危险命令&#xff0c;所有涉及磁盘的操作都由该命令完成&#xff0c;包括&#xff1a;新增磁盘、增删改磁盘分区等。 1.fdisk -l 查看磁盘分区情况 Disk /dev/sda: 27.8…

一起学并发编程 - 优雅关闭

Java中原来在Thread中提供了stop()方法来终止线程&#xff0c;但这个方法是不安全的&#xff0c;所以一般不建议使用。文本将介绍两种可以优雅的终止线程的方式...<!-- more --> 第一种 在JAVA《Java多线程模式》中有一种叫Two-Phase Termination&#xff08;两步终止&am…

mac安装完mysql后关机特别慢_mysql-Mac终端下遇到的问题总结

为了方便启动mysql服务&#xff0c;修改/etc/.bash_profile文件&#xff0c;如下alias mysql"/usr/local/mysql/bin/mysql"alias mysqladmin"/usr/local/mysql/bin/mysqladmin"或者alias mysqlstart"sudo /usr/local/mysql/support-files/mysql.serve…

sending data mysql slow Mysql查询非常慢的可能原因

1.用explain看看mysql的执行情况,可以得知,task_id扫描了近20万条数据,而且这个task_id不是索引 2.为这个task_id所在的表,将此字段添加索引后,查询就变得很快了 转载于:https://www.cnblogs.com/Skrillex/p/7365590.html

mybatis 添加语句返回对象_mybatis的insert语句插入数据时的返回值的实现

mybatis的insert语句插入数据时的返回值的实现,语句,返回值,那条,都是,站长站mybatis的insert语句插入数据时的返回值的实现易采站长站&#xff0c;站长之家为您整理了mybatis的insert语句插入数据时的返回值的实现的相关内容。mybatis的sql语句一般是配置在配置文件中&#xf…

打包上架

昨天写的打包上架&#xff0c;分组到了文章&#xff0c;发现不便查看贴链接到这里&#xff1a; http://www.cnblogs.com/ITCoderW/articles/7597969.html 最近一个版本的审核的过程 当我们上传到APP Store一个新的版本后 登录ITunes Connect就可以看到相应的版本的审核的状态 粗…

inet_pton函数和inet_ntop函数的用法及简单实现

http://blog.csdn.net/eagle51/article/details/53157643?utm_sourceitdadao&utm_mediumreferral 这两个函数是随IPv6出现的新函数&#xff0c;对于IPv4地址和IPv6地址都适用。函数名中的p和n非别代表表达&#xff08;presentation&#xff09;和数值&#xff08;numeric&…

mysql 5.7 延迟同步_MySQL5.6升级5.7时出现主从延迟问题排查过程

最近在做zabbix的数据库MySQL5.6升级5.7时&#xff0c;出现主从延迟问题&#xff0c;这个问题困扰了很久没有解决&#xff0c;昨天终于解决了&#xff0c;整理了一下整个排查过程&#xff0c;分享给大家。环境说明&#xff1a;mysql主库为5.6的版本&#xff0c;有四个从库&…

架构设计--仅是软件开发之第二大影响力?!

SDWest2006&#xff08;译注1&#xff09;对我来说是个有趣的大会。我除了星期三之外&#xff08;当时我正飞往费城参加一个客户会议 因此错过了Jolt颁奖部分&#xff09;每天都在演讲。我也参加了一些谈话和会议&#xff1b;其中最引人关注的是Mike Cohn的计划与估算的谈话。…

WiFi密码分享有妙招 不必口头相传

移动互联网的迅速崛起&#xff0c;使得我们可以方便的使用手持移动设备进行上网。尤其是在家庭中&#xff0c;使用智能手机、平板电脑、笔记本电脑等移动设备进行上网和娱乐已经成为主流&#xff0c;台式机上网正日渐式微。在家中时&#xff0c;我们通过无线路由器提供的WiFi网…

javaweb(二十一)——JavaWeb的两种开发模式

一、JSPJavaBean开发模式 1.1、jspjavabean开发模式架构 jspjavabean开发模式的架构图如下图(图1-1)所示 图1-1 在jspjavabean架构中&#xff0c;JSP负责控制逻辑、表现逻辑、业务对象&#xff08;javabean&#xff09;的调用。 JSPJavaBean模式适合开发业务逻辑不太复杂的web应…

Redis基于客户端分片的集群案例(待实践)

说明&#xff1a; 下面的示例基本都是基于Linux去实现&#xff0c;目的是为了环境的统一&#xff0c;以便于把性能调整到最优。且基于Java。建议生产环境不要使用Windows/Mac OS这些。 在Java领域&#xff0c;基于客户端进行分片最常用的库应该是Jedis&#xff0c;下面基本是基…

mysql select 效能_MYSQL的联合查询最好是少用,效能差异巨大

同样的功能,不同的写法,时间和内存占用差了几千倍,不废话,直接上代码第一种写法:代码如下:$RsDB::get($_ENV[DB],3,"SELECT * FROM _xiazhu WHERE uid IN(SELECT id FROM _user WHERE id<5000)");var_dump($Rs);内存和时间:内存使用:96514.53Kb 运行时间:1272.73m…

高效程序员应该养成的七个习惯

对于软件工程师来说&#xff0c;工作也许意味着许多东西 -- 稳定的收入、做自己感兴趣的项目、找一份更好工作的跳板&#xff0c;或者你只是喜欢与其他程序员共事。但说到“效率”&#xff0c;强调的是在一定时间内按质完成项目的能力。Phil Chu根据自己的经验提出了高效程序员…

概率论与数理统计思维导图知识框架_考研概率论与数理统计 综合题型秘籍思维导图① 随机变量1~3章 [21考研上岸之旅]...

Hello World&#xff0c;我的朋友&#xff0c;这里是一颗小白蛋&#xff0c;大千世界&#xff0c;很高兴以这样的方式与你相遇前言在复习过程中发现概率论知识点很少且集中 所以没有分开章节去整理王安式概率论辅导讲义张宇概率论9讲方浩强化班1800题 综合题型秘籍在学习过程中…

关于软件的架构设计

好的开始相当于成功一半 开始之初的架构设计决定着软件产品的生死存亡。“好的开始相当于成功一半”。 开始的架构设计也是最难的&#xff0c;需要调研同类产品的情况以及技术特征&#xff0c;了解当前世界上对这种产品所能提供的理论支持和技术平台支持。再结合自己项目的特…

[Oracle]快速构造大量数据的方法

[Oracle]快速构造大量数据的方法&#xff1a;create table tab001(id integer primary key, val varchar2(100));insert into tab001 select ij,rpad(to_char(ij),100,A) from ( with DATA2(j) as ( select 0 j from DUAL …

mysql用supervisor管理_Supervisor使用详解

一、supervisor简介Supervisor是用Python开发的一套通用的进程管理程序&#xff0c;能将一个普通的命令行进程变为后台daemon&#xff0c;并监控进程状态&#xff0c;异常退出时能自动重启。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动&#xff0c…

Handle/Body pattern(Wrapper pattern)

Handle Body Pattern 一些设计模式&#xff0c;通过一系列非直接的间接的方式&#xff08;这种间接的方式&#xff0c;可称其为 handle&#xff08;把手&#xff09;&#xff09;&#xff0c;完成接口与实现&#xff08;实现可称为 body&#xff08;主体&#xff09;&#xff0…

架构设计贵在务实

(本文是我给温昱先生新书写的读后感节选。&#xff09; 我最早听说“软件架构”这个概念以及UML的名字&#xff0c;是在1999年的水木清华BBS上。当时有一篇文章介绍了软件架构作为一个相对独立的领域的发展情况&#xff0c;顺便提到在此前一年被接纳为OMG标准的UML。该文作者…