AI智能
改变未来

Oracle常用操作命令

目 录
Oracle数据库 0
1 版本日志 3
2 增删改查 3
2.1 表操作 3
2.1.1 创建表 3
2.1.2 新增字段 4
2.1.3 修改字段 4
2.1.4 删除字段 4
2.1.5 删除表 4
2.1.6 清空表 4
2.1.7 修改表名 4
2.1.8 复制表数据 4
2.1.9 复制表结构 4
2.1.10 复制表结构及内容 5
2.1.11 复制表的指定字段 5
2.2 普通查询 5
2.2.1 条件查询 5
2.2.2 数学函数 5
2.2.3 查询数据行数 6
2.2.4 分页查询 7
2.2.5 时间处理 7
2.2.6 字符串处理 9
2.2.7 数据转换 10
2.2.8 统计 11
2.2.9 查询去重 12
2.3 新增 12
2.3.1 常用新增 12
2.3.2 数据结果新增 12
2.3.3 数据结果新增指定列 12
2.3.4 新增表结构及数据 12
2.3.5 新增表结构及指定数据 12
2.4 修改 13
2.4.1 常用修改 13
2.4.2 表关联修改 13
2.5 删除 13
2.5.1 常用删除 13
2.5.2 条件删除 13
3 数据备份与恢复 13
3.1 数据备份 13
3.1.1 Exp全库导出 13
3.1.2 数据泵全库导出 14
3.2 数据恢复 14
3.2.1 Imp全库导入 14
3.2.2 数据泵全库导入 14
3.2.3 Delete数据删除恢复 14
3.2.4 更新后的数据恢复 14
4 高级查询 15
4.1 内连接 15
4.2 右连接 15
4.3 左连接 15
4.4 结果集筛选 16
4.5 查询时判断并替换 16
4.6 分组统计 16
4.7 结果集拼接 17
4.7.1 过滤重复数据 17
4.7.2 不过滤重复数据 17
5 存储过程 17
6 触发器 18

2 增删改查
2.1 表操作
2.1.1 创建表
create table Student ( id varchar(300) primary key, name varchar(200) not null);
解析:varchar 代表数据格式为字符型
primary key代表唯一约束
2.1.2 新增字段
alter table Student add (sex varchar2(30) default ‘性别’ not null);
2.1.3 修改字段
alter table Student rename column sex to sex_new;
alter table Student modify (sex_new varchar2(16) default ‘unknown’);
2.1.4 删除字段
alter table Student drop column sex_new;
2.1.5 删除表
drop table Student;
2.1.6 清空表
Delete table Student;
2.1.7 修改表名
alter table Student rename to Student1;
2.1.8 复制表数据
insert into Student (select * from Student1);
2.1.9 复制表结构
create table Student as select * from Student1 where 1>1;
2.1.10 复制表结构及内容
create table Student as select * from Student1;
2.1.11 复制表的指定字段
create table Student as select id, name from Student1 where 1>1;
2.2 普通查询
2.2.1 条件查询
select id,name (case sex when 0 then ‘男’ when 1 then ‘女’ end ) sex from Student;
解析: case 字段 when 值 then 显示值 when 值 then 显示值 end
以上sql查询代表sex值为0时显示男,值为1时显示女
2.2.2 数学函数
2.2.2.1 绝对值
select abs(-2) value from dual;
2.2.2.2 取整函数(大)
select ceil(-2.001) value from dual;
2.2.2.3 取整函数(小)
select floor(-2.001) value from dual;
2.2.2.4 取整函数(截取)
select trunc(-2.001) value from dual;
2.2.2.5 四舍五入
select round(1.234564,4) value from dual;
2.2.2.6 取平方
select power(4,2) value from dual;
2.2.2.7 取平方根
select sqrt(16) value from dual;
2.2.2.8 取随机数
select dbms_random.value() from dual; (默认是0到1之间)
select dbms_random.value(2,4) value from dual; (2-4之间随机数)
2.2.2.9 取符号
select sign(-3) value from dual; –(-1)
select sign(3) value from dual; –(1)
2.2.2.10 取集合的最大值
select greatest(-1,3,5,7,9) value from dual;
2.2.2.11 取集合的最小值
select least(-1,3,5,7,9) value from dual;
2.2.2.12 处理Null值
select nvl(null,10) value from dual;
select nvl(sex,10) score from student;
2.2.3 查询数据行数
2.2.3.1 查询2行数据
select * from student where rownum ❤️;
2.2.3.2 子条件查询
select * from(select rownum rn ,id,name from student) where rn>2;
解析:rownum 代表Oracel行数,赋值给 rn;此刻rn代表行数;因涉及嵌套,所以我们查询时不能直接使用rownum>2来限定行数。
2.2.3.3 区间查询
select * from (select rownum rn, student.* from student) where rn >3 and rn<6;
2.2.3.4 区间排序查询(普通)
select * from (select rownum rn, student.* from student order by id) where rn >3 and rn<6;
2.2.3.5 区间排序查询(高效)
select * from (select rownum rn, t.* from ( select d.* from student d order by id)t where rownum<6 )p where p.rn>3;
2.2.4 分页查询
2.2.4.1 效率低
select * from (select rownum rn, d.* from student d )p where p.rn<=3 and p.rn>=2;
select * from (select rownum rn, d.* from student d )p where p.rn between 2 and 3;
2.2.4.2 效率高
select * from (select rownum rn, d.* from student d where rownum<=3 )p where p.rn>=2;
2.2.5 时间处理
2.2.5.1 查询时间
select to_date(‘2009-07-04 05:02:01’,‘yyyy-mm-dd hh24:mi:ss’)currenttime,
to_char(to_date(‘2009-07-04 05:02:01’,‘yyyy-mm-dd hh24:mi:ss’),‘yyyy’)year,
to_char(to_date(‘2009-07-04 05:02:01’,‘yyyy-mm-dd hh24:mi:ss’),‘mm’)month,
to_char(to_date(‘2009-07-04 05:02:01’,‘yyyy-mm-dd hh24:mi:ss’),‘dd’) day,
to_char(to_date(‘2009-07-04 05:02:01’,‘yyyy-mm-dd hh24:mi:ss’),‘day’) week,
to_char(to_date(‘2009-07-04 05:02:01’,‘yyyy-mm-dd hh24:mi:ss’),
‘day’,‘NLS_DATE_LANGUAGE=American’) week, –设置语言
to_char(to_date(‘2009-07-04 05:02:01’,‘yyyy-mm-dd hh24:mi:ss’),‘hh24’)hour,
to_char(to_date(‘2009-07-04 05:02:01’,‘yyyy-mm-dd hh24:mi:ss’),‘mi’) minute,
to_char(to_date(‘2009-07-04 05:02:01’,‘yyyy-mm-dd hh24:mi:ss’),‘ss’) second
from dual;
2.2.5.2 计算差额月份
Select months_between(
to_date(‘03-31-2014’,‘MM-DD-YYYY’),
to_date(‘12-31-2013’,‘MM-DD-YYYY’)
) “MONTHS”
FROM DUAL;
2.2.5.3 时间段查询
select name, CreateDate from Student where
to_date(CreateDate,‘yyyy-mm-dd hh24:mi:ss’) between
to_date(‘2014-02-01 00:00:00’,‘yyyy-mm-dd hh24:mi:ss’) and
to_date(‘2014-05-01 00:00:00’,‘yyyy-mm-dd hh24:mi:ss’);
2.2.5.4 时间向前推移
select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’) currenttime,
to_char(sysdate – interval ‘7’ year,‘yyyy-mm-dd hh24:mi:ss’) intervalyear,
to_char(sysdate – interval ‘7’ month,‘yyyy-mm-dd hh24:mi:ss’) intervalMonth,
to_char(sysdate – interval ‘7’ day,‘yyyy-mm-dd hh24:mi:ss’) intervalday,
to_char(sysdate – interval ‘7’ hour,‘yyyy-mm-dd hh24:mi:ss’) intervalHour,
to_char(sysdate – interval ‘7’ minute,‘yyyy-mm-dd hh24:mi:ss’) intervalMinute,
o_char(sysdate – interval ‘7’ second,‘yyyy-mm-dd hh24:mi:ss’) intervalSecond
from dual;
解析:year代表年,month代表月,day代表日,hour代表小时,minute代表分,second代表秒。
2.2.5.5 增加月份
select add_months(sysdate,12) newtime from dual;
解析:向前推进12个月
2.2.5.6 月份处理
select extract(month from sysdate) “This Month”,
extract(year from add_months(sysdate,36)) \” Years\” from dual;
解析:显示当前月份与显示当前月份向后36个月的年份
2.2.6 字符串处理
Select substr(‘abcdefg’,1,5)substr, –字符串截取
instr(‘abcdefg’,‘bc’) instr, –查找子串

\'Hello\'||\'World\' concat,                         --连接trim(\'  wish  \') trim,                           --去前后空格rtrim(\'wish  \') rtrim,                           --去后面空格ltrim(\'  wish\') ltrim,                           --去前面空格trim(leading \'w\' from \'wish\') deleteprefix,      --去前缀trim(trailing \'h\' from \'wish\') deletetrailing,   --去后缀trim(\'w\' from \'wish\') trim1,ascii(\'A\') A1,ascii(\'a\') A2, --ascii(转换为对应的十进制数)chr(65) C1,chr(97) C2,   --chr(十进制转对应字符)length(\'abcdefg\') len,                           --lengthlower(\'WISH\')lower,upper(\'wish\')upper,initcap(\'wish\')initcap,                        --大小写变换replace(\'wish1\',\'1\',\'youhappy\') replace,           --替换translate(\'wish1\',\'1\',\'y\')translate,

–转换,对应一位(前面的位数大于等于后面的位数)
translate(‘wish1’,‘sh1’,‘hy’)translate1,
concat(‘11’,‘22’) concat          –连接
from dual;
2.2.7 数据转换
2.2.7.1 转换数字
select to_number(‘0123’)number1, –converts a string to number
trunc(to_number(‘0123.123’),2) number2,
to_number(‘120.11’,‘999.99’) number3,
   to_number(‘0a’,‘xx’) number4, –converts a hex number to decimal
to_number(100000,‘xxxxxx’) number5
from dual;
2.2.7.2 转换字符
select to_char(sysdate,‘yyyymmdd’) from dual;
2.2.7.3 转换日期
select to_date(‘2011-03-24’,‘yyyy/mm/dd’) from dual;
2.2.7.4 转换为原始编码
应用场景:通常应用于两种数据库编码格式不统一时,用于跨库直连处理。
create or replace view v_user_info as select utl_raw.cast_to_raw(cn_name) cn_name from user_info;
解析:原始库创建视图v_user_info 转换为原始编码
create or replace view v_user_info as select utl_raw.cast_to_varchar2(cn_name) cn_name from v_user_info@to_b;
解析:通过DBLINK跨库转换为字符型
2.2.8 统计
2.2.8.1 统计个数
select count(1) as count from student;–效率最高
select count() as count from student;
select count(distinct score) from student;
2.2.8.2 求平均
select avg(score) score from student;
select avg(distinct score) from student;
select name,avg(score) score from student group by name;
2.2.8.3 取最大值
select max(score) from student;
2.2.8.4 取最小值
select min(score) from student;
2.2.8.5 求和
select sum(score) from student;
2.2.9 查询去重
Select distinct name from
Student where to_char(to_date(CreteDate,‘yyyy-mm-dd’),‘yyyy’)=‘2014’ –查询+去重
and to_char(to_date(CreteDate,‘yyyy-mm-dd’),‘yyyy-mm’)>=‘2014-02’
and to_char(to_date(CreteDate,‘yyyy-mm-dd’),‘yyyy-mm’)<=‘2014-04’;
2.3 新增
2.3.1 常用新增
insert into table_name values(id_s.next_value,name_value,age_value,sex_value);
2.3.2 数据结果新增
insert into table1 (select * from table2);
2.3.3 数据结果新增指定列
insert into Student (id,name,sex) select a.user_id,a.user_name,a.sex from hr_users a where a.user_status=‘A’;
2.3.4 新增表结构及数据
create table table1 select * from table2;
2.3.5 新增表结构及指定数据
create table table1 as select id, name from table2 where 1>1;
2.4 修改
2.4.1 常用修改
update Studetn set id = 10 where id=1;
select * from Student for update;
解析:for update 会造成锁表,慎用。
2.4.2 表关联修改
update Student a set a.createdate=(select b.create_time from hr_users b where b.user_id=a.id )
2.5 删除
2.5.1 常用删除
Delete from Student where id=1;
2.5.2 条件删除
Delete from Student where id not in(select b.user_id from hr_users b);
3 数据备份与恢复
3.1 数据备份
3.1.1 Exp全库导出
exp 用户名/密码@SID file=D:\\xx.dmp
3.1.2 数据泵全库导出
create directory expnc_dir as ‘E:\\ncdatabak’;
Grant read,write on directory expnc_dir to scott;
expdp scott/tiger directory=dump_scottdumpfile=full.dmp parallel=4 full=y
注意:执行数据泵一定要在cmd下执行
3.2 数据恢复
3.2.1 Imp全库导入
Imp 用户名/密码@SID file=D:\\xx.dmp full=y
3.2.2 数据泵全库导入
impdp scott/tiger directory=dump_scottdumpfile= xx.dmp full=y
3.2.3 Delete数据删除恢复
select dbms_flashback.get_system_change_number from dual;
解析:查询删除数据的是简单。
select count() from student as of scn 5981166942331;
解析:查询删除数据的总数。
select * from student as of scn 5981166942331;
解析:查询删除的数据。
insert into student select * from student as of scn 5981166942331;
解析:恢复删除的数据
3.2.4 删除数据后回退
3.2.4.1 方法一
flashback table ACT_RU_TASK(回退的表名)
to timestamp to_timestamp(‘2017-07-13 15:30:47’,‘yyyy-mm-dd hh24:mi:ss’);
3.2.4.2 方法二
insert into DP_ORIGINAL_WORKLOAD
select *
from (select *
from DP_ORIGINAL_WORKLOAD hu
where hu.data_source_type = ‘0’)
as of timestamp to_timestamp(‘2017-08-4 16:00:00’, ‘yyyy-mm-dd hh24:mi:ss’);
3.2.5 更新后的数据恢复
create table t_test_1 as
select * from DP_SUB_UNIT_HUMANSET
as of timestamp to_timestamp(‘2016-12-28 13:21:47’,‘yyyy-mm-dd hh24:mi:ss’);
解析:将更新的数据插入至t_test_1表中。
4 高级查询
4.1 内连接
select * from V_SYS_DEPT_DICT a
inner join hr_users b
on a.SERIAL_NO=b.dept_id
where a.SERIAL_NO=‘15564120’;
解析:查询部门中所有人员
4.2 右连接
select * from Student b
right join V_SYS_DEPT_DICT a
on a.SERIAL_NO=b.id
where a.SERIAL_NO=‘15564120’
解析:查询部门及部门员工,如部门员工未空则至显示部门信息;以V_SYS_DEPT_DICT为主表;
4.3 左连接
select b.user_name,b.user_type from hr_users b
left join sys_code_item c1
on b.user_type=c1.item_id
where b.user_type is null;
解析:查询所有用户信息,当关联内容为空时只返回用户信息;以hr_users为主表。
4.4 结果集筛选
select a.dept_id,count(a.user_id) from hr_users a
where a.user_status=‘A’ and a.age>40
group by a.dept_id
having count(a.user_id)>10;
解析:统计科室年龄大于40岁的员工,且员工数量大于10人。
4.5 查询时判断并替换
select a.user_name,
CASE a.sex
WHEN ‘8002-1’ THEN ‘男’
WHEN ‘8002-2’ THEN ‘女’
ELSE ‘未知’ END
from hr_users a where a.user_status=‘A’ and a.age>40;
解析:当a.sex结果为8002-1时显示男,当a.sex结果为8002-2时显示女。
扩展:
CASE WHEN age<= 20 THEN ‘未成年’
WHEN age > 20 AND age <= 30 HEN \’青年
WHEN age > 30 AND age <= 50 HEN \’中年
WHEN age > 50 AND age <= 60 THEN ‘退休’
ELSE NULL END age_class, – 别名命名
4.6 分组统计
SELECT a.dept_id,
SUM(CASE WHEN
a.sex = ‘8002-1’ THEN
1 ELSE 0 END), –男性数量
SUM (CASE WHEN
a.sex = ‘8002-2’ THEN
1 ELSE 0 END) –女性数量
FROM hr_users a GROUP BY a.dept_id;
解析:按列显示一个科室的男性数量与女性数量。
4.7 结果集拼接
4.7.1 过滤重复数据
SELECT A.ID,A.NAME FROM Student a
UNION
SELECT TO_CHAR(B.USER_ID),B.USER_NAME FROM HR_USERS B WHERE B.USER_STATUS=‘A’;
解析:当A表中的数据与B表中的数据一致时,将不显示重复的数据。
4.7.2 不过滤重复数据
SELECT A.ID,A.NAME FROM Student a
UNION ALL
SELECT TO_CHAR(B.USER_ID),B.USER_NAME FROM HR_USERS B WHERE B.USER_STATUS=‘A’
解析:当A表中的数据与B表中的数据一致时,依旧全部显示。
5 存储过程
CREATE OR REPLACE PROCEDURE 存储过程名(传入参数A,传入参数B)
–解析:设置传入参数A,传入参数B
AS
变量A number(12);
变量B varchar(50);
–解析:申明变量A 为数字型,申明变量B为字符型
BEGIN
IF传入参数A IS NOT NULL THEN
–解析:当传入参数A为空时
SELECT GET_NEW_ID() into变量A FROM DUAL;
–解析:给变量A赋值
SELECT GET_INPUT_CODE(传入参数A) into 变量B FROM DUAL;
–解析:给变量B赋值
INSERT INTO Student (id,name,input_name) values (变量A, 传入参数B, 变量B);
ELSE
INSERT INTO logs(menu) values (调用存储过程插入失败);
END IF;
END存储过程名;
/
6 触发器
create or replace trigger Tri_Student_delete
–解析:Tri_Student为触发器名称
after delete On Student
–before:表示在数据库动作之前触发器执行;
–after:表示在数据库动作之后触发器执行。
–insert:数据库插入会触发此触发器;
–update:数据库修改会触发此触发器;
– delete:数据库删除会触发此触发器;
–解析:在删除Student表之前执行此触发器
Declare
v_id number(20);
Begin
Insert into Student_Delete(Select * from Student);
END;
/

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Oracle常用操作命令