Orcale常用语句汇总

--创建表空间
create tablespace itheima
datafile 'c:\itheima.dbf'
size 100m
autoextend on
next 10m;
--删除表空间
drop tablespace itheima;

--创建用户
create user itheima
identified by itheima
default tablespace itheima;

--给用户授权
--oracle数据库中常用角色
connect--连接角色,基本角色
resource--开发者角色
dba--超级管理员角色
--给itheima用户授予dba角色
grant dba to itheima;

---切换到itheima用户下

---创建一个person表
create table person(
      pid number(20),
      pname varchar2(10)
);

---修改表结构
---添加一列
alter table person add (gender number(1));
---修改列类型
alter table person modify gender char(1);
---修改列名称
alter table person rename column gender to sex;
---删除一列
alter table person drop column sex;

---查询表中记录
select * from person;
----添加一条记录
insert into person (pid, pname) values (1, '小明');
commit;
----修改一条记录
update person set pname = '小马' where pid = 1;
commit;

-- 三个删除
-- 删除表中全部记录
delete from person;
-- 删除表结构
drop table person;
-- 先删除表,再次创建表。效果等同于删除表中全部记录。
-- 在数据量的情况下,尤其在表中带有索引的情况下,该操作效率高。
-- 索引可以提供查询效率,但是会影响增删改效率。
truncate table person;
-- 序列不真的属于任何一张表,但是可以逻辑和表做绑定
-- 序列:默认从1开始,依次递增,主要用来给主键赋值使用。
create sequence s_person;
select s_person.nextval from dual;

-- 添加一条记录
insert into person (pid,pname) values (s_person.nextval,'小明');
commit;
select * from person;


-- 刚刚安装完orcale,scott用户是被锁定的,需要用有管理员权限的账户进行解锁;
-- scott用户,密码tiger
-- 解锁scott用户
alter user scott account unlock;
-- 解锁scott用户的密码[此句也可以用来重置密码]
alter user scott identified by tiger;
-- 切换到scott用户下



-- MGR 直属领导,HIREDATE 入职日期  ,SAL工资,COMM年终奖 , DEPTNO部门编号,LOC部门地址
-- GRAGE工资等级 ,

-- 单行函数:作用于一行,返回一个值'
-- 字符函数
-- 小写变大写
select upper('yes') from dual;    -- YES
-- 大写变小写
select lower('YES') from dual;    -- yes

-- 数值函数
select round(26.16,-1) from dual;    -- 四舍五入,后面的参数表示保留的位数  正数是往后算,负数是往前算,以'.'开始
select trunc(56.16,-1) from dual;    -- 直接截取,不再看后面位数的数字是否大于5
select mod(10,3) from dual;   -- 求余数     这些在java中实现很容易,所以我们尽可能的直接在java中完成,不在数据库中进行这些操作;

-- 日期函数
-- 查询出emp表汇总所有员工入职距离现在几天
select sysdate-e.hiredate from emp e;
-- 算出明天此刻
select sysdate+1 form dual;
-- 查询出emp表中所有员工入职距离现在几月
select months_between(sysdate,e.hiredate) from emp e;
-- 查询emp表中所有员工入职距离现在几年;
select months_between(sysdate,e.hiredate)/12 from emp e;
-- 查询出emp表中所有员工入职距离现在几周;
select round((sysdate-e.hiredate)/7) from emp e;
-- 转换函数
-- 日期转字符串
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
-- 24小时制转换
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
-- 字符串转日期       [虽然它的结果可能跟上面的一样,但上面的类型是字符串,它的类型是日期类型,结果一样但是类型不一样]
select to_date('2018-6-7 16:39:56','fm yyyy-mm-dd hh24:mi:ss') from dual;
-- 通用函数
-- 算出emp表中所有员工的年薪
-- 奖金里面有null值,如果null值和任意数字做算数运算,结果都是null;
-- 可以使用val来处理null
select e.sal*12+nvl(e.comm,0) from emp e;

-- 条件表达式[这两个,一个等值一个范围是mysql和orcale通用写法;]

-- 等值判断
-- 给emp表中员工起中文名 
select e.ename,
      case e.ename
        when 'SMTTH' then '曹贼'
          when 'ALLEN' then '大耳贼'
            when 'WARD' then '诸葛小儿'
              else '无名'
                end
from emp e;
-- 范围判断
-- 判断emp表中员工工资,如果高于3000显示高收入,如果高于1500低于3000显示中收入,如果低于1500,显示低收入
select e.sal,
      case e.sal
        when e.sal>3000 then '高收入'
          when e.sal>1500 then '中等收入'
            else '低收入'
              end
from emp e;

-- orcale专用条件表达式
select e.ename,
      decode(e.ename,
      'SMITH','曹贼',
      'ALLEN','大耳贼',
      'WARD','诸葛小儿',
      '无名')"中文名" from emp;    -- 这里的中文名是将该列的列名改为'中文名'




-- 列名使用中文可以使用双引号或者不带引号,如果用单引号会报错。
-- 使用条件表达式一般用通用方式而尽可能少用专用条件表达式,因为使用通用表达式使代码可重用性提高;
-- 多行函数【聚合函数】:作用于多行,返回一个值;
-- 参数1相当于直接输入主键,推荐写count(1)
select count(1) from emp;    -- 查询总数量
select sum(sal) from emp;    -- 工资总和
select max(sal) from emp;    -- 最大工资
select min(sal) from emp;      -- 最低工资
select avg(sal) from emp;      -- 平均工资
-- 分号是一次查询的结束,如果有多条查询语句,每句查询语句末尾应该添加分号;


-- 分组查询[工作中常用]
-- 查询出每个部门的平均工资
-- 分组查询中,出现在group by后面的原始列,才能出现在select后面
-- 没有出现在group by后面的列,想在select 后面,必须加上聚合函数。
select e.deptno,avg(e.sal)   -- e.ename【这里就不能加上e.ename,因为没有出现在group by,并且逻辑上也不对】
from emp e 
group by e.deptno;

-- 查询出平均工资高于2000的部门信息
-- 所有条件都不能使用别名来判断。
-- 因为所有的查询都有先后顺序,where条件优先于select进行,where条件下起了别名,而系统就不知道别名是什么意思了,因为别名代表的数据库名字的内容还未执行;
select e.deptno, avg(e.sal) asal
from emp e
group by e.deptno
having avg(e.sal)>2000;

-- 查询每个部门工资高于800的员工的平均工资
-- where是过滤分组前的数据,having是过滤分组后的数据
-- 表现形式:where必须在group by之前,having是在group by之后
-- 查询出每个部门工资高于800的员工的平均工资
-- 然后再查询出平均工资高于2000的部门
select e.deptno,avg(e.sal) asal
from emp e
where e.sal>800
group by e.deptno
having avg(e.sal)>2000;


-- 执行顺序
-- 分组[having]-排序   排序是在分组的后面


-- 多表查询中的一些概念
-- 笛卡尔积: 两张表的所有记录互相相乘
-- 它没有多大的实际意义,因为很多数据是没有用的
select * from emp e, dept d;

-- 如何过滤掉没用的数据,显示有用的数据,可以使用'等值连接'
-- 等值连接
select * 
form emp e, dept d
where e.deptno=d.deptno;
-- 内连接
select * 
from emp e inner join dept d
on e.deptno=d.deptno;

-- 推荐大家使用等值连接,但是内连接也要学习,方便看别人的。等值连接是在内连接的基础上建立起来的;



-- 查询出所有部门,以及部门下的员工信息,可以使用外连接
-- 外连接 ,分为左外连接和右外连接
-- 右外连接
select * 
from emp e right join dept d
on e.deptno=d.deptno;

-- 查询所有员工信息,以及员工所属部门
-- 左外连接
select * 
from emp e left join dept d
on e.deptno=d.deptno;

-- orcale 中专用外连接
-- 左外连接
select * 
form emp e, dept d
where e.deptno(+)=d.deptno;
-- 右外连接
select * 
form emp e, dept d
where e.deptno=d.deptno(+);




-- 查询出员工姓名,员工领导姓名         [这里的员工表和领导表是同一张表]
-- 自连接:自连接其实就是站在不同的角度把一张表看成多张表;
select e1.ename,e2.ename 
from emp e1, emp e2
where e1.mgr=e2.emp.empno;   -- e1表的所有领导都是e2表的所有员工,这就表示了,e1表示员工表,e2是领导表

-- 查询出员工姓名,员工部门名称,员工领导姓名,员工领导部门名称
select e1.ename,e2.ename
form emp e1, emp e2,dept d1,dept d2
where e1.mgr= e2.empno
and e1.deptno=d1.deptno; 
and e2.deptno=d2.deptno;

-- 我们在工作中更多的是写语句,实现功能,各种sql,所以sql与我们的开发中联系是很紧密的;


-- 子查询:在一条sql语句中嵌套了另外一条sql语句
-- 子查询返回一个值
-- 查询出工资和SCOTT一样的员工信息,这里ename不是主键不能保证非空唯一,如果查询出来是一个集合,就会出问题了,最好是以主键为条件进行查询
select * from emp where sal =
(select sal from emp where ename='SCOTT');
-- 子查询返回一个集合
-- 查询出员工工资和10号部门任意员工一样的员工信息
select * from emp where sal in 
(select sal from emp where deptno =10);
-- 子查询返回一张表
-- 查询出每个部门最低工资,和最低工资员工姓名和该员工所在部门名称
-- 1.先查询出每个部门的最低工资
select deptno,min(sal)
from emp
group by deptno;
-- 2. 三表联查,得到最终结果
select t.deptno,t.msal,e.ename,d.dname
from (select deptno,min(sal) msal
    from emp
    group by deptno ) t,emp e,dept d
where t.deptno= e.deptno
and t.msal=e.sal
and e.deptno=d.deptno;


-- orcale 分页查询 一般要使用三层嵌套;
-- orcale中的分页
-- rownum行号:当我们做select操作的时候,
-- 每查询出一行记录,就会在该行上加一个行号,
-- 行号从1开始,依次递增,不能跳着走。
-- emp表工资倒序排列后,每页五条记录,查询第二页。
select rownum* from emp e rownum <4 order by e.sal desc;
-- select 先执行,然后才执行order by,顺序会乱,因为排序操作会影响rownum的顺序
-- 解决办法:先排序再加行号,使用嵌套查询;
select rownum, * from(
select ruwnum, e.* from emp e order by e.sal desc);
-- 如果要去掉里面的乱的行号,子查询直接不查询里面的rownum即可。

-- emp表工资倒叙排列后,每页五条记录,查询第二页
select * from(
select rownum rn,e.* from(
select * from emp order by sal desc 
) e where rownum <11
) tt where rn >5;


-- rownum 行号不能写上大于一个正数;行号大于一个正数可以使用嵌套,并且给它一个别名,即可进行条件限制;
-- orcale中所有的分页查询都是嵌套两个固定的查询语句,里面可以添加无数个select查询语句进行筛选;该格式记死即可;

Oracle基本语句 1 进入界面 在cmd里面进入oracle的sqlplus界面:sqlplus scott/orcl@orcl 2 连接管理 连接命令 conn[ect] sys/orcl@orcl as sysdba 断开连接 disc[onnect] 修改密码 psssw[ord] 显示用户 show user 退出界面 exit 3 执行编辑sql语句 执行sql语句 start D:\1.sql 或者 @ D:\1.sql 编辑sql语句 edit D:\1.sql www.2cto.com 截取屏幕上的内容 spool D:\1.sql(开始截取) spool off(停止截取) 4 用户管理 创建用户 create user zhu identified by zhu 修改密码 alter user zhu identified by orcl 删除用户 drop user zhu(cascade) cascade代表删除这个用户对应的所有对象 赋予权限 grant create session to zhu grant all on emp to zhu 权限传递 grant all on emp to zhu with grant option(对象权限) 根表有关的权限 grant create session to zhu with admin option(系统权限)其他的权限 收回权限 revoke all on emp to zhu(株连制度) 5 用户口令管理profile 6 表操作 创建表 create table student(SNo number(4),Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null) 修改表 添加一个字段 alter table student add(Address nvarchar2(100) [default value][null/not null]); 修改一个字段的长度 alter table student modify(Name nvarchar2(10),Address nvarchar2(10)); 修改一个字段的类型 alter table student modify(Name varchar2(10)); 修改一个字段的名称 alter table student rename column Name to Name2; 删除一个字段 alter table student drop column Salary; 修改表的名字 rename student to stu; 删除表 drop table student; delete from student;删除所有记录,表结构还在,写日志,可以恢复的,速度慢,Delete 的数据可以恢复。 查看表字段结构 desc student; 7 增删改查 增 insert into student(SNo,Name,Sex,Birthday,Salary) values(1002,'朱文锋','男','01-5月-12',2000); 修改日期的默认格式(临时修改,数据库重启后仍为默认;如要修改需要修改注册表) alter session set nls_date_format='yyyy-mm-dd'; insert into student(SNo,Name,Sex,Birthday,Salary) values(1002,'朱文锋','男','2010-12-12',2000); 插入部分字段和空值 快速加大表中数据 insert into student(SNo,Name,Sex,Birthday,Salary) select * from student; www.2cto.com 改 update student set Name='陈慧琳',Sex='女' where SNo=1002; 删 savepoint a; delete from student where SNo=1003; rollback to a; 总结:删除表的三种方式 delete from student; 删除所有记录,表结构还在,写日志,可以恢复的,速度慢。 truncate table student; 删除表中的所有记录,表结构还在,不写日志,无法找回删除的记录,速度快。 drop table student; 删除表的结构和数据。 查 开启计时 set timing on; 取消重复行 select distinct * from emp; 空值计算 select sal*13+nvl(comm,0)*13 as 年薪 from emp; 子查询(嵌套查询) 单行子查询 多行子查询 多列子查询 分页查询 合并查询 8 数据备份和恢复 备份(多表多文件加上大括号) 导出整个数据库 exp userid=system/orcl@orcl file=d:\all.dmp full=y log=d:\all.log 导出自己的方案 exp userid=scott/orcl@orcl owner=scott file=d:\scott.dmp log=d:\scott.log www.2cto.com 导出其它方案 exp userid=system/orcl@orcl owner=scott file=d:\scott2.dmp log=d:\scott2.log 导出自己的表 exp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp log=d:\emp.log 导出其它方案的表 exp userid=system/orcl@orcl tables=scott.emp file=d:\emp.dmp log=d:\emp.log 导出表的结构 exp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp rows=n log=d:\emp.log 使用直接导出方式 exp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp direct=y log=d:\emp.log 这种方式比默认的常规方式速度要快,当数据量大时,可以考虑使用这样的方法。 这时需要数据库的字符集要与客户端字符集完全一致,否则会报错 恢复(多表多文件加上大括号) 导入整个数据库 imp userid=system/orcl@orcl file=d:\all.dmp full=y log=d:\allimp.log ignore=y 导入自己的方案 imp userid=scott/orcl@orcl file=d:\emp.dmp log=d:\empimp.log 导入其它方案 imp userid=system/orcl@orcl file=d:\emp.dmp fromuser=system touser=scott log=d:\empimp.log 导入自己的表 imp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp 导入表到其它用户 imp userid=system/orcl@orcl tables=emp file=d:\emp.dmp fromuser=system touser=scott log=d:\empimp.log 导入表的结构 imp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp rows=n log=d:\empimp.log 导入数据 如果对象(如比表)已经存在可以只导入表的数据 imp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp ignore=y log=d:\empimp.log 注意formuser是表本来属于哪个用户 touser现在传递给哪个用户 9 表空间 www.2cto.com 创建表空间 create tablespace zhu datafile 'C:\oracle\product\10.2.0\oradata\zhu.dbf' size 50m autoextend on next 50m maxsize unlimited extent management local; create user zhu identified by zhu default tablespace zhu; create table student(SNo number(4),Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null) tablespace zhu; 知道表空间名,显示该表空间包括的所有表 select * from all_tables where tablespace_name='zhu'; 知道表名,查看该表属于那个表空间 select tablespace_name, table_name from user_tables where table_name='emp'; 删除表空间 drop tablespace zhu including contents and datafiles cascade constraints; 10 约束 not null unique primary key foreign key check alter table class add constraint class_key primary key (classid); 11 主键 自动增长 先创建一个表 create table student(SNo number(4) primary key,Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null) 自定义一个sequence create sequence student_sequence increment by 1 start with 1 nomaxvalue nocycle nocache; www.2cto.com 创建一个触发器 create trigger student_trigger before insert on student for each row when(new.SNo is null) begin select student_sequence.nextval into:new.SNo from dual;end;/ 最近插入一行数据 insert into student(Name,Sex,Birthday,Salary) values('朱文锋','男','01-5月-12',2000); GUID 先创建一个表 create table student2(SNo char(32) primary key,Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null) 然后插入一行数据 insert into student2(SNo,Name,Sex,Birthday,Salary) values(sys_guid(),'朱文锋','男','01-5月-12',2000); 在sql server中是newid()函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暗余

码字来之不易,您的鼓励我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值