1. 程式人生 > >Oracle常用表與檢視操作

Oracle常用表與檢視操作

  • 新建表

CREATE TABLE 表名;
  • 刪除表

DROP TABLE 表名;
  • 刪除表資料

DELETE FROM 表名;
  • 修改表資料

UPDATE 表名 SET 欄位=值 WHERE 條件;
  • 複製表結構

CREATE TABLE 新表名 AS SLECET * 舊錶名 FROM WHERE 1=2;
  • 複製表結構和資料

CREATE TABLE 新表名 AS SELECT * FROM 舊錶名;
  • 建立檢視

CREATE VIEW 檢視名 AS SELECT 欄位 FROM 表名;
  • 檢視被鎖表

SELECT B.OWNER TABLEOWNER, B.OBJECT_NAME TABLENAME, C.OSUSER LOCKBY,
C.USERNAME LOGINID, C.SID SID, C.SERIAL# SERIAL
FROM V$LOCKED_OBJECT A,DBA_OBJECTS B, V$SESSION C 
WHERE B.OBJECT_ID = A.OBJECT_ID AND A.SESSION_ID =C.SID;
  • 查詢被鎖原因

SELECT L.SESSION_ID SID, S.SERIAL#,L.LOCKED_MODE,L.ORACLE_USERNAME,S.USER#,L.OS_USER_NAME,S.MACHINE, S.TERMINAL,A.SQL_TEXT,A.ACTION
FROM V$SQLAREA A, V$SESSION S, V$LOCKED_OBJECT L WHERE L.SESSION_ID = S.SID AND S.PREV_SQL_ADDR = A.ADDRESS ORDER BY SID, S.SERIAL#;
  • 解鎖所有表

DELETE  SDE.TABLE_LOCKS;
  • 解鎖指定表

ALTER SYSTEM KILL SESSION 'SID,SERIAL';
  • 查看錶空間中的表

SELECT TABLE_NAME, TABLESPACE_NAME FROM DBA_TABLES WHERE TABLESPACE_NAME = 表空間名; 
查看錶佔用空間記憶體:SELECT T.SEGMENT_NAME, T.SEGMENT_TYPE, SUM(T.BYTES / 1024 / 1024) "佔用空間(M)"
FROM DBA_SEGMENTS T
WHERE T.SEGMENT_TYPE='TABLE'
GROUP BY OWNER, T.SEGMENT_NAME, T.SEGMENT_TYPE;
  • 查詢表空間是否溢位

SELECT DBF.TABLESPACE_NAME,
DBF.TOTALSPACE "總量(M)",
DBF.TOTALBLOCKS AS 總塊數,
DFS.FREESPACE "剩餘總量(M)",
DFS.FREEBLOCKS "剩餘塊數",
(DFS.FREESPACE / DBF.TOTALSPACE) * 100 "空閒比例"
FROM (SELECT T.TABLESPACE_NAME,
SUM(T.BYTES) / 1024 / 1024 TOTALSPACE,
SUM(T.BLOCKS) TOTALBLOCKS
FROM DBA_DATA_FILES T
GROUP BY T.TABLESPACE_NAME) DBF,
(SELECT TT.TABLESPACE_NAME,
SUM(TT.BYTES) / 1024 / 1024 FREESPACE,
SUM(TT.BLOCKS) FREEBLOCKS
FROM DBA_FREE_SPACE TT
GROUP BY TT.TABLESPACE_NAME) DFS
WHERE TRIM(DBF.TABLESPACE_NAME) = TRIM(DFS.TABLESPACE_NAME);
  • 查看錶空間是否自動擴充套件

SELECT T.TABLESPACE_NAME,
D.FILE_NAME,
D.AUTOEXTENSIBLE,
D.BYTES,
D.MAXBYTES,
D.STATUS
FROM DBA_TABLESPACES T, DBA_DATA_FILES D
WHERE T.TABLESPACE_NAME = D.TABLESPACE_NAME
ORDER BY TABLESPACE_NAME, FILE_NAME;
  • 查看回收站中的表

SELECT * FROM USER_RECYCLEBIN T WHERE T.TYPE='TABLE'
  • 去除重複資料

DELETE FROM 待清理表名 A  WHERE A.ROWID != (SELECT MAX(ROWID) FROM 待清理表名 D WHERE D.區分欄位= A.區分欄位);