鎖表的sql語句
⑴ Oracle 鎖表情況查詢sql
select LOCK_INFO.OWNER || '.' || LOCK_INFO.OBJ_NAME as "已鎖物件名稱", --物件名稱(已經被鎖住)
LOCK_INFO.SUBOBJ_NAME as "已鎖子物件名稱", -- 子物件名稱(已經被鎖住)
SESS_INFO.MACHINE as "機器名稱", -- 機器名稱
LOCK_INFO.SESSION_ID as "會話ID", -- 會話SESSION_ID
SESS_INFO.SERIAL# as "會話SERIAL#", -- 會話SERIAL#
SESS_INFO.SPID as "OS系統的SPID", -- OS系統的SPID
(SELECT INSTANCE_NAME FROM V$INSTANCE) "實例名SID", --實例名SID
LOCK_INFO.ORA_USERNAME as "ORACLE用戶", -- ORACLE系統用戶名稱
LOCK_INFO.OS_USERNAME as "OS用戶", -- 作業系統用戶名稱
LOCK_INFO.PROCESS as "進程編號", -- 進程編號
LOCK_INFO.OBJ_ID as "對象ID", -- 對象ID
LOCK_INFO.OBJ_TYPE as "對象類型", -- 對象類型
SESS_INFO.LOGON_TIME as "登錄時間", -- 登錄時間
SESS_INFO.PROGRAM as "程式名稱", -- 程式名稱
SESS_INFO.STATUS as "會話狀態", -- 會話狀態
SESS_INFO.LOCKWAIT as "等待鎖", -- 等待鎖
SESS_INFO.ACTION as "動作", -- 動作
SESS_INFO.CLIENT_INFO as "客戶資訊" -- 客戶資訊
from (select obj.OWNER as OWNER,
obj.OBJECT_NAME as OBJ_NAME,
obj.SUBOBJECT_NAME as SUBOBJ_NAME,
obj.OBJECT_ID as OBJ_ID,
obj.OBJECT_TYPE as OBJ_TYPE,
lock_obj.SESSION_ID as SESSION_ID,
lock_obj.ORACLE_USERNAME as ORA_USERNAME,
lock_obj.OS_USER_NAME as OS_USERNAME,
lock_obj.PROCESS as PROCESS
from (select *
from all_objects
where object_id in (select object_id from v$locked_object)) obj,
v$locked_object lock_obj
where obj.object_id = lock_obj.object_id) LOCK_INFO,
(select SID,
SERIAL#,
LOCKWAIT,
STATUS,
(select spid from v$process where addr = a.paddr) spid,
PROGRAM,
ACTION,
CLIENT_INFO,
LOGON_TIME,
MACHINE
from v$session a) SESS_INFO
where LOCK_INFO.SESSION_ID = SESS_INFO.SID
order by LOCK_INFO.SESSION_ID;
⑵ MySQL資料庫如何鎖定和解鎖資料庫表
第一步,創建資料庫表writer和查看錶結構,利用SQL語句:
create table writer(
wid int(10),
wno int(10),
wname varchar(20),
wsex varchar(2),
wage int(2)
第二步,向資料庫表writer插入五條數據,插入後查看錶里數據
第三步,利用鎖定語句鎖定資料庫表writer,利用SQL語句:
lock table writer read;
讓資料庫表只讀不能進行寫
第四步,為了驗證鎖定效果,可以查看資料庫表數據,利用SQL語句:
select * from writer;
第五步,利用update語句對id=5進行更新,SQL語句為:
update writer set wname = '胡思思' where id = 5;
第六步,利用unlock進行解鎖,SQL語句為:
unlock tables;
⑶ 兩個SQL的鎖表問題
不是很明白的你的意思,查看是否鎖表的sql:
select s.SID,s.SERIAL#,s.username,
decode(l.type,'TM','TABLE LOCK',
'TX','ROW LOCK',
NULL) LOCK_LEVEL,
o.owner,o.object_name,o.object_type,s.terminal,s.machine,s.program,s.osuser
from v$session s,v$lock l,dba_objects o
where s.sid=l.sid
and o.object_id=l.id1
and s.username is not null ;
如果1 .2是同時操作一張表,最好是執行完一條sql先commit一下在執行第二條這樣肯定不會鎖表,我不知道我理解對了沒有?
ps:不能,資料庫的事務機制不允許同一時刻同一記錄update 如果先update操作會先lock table等執行完釋放資源才有其他的操作。
以下是我做過的測試:
eg:select * from table for update;
1.不允許做for update查詢。
2.允許普通search查詢。
3.不允許對表中任何記錄做update操作;
4.允許insert操作;
5.不允許delete操作。
另:一樓說的是對的。
⑷ oracle 鎖表時,怎麼查出是哪些SQL語句導致了鎖表
SELECT S.SID SESSION_ID, S.USERNAME, DECODE(LMODE, 0, 'None', 1, 'Null', 2, 'Row-S (SS)', 3, 'Row-X (SX)', 4, 'Share', 5, 'S/Row-X (SSX)', 6, 'Exclusive', TO_CHAR(LMODE)) MODE_HELD, DECODE(REQUEST, 0, 'None', 1, 'Null', 2, 'Row-S (SS)', 3, 'Row-X (SX)', 4, 'Share', 5, 'S/Row-X (SSX)', 6, 'Exclusive', TO_CHAR(REQUEST)) MODE_REQUESTED, O.OWNER||'.'||O.OBJECT_NAME||' ('||O.OBJECT_TYPE||')', S.TYPE LOCK_TYPE, L.ID1 LOCK_ID1, L.ID2 LOCK_ID2
FROM V$LOCK L, SYS.DBA_OBJECTS O, V$SESSION S
WHERE L.SID = S.SID AND L.ID1 = O.OBJECT_ID;
執行上記SQL語句,可以查尋到資料庫中的鎖的情報.
SESSION_ID, USERNAME, MODE_HELD, MODE_REQUESTED, OBJECT_NAME, LOCK_TYPE, LOCK_ID
分別是 擁有鎖的SESSION_ID,擁有鎖的USERNAME,鎖的執行模式MODE_HELD,鎖的請求MODE_REQUESTED,鎖所在的資料庫對象名
,鎖的類型,鎖的ID
還有你問的應該是資料庫中表出現死鎖情況吧,是哪些sql過程導致了表死鎖:
解決方案如下:
1.查哪個過程被鎖:
查V$DB_OBJECT_CACHE視圖:
SELECT * FROM V$DB_OBJECT_CACHE WHERE OWNER='過程的所屬用戶' AND CLOCKS!='0';
2. 查是哪一個SID,通過SID可知道是哪個SESSION:
查V$ACCESS視圖:
SELECT * FROM V$ACCESS WHERE OWNER='過程的所屬用戶' AND NAME='剛才查到的過程名';
3. 查出SID和SERIAL#:
查V$SESSION視圖:
SELECT SID,SERIAL#,PADDR FROM V$SESSION WHERE SID='剛才查到的SID';
查V$PROCESS視圖:
SELECT SPID FROM V$PROCESS WHERE ADDR='剛才查到的PADDR';
4. 殺進程:
(1)先殺ORACLE進程:
ALTER SYSTEM KILL SESSION '查出的SID,查出的SERIAL#';
(2)再殺操作系統進程: (linux)
KILL -9 剛才查出的SPID或ORAKILL 剛才查出的SID 剛才查出的SPID。
⑸ 怎樣用SQL給SQL2880特定表加鎖解鎖
加鎖的語句如下:
SELECT*FROM表名WITH(TABLOCK);
這里沒有解鎖的概念,只有不加鎖的概念,語句如下:
SELECT*FROM表名WITH(NOLOCK);
加鎖的解釋:
TABLOCK(表鎖)
此選項被選中時,SQLServer將在整個表上置共享鎖直至該命令結束。這個選項保證其他進程只能讀取而不能修改數據。
不加鎖的解釋:
NOLOCK(不加鎖)
此選項被選中時,SQLServer在讀取或修改數據時不加任何鎖。在這種情況下,用戶有可能讀取到
⑹ 如何查詢鎖表的SQL
用下邊的語句查詢,如果想結束直接kill
SELECT SPID=p.spid,
DBName = convert(CHAR(20),d.name),
ProgramName = program_name,
LoginName = convert(CHAR(20),l.name),
HostName = convert(CHAR(20),hostname),
Status = p.status,
BlockedBy = p.blocked,
LoginTime = login_time,
QUERY = CAST(t.TEXT AS VARCHAR(MAX))
FROM MASTER.dbo.sysprocesses p
INNER JOIN MASTER.dbo.sysdatabases d
ON p.dbid = d.dbid
INNER JOIN MASTER.dbo.syslogins l
ON p.sid = l.sid
CROSS APPLY sys.dm_exec_sql_text(sql_handle) t
WHERE p.blocked = 0
AND EXISTS (SELECT 1
FROM MASTER.dbo.sysprocesses p1
WHERE p1.blocked = p.spid)
⑺ 怎樣查詢出SQLSERVER被鎖的表,以鎖表的SQL語句
查看被鎖表:
select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName
from sys.dm_tran_locks where resource_type='OBJECT'
spid 鎖表進程
tableName 被鎖表名