1. 程式人生 > >oracle日常管理維護sql語句

oracle日常管理維護sql語句

------------索引
建立普通索引:用itpux登入,先建立itpux表,根據表的id欄位來建立索引
create table  itpux(id int,sex char(1),name char(10));
create index itpux_idx1 on itpux(id);
根據性別名字建立索引
create index itpux_idx2 on itpux(sex,name);  
建立點陣圖索引
create bitmap index  itpux_bit_idx on itpux(sex)
查詢建立的索引
select object_name,object_type from user_objects
更改索引下一段的大小
alter index ITPUX_BIT_IDX storage(next 2M);
更改索引名字itpux_idx1改為itpux_index_id
alter index itpux_idx1  rename to  itpux_index_id;
重建索引
alter index itpux_index_id  rebuild tablespace index1
刪除索引(只是被標記,但是實際還佔用了很多塊)
exec dbms_stats.gather_table_stats('ITPUX','ITPUX');
進行碎片整理後,刪除的佔用空間就被釋放了
alter index  ITPUX_INDEX_ID coalesce;
查詢索引狀態
select * from index_stats


------------表空間
建立表空間
create tablespace itpux01 datafile '/oracle/oradata/db01/itpux01.dbf' size 2m autoextend off segment space management auto;
建立臨時表空間
create temporary tablespace temp1 tempfile '/oracle/oradata/db01/temp1.dbf' size 5m autoextend off;
建立undo表空間
create undo tablespace itpuxundo1 datafile '/oracle/oradata/db01/itpuxundo1.dbf' size 2m autoextend off;
查看錶空間
select * from dba_tablespaces;
select name from  v$tablespace;
檢視資料檔案
select * from dba_data_files
select  name from  v$datafile;
檢視臨時檔案資訊
select * from dba_temp_files
select * from v$tempfile
建立多個數據檔案表空間
CREATE TABLESPACE itpux02 DATAFILE '/oracle/oradata/db01/itpux03.dbf' SIZE 2M AUTOEXTEND OFF,
'/oracle/oradata/db01/itpux02.dbf' SIZE 2M AUTOEXTEND OFF,
'/oracle/oradata/db01/itpux04.dbf' SIZE 1M AUTOEXTEND ON NEXT 1M MAXSIZE 10M
EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
根據表空間名字檢視資料檔案
select * from dba_data_files where tablespace_name='ITPUX02';
建立大表空間
CREATE BIGFILE TABLESPACE itpuxbig DATAFILE '/oracle/oradata/db01/itpux05.dbf' SIZE 1M;
表空間擴充套件(新增一個2M資料檔案到表空間)
alter tablespace itpux02 add datafile '/oracle/oradata/db01/itpux10.dbf' size 2M autoextend off;
擴充套件臨時表空間
alter tablespace temp1 add tempfile '/oracle/oradata/db01/itpux11.dbf' size 2M autoextend off;
修改表空間裡面資料檔案的大小
alter database datafile '/oracle/oradata/db01/itpux10.dbf'  resize 3M;
alter database tempfile '/oracle/oradata/db01/itpux11.dbf'  resize 3M;
修改完表空間可以檢視下
select  name,bytes/1024/1024 from  v$datafile;
select  name,bytes/1024/1024 from  v$tempfile;
表空間重新命名
alter tablespace itpux02 rename to  itpux03;
刪除表空間
drop tablespace itpux03 including contents and datafiles;
更改表空間模式為只讀 
alter tablespace itpux01 read only;
更改表空間模式為只讀寫模式
alter tablespace itpux01 read write;
更改表空間線上
alter tablespace itpux01 online
更改表空間離線
alter tablespace itpux01 offline
將資料檔案改為離線
alter datafile  6 offline
將資料檔案改為離線並刪除
alter database datafile '/oracle/oradata/db01/itpux03.dbf' offline for drop;
查出這些表空間哪些是寫重做日誌檔案的那些是不寫的
select tablespace_name,logging from dba_tablespaces;
建立一個不寫重做日誌檔案的表空間
create tablespace itpux06 datafile '/oracle/oradata/db01/itpux16.dbf' size 2m autoextend off nologging;
把表空間修改為寫重做日誌
alter tablespace itpux06 logging;
------------------------------------------------表空間線上遷移
        案例:表空間線上遷移
1.查詢這個表空間檔案狀態是 online
select * from dba_data_files where tablespace_name='ITPUX01';
2.把表空見狀態修改為 offline 在查詢下
alter tablespace itpux01 offline;
3.更改作業系統層面資料檔名字 
host cp  '/oracle/oradata/db01/itpux01.dbf'   '/oracle/oradata/db01/itpux001.dbf' 
4.重新命名資料檔案,我們在把系統中檔名字改了但是資料庫表空間還不知道我們需要更新表空間資訊
alter tablespace itpux01 rename datafile '/oracle/oradata/db01/itpux01.dbf' to  '/oracle/oradata/db01/itpux001.dbf' 
5 改為online 在查詢下資料檔案
alter tablespace itpux01 online;
------------------------------------------------表空間離線遷移
shutdown immediate;
startup mount;
host cp  '/oracle/oradata/db01/itpux001.dbf'   '/oracle/oradata/db01/itpux01.dbf' 
重新命名這個檔案
alter database rename  file '/oracle/oradata/db01/itpux001.dbf' to  '/oracle/oradata/db01/itpux01.dbf' ;
alter database open
-----------------------------------------------把users表空間itpx使用者的物件遷移到itpux01表空間去
預設建立的itpux,該使用者物件是存在USERS表空間的
PLSQL:
create table itpux.table01(id number(12),c_data date)
insert into itpux.table01 values(1,sysdate);
insert into itpux.table01 values(1,sysdate);
為該表建立索引
create index idx_table01_id on itpux.table01('id')
查詢下發現該物件該使用者存在USERS表空間 需要將該物件遷移到itpux01表空間裡
select * from dba_segments where owner='ITPUX'
檢查物件資訊狀態記錄下來
......
遷移
alter table itpux.TABLE01 move tablespace itpux01;
重建索引
alter index  itpux.IDX_TABLE01_ID rebuild tablespace itpux01;
遷移完成之後在查詢,做各種檢查判斷是是否全部遷移,具體見2.3.16表空間與物件遷移
.......
如果這個表中有大欄位 就這樣子修改下儲存位置就好了
alter table itpux.table01 move  lob(date) store as (tablespace itpux01);


----------------------------------------------------------使用者管理
-建立使用者
create user itpux01 identified by itpux01
default tablespace users
temporary tablespace temp;
select * from dba_users where username='ITPUX01';
-授權
grant dba to  itpux01
-檢視使用者資訊
select * from dba_users where username='ITPUX01'
select * from all_users where username='ITPUX01'
-查詢使用者下的物件
select* from dba_objects where owner='ITPUX'
-修改使用者密碼
alter user itpux01 identified by  itpux01;
-鎖使用者或解鎖使用者
alter user itpux01 account lock
alter user itpux01 account unlock
-刪除使用者
drop user  itpux cascade;
--------------使用者許可權
--授予itpux能建立會話,建立表的許可權
grant create session,create table to  itpux01;
--授予itpux01能授予別人建立會話的許可權
grant create session to itpux01 with admin option;
--授予所有使用者dba許可權,不能做
grant dba to public
--回收所有使用者的dba許可權
revoke dba from  public;
--回收建立表的許可權
revoke create table from itpux;
--讓所有使用者都有許可權去訪問這張表,給所有使用者授權這張表
grant select on  scott.emp to public;
---------------------------使用者角色
--先建立itpux_role角色  在查詢看下
create role itpux_role;
select * from dba_roles where role='ITPUX_ROLE'
--執行角色授權,就是說在角色itpux_role下的所有使用者都對scott.emp表具有查詢許可權
grant select on  scott.emp  to  itpux_role
--把這個角色給刪掉
drop role  itpux_role;
--檢查角色授權資訊
select * from dba_role_privs where granted_role='ITPUX_ROLE'
--查詢使用者擁有哪些角色,這是在itpux使用者下查詢的,擁有itpux_role角色
select * from user_role_privs;
--查詢角色授權資訊
select * from dba_role_privs where granted_role='ITPUX_ROLE'
--取消角色
revoke itpux_role  from itpux;
--刪除角色
drop role  itpux_role;
--設定預設的角色
grant resource,connect to itpux;
grant dba to  itpux;
--啟用所有角色
set role  all;
--禁用所有角色
set role none;
--啟用某個角色
set  role  itpux_role;