11sql

-- 添加字段
alter table hist_trans add (sett_batch_no nvarchar2(50));
alter table hist_trans add (check_time date) ;
alter table hist_trans add (refund_status integer default 0) ;
alter table hist_trans add sett_status varchar2(100);

-- 修改字段
alter table hist_trans modify mch_ev varchar2(100);

-- 删除字段
alter table hist_trans drop column refund_status ; 

-- 添加注释
comment on column hist_trans.refund_status is '退款状态:1为已退款,2为未退款';

-- 添加主键
alter table sys_syspara_info add constraint pk_sys_syspara_info primary key (si_id)

-- 添加外键
alter table sys_syspara_info add constraint fk_systype_ref_syspara foreign key (si_para_id) references sys_syspara_type (st_id);

-- 删除主键
alter table sys_syspara_info drop constraint pk_sys_syspara_info;

-- 删除外键
alter table sys_syspara_info drop constraint fk_systype_ref_syspara;

-- 建一个和a表结构一样的空表 1=2表示不添加数据,只创建表结构
create table hist_trans as select * from sys_syspara_info where 1=2;
create table hist_trans(refund_status) as select refund_status from sys_syspara_info where 1=2;

-- 连接字符串
select t.mid||t.cname showName from mch_info t;
select concat(t.mid, t.cname) showName from mch_info t;

-- 查出当前用户所有表名。
select unique tname from col;
select * from tab;

-- 修改字段名称 qq 改为qq2
create table alist_table_copy as select ID,NAME,PHONE,EMAIL,QQ as QQ2,ADDRESS from alist_table;
drop table alist_table;
rename alist_table_copy to alist_table

-- 处理小数
select trunc(1.1415926) from dual;    --截掉小数部分
select trunc(1.1415926,2) from dual ;  --保留两位小数(不做四舍五入处理)
select trunc(101.1415926,-1) from dual; --返回零 截取整数部分第一位,并以零代替

-- 返回字符串的长度
select txn_date,length(to_char(txn_date)) len from hist_trans;

-- 取子字符串,从start开始,取count个  
select substr('13088888888',3,8) from dual; 

-- 在一个字符串中搜索指定的字符,返回发现指定的字符的位置 函数instr(c1,c2,i,j) c1被搜索的字符串  C2希望搜索的字符串 i搜索的开始位置默认为1 j出现的位置默认为1  
select t.ac_merno, instr(t.ac_operator, 'ys', 1) from posp_account t;
select instr('oracle traning','ra',1,2) instring from dual;

-- 日期格式转换
select to_char(sysdate,'yyyy mm dd hh24:mi:ss') from dual;
select to_date(t.txn_date||t.txn_time, 'yyyy-mm-dd hh24:mi:ss') as txn_time  from hist_trans t;
select sysdate,to_char(sysdate,'D') from dual;  
select to_char(sysdate,'yyyy') from dual;    --年  
select to_char(sysdate,'Q') from dual;        --季  
select to_char(sysdate,'mm') from dual;       --月  
select to_char(sysdate,'dd') from dual;       --日  
ddd   年中的第几天  
WW    年中的第几个星期  
W    该月中第几个星期  
D    周中的星期几  
hh    小时(12)  
hh24  小时(24)  
Mi    分  
ss    秒

-- decode if语句的另一形式。将输入数值与参数列表比较,返回对应值。应用于将表的行转换成列以及if语句无法应用的场合。当与sign联合使用时功能扩展,可以判断大于小于的情况.
select decode(t.recorded_method, 0, '自动入账', 1, '人工入账', '不入账') from posp_recorded_method t;
decode(sign(values-100), -1, -10, 1, 10, 0) 当VALUE<100时返回-10 当VALUE>100时返回10 当VALUE=100时返回0
select sum(decode(est_int_key,77771,1,0)) a, sum(decode(est_int_key,77772,1,0)) b, sum(decode(est_int_key,77773,1,0)) c from pms_blk

-- nvl 空值置换 当value为NULL值时返回nullvalue 否则返回value的值
select nvl(t.limited_amount, 0) FROM posp_recorded_method t

-- 创建索引
create index 索引名 on 表名 (字段名);
create index index_sb_bank_id on posp_subbranch (sb_bank_id)
tablespace qbposp
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64k
    next 1m
    minextents 1
    maxextents unlimited
  ) ;

-- 创建序列
create sequence SYS_MER_ROLE_SEQ
minvalue 1
maxvalue 9999999999999999999999999999
start with 100
increment by 1 ;
cache 20;
 
-- 创建表
create table posp_news
(
  news_id            integer not null,
  news_title_cn      varchar2(100) not null,
  news_content_cn    varchar2(4000) not null,
  news_status        integer not null,
  news_expirydate    date not null,
  news_send_name     varchar2(50),
  news_send_time     date,
  news_remark        varchar2(1000)
)
tablespace qbposp
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64k
    next 1m
    minextents 1
    maxextents unlimited
  );

commit;
EXIT;

猜你喜欢

转载自bybed.iteye.com/blog/1595458
今日推荐