Oracle資料庫開發SQL基礎 02使用者和表空間

2021-09-24 03:45:17 字數 2823 閱讀 2439

檢視登入使用者

啟用scott使用者

系統使用者

sys, system(sys許可權高於system)

sysman

scott(預設密碼是tiger)

使用system登入

[username/password] [@server] [as sysdba|sysoper]
首先是使用者名稱和密碼,其次,如果需要登入遠端資料庫,需要@+資料庫名或者ip位址+服務埠號,如果需要以管理員身份登入,再新增 as sysdba 或者 as sysoper。

show user;
可以檢視當前登入使用者。

desc dba_users;
可以檢視dba_users中包含哪些字段,例如使用者名稱,密碼,賬號狀態,預設表空間等,檢視所有使用者名稱的語句如下:

select username from dba_users;
啟用/鎖定使用者的語句

alter user username account unlock|lock;

檢視使用者表空間

建立、修改、刪除表空間

資料庫與表空間

表空間實際上是資料庫的邏輯儲存空間,可以把錶空間理解為在資料庫中開闢的一塊空間,用於存放資料庫的物件,乙個資料庫可以由多個表空間構成,oracle中有很多優化都是由表空間來實現的

表空間與資料檔案

表空間實際上是由乙個或者多個資料檔案構成的,資料檔案的位置和大小可以由使用者自定義,使用者儲存的一些表和資料庫中的一些其他的物件都是存放在表空間的資料檔案裡的。

表空間的分類

永久表空間

一般用來存放資料庫中一些需要永久存放的物件,比如某些表、檢視和儲存過程等內容。

臨時表空間

主要用於存放一些資料庫操作當中,中間執行的過程,之行結束之後,存放的內容就會被自動釋放,不會進行永久儲存。

undo表空間

用於儲存事務所修改的資料的舊址,即被修改之前的資料,可用於資料的回滾。

dba_tablespaces和user_tablespaces資料字典

同樣可用desc dba_tablespaces;來檢視資料字典的字段內容。

select tablespace_name from dba_tablespaces;可以檢視其中的表空間。

dba_users和user_users資料字典

可以從這兩個資料字典中查詢使用者所使用的表空間。比如

select default_tablespace,temporary_tablespace from dba_users where uaername='system';

設定使用者的預設或者臨時表空間

可以通過以下語句設定使用者的表空間:

alter user username					

default|temporary

tablespace tablespace_name;

建立表空間

可以通過以下語句建立表空間:

create [temporary] tablespace

tablespace_name

tempfile|datafile 'xx.dbf' size xx;

永久表空間的資料檔案可通過語句desc dba_data_files;檢視,臨時表空間的資料檔案可通過語句desc dba_temp_files;檢視。如select file_name from dba_data_files where tablespace_name='***';

修改(永久)表空間

修改表空間的狀態:

alter tablespace tablespace_name

online|offline;

當乙個表空間被設定成離線狀態後就無法使用了。

表空間的狀態可通過資料字典dba_tablespaces中的status欄位檢視。

select status from dba_tablespaces where tablespace_name='***';

設定唯讀或者可讀寫狀態:

alter tablespace tablespace_name

read only|read write;

修改資料檔案

向表空間中增加資料檔案:

alter tablespace tablespace_name

add datafile 'xx.dbf' size xx;

刪除表空間中的資料檔案:

alter tablespace tablespace_name

drop datafile 'xx.dbf';

刪除表空間

drop tablespace

tablespace_name [including contents];

Oracle資料庫開發SQL基礎 06 查詢語句

select distinct column name1,distinct 關鍵字可以去掉重複的資料 from table name where conditions 在sql plus中設定格式 更改查詢結果中的欄位名 column column name heading new name col...

Oracle資料庫SQL總結

1oracle時間段的查詢 1.1 場景 根據使用者輸入的時間段過濾出相應記錄。1.2 解決辦法 第一種寫法 sql select from t xjxx xjgl where createdate to date 2011 6 13 yyyy mm dd and createdate to dat...

Oracle資料庫SQL總結

1oracle時間段的查詢 1.1 場景 根據使用者輸入的時間段過濾出相應記錄。1.2 解決辦法 第一種寫法 select from t xjxx xjgl where createdate to date 2011 6 13 yyyy mm dd and createdate to date 20...