137.Oracle数据库SQL开发之 集合——填充集合元素
1. 填充变长数组元素
INSERT语句向customers_with_varray表中添加行。
如下: INSERT INTO customers_with_varray VALUES (
1, 'Steve', 'Brown',
t_varray_address(
'2 State Street, Beantown, MA, 12345',
'4 Hill Street, Lost Town, CA, 54321'
)
);
INSERT INTO customers_with_varray VALUES (
2, 'John', 'Smith',
t_varray_address(
'1 High Street, Newtown, CA, 12347',
'3 New Street, Anytown, MI, 54323',
'7 Market Street, Main Town, MA, 54323'
)
);
插入2行。
2. 填充嵌套表元素
用INSERT语句向CUSTOMERS_WITH_NESTED_TABLE中添加行,
插入如下:
INSERT INTO customers_with_nested_table VALUES (
1, 'Steve', 'Brown',
t_nested_table_address(
t_address('2 State Street', 'Beantown', 'MA', '12345'),
t_address('4 Hill Street', 'Lost Town', 'CA', '54321')
)
);
INSERT INTO customers_with_nested_table VALUES (
2, 'John', 'Smith',
t_nested_table_address(
t_address('1 High Street', 'Newtown', 'CA', '12347'),
t_address('3 New Street', 'Anytown', 'MI', '54323'), t_address('7 Market Street', 'Main Town', 'MA', '54323') )
);
插入两行。
Oracle数据库维护常用SQL语句集合(1)
进程相关:
1、 求当前会话的SID,SERIAL#
SELECT Sid, Serial#
FROM V$session
WHERE Audsid = Sys_Context('USERENV', 'SESSIONID');
2、 查询session的OS进程ID
SELECT p.Spid "OS Thread", b.NAME "Name-User", s.Program, s.Sid, s.Serial#, s.Osuser, s.Machine
FROM V$process p, V$session s, V$bgprocess b
WHERE p.Addr = s.Paddr
AND p.Addr = b.Paddr
And (s.sid=&1 or p.spid=&1)
UNION ALL
SELECT p.Spid "OS Thread", s.Username "Name-User", s.Program, s.Sid, s.Serial#, s.Osuser, s.Machine
FROM V$process p, V$session s
WHERE p.Addr = s.Paddr
And (s.sid=&1 or p.spid=&1)
AND s.Username IS NOT NULL;
3、根据sid查看对应连接正在运行的sql
SELECT /*+ PUSH_SUBQ */
Command_Type, Sql_Text, Sharable_Mem, Persistent_Mem, Runtime_Mem, Sorts,
Version_Count, Loaded_Versions, Open_Versions, Users_Opening, Executions,
Users_Executing, Loads, First_Load_Time, Invalidations, Parse_Calls,
Disk_Reads, Buffer_Gets, Rows_Processed, SYSDATE Start_Time,
SYSDATE Finish_Time, '>' || Address Sql_Address, 'N' Status
FROM V$sqlarea
WHERE Address = (SELECT Sql_Address
FROM V$session
WHERE Sid = &sid );
4、查找object为哪些进程所用
SELECT p.Spid, s.Sid, s.Serial# Serial_Num, s.Username User_Name,
a.TYPE Object_Type, s.Osuser Os_User_Name, a.Owner,
a.OBJECT Object_Name,
Decode(Sign(48 - Command), 1, To_Char(Command), 'Action Code #' || To_Char(Command)) Action, p.Program Oracle_Process, s.Terminal Terminal, s.Program Program,
s.Status Session_Status
FROM V$session s, V$access a, V$process p
WHERE s.Paddr = p.Addr
AND s.TYPE = 'USER'
AND a.Sid = s.Sid
AND a.OBJECT = '&obj'
ORDER BY s.Username, s.Osuser
5、查看有哪些用户连接
SELECT s.Osuser Os_User_Name,
Decode(Sign(48 - Command),1,To_Char(Command),
'Action Code #' || To_Char(Command)) Action,
p.Program Oracle_Process, Status Session_Status, s.Terminal Terminal, s.Program Program, s.Username User_Name,
s.Fixed_Table_Sequence Activity_Meter, '' Query, 0 Memory, 0 Max_Memory, 0 Cpu_Usage, s.Sid, s.Serial# Serial_Num FROM V$session s, V$process p
WHERE s.Paddr = p.Addr
AND s.TYPE = 'USER'
ORDER BY s.Username, s.Osuser
6、根据v.sid查看对应连接的资源占用等情况
SELECT n.NAME, v.VALUE, n.CLASS, n.Statistic#
FROM V$statname n, V$sesstat v
WHERE v.Sid = &sid
AND v.Statistic# = n.Statistic#
ORDER BY n.CLASS, n.Statistic#
7、查询耗资源的进程(top session)
SELECT s.Schemaname Schema_Name,
Decode(Sign(48 - Command),
1, To_Char(Command), 'Action Code #' || To_Char(Command)) Action, Status Session_Status, s.Osuser Os_User_Name, s.Sid, p.Spid,
s.Serial# Serial_Num, Nvl(s.Username, '[Oracle process]') User_Name, s.Terminal Terminal, s.Program Program, St.VALUE Criteria_Value FROM V$sesstat St, V$session s, V$process p
WHERE St.Sid = s.Sid
AND St.Statistic# = To_Number('38')
AND ('ALL' = 'ALL' OR s.Status = 'ALL')
AND p.Addr = s.Paddr
ORDER BY St.VALUE DESC, p.Spid ASC, s.Username ASC, s.Osuser ASC
8、查看锁(lock)情况
SELECT /*+ RULE */
Ls.Osuser Os_User_Name, Ls.Username User_Name,
Decode(Ls.TYPE,
'RW', 'Row wait enqueue lock', 'TM', 'DML enqueue lock',
'TX', 'Transaction enqueue lock', 'UL', 'User supplied lock') Lock_Type, o.Object_Name OBJECT,
Decode(Ls.Lmode,
1, NULL, 2, 'Row Share', 3, 'Row Exclusive',
4, 'Share', 5, 'Share Row Exclusive', 6, 'Exclusive',
NULL) Lock_Mode,
o.Owner, Ls.Sid, Ls.Serial# Serial_Num, Ls.Id1, Ls.Id2
FROM Sys.Dba_Objects o,
(SELECT s.Osuser, s.Username, l.TYPE, l.Lmode, s.Sid, s.Serial#, l.Id1, l.Id2
FROM V$session s, V$lock l
WHERE s.Sid = l.Sid) Ls
WHERE o.Object_Id = Ls.Id1
AND o.Owner <> 'SYS'
ORDER BY o.Owner, o.Object_Name
9、查看等待(wait)情况
SELECT Ws.CLASS, Ws.COUNT COUNT, SUM(Ss.VALUE) Sum_Value FROM V$waitstat Ws, V$sysstat Ss
WHERE Ss.NAME IN ('db block gets', 'consistent gets')
GROUP BY Ws.CLASS, Ws.COUNT
10、求process/session的状态
SELECT p.Pid, p.Spid, s.Program, s.Sid, s.Serial#
FROM V$process p, V$session s
WHERE s.Paddr = p.Addr;
11、求谁阻塞了某个session(10g)
SELECT Sid, Username, Event, Blocking_Session, Seconds_In_Wait, Wait_Time FROM V$session
WHERE State IN ('WAITING')
AND Wait_Class != 'Idle';
12、查会话的阻塞
col user_name format a32
SELECT /*+ rule */
Lpad(' ', Decode(l.Xidusn, 0, 3, 0)) || l.Oracle_Username User_Name,
o.Owner, o.Object_Name, s.Sid, s.Serial#
FROM V$locked_Object l, Dba_Objects o, V$session s
WHERE l.Object_Id = o.Object_Id
AND l.Session_Id = s.Sid
ORDER BY o.Object_Id, Xidusn DESC;
col username format a15
col lock_level format a8
col owner format a18
col object_name format a32
SELECT /*+ rule */
s.Username,
Decode(l.TYPE, 'tm', 'table lock', 'tx', 'row lock', NULL) Lock_Level,
o.Owner, o.Object_Name, s.Sid, s.Serial#
FROM V$session s, V$lock l, Dba_Objects o
WHERE l.Sid = s.Sid
AND l.Id1 = o.Object_Id(+)
AND s.Username IS NOT NULL;
13、求等待的事件及会话信息/求会话的等待及会话信息
SELECT Se.Sid, s.Username, Se.Event, Se.Total_Waits, Se.Time_Waited, Se.Average_Wait
FROM V$session s, V$session_Event Se
WHERE s.Username IS NOT NULL
AND Se.Sid = s.Sid
AND s.Status = 'ACTIVE'
AND Se.Event NOT LIKE '%SQL*Net%'
ORDER BY s.Username;
SELECT s.Sid, s.Username, Sw.Event, Sw.Wait_Time, Sw.State, Sw.Seconds_In_Wait
FROM V$session s, V$session_Wait Sw
WHERE s.Username IS NOT NULL
AND Sw.Sid = s.Sid
AND Sw.Event NOT LIKE '%SQL*Net%'
ORDER BY s.Username;
14、求会话等待的file_id/block_id
col event format a24
col p1text format a12
col p2text format a12
col p3text format a12
SELECT Sid, Event, P1text, P1, P2text, P2, P3text, P3 FROM V$session_Wait
WHERE Event NOT LIKE '%SQL%'
AND Event NOT LIKE '%rdbms%'
AND Event NOT LIKE '%mon%'
ORDER BY Event;
SELECT NAME, Wait_Time
FROM V$latch l
WHERE EXISTS (SELECT 1
FROM (SELECT Sid, Event, P1text, P1, P2text, P2, P3text, P3 FROM V$session_Wait
WHERE Event NOT LIKE '%SQL%'
AND Event NOT LIKE '%rdbms%'
AND Event NOT LIKE '%mon%') x
WHERE x.P1 = l.Latch#);
15、求会话等待的对象
col owner format a18
col segment_name format a32
col segment_type format a32
SELECT Owner, Segment_Name, Segment_Type
FROM Dba_Extents
WHERE File_Id = &File_Id
AND &Block_Id BETWEEN Block_Id AND Block_Id + Blocks - 1;
16、求出某个进程,并对它进行跟踪
SELECT s.Sid, s.Serial#
FROM V$session s, V$process p
WHERE s.Paddr = p.Addr
AND p.Spid = &1;
Exec Dbms_System.Set_Sql_Trace_In_Session(&1, &2, TRUE); Exec Dbms_System.Set_Sql_Trace_In_Session(&1, &2, FALSE);
17、求当前session的跟踪文件
SELECT P1.VALUE || '/' || P2.VALUE || '_ora_' || p.Spid || '.ora' Filename FROM V$process p, V$session s, V$parameter P1, V$parameter P2 WHERE P1.NAME = 'user_dump_dest'
AND P2.NAME = 'instance_name'
AND p.Addr = s.Paddr
AND s.Audsid = Userenv('SESSIONID')
AND p.Background IS NULL
AND Instr(p.Program, 'CJQ') = 0;
18、求出锁定的对象
SELECT Do.Object_Name, Session_Id, Process, Locked_Mode
FROM V$locked_Object Lo, Dba_Objects Do
WHERE Lo.Object_Id = Do.Object_Id;
PL/SQL----触发器
创建触发器:
CREATE [OR REPLACE] TRIGGER <触发器名>
BEFORE|AFTER
INSERT|DELETE|UPDATE [OF <列名>] ON <表名>
[FOR EACH ROW]
WHEN (<条件>)
<pl/sql块>
关键字"BEFORE"在操作完成前触发;"AFTER"则是在操作完成后触发;
关键字"FOR EACH ROW"指定触发器每行触发一次.
关键字"OF <列名>" 不写表示对整个表的所有列.
WHEN (<条件>)表达式的值必须为"TRUE".
特殊变量:
:new --为一个引用最新的列值;
:old --为一个引用以前的列值;
这些变量只有在使用了关键字 "FOR EACH ROW"时才存在.且update语句两个都有,而insert只有:new ,delect 只有:old;
使用RAISE_APPLICATION_ERROR
语法:RAISE_APPLICATION_ERROR(错误号(-20000到-20999),消息[,{true|false}]);
抛出用户自定义错误.
如果参数为'TRUE',则错误放在先前的堆栈上.
INSTEAD OF 触发器
INSTEAD OF 触发器主要针对视图(VIEW)将触发的dml语句替换成为触发器中的执行语句,而不执行dml语句.
oracle用SQL语句新建用户时的一个bug
如果是我们公司的测试员来测的话,一定会把这个问题算做严重BUG^_^ 我这次一定会坚决支持这个观点,因为此
问题至少折磨了我一个钟头:(
在SQL Plus里执行这条语句:
create user "yellow" identified by "gentleman";
你猜会发生什么结果?如果你是个oracle的老手估计一眼就看出来了,而新手的话(比如说我)肯定要在这里折磨
一段时间。因为执行完以后,提示添加成功,并且从oracle的企业管理器中可以看到也确实添加了yellow这个用户,
但是!如果要用yellow来登录会提示帐号或密码出错,没天理啊!
开头我还以为是权限,或者其他的原因,后来折磨了好久,才发现,oracle建用户时用户名为大写的时候才行。
把语句改成
create user "YELLOW" identified by "gentleman";再测一下,ok,可以登录了。
并且!这个时候用"yellow"来登录也可以,也就是说,登录时它是不分大小写的。不说它是bug都说不过去了^_^
-------------------------
附:oracle对用户的操作SQL语句
1.添加帐号:
create user "用户名" identified by "密码";
create user "用户名" identified by "密码" TABLESPACE "USERS" ACCOUNT UNLOCK; //添加帐号时,用户名不能为小写。否则将造成不能登录的问题。
2.授权:
grant "某角色" to "用户名";
grant 某系统权限 to "用户名";
3.修改密码
alter user "用户名" identified by "密码";
4.删除用户
drop user "用户名"
5.列举所有用户
select * from all_users;
禁用某个触发器
ALTER TRIGGER <触发器名> DISABLE
重新启用触发器
ALTER TRIGGER <触发器名> ENABLE
禁用所有触发器
ALTER TRIGGER <触发器名> DISABLE ALL TRIGGERS 启用所有触发器
ALTER TRIGGER <触发器名> ENABLE ALL TRIGGERS 删除触发器
DROP TRIGGER <触发器名
CareergoalsModernsocietyissuchasocietyofrapideconomicdevelopmentandisalsoah…
HowToWriteACareerGoalsEssayTheobjectiveofaCareerGoalsessayistogaugewhethert…
TherearesomekeystepsincareerplanningTherearesomekeystepsincareerplanningThe…
1ShorttermcareergoalsIamveryinterestedinEnglishlearninganddedicatedtoEnglis…
工作目标篇careergoal求职英语有时候面试中招聘者会提问应聘者有关工作目标的问题目的就是了解应聘者做事的风格以及应聘者对这份…
本学期我校在镇教委、中心小学、教研室的指导下,坚持以科学发展观为统领,以推进课程改革为重点,以提升教师队伍素质、提高教学质量为核心…
团委办公室工作总结及工作计划经历了一个学期在团委办公室工作,收获了,体验了,学习了,努力了,也渐渐地认识到自己有很多方面需要改进。…
-摄像课结课总结经过这学期对摄影摄像的学习以及大量的实践操作,自己对摄像的理解加深了,无论是灯光的选择,还是影视中应有的拍摄流程,…
暑期美国游学总结报告暑假里,我参加了赴美文化交流团,受益匪浅。我这次去美国,从7月15号到8月4号,共21天,其中有2天是在飞机上…
中央确定20xx年为基层组织建设年。根据中央和省、市、区委有关文件精神,深入推进创先争优活动和基层组织建设年工作,促进社区基层党组…