Oracle常用命令集合

175 阅读3分钟

删 除 表:drop table 表名; 改字段名:alter table 表名 rename column 列名 to 新列名 创 建 表:create table 表名(字段名 字段类型,字段名); 删表数据:truncate table 表名; delete from 表名; 增加字段:alter table 表名 add(字段名 varchar2(30) 修改字段类型: alter table tableName modify(columnName 类型); ps:alter table test modify(name varchar(255)); oracle为应对大量的文本可以设置字段类型为clob 查看具体的字符:select to_char(clob字段) from table; 1.rownum oracle每读取到一行就给改行加一个编号。rownum是在查询语句扫描每条记录时产生的, 所以不能使用“大于”或“大于等于”符号,只能使用“小于”或“小于等于” 。(只用“等于”也不行)

2.求字符串长度 length 3.字符串拼接concat 4.字符串替换replace select replace('adbcd' , 'd','d') from dual 5.求字符串的子串substr 6.四舍五入函数round 7.截取函数trunc 不会四舍五入 8.加月函数 add_months :在当前日期基础上加指定的月 9.求所在月最后一天last_day 10.日期截取trunc 11.数字转字符串to_char 12.日期转字符串to_char select to_char(sysdate,'yyyy-mm-dd') from dual; 13.字符串转数字to_number 14.字符串转日期to_date select to_date('2018-03-09','yyyy-mm-dd') from dual; 15.空值处理函数 nvl select nvl(null,0) from dual; select nvl2(检测值,不为null的值,为null的值) from dual; 16.条件取值 decode decode()函数:多数值判断,类似于if…else语句,不同的是decode()判断的是数值而不是逻辑条件

17.select (select name from t_area where id= areaid ) 地区, sum( case when month>='01' and month<='03' then money else 0 end) 第一季度, sum( case when month>='04' and month<='06' then money else 0 end) 第二季度, sum( case when month>='07' and month<='09' then money else 0 end) 第三季度, sum( case when month>='10' and month<='12' then money else 0 end) 第四季度 from t_account where year='2012' group by areaid

行列转换 18.union all(并集),返回各个查询的所有记录,包括重复记录。 union(并集),返回各个查询的所有记录,不包括重复记录。 intersect(交集),返回两个查询共有的记录。 minus(差集),返回第一个查询检索出的记录减去第二个查询检索出的记录之后剩余的记录。 在使用链接查询的时候需要返回的两列有相同的列数。

  1. declare v_num number:=1; 定义变量,定义循环初始值 begin loop 开始循环 dbms_output.put_line(v_num); v_num:=v_num+1; exit when v_num >100; 循环终止条件 end loop; end;

  2. begin for v_num in 1..200 for循环 loop dbms_output.put_line(v_num); end loop; end; 21.存储过程: create [ or replace ] procedure 存储过程名称 (参数名 类型, 参数名 类型, 参数名 类型) is|as 变量声明部分; begin 逻辑部分 [exception 异常处理部分] end;

22.call 存储过程名称(参数); 在代码中调用存储函数 23. 得到星期一的日期 select trunc(sysdate,''DD'')-to_char(sysdate,''D'')+2 from dual; --得到星期天的日期 select trunc(sysdate,''DD'')-to_char(sysdate,''D'')+8 from dual;