您的位置:首页 > 数据库 > Oracle

项目中 oracle操作命令记录

2017-11-14 16:54 274 查看
模糊查询表名

 select   table_name from user_tables where table_name like '%表名%'

将两个字段结果拼接在一起

select  concat("字段一","字段二")    from dual;    结果为 “字段一字段二”

更改表中 字段为另一个表中字段的值

update tabeName(表名)  tname(别名)  set  字段名 = ( selet 字段名  from  表名  别名   where  别名。字段名 =  别名。字段名)

将表中的字段替换,并且字段的长度大于指定的位数

update 表名  set 字段名 = replace(字段名,'替换的字符','替换后的字符') where 字段  in  (select 字段名 from  表名 where  length(字段)  >  指定长度);

导出序列

select 'create sequence ' || SEQUENCE_NAME || ' minvalue ' || MIN_VALUE ||

       ' maxvalue ' || MAX_VALUE || ' start with ' || LAST_NUMBER ||

       ' increment by ' || INCREMENT_BY || ' nocache ' || ' ;'

from DBA_SEQUENCES

where SEQUENCE_OWNER = UPPER('用户名称');

克隆表的结构:

create table tableName as select * from 表名 where 1=2;

克隆整个表:

create table 表名 as select * from 数据库.表名

SELECT * FROM T_PUB_JQXZDM ORDER BY JQXZBH;

select JQXZBH FROM T_PUB_JQXZDM order BY JQXZBH;

select JQXZBH,bzjqxzfbh,bzjqxzbh FROM T_PUB_JQXZDM order BY JQXZBH;

--删除表中的列

ALTER TABLE T_PUB_JQXZDM DROP COLUMN gxsj;

--查看表中列的类型

select   *   from   cols     

WHERE   TABLE_name=upper('T_PUB_JQXZDM');

--查询长度为 4 的结果

select * from T_PUB_JQXZDM tt where length(tt.jqxzbh)=2

order by jqxzbh 

--截取字符串

select substr(jqxzbh,0,4) from t_pub_jqxzdm order by jqxzbh

--截取字符串

SELECT 

      DISTINCT 

      CASE

         WHEN LENGTH(TRIM(TT.JQXZBH)) = 8

          THEN

          TT.JQXZBH

       END A, --子类

       CASE

         WHEN LENGTH(TRIM(TT.JQXZBH)) = 6 THEN

          TT.JQXZBH

       END B --父类

  FROM T_PUB_JQXZDM TT

 WHERE TT.JQXZBH like '010101%'

 AND TT.JQXZBH IS NOT NULL

  order by b;

 

--创建临时表,数据从查询结果中提出         

create table tests as SELECT trim(TT.JQXZBH) A, --子类

       SUBSTR(trim(TT.JQXZBH), 1, LENGTH(trim(TT.JQXZBH))-2) B --父类

  FROM T_PUB_JQXZDM TT

 WHERE SUBSTR(trim(TT.JQXZBH), 1, LENGTH(trim(TT.JQXZBH))-2) IN

       (SELECT trim(BB.JQXZBH) --子类

          FROM T_PUB_JQXZDM BB) order by b;

          

--创建临时表,并将原表清除,从临时表插入          

CREATE TABLE temp AS

SELECT trim(TT.JQXZBH) A, --子类

       SUBSTR(trim(TT.JQXZBH), 1, LENGTH(trim(TT.JQXZBH))-2) B, --父类
tt.bzjqxzbh

  FROM T_PUB_JQXZDM TT

 WHERE SUBSTR(trim(TT.JQXZBH), 1, LENGTH(trim(TT.JQXZBH))-2) IN

       (SELECT trim(BB.JQXZBH) 

          FROM T_PUB_JQXZDM BB);

TRUNCATE TABLE T_PUB_JQXZDM;--清除原表从临时表插入数据

INSERT INTO T_PUB_JQXZDM  --插入

SELECT * FROM temp;

COMMIT;

--查询临时表

select * from temp order by b;

--更改

UPDATE T_PUB_JQXZDM TT

   SET TT.BZJQXZFBH = SUBSTR(TRIM(TT.JQXZBH), 1, LENGTH(TRIM(TT.JQXZBH)) - 2) --父类

 WHERE SUBSTR(TRIM(TT.JQXZBH), 1, LENGTH(TRIM(TT.JQXZBH)) - 2) IN

       (SELECT TRIM(BB.JQXZBH) FROM T_PUB_JQXZDM BB);

COMMIT;

000

SELECT JQXZBH FROM T_PUB_JQXZDM ORDER BY JQXZBH;

select SUBSTR(TRIM(TT.JQXZBH),1,LENGTH(TRIM(TT.JQXZBH))-2) FROM T_PUB_JQXZDM TT ORDER BY JQXZBH;

select length(t.jqxzbh)-2 from t_pub_jqxzdm t;

select substr(t.jqxzbh,1,length(t.jqxzbh)-2) from t_pub_jqxzdm t order by jqxzbh;

select length(jqxzbh)-1 from t_pub_jqxzdm t order by jqxzbh;

-- 删除表

drop table T_PUB_JQXZDM;

--查询两张表不同的数据

select * from 表名 a where a.id not in (select b.id from 表名 b where a.id = b.id  )

--创建临时表从查询的结构

create table tests as 

select * from 表名 a where a.id not in (select b.id from 表名 b where a.id = b.id  )

--插入数据从临时表

inset into 表名 (字段)

select 字段 from 表名

--表插入一些字段的数据  从别的表

insert into 表名  (字段名)

select 字段名   对应上面插入的字段名  ,字段名,对应的字段名  from  表名

--使用case when 修改数据

update 表名 set 列名 = 

case 

  when 条件 = 条件 then  值

  when 条件 = 条件 then 值

end

 where  条件

 

 

 

--只删除表数据,不删除表结构

 delete from 表名 where 1=1

--模糊查询数据库中的表

select table_name from user_tables where table_name like '%T_JQHF%';

--在表中添加一列

alter table 表名 add(列名 类型)

--将添加的一列添加数据

update 表名  set  字段 = 值

--将添加的列设为非空

alter table 表名 modify  字段名 not null

--替换一列中指定的字符

update 表名 set 列 = replace(列,'需要替换的字符','替换后的字符');

--修改列名

alter table  表名  rename column 列名 to  新的列名

--删除表中的一列

alter table 表名 drop column 列

--修改表名

alter table 表名  rename to 新的表名

--列和表添加注释

comment  on  column  表名.字段名   is  '注释内容';

comment on column OPERATOR_INFO.MAIN_OPER_ID is '归属操作员';

comment on table 表名  is  '注释内容';

comment on table OPERATOR_INFO is  '操作员信息表';

--查询删除的表数据

select * from 表名 as of timestamp to_timestamp('时间点','yyyy-mm-dd hh24:mi:ss')

--插入查询出的删除的数据

insert into t_jqhf_dxfs (select * from t_jqhf_dxfs as of timestamp to_timestamp('2017-08-23 17:00:00','yyyy-mm-dd hh24:mi:ss'))

--递归查询,根据编号查询子节点

SELECT A.gztzbh

   FROM T_JCJ_GZTZ A

  START WITH gztzbh = '441900100500'

 CONNECT BY NOCYCLE PRIOR A.gztzbh = A.gztzbhfbh

 ORDER BY A.gztzbh;

    

    

    

    

    

    

    

    --让那个字符串字段字段相加 根据编号

    select u.yhbh,wm_concat(r.jsmc) jsmc from T_PUB_ROLE R
left join T_PUB_AUTH t on
t.jsid = R.jsid
left join T_Pub_USER U on t.yhbh = U.yhbh
where t.yyid = '481' GROUP BY u.yhbh

--查询表的存储过程名称
select object_name,status from user_objects where object_type='PROCEDURE';

--查询处存储过程的名称后查看源码定义,name =  存储过程名称
select object_name,status from user_objects where object_type='PROCEDURE';

SELECT * FROM ALL_SOURCE where TYPE='PROCEDURE' AND NAME ='P_CTI_GETCALLERINFO';
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: