存储过程

全局数据库名(即SID):
select * from global_name;

同义词:
create synonym sys_test for 表名;
  CREATE SYNONYM同义词名FOR 表名@数据库链接名;

trunc(sysdate-1)

数据类型转换:
to_char() to_number() to_date
to_date(‘2009-07-08 09:01:00’, ‘yyyy-mm-dd hh24:mi:ss’)
-----------
select nvl(max(dummy),0) from dual;
select to_date('2010-11-08 13:00:24','yyyy-mm-dd hh24:mi:ss') from dual;

知到做到:笔记,重复,少而精,成为别人

--
循环语句1:
declare
  i number(3):=0;
begin
  loop
    i:=i+1;
  exit when i>=10;
  end loop;
end;
循环语句2:
declare
  i number;
begin
  i:=0;
  while i<=10 loop--while表达式可以加上()
    i:=i+1;
  end loop;
end;
循环语句3:
declare

begin
    for i in 1..9 loop--注意是两点
      dbms_output.put_line(i);
    end loop;
end;
变量i不必定义(可以定义),默认为整型
只能引用,不能赋值
初始值不能小于结束值
增量或减量只能是1
规律:第一个关键词不加分号
   if...else...;
   loop...exit when...;
   while...loop...;
example:
begin
  for i in 1..10 loop
    dbms_output.put_line(i);
    for j in 1..20 loop
      if i:=15 then exit;
      end if;
    end loop;
  end loop;
end;
--exit只能退出当前循环
goto的用法:
declare
  i number;
begin
  i:=0;
  if i=0 then
    goto i0;
  elsif i=2 then
    goto i1;
  eles
    goto i2;
  end if;
  <<i0>>
     dbms_output.put_line('i=0');
  <<i1>>
    dbms_output.put_line('i=2');
end;
--<<>>标号后必须有可执行的pl/sql块。
不能跳转到if或loop
不能从异常处理部分跳到当前块。

record
相当于数据库中的一条记录(不能是多条),可看作一种数据类型,或是java中的DTO
example:
declare
  type record_sale is record(
       area_code varchar(20) not null default '',--注意是逗号
       year integer not null := 1701,
       month integer not null :=1,
       sdate integer not null :=1,
       saleroom number not null :=0,
       saler varchar2(10) not null :='who?'
  );
  var_sale record_sale;
  i number :=1;
 
begin
  var_sale.area_code := 'HUNAN';
  var_sale.year :=1324;
  var_sale.month :=10;
  var_sale.sdate :=3;
  var_sale.saleroom :=423.23;
  var_sale.saler :='jar';
  loop
    var_sale.year := var_sale.year + 1;   
    var_sale.month := var_sale.month + 1;   
    var_sale.sdate := var_sale.sdate + 1;   
    var_sale.saleroom := var_sale.saleroom + 100;   
    insert into T_SALE_TEST values var_sale;   
    i := i + 1;   
    exit when i = 10;
  end loop;
  commit;
end;
--not null 字段要赋初始值。
不能使用record_sale:=('value1','value2')赋值;

复制表结构到新表:
create table newtable as select * from oldtable;   --as
如果现在子查询写的是: select * from emp ;复制表结构以及表内容  
如果现在子查询写的是:select * from emp where 1=2,加入了一个永远不可能成立的条件,  
则此时表示只复制表结构,不复制表内容
--
delete清空数据 可以使用rollback回滚  
truncate清空数据,不能回滚,会立即释放资源
CLOB : char large object : 大文本对象,最大存放4G的文本  
BLOB: byte large object : 大二进制对象,最大存放4G,放非文本,如电影,mp3,图片
--建表默认值问题
create table t_name(
  f_name f_type {default value}
);  --可以同时出not null,但必须放在default后面。
--

-- Add/modify columns
alter table T_SALE_TEST modify YEAR number;--不用管原来的类型
alter table T_SALE_TEST add addtest clob;--新增字段
alter table T_SALE_TEST rename column SALER to sALERs;--更改字段名称
 alter table myTab drop column tel_no;删除表myTab的tel_no列;
--两种格式时间
select to_char(sysdate,'year month day ddspth')from dual;
select to_char(sysdate,'yyyy mm dd hh24:mi:ss')from dual; 

--温馨提示:在不知道如何写sql语句时可以在pl/sql工具中,用可视化操作,然后查看其sql。
--一个很有意思的题目:
create table conutry(  
   name varchar2(4)  
);
alter table conutry modify name varchar2(10);
select t.*,t.rowid from conutry t; 
 
select c1.name ||'---->'||c2.name   
from conutry c1,conutry c2  
where c1.name <>c2.name 
--
select sysdate||'--'||sysdate from dual;--连接符
--不等于表示
!=,<>,^=,这三个都可以表示不等于。

 

--建立主外键约束


select name mingzi from table;--别名或者select name as mingzi from table;

---
要是知道表属于哪个用户,可把sql语句写成:scott.emp
--游标:
declare
  v_empno scott.emp.empno %type:=7369;--单个字段声明
 
begin
  update scott.emp t set t.ename = 'Jay' where t.empno = v_empno;
  if sql%found then
    dbms_output.put_line('delete OK!');
  end if;
 
  if sql%notfound then
    dbms_output.put_line('雇员编号'||v_empno||'不存在');
  end if;
end;
example2:
declare
  cursor emp_cur is select * from scott.emp;--声明无参游标,并关联Sql
  empRecord scott.emp%rowtype;--表示一行记录
 
begin
  open emp_cur;
  loop fetch emp_cur into empRecord;--取出emp_cur 这个游标所对应的记录放入empRecord变量里
    exit when emp_cur%notfound;--没有记录是退出循环
    dbms_output.put_line(empRecord.ename);
  end loop;
  close emp_cur;
end;

--example3带参数游标:
declare 
 
destination varchar2(20);  
 
cursor emp_cur(dest varchar2) --声明带参显示游标  
 
   is select * from scott.emp where scott.emp.empno=dest; --关联到SQL  
 
empRecord scott.emp%rowtype;  -- 行类弄的变量empRecord  
    
begin 
 
destination:=&empno;  --弹出用户输入框  
 
open emp_cur(destination); --打开游标  
 
loop  
 
fetch emp_cur into empRecord; -- 循环每一游标所对应的记录放入empRecord  
 
exit when emp_cur%notfound;  
 
dbms_output.put_line(empRecord.ename);  
 
end loop;  
 
close emp_cur;  
 
end;
--
%FOUND返回boolean,当fetch得到记录,返回true,否则,返回false,如果游标没有打开,则返回ora-1001(游标不可用)错误,如果游标打开,但是没有使用fetch推进,则返回null.
%NOTFOUND和%FOUND相反
--隐式游标的使用
declarel_emp SCOTT.EMP%ROWTYPE;beginselect *INTO l_empfrom scott.empwhere ename=‘ISS’--for update of sal;if sql%found thenupdate scott.emp set sal=sal+200where ename=‘ISS’;end if;exceptionwhen no_data_found thendbms_output.put_line(‘ISS does not exist’);when too_many_rows thendbms_output.put_line(‘More than one ISS exists’); end;

当执行insert/update/delete时,pl/sql自动创建隐式游标
隐式游标不能open/close/fetch

在命令窗口下执行打开输出:
set serveroutput on;--打开dbms_output的输出。
--区别:
select trunc(sysdate) from dual;
select sysdate from dual;
select to_date(sysdate) from dual;
select to_char(sysdate) from dual;

select to_char(sysdate,'year month day ddspth')from dual;

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')from dual;
select to_char(to_date('2006-11-03','yyyy:mm:dd'),'dd:month:yy') from dual;
--
对于Record的应用,例:Dept_obj.ID :=1,表示赋值
select upper('a') from dual;
%rowtype也是定义的一个Record类型
游标也可以进行变量的传入与传出
NO_Data_found是一个内置的Exception
NVL(EXPR1,EXPR2) 若EXPR1是NULL,则返回EXPR2,否则返回EXPR1.

各种函数:
字符函数
upper(字段名)、lower(字段名)
截取字符:substr(字段名,起始位置,取字符个数)
首字母大写:initcap(字段名)
字符串拼接:concat(字段1,字段2)
数值函数:
四舍五入:round(数据,保留小数点后几位)
截取数值函数 trunc(数据,保留到小数点后几位) --还有另外的用法,注意区别于round
日期函数
将日期转成字符 to_char(date,'日期格式')
   日期格式要用有效格式,格式大小写敏感 'yyyy mm dd hh24:mi:ss'(标准日期格式),'year'(年的全拼),'month'(月的全拼),'day'(星期的全拼),'ddspth' (日期的全拼)
   例:select to_char(sysdate,'yyyy mm dd hh24:mi:ss')from dual;
select to_char(sysdate,'year month day ddspth')from dual;

   将字符串转成日期 to_date('...','日期格式')
   例:select to_char(to_date('2006 11 03','yyyy mm dd'),'dd-month-yy') from dual;

连接查询和子查询

oracle中的伪列:
rowid:可提高查询效率。
rownum:用法
select * from emp where rownum <=5;--不能写成=>
创建和删除序列:
create sequence seq_tttt (increment by 3);
drop sequence seq_tttt;
查看数据库中的所有序列和描述:
select * from user_sequences;
desc user_sequences;

视图(关于视图有多表系统表供参考):
查看视图
select * from USER_SYS_PRIVS; 来查看系统的权限
查看数据库所有的表:
select * from all_tables;
创建视图:
 create or replace view test as select * from scott.emp;
删除视图:
drop view test;
注意:向视图中插入数据时,会直接插进基表中,查看视图中的数据时,相当于就是执行创建时的select语句。

索引:
建立索引的目的就是为了加快查询速度,建立索引后会使DML操作效率慢,但是对用户查询会提高效率。删除一个表时,相对应的索引也会删除。另外,索引是会进行排序。
create index index_test on scott.emp(job);
ORA-01408: such column list already indexed不能在重复的列上建索引
ORA-01702: a view is not appropriate here
不能在视图上建索引
删除索引:
drop index index_test;
---
--求所有人名中包含'a'的员工信息不区分大小写
select * from emp where lower(ename) like '%a%';
--求字符对应的ascii码
select ascii('中')from dual;

oracle10g新特性:
flashback table tttt to before drop;
 

---
查看死锁:
select username,lockwait,status,machine,program from v$session where sid in (select session_id from v$locked_object);

 

---打aar包时,代码中不能有错误!!

另存为03:
   在office按扭-->另存为-->97-2003
另存为不可编辑的文档:
   另存为ppt放映。
PPT选项按钮。
   编辑主题及PPT使用属性等。
   如工具栏中添加'超链接'功能。

幻灯片方向:
   横向纵向的转换。在‘设计’-->左上角的‘幻灯片方向’
占位符与文本框在大纲中的是否显示的区别。

文字图片输入小tip
   字体
  .首字母大写。
   段落
格式刷也可以刷图片。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值