oracle常用命令

1、常用的命令:

系统登陆:sqlplus / as sysdba

用户登陆:sqlplus 用户名/密码

授权:grant connect ,resource,dba to 用户名;

1.1查询

1.1.1单表查询

查看当前登陆的用户:show user

查看当前用户的表:select *|列名  from tab;

查看当前用户的表的数据:select * from 表名;

查看表的结构:desc 表名;

跨用户查表:select *|列名 from 用户.表名;

设置别名:select 列名 as xxx from 表名;或者不写as:select 列名  xxx from 表名;

去重数据:select distinct 列名 from 表名;

字符串连接查询:select ‘xxx’  || 列名 || ‘xxx’  || 列名 || ‘xxx’  || 列名 from 表名;

四则运算(不推荐使用,影响性能):select 列名*10 from 表名;

限定查询:select 列名 from 表名  where 条件;

非空限制:select * from 表名  where 列名 is not null;select * from 表名  where 列名 is null;

查询日期区间:select * from 表名  where 列名 between to_date('1991-1-1','yyyy-MM-dd') and to_date('1991-1-9','yyyy-MM-dd')

范围查询:select * from 表名  where 列名 in (值1,值2....值n);select * from 表名  where 列名 not in (值1,值2....值n);

模糊查询(%匹配任何长度,_匹配一个长度内容):select * from 表名  where 列名 like ‘_值%’;

不等于:select * from 表名  where 列名 != 值;或者select * from 表名  where 列名 <> 值;

排序(默认正序):select * from 表名 order by 列名 asc;倒序:select * from 表名 order by 列名1 desc 列明2 desc;

分页查询:1)查询全量数据 select * from 表名;

     2)以第一步的结果集作为一张表 select rownum rn,a.* from (select * from 表名) a where rownum<6

     3)以第二步的结果集作为一张表,每页6条数据 :select * from (select rownum rn,a.* from (select * from 表名) a where rownum<11)b where b.rn >5

     注:开始行号  =(当前页码-1)*每页记录数;结束行号 = 当前页码*每页记录数+1

 1.1.2多表查询

 联合查询:select * from 表1 表2 where 表1.列1=表2.列1;

外连接-左连接:select * from 表1 表2 where 表1.列1=表2.列1(+);  select * from 表1 left join 表2 on 表1.列1=表2.列1;

1.1.3其他查询

exists、not exists,in关键字尽量少用影响性能,可以用exists代替:select * from 表名 where 列 in (子查询)

            select * from 表名 where exists(子查询)

并集操作,union(去除相同数据)、union all(不去除相同数据),合并的时候列数一致、类型一致:select * from 表名 where 列 >10 union select * from 表名 where 列 <20

1.2函数

 1.2.1单行函数(在dual伪表中查询)

upper将小写转换为大写:select upper('abc') from dual;

lower将大写转换为小写:select lower('ABC') from dual;

initcap首字母daxie:select initcap('aBC') from dual;

concat字符串连接或者使用||:select concat('abc',‘def’) from dual;select 'abc' || ‘def’ from dual;

substr字符串的切分(索引从1开始,到3结束):select substr('abcdef',‘1,3) from dual;

length查询长度:select length('abcdef') from dual;

replace字符串替换(将a替换为z):select replace('abcdef'',‘a’,‘z’) from dual;

 1.2.2数值函数

round四舍五入(2保留两位小数):select round(10.4222,2) from dual;

trunc去除小数(2保留两位小数):select trunc(10.4222,2) from dual;

mod取余数:select mod(10,3) from dual;

1.2.3日期函数

sysdate当前日期:select sysdate from dual;

months_between两个月中间的月数:select months_between(sysdate,crea_time) from 表名;

add_months(增加12月):select  add_months(sysdate,12) from dual;

next_day(下个星期一的日子):select  next_day(sysdate,‘星期一’) from dual;

last_day( 本月最后一个日子):select  last_day(sysdate,‘星期一’) from dual;

1.2.4 转换函数

to_char转换日期格式,fm去日期前面0:select  to_char(sysdate,‘fmyyyy-MM-dd HH24:mi:ss’) from dual;

to_char转换数值格式:select  to_char(‘11111’,'99.999') from dual;

to_number将字符串转换为数值:select  to_number(‘11111’) from dual;

to_date将字符串转换为日期:select  to_date(‘2020-04-04’,'yyyy-MM-dd') from dual;

1.2.5通用函数

nvl将空值转换为默认的值:select  nvl(列,‘默认的值’) from 表名;

decode相当于if,else:select  decode(列,‘列值1’,‘值1’,‘列值2’,‘值2’,......‘其他’) from 表名;

case when相当于decode:select  case when 列=‘列值1’ then‘值1’,when 列=‘列值2’ then ‘值2’,......else ‘其他’) from 表名;

1.2.6分组函数

count查询统计记录数:select  count(*) from 表名;

min最小值:select  min(列) from 表名;

max最大值:select  max(列) from 表名;

avg平均值:select  avg(列) from 表名;

sum求和:select  sum(列) from 表名;

分组查询(having作用于分组函数上):select  sum(列1) 列2 列3 from 表名 group by 列2, 列3;

    select  sum(列1) 列2  from 表名 group by 列2 having 条件;

1.3增删改

插入数据:insert into 表名 (列1,列2,列3)values (值1,值2,值3);

修改数据:update 表名 set  列1=值1,列2=值2,列3=值3 where 列0=‘值0’;

删除数据:delete from 表名 where 列1=值1

1.4表操作

1.4.1表管理

建表:create table 表名(字段1 数据类型 defalut 值,字段2 数据类型 defalut 值,......);

删表:drop table 表名;

改表-添加列:alter table 表名 add (字段1 数据类型 ,字段2 数据类型)

改表-修改列:alter table 表名 modify (字段1 数据类型 ,字段2 数据类型)

1.4.2表约束

主键primary key:create table 表名(字段1 数据类型 primary key,字段2 数据类型 defalut 值,......);

非空约束not null:create table 表名(字段1 数据类型 ,字段2 数据类型 not null defalut 值,......);

唯一约束unique:create table 表名(字段1 数据类型 ,字段2 数据类型 unique ,......);

检查约束check:create table 表名(字段1 数据类型 ,字段2 数据类型 check (列 in (值1,值2)) ,......);

外键约束(on delete cascade 删除主表同时删除子表数据):create table 表名(字段1 数据类型 ,字段2 数据类型 ,......,constraint 表名_列_别名  foreign key(列) references 外键表名(外键表列)on delete cascade);

2、ORACLE的数据类型 

字段类型  中文说明  限制条件  其它说明

CHAR  固定长度字符串 最大长度2000 bytes

VARCHAR2   可变长度的字符串 最大长度4000 bytes 可做索引的最大长度749

NCHAR   根据字符集而定的固定长度字符串 最大长度2000 bytes

NVARCHAR2   根据字符集而定的可变长度字符串 最大长度4000 bytes

DATE   日期(日-月-年) DD-MM-YY(HH-MI-SS) 经过严格测试,无千虫问题

LONG   超长字符串 最大长度2G(231-1) 足够存储大部头著作

RAW   固定长度的二进制数据 最大长度2000 bytes 可存放多媒体图象声音等

LONG   RAW 可变长度的二进制数据 最大长度2G 同上

BLOB   二进制数据 最大长度4G

CLOB   字符数据 最大长度4G

NCLOB   根据字符集而定的字符数据 最大长度4G

BFILE   存放在数据库外的二进制数据 最大长度4G

ROWID   数据表中记录的唯一行号 10 bytes ********.****.****格式,*为0或1

NROWID   二进制数据表中记录的唯一行号 最大长度4000 bytes

NUMBER(P,S)   数字类型 P为总位数,S为小数位数

DECIMAL(P,S)   数字类型 P为总位数,S为小数位数

INTEGER   整数类型 小的整数

FLOAT   浮点数类型 NUMBER(38),双精度

REAL   实数类型 NUMBER(63),精度更高

3、其他

3.1事务

commit; oracle数据库增删改要开启事务,事务必须提交之后才能变更

rollbacnk;没有提交之前,可以进行回滚

执行update时没有提交之前事务是处于挂起的状态,这条数据会锁住

3.2视图

创建视图:create view 视图名 as sql语句;

查询视图:select * from 视图名;

创建或者覆盖视图:create or replace view 视图名 as sql语句;

创建只读视图:create view 视图名 as sql语句 with read only;

3.3序列

创建自增长序列(序列虽然是给表使用的,但是没有绑定表,任何一张表都可以使用这个序列):create sequence 列名(或者序列名);

查询序列的下一个值:select 序列名.nextval from dual;

插入值时的用法:insert into 表名(id 列1) values  (序列名.nextval,值1)

查询序列当前的值:select 序列名.currval from dual;

3.4索引

创建单例索引:create index 索引名 on 表名(列);

复合索引(查询数据使用的时候是有顺序的):create index 索引名 on 表名(列1,列2);

4、oracle高级(简单学习记录)

4.1PLSQL

4.1.1程序语法

declare 说明部分 begin 语句序列 exception 例外处理语句 End;

1)输出hello world!例:

begin

  dbms_output.put_line('hello word!');

end;

2)定义一个变量、常量并进行输出,例:

declare

  字段名1 字段类型;

  字段名2 字段类型 :=10;

begin

  字段名 := 值;

  dbms_output.put_line(字段名1);

  dbms_output.put_line(字段名2);

end;

3)引用某表某列的数据类型变量

declare

  字段名1 表名.ename%type;

begin

  select 列 into 字段名1 from 表名 where 条件;

  dbms_output.put_line(字段名1);

end;

4)记录型变量

declare

  字段名1 表名%rowtype;

begin

  select * into 字段名1 from 表名 where 条件;

  dbms_output.put_line(字段名1.列);

end;

4.1.2if分支

例:

declare

  字段名1 数据类型:=&num;

begin

  if 字段名1 < 10 then

    dbms_output.put_line(xxx);

  elseif 字段名1 < 20 then

    dbms_output.put_line(vvv);

  else

    dbms_output.put_line(zzz);

  end if;

end;

4.1.3循环loop

例1:

declare

  字段名1 数据类型:= 0;

begin

  while 字段名1 <=10 loop

    字段名1 := 字段名1+1;

    dbms_output.put_line(字段名1);

  end loop;

end;

例2:

declare

  字段名1 数据类型:= 0;

begin

   loop

    exit when 字段名1=10

    字段名1 := 字段名1+1;

    dbms_output.put_line(字段名1);

  end loop;

end;

例3:

declare

  字段名1 数据类型:= 0;

begin

   for 字段名1 in 1...10 loop

    dbms_output.put_line(字段名1);

  end loop;

end;

4.1.4游标

例:

declare

  变量1 表名%rowtype;                      # 定义记录型变量

  cursor c1 is select * from 表名;       # 定义游标

begin

  open c1;

  loop

    fetch c1

      into 变量1;      # 从游标中取值

    exit when c1%notfound;

    dbms_output.put_line(变量1.列);

  end loop;

  close c1;

end;

4.1.4例外(异常)

1、no data found  没有找到数据

2、too_many_rows  返回太多行

3、zere_divide  被零除

4、value_error  算术或转换错误

5、timeout_on_resource  等待资源发生超时

4.2存储过程

1、 简单创建存储过程:

create or  replace procedure 存储过程名 as

begin

  dbms_output.put_line(‘hello world’);

END;

2、给指定的的员工涨工资创建存储过程(事务的提交在调用端使用)

create or  replace procedure 存储过程名 (变量 in 表名.列名%type)as   # 指定条件的数据类型 

  变量1  表名%rowtype;  # 定义变量

begin

  select * 变量1 from 表 where 表名.列 = 变量;

  update 表名 set 表名.列2 = 表名.列2 + 20 where 表名.列 = 变量;

end;

3、调用存储过程

declare

  ysal number;

begin 

   存储过程名(7369,ysql);

  dbms_output.put_line(ysal);

end;

4、存储函数(老项目用)

4.3触发器

触发器用于:数据的确认、实施复杂的安全性检查、做审计,跟踪表上所做的数据操作等、数据的备份和同步

触发器类型:语句级触发器—在指定的操作语句操作之前或之后执行一次,

      行级触发器—触发语句作用的每一条记录都被触发,在行级触发器使用old和new伪记录变量,识别值的状态

创建触发器:

create or  replace trigger 触发器名

before insert on 表名

begin

  dbms_output.put_line(‘插入成功‘’);

end 触发器名;

 

原文地址:https://www.cnblogs.com/wu-wu/p/13081871.html