oracle实用基本操作

1、建表

create table t_test0819 ( id number,name varchar2(32),age number(3),birthday date);


2、建序列

create sequence se_test0819
start with 1
increment by 1
minvalue 1
maxvalue 9999999999999999999
alter table t_test0819 add constraints se_test0819 primary key (id)

 

3.建索引

create index ind_test0819 on t_test0819(age)


4.表结构的操作

alter table t_test0819 add  id1 number;--增加一列id1
alter table t_test0819 rename  column id1 to agee;--改列名
alter table t_test0819 drop column agee;--删除列agee
alter table t_test0819 modify name varchar(64);--把列name的数据类型改为varchar(64)


4.基本的增删除改查

insert into t_test0819(id,name,age,birthday) values (se_test0819.nextval,'tom',23,to_date('2013-01-01','yyyy-mm-dd'))
update t_test0819 set age=100 where id=1;
delete from t_test0819 where id=2;
select * from t_test0819;


5.建函数

create function fun_test0819(id number)
return varchar2
as 
name varchar2(20);
begin 
  for ids in (select name from t_test0819 where id between 0 and id) loop
    name:=name||'=>'||ids.name;
    end loop;
    return name;
    end fun_test0819;

select fun_test0819(20) from dual


6.建存储过程

create procedure pro_t_test0819(num in varchar2)
as
i number; 
begin
  i:=0;
  while i<num loop
    i:=i+1;
    insert into t_test0819(id,name,age,birthday) values (se_test0819.nextval,'tom',i,to_date('2013-01-01','yyyy-mm-dd'));
    commit;
    end loop;
    end  pro_t_test0819;
    

declare
  i number;
begin 
  i:=5;
  pro_t_test0819(i);
  end;

select * from t_test0819


6.常用操作

delete from t_test0819 t
where t.rowid>(select min(rowid) from t_test0819 t1 where t1.name=t.name)--删除相同数据留一条
select * from user_recyclebin--查找删除的表
flashback  TABLE a TO BEFORE DROP;--利用闪回恢复表
select * from t_test0819 as of timestamp sysdate-1/(24)--1小时前的表
select * from t_test0819 t inner join (select * from t_test0819 where age=1) t1 on t.id=t1.id--内连接,只返关联的数据
select * from t_test0819 t left join (select * from t_test0819 where age=1) t1 on t.id=t1.id--左连接,返回on左边的表的所有数据和右边表关联数据
select * from t_test0819 t right join (select * from t_test0819 where age between 1 and 3) t1 on t.id=t1.id--右连接,返回on右边的表的所有数据和左边表关联数据
select * from t_test0819 t full join (select * from t_test0819 where age between 1 and 3) t1 on t.id=t1.id--全连接,返回关联数据和两表没关联数据
select * from t_test0819 t cross join (select * from t_test0819 where age between 1 and 3) t1 --返回M*N条数据

select * from t_test0819 t where exists (select * from t_test0819 where age between 1 and 3 and id=t.id )--exists的作用等同于in select * from t_test0819 t where not exists (select * from t_test0819 where age between 1 and 3 and id=t.id )--exists的作用等同于not in select * from t_test0819 t union all (select * from t_test0819 where age between 1 and 3  )--将两表相同列的数据拼在一起,要求列数目一样,数据类型一样(包含相同数据) select * from t_test0819 t union (select * from t_test0819 where age between 1 and 3  )--将两表相同列的数据拼在一起,要求列数目一样,数据类型一样(不包含相同数据)

select case age when 1 then 100 when 2 then 200 else 300 end from t_test0819 --当列age为何值时,输出什么




 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数; SQL> select ascii(’A’) A,ascii(’a’) a,ascii(’0’) zero,ascii(’ ’) space from dual; A A ZERO SPACE --------- --------- --------- --------- 65 97 48 32 2.CHR 给出整数,返回对应的字符; SQL> select chr(54740) zhao,chr(65) chr65 from dual; ZH C -- - 赵 A 3.CONCAT 连接两个字符串; SQL> select concat(’010-’,’88888888’)||’转23’ 高乾竞电话 from dual; 高乾竞电话 ---------------- 010-88888888转23 4.INITCAP 返回字符串并将字符串的第一个字母变为大写; SQL> select initcap(’smith’) upp from dual; UPP ----- Smith 5.INSTR(C1,C2,I,J) 在一个字符串中搜索指定的字符,返回发现指定的字符的位置; C1 被搜索的字符串 C2 希望搜索的字符串 I 搜索的开始位置,默认为1 J 出现的位置,默认为1 SQL> select instr(’oracle traning’,’ra’,1,2) instring from dual; INSTRING --------- 9 6.LENGTH 返回字符串的长度; SQL> select name,length(name),addr,length(addr),sal,length(to_char(sal)) from gao.nchar_tst; NAME LENGTH(NAME) ADDR LENGTH(ADDR) SAL LENGTH(TO_CHAR(SAL)) ------ ------------ ---------------- ------------ --------- -------------------- 高乾竞 3 北京市海锭区 6 9999.99 7 7.LOWER 返回字符串,并将所有的字符小写 SQL> select lower(’AaBbCcDd’)AaBbCcDd from dual; AABBCCDD -------- aabbccdd 8.UPPER 返回字符串,并将所有的字符大写 SQL> select upper(’AaBbCcDd’) upper from dual; UPPER -------- AABBCCDD 9.RPAD和LPAD(粘贴字符) RPAD 在列的右边粘贴字符 LPAD 在列的左边粘贴字符 SQL> select lpad(rpad(’gao’,10,’*’),17,’*’)from dual; LPAD(RPAD(’GAO’,1 ----------------- *******gao******* 不够字符则用*来填满 10.LTRIM和RTRIM LTRIM 删除左边出现的字符串 RTRIM 删除右边出现的字符串 SQL> select ltrim(rtrim(’ gao qian jing ’,’ ’),’ ’) from dual; LTRIM(RTRIM(’ ------------- gao qian jing
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值