oracle学习笔记

set linesize n 每一个字段的显示宽度
set pagesize n 每一面显示n行
alter session set NLS_date_format="YYYY-MM-DD";将时间显示格转换为"YYYY-MM-DD"格式
LOWER('AAA')将大写字母转换为"aaa" UPPER("aaa")则相反
insert into tablename(field1,field2,field3……)values('&field1',&field2,'&field3',……)
实现交互式向数据库插入数据,不同的类的字段只要对应格式下在前加'&'即可
savepoint pointname创建事务保存点,可以通过rollback to printname回滚到指的定的事务保存点
通配符:%表示多个字符,_表示单个字符

第二个视频
字符函数有两种:
1、字符转换函数
lower\upper可以望文生意
initcap将字符串中的单词的首字母大字,其它的小写
2、字符操纵函数
to_char(sysdate,'yyyy-mm')载取当前日期的年月
concat('aaa','bbb')='aaabbb'
substr('abcbc',offset,length)
instr('string','r')=3首次出现的位置
lpad(sal,10,'*')如果sal=5000,则输出结果为******5000,按固定长度输出 ,不够常有第三个字符参数补充
Number函数:
round(45.926,2)=45.93采取四舍五入的方法截取小数,当第二个参数为负数时,表示四舍五入小数点向左移,结果为50
Trunc(45.926,2)=45.92不考虑四舍五入截取小数,当第二个参数为负数时数点向左移对应的位数截取,结果为40
以上两个函数也可以对时期做相同的操作
时间函数:
SYSDATE返回当前系统明期和时间
DUAL名誉表本身没有任何意义
MONTHS_BETWEEN返回两个日期之间月份的差值
ADD_MONTHS在日期上加上月份数
NEXT_DAY指定日期的后一天
LAST_DAY月份中最后一天

第三天视频
类型转换:
oracle数据库中最常用的数据类型是字符类型,也是最基本的
date\number类型都可以通过方法从字符转换
to_char(date/number),date 类型可以设置是期格式
to_date(char,'日期格式')
NVL(字段名,对应类型的替换值)当字段返回为NULL时,就用替换值替换NULL
DECODE(expression,'search1',result1
,'search2',result2
,result3)
相当于switch(expression){case 'search1':result1;break;
case 'search2':result2;break;
default:resutl3;)
||表示字符连接,字符有单引号“''”:字段值的连接
get可以得到脚本文件加载到缓冲区,通过run执行文件命令
'@'命令可以执行get+run效果
r表示执行,/也表示执行当前缓冲令
save将当前缓冲的命令保存到指定的文件,后加replace覆盖存在的文件
conn username/passwrod连接本地数据库;后加@远程数据库主机名表示连远程数据库
当访问非本用户的表时,只需在from后用username.tablename即可,当然得有权限
第四天视频
alter table tablename add fied
update tablename set field newvalue
查询获得行级锁
select…for update of 列名
select^^^^for u

pdate waite seconds(时间以秒为单位)
自动获得行级锁,锁多少行由select条件语句决定
对表加锁有:share mode(共享锁,其它用户可以加锁,有加锁的只能查询不能删改)
Lock table tablename in exclusive mode( nowait 如果表以锁定不用等待,显示信息);
第五天视频
alter table tablename drop column fieldname;
alter table tablename modify(fieldname shuxin)
alter table tablename add(fieldname shuxin)
alter table tablename set unused column fieldname//当一个表用了很久,想删除一个字段,由于有用户在用,oracle不允许删除,就可以设为不可用,等到后面可以删除时删除,下面就是删除表中所有不可用字段
alter table tablename drop unused columns
对表增加约束:
alter table tablename add constraint constraintname[operation]
alter table tablename drop constraint constraintname
desc user_constraints(查询表约束的数据字典)

对表分区,
范围分区:(根某一个字段的值,划分范围)
Partition by range(score)(
Partition p1 values lessthan(60),
Partition p2 values lessthan(75),
Partition p3 values lessthan(85),
Partition p1 values lessthan(100));

create table logger(
id number primary key,
write_date date not null)
Partition by range(write_date)(
Partition p1 values less than(to_date('2009-1-31','yyyy-mm-dd')),
Partition p2 values less than(to_date('2009-2-28','yyyy-mm-dd')),
Partition p3 values less than(to_date('2009-3-31','yyyy-mm-dd')),
Partition p4 values less than(to_date('2009-4-30','yyyy-mm-dd')),
Partition p5 values less than(to_date('2009-5-31','yyyy-mm-dd')),
Partition p6 values less than(to_date('2009-6-30','yyyy-mm-dd')),
Partition p7 values less than(to_date('2009-7-31','yyyy-mm-dd')));

散列分区:(通过字段哈希函数值划分区,分区均匀,趋于一致)
Paritition by hash(字段名)(
分区1,分区2);
复合分区:(先范围分区,再进行散列分区)
create table salgrade(
grade number,
losal number,
hisal number)
Partition by range(grade)(
Subparitition by hash(losal,hisal)(
Partitiong p1 values less than(10),
(subpartition sp1,subparition sp2),
Partitiong p2 values less than(20),
(subpartition sp3,subpartition sp4)
)


列表分区:
Partition by list(custState)(
Partition asia values('中国','韩国','新加坡'),
Partition Europe values('英国','法国','德国'),
Partition ameria values('美国','加拿大','墨西哥'),);
查询表中某一个分区的值:
select * from tablename partition(partitionname);
第六视频
查看表的分区的数据字典:desc user_tab_partitions;table_name,parition_name,partition_postion,都是user_tab_partitions数据字典的字段
对表分区的操作:
添加分区
alter table student add partition p5 values less than(120);
删除分区l:
alter table student drop partition partitionname;
截短分区:
alter table stude

nt truncate partition p5;
合并分区
alter table student merge partitions p3,p4 into partition p6
表分区索引数据字典:User_ind_partitions
数据对象:同义词,序列,视图
同义词:create synonym name for table(DBname.tablename)数据字典user_synonyms
序列 create sequence sequencename[increment by n][start with n][maxvalue n|nomaxvalue][minvalue n|nominvalue][cycle|nocycle][cache n(20)|nocahe];数据字典user_sequences;
sequencename.[nextval|currval]
视图
create or replace view 创建视图(force先创建视图后创建表)viewname as (select……)
视频8
索引:
唯一索引
oracle自动为主键和唯一键列创建唯一索引
create unique index indexname on tablename(fieldname)创建唯一索引
组合索引
create index indexname on table(field1,field2);
反向健索引
位图索引
键压缩索引
分区索引
本地索引
本地前缀索引
本地无前缀索引
全局索引
全局前缀索引
全局无前缀索引

有公共列的两个或多个表的集合
簇表中的数据存储在公共的数据块中
create cluster
oracle oop
创建类型,oracle oop中所有的都是对象
create or replace type typename as object
(和表一样但是不能有约束)
如:
create or replace type address as object(
province varchar(15),
city varchar(20),
street varchar(30)
);
自定义数据类型,插入数据时要用构造函数如:address('湖南省','衡阳市','常胜路28号')
查询时可以正常查询结果是:address('湖南省','衡阳市','常胜路28号')
具体查询要用表的别名如:select name.address.province……
更新要用新的对象才能更新
not final表时创建的类型可以扩展(继承)
同java中的extends的继承对的是under
基于抽象类型创建对象表
格式:create table 对象表名 of 抽象对象类型
实例:create table tablename of typename;
这种表可以像正常建的表一样造作
创建带方法的对象类型
create or replace type studenttype as object
(
--声明属性
stuid varchar(10),
stuname varchar(10),
homeaddress Address,
--声明方法
member function getAddresss Return Address,
member Procedure setAddress(newAddress Address)
);
create or replace type body studentType as
member function getAddress return Address is
begin
return homeAddress;
end;
member Procedure setAddress(newAddress Address) is
begin
homeAddress:=newAddress;
end;
end;
可变数组:
1、创建带有可变数据组的表
格式:create or replace type 基类型名 as object(字段清单)
2、建立嵌套项类型的可变数组
格式:create or replace 可变数组类型名 as Varray(最大行数) of 可变数组的基类型;
3、创建一个主表
创表时在对应的字段有对应的可变数组类型
可变数组用来保存不变信息,一般不对其做修改更新操作的信息
第11视频
嵌套表(创建类似于可变

数组)
1、创建基类型
2、创建嵌套表类型
3、创建主表,其中一个列是嵌套表类型
第13视频
限制用户
锁定:alter user 用户名 account lock;
解锁:alter user 用户名 account unlock;
口令失效:alter 用户名 passwordexprie;
drop user username cascade;强制删除用户
授权
grant priv1,priv2 [on tablename] to username [with admin option(授于用户可以将权力授于给别的用户)]
revoke [privname] from [username];
第14视频
pl/sql

过程编程程序基本块:
declare
--定义变量 变量名+空格+变量类型
begin
--程序实现代码
exception 日志
--程序异常执行代码
when NO_DATA_FOUND then
DBMS_Output.put_line('Data did't find');
end;

在过程编程定义的变量可以从通过三程方式:
1、定义时赋值
eno varchar(6):='smith';
2、通过select语句赋值 要求一个变量一个值,并通过into语句
select enumber into eno from s_emp where enumber='7345';
3、在begin块赋值
eno:='smith';
对变量的类型可以在二个方面指定:
定义时指定:eno varchar2(6):
字符串类型
varchar2必须给长度
char 不给长度时默认是1
long 是自动变长类型,可以存储大量的字符
通过自适应(查询语句):eno s_emp.enumber%type;emprow emp%rowtype;指定一行类型
异常:
NO_Data_Found
Too_many_rows

others 不知具体异常时代替所有异常
自定义异常:
set serveroutput on--开启输出
declare
--定义异常
exceptionname Exception
--定义变量
var
begin
--抛异常
raise exceptionname;
exception
when exceptionname then
DBMS_output.put_line("exception information");
end;

游标:cursor cursorname(paramter type) is select语句
游标类型:静态和动态
静态:隐式游标、显式游标
隐式游标:
名字:SQL
属性:%NOTFOUND(boolean),%FOUND(boolean),%ROWCOUNT,%ISOPEN(boolean)false
游标工作原理:
声明一个游标
打开游标
提取记录(被提取的,在游标中消失,单向顺序提取)
关闭游标
游标使用的是服务器资源,一般做复杂的运算
游标是一个指向上下义区域的指针
例:
set serveroutput on
declare
cursor empCur is select * from emp;
emprow emp%rowtype;
begin
open empcur;
fetch empcur into emprow;
DBMS_output.put_line('employee no:'||emprow.empno||'employee name:'||emprow.ename);
close empcur;
end;
各种循环提取游标:
loop:
open empcur
loop
fetch empcur into emprow;
DBMS_output.put_line('employee no:'||emprow.empno||'employee name:'||emprow.ename);
exit when empcur%notfound;
end loop;
close empcur;
while:
open empcur
fetch empcur into emprow;--while 循环前要先提取 found值才是正确的值
while empcur%found loop
DBMS_output.put_line('employee no:'||emprow.empno||'employee name:'||emprow.ename);
fet

ch empcur into emprow;--如果在while内不提取则found值永为真
end loop;
close empcur;
while 条件 loop
end loop;
for:--与前两种循环不同,将游标的值一次性提出
for emprow in empcur loop
DBMS_output.put_line('employee no:'||emprow.empno||'employee name:'||emprow.ename);
end loop
for var 1..10 loop var 自动递增不能手动控制大小只会递增
end loop;
动态游标:
定义时不指定select 语句,这和静态相反
动态游标是在open游标时指定select语句--隐式打开(for也是隐式打开,所以for不能用在动态游标是取)
类型:受约束游标(强游标
不受约束游标(弱游标
声明如下:
弱:
declare
----REF 游标用于存运行时使不同的SQL语句与之关联
type RefEmpCur is ref cursor;--定义一个游标类型
EmpCur RefEmpCur;--声明游标变量
Emprow emp%rowtype;--定义结果存储变量
flag int:=0;
begin
flag :=&flag;
if flag=0 then
Open EmpCur for select * from emp;
elsif flag=1 then
Open EmpCur for select * from emp where sal>10000;
else
Open EmpCur for select * from emp where sal<10000;
end if;
fetch EmpCur into Emprow;
while EmpCur%found loop
DBMS_output.put_line('');
fetch EmpCur into Emprow;
end loop;
end;
强:
指定返回类型(在定义时指明)如:
type RefEmpCur is ref cursor return emp%rowtype;--游标只能从emp表中查询了
游标管理:
不能在程序包中声明游标变量
远程子程序不能接受游标变量的值
不能使用比较操作符对游标变量进行相等或不相等测试
不能将空值赋予游标变量
表不能存储游标变量的值
视频23
调用存储过程:execute procedurename;/(匿名块)begin procedurename;end;
create or replace procedure[parameter list]
is|as
;
begin
(executable statements)
exception
--异常处理
end;
parameter partern
IN:接受值,默认模式 在存储过程体内只能读不能赋值
OUT:将子程序返回值给调用使用 当成另类的返回值相当于传的是引用即传入的必须是一个变量 第一次使用时会初始化,默认值为NULL
IN_OUT:综合上两个
数据字典:user_source,可以查到存储过程(数据字典中对象名都是大写)

函数
create or replace function[argument list]
return datatype is|as
;
begin
(executable statements)
return var(datatype);
end
函数的访问:使用PL/SQL程序块、SQL语句
注意:
函数return只能指明类型不能指明精度
create or replace function getname(sNo varchar2)
return varchar is
name varchar(12);
begin
select ename into name from emp
where empno=sNo;
return name;
exception
when too_many_rows then
dbms_output.put_line('返回值多于一条');
when others then
dbms_output.p

ut_line('在getname函数中出现以外错误');
end;
过程与函数的比较:
过程 函数
作为PL/SQL语句执行 作为表达式的一部分调用
在规格说明中不包含RETURN子句 必须在规格说明中包含RETURN子句
可以返回任何值 必须返回单个值
可以包含RETURN语句,但是与函数不同 必须包含至少一条RETURN语句
它不能用于返回值
参数传递时可以用带联合调整参数的顺序否则要与参数列表一一对应
视频24
过程可以相互嵌套但是过程存在事务会相互影响
只有在过程中设置自主事务处理:pragma AUTONMOUS_TRANSACTION,但是并没有提交
///////////////////////////////
/*触发器 trigger
触发器创建具有三个部分:
A。触发器事件
B。可选的触发器约束条件
C。触发器动作
可以创建对应如下语句触发的触发器:
1、DML语句(insert delete update
2、DDL语句(create alter drop
3、数据库操作(servererror logon logoff startup shutdown

可创建触发器的对象:1、数据库表 2、数据库视图 3、用户模式 4、数据库实例
create or replace trigger triggername
before/after insert|update|delete of 列名
on tablename
[for each row]
when condition
end;
替代触发器是用在视图上的
create or replace trigger trginsertview
insert of insert onstusubscore
referencing new n
fro each row
declare int;
begin
select count(*) into icount from student where stuid=:n.stuid;



*/
/*

create or replace package studentpac is
type studentcur is ref cursor return student%rowtype;
procedure insertstudent(stu student%rowtype);
function getstudentcount return number;
end studentpac;

create or replace package body studentpac is
procedure insertstudent(stu student%rowtype)
as
icount number;
begin
select count(*) into icount from student where stuid=stu.stuid;
if icount > 0 then
println('student has exsited');
else
insert into student values(stu,stuid,stu.sname);
commit;
end if;
Exception
when too_many_rows then
println('student has existed');
end insertstudent;
function getstudentcount return number
is
incount number;
begin
select count(*) into icount from student;
return icount;
end getstudentcount;
end studentpac;
包:
包头:package
包体:package body

包中的变量,会话唯一.

包体中定义的变量,必须在所有的存储过程和函数之上
包体中定义的类型,必须在所有的存储过程和函数之上
如果包体中有方法调用关系,则被调用者必须在调用者之上

plsql中,对代码的编译和运行过程是线性结构的

定义游标引用类

型:
type 类型名 is ref cursor;
变量名 类型名;
open 变量名 for select .....;
close 变量名;
*/
package 包等同与JAVA里的包
包头:package 类似于接口
create or replace package packagename
as

--can define:
--var 包中的变量session唯一,
--type 省略去create or replace,从关键字type开始
--procedure 只有定义没有实现
--function 只有定义没有实现
--cursor
end packagename;

包体: 类似于实现类 包必须有包体才允许访问其内部变量
create or replace package body packagename
as
procedure
begin
end;
function
begin
end;
end;


删除一表中相同的行只留一行:
delete from test t1 where t1.rowid!=(select max(rowid) from test t2 where t1.id=t2.id and https://www.360docs.net/doc/a68973401.html,=https://www.360docs.net/doc/a68973401.html,);

declare
num number;
v_id number;
v_name varchar2(15);
begin
select count(*) into num from test;
loop
select id,name into v_id,v_name from (
select id, name, rownum rnum from test order by rnum
) where rnum = num;
dbms_output.put_line('id:'||v_id||' name:'||v_name);
num := num-1;
exit when num < 1;
end loop;
end;

declare
num number;
v_id test.id%type;
v_name https://www.360docs.net/doc/a68973401.html,%type;
begin
select count(*) into num from test;
loop
select id,name into v_id,v_name from (
select id, name, rownum rnum from test order by rnum
) where rnum = num;
dbms_output.put_line('id:'||v_id||' name:'||v_name);
num := num-1;
exit when num < 1;
end loop;
end;


declare
num number;
v_test test%rowtype;
begin
select count(*) into num from test;
loop
select id,name into v_test.id,v_https://www.360docs.net/doc/a68973401.html, from (
select id, name, rownum rnum from test order by rnum
) where rnum = num;
dbms_output.put_line('id:'||v_test.id||' name:'||v_https://www.360docs.net/doc/a68973401.html,);
num := num-1;
exit when num < 1;
end loop;
end;

declare
num number;
v_test test%rowtype;
begin
select count(*) into num from test;
loop
select id,name into v_test from (
select id, name, rownum rnum from test order by id
) where rnum = num;
dbms_output.put_line('id:'||v_test.id||' name:'||v_https://www.360docs.net/doc/a68973401.html,);
num := num-1;
exit when num < 1;
end loop;
end;

游标:

declare
--声明游标(游标即是查询结果集)
cursor testcur is select * from test;
testrow test%rowtype;
begin
--开启游标
open testcur;
--提取记录(被提取的,在游标中消失,单向顺序提取)
fetch testcur into testrow;
--%found fetch以后才有值否则为NULL
while testcur%found loop
DBMS_output.put_line('id:'||testrow.id||' name:'||https://www.360docs.net/doc/a68973401.html,);
fetch testcur into testrow;
end loop;
--关闭游标
close testcur;
end;

declare
--声明游标(游标即是查询结果集)
cursor testcur is select * from test;
testrow test%rowtype;
begin
--开启游标
open testcur

;
--提取记录(被提取的,在游标中消失,单向顺序提取)
--fetch testcur into testrow;
--%found fetch以后才有值否则为NULL
while nvl(testcur%notfound,true) loop
fetch testcur into testrow;
DBMS_output.put_line('id:'||testrow.id||' name:'||https://www.360docs.net/doc/a68973401.html,);
end loop;
--关闭游标
close testcur;
end;

declare
cursor testcur is(
select * from test
);
testrow test%rowtype;
begin
open testcur;
while nvl(testcur%found, true) loop
fetch testcur into testrow;
exit when testcur%notfound;
DBMS_output.put_line('id:' || testrow.id || ' name:' || https://www.360docs.net/doc/a68973401.html,);
end loop;
close testcur;
end;

declare
cursor testcur is select * from test;
testrow test%rowtype;
begin
open testCur;
loop
fetch testcur into testrow;
if testcur%notfound then
exit;
end if;
DBMS_output.put_line('id:'||testrow.id||' name:'||https://www.360docs.net/doc/a68973401.html,);
end loop;
close testcur;
end;

declare
cursor testcur is select * from test;
--num number;
--testrow test%rowtype;
begin
--select count(*) into num from test;
-- open testCur;
for testrow in testcur loop
--fetch testcur into testrow;
DBMS_output.put_line('id:'||testrow.id||' name:'||https://www.360docs.net/doc/a68973401.html,);
end loop;
--close testcur;
end;

set sevrviceoutput on;
declare
blean boolean;
begin
blean := &boolean;
if nvl(blean,true) then
dbms_output.put_line('true');
else
dbms_output.put_line('false');
end if;
end;
/
包:
head--
create or replace package testPac
is
pac_i number;
procedure pac_procedure;
cursor pac_cur is select * from test;
end testPac;
body--
create or replace package body testPac as
procedure pac_procedure as
begin
println('pac_procedure');
for cur_i in testPac.pac_cur loop
println('id:'||cur_i.id||' name:'||cur_https://www.360docs.net/doc/a68973401.html,);
end loop;
end pac_procedure;
end testPac;



create table downFileList(
id number primary key,
name varchar2(40) not null,
fileLocation bfile,
description clob);

declare
tempdesc clob;
ireadcount number;
istart number;
soutputdesc varchar2(100);
begin
ireadcount := 5; --读出5个字符
istart :=1;
select description into tempdesc from downfilelist where id=1;
dbms_lob.read(tempdesc,ireadcount,istart,soutputdesc);
dbms_output.put_line('the before 5 characters:'||soutputdesc)
end;


select student as name,
max(case when course='corejava' then score else 0 end) as COREJAVA,
max(case when course='jdbc' then score else 0 end) as jdbc,
max(case when course='hibernate' then score else 0 end) as hibernate from score group by student

解决DBCONSOLE启动异常问题:
1、c:>emctl start dbconsole
Unable to determin local host from URL REPOSITORY_URL=http://kettas-DBWorm:%EM_UPLOAD_PORT%/em/upload
emtcl start dbconsole
////////////////////////////////////////////
日表:原始表一分钟一张表.
原则:这小时创下一小时的分表
create or replace procedure auto_create

_user_table_min is
name varchar2(20);
table_name varchar(50);
v_time varchar2(20);
v_min varchar2(2);
begin
v_time :=to_char(sysdate,'yyyymmddhh24')+1;
name :=concat('user_min_',v_time);
for v_i in 0..59 loop
if v_i < 10 then
v_min := concat('0',to_char(v_i));
table_name :=concat(name,v_min);
else
table_name := concat(name,to_char(v_i));
end if;
execute immediate 'create table '||table_name||'(id number primary key,username varchar2(20) not null,onlinetime timestamp not null,offlinetime timestamp not null,sumtime number(5) not null,aaaip varchar2(15) not null)';
execute immediate 'create sequence '||table_name||'_id_seq';
end loop;
end;
/
create or replace procedure auto_conformity_day_table
type refempcur is ref cursor;--定义一个引用游标
cur_temp refempcur;--声明一个临时游标类型
v_temp temp_min%rowtype;
name varchar2(20);
table_name varchar(50);
v_time varchar2(20);
v_min varchar2(2);
begin
v_time :=to_char(sysdate,'yyyymmddhh24')-1;
name :=concat('user_min_',v_time);

--取出一小时内所有的表放到一张小是表中,60->1
for v_i in 0..59 loop
if v_i < 10 then
v_min := concat('0',to_char(v_i));
table_name :=concat(name,v_min);
else
table_name := concat(name,to_char(v_i));
end if;
execute immediate 'insert into temp_min select username,sum(sumtime),aaaip from '||table_name||' group by username,aaaip';
end loop;
open cur_temp for select * from temp_min;
fetch cur_temp into v_temp;
close cur_temp;
end;
/
日表:一个小时一张表.某一天中的数据表.60->1
原则:今天创建明天的24张表
create or replace procedure auto_create_user_table_day is
name varchar2(20);
table_name varchar(50);
v_time varchar2(20);
v_hour varchar2(2);
begin
v_time :=to_char(sysdate,'yyyymmdd')+1;
name :=concat('user_day_',v_time);
for v_i in 0..23 loop
if v_i < 10 then
v_hour := concat('0',to_char(v_i));
table_name :=concat(name,v_hour);
else
table_name := concat(name,to_char(v_i));
end if;
execute immediate 'create table '||table_name||'(id number primary key,name varchar2(12) not null,sumtime number(8) not null,aaaip varchar2(15) not null)';
execute immediate 'create sequence '||table_name||'_id_seq';
end loop;
end;
/
月表:一天一张表.24->1
原则:这个月创建下一个月的所有天的表
create or replace procedure auto_create_user_table_month is
name varchar2(20);
table_name varchar(50);
v_time varchar2(20);
v_month varchar2(2);
v_last_day number;
begin
v_time :=to_char(sysdate,'yyyymm')+1;
v_last_day :=to_number(to_char(last_day(last_day(sysdate)+1),'dd'));
name :=concat('user_month_',v_time);
for v_i in 1..v_last_day loop
if v_i

< 10 then
v_month := concat('0',to_char(v_i));
table_name :=concat(name,v_month);
else
table_name := concat(name,to_char(v_i));
end if;
execute immediate 'create table '||table_name||'(id number primary key,name varchar2(12) not null,sumtime number(9,1) not null,aaaip varchar2(15) not null)';
execute immediate 'create sequence '||table_name||'_id_seq';
end loop;
end;
/
/**
年表:一个月一张
create or replace procedure auto_create_user_table_year is
name varchar2(20);
table_name varchar(50);
v_time varchar2(20);
v_year varchar2(2);
begin
v_time :=to_char(sysdate,'yyyy');
name :=concat('user_year_',v_time);
for v_i in 1..12 loop
if v_i < 10 then
v_year := concat('0',to_char(v_i));
table_name :=concat(name,v_year);
else
table_name := concat(name,to_char(v_i));
end if;
execute immediate 'create table '||table_name||'(id number primary key,name varchar2(12) not null,sumtime number(10,1) not null,aaaip varchar2(15) not null)';
execute immediate 'create sequence '||table_name||'_id_seq';
dbms_output.put_line(v_time);
end loop;
end;
/
*/
年表:一个月一张
create or replace procedure auto_create_user_table_year is
table_name varchar(50);
v_time varchar2(20);
begin
v_time :=to_char(sysdate,'yyyymm')+1;
table_name :=concat('user_year_',v_time);
execute immediate 'create table '||table_name||'(id number primary key,name varchar2(12) not null,sumtime number(10,1) not null,aaaip varchar2(15) not null)';
execute immediate 'create sequence '||table_name||'_id_seq';
dbms_output.put_line(v_time);
end;
/
declare
v_job integer;
begin
dbms_job.submit(v_job,'auto_create_user_table_month;',sysdate,'sysdate+1');
dbms_output.put_line('v_job='||v_job);
end;
/
//////////////////////////////////////////////////////////////////
create global temporary table temp_min(
username varchar2(20) not null,
sumtime number(8) not null,
aaaip varchar2(15) not null
) on commit delete rows;
insert into temp_min select username,sum(sumtime),aaaip from user_min_200903021348 group by username,aaaip;
////////////////////////////////////////////
declare
cursor testcur is select * from test;
testrow test%rowtype;
begin
open testCur;
loop
fetch testcur into testrow;
if testcur%notfound then
exit;
end if;
DBMS_output.put_line('id:'||testrow.id||' name:'||https://www.360docs.net/doc/a68973401.html,);
end loop;
close testcur;
end;
create or replace procedure auto_conformity_day_table
cursor cur_temp is
temp_data temp_min%rowtype;
begin
end;
/


create global temporary table temp_min(
username varchar2(20) not null,
sumtime number(8) not null,
aaaip varchar2(15) not null
) on commit delete rows;
create global temporary table temp_min(
username varchar2(20) not null,
sumtime number(8) not null,
aaaip varchar2(15) not null
) on comm

it delete rows;


select * from v$session where username is not null

select username,count(username) from v$session where username is not null group by username #查看不同用户的连接数

select count(*) from v$session #连接数

Select count(*) from v$session where status='ACTIVE' #并发连接数

show parameter processes #最大连接

alter system set processes = value scope = spfile;重启数据库 #修改连接

/home/oracle9i/app/oracle9i/dbs/init.ora

/home/oracle9i/app/oracle9i/dbs/spfilexxx.ora ## open_cursor

declare
kill_session varchar2(2000);
cursor user_session is select t.SID,t.SERIAL# from v$session t where t.PROGRAM like'%JDBC%' and t.LOGON_TIMEbegin
for v_cur in user_session loop
kill_session :='alter system kill session '''||v_cur.SID||''||','||v_cur.SERIAL#||'''';
execute immediate kill_session;
execute immediate 'commit';
end loop;
end;
/





相关文档
最新文档