Oracle資料庫常用總結(持續更新)
Oracle是甲骨文(Oracle)公司的一款關係型資料庫管理系統(Relational Database Management System:RDBMS),在關係型資料庫領域,是最常用的資料庫之一,其他常用關係型資料庫還有:開源的MySQL,IBM的DB2,微軟的SQL Server。
Oracle 採用使用者的方式隔離資料,MySQL採用資料庫的方式隔離資料。
使用者管理
使用者級別:超級管理員、系統管理員、普通使用者
- sys:超級管理員 (登入 使用者名稱:sys /as sysdba ,密碼:隨意)
- system:系統管理員
- futao:普通使用者
建立使用者:
建立使用者futao、密碼為ft
create user futao identified by ft;
修改使用者futao密碼為123
alter user futao identified by 123;
命令列中切換另一個使用者: conn username/password
授權:
許可權:create any table
角色:connect, resource
建立角色:create role myrole;
給角色授權:grant create any table, connect, resource to myrole;
給使用者授權:grant myrole ,connect, create any table to futao;–可以將角色、許可權授予給使用者
刪除使用者:
drop user futao;
表空間管理
表空間(tablespace),邏輯概念,對應一個數據檔案,系統預設表空間:user,temp(臨時空間)。
建立表空間(以管理員的身份):
create tablespace myts datafile ‘e:/myts.dbf’ size 50m;
修改使用者表空間:
alter user futao default tablespace myts;
表的管理
Oracle中表的命名規則:
- 必須以字母開頭 ,字元在1~38位之間 名字中只能包含字母、數字、下劃線、$、#
- 不能使用Oracle中的保留字和關鍵字,如:user
- 一個Oracle伺服器中同一個使用者,不能建立同名字的物件
- Oracle中所有物件名不區分大小寫
- 建議表名有實際意義
Oracle中常用的資料型別:
1、數值
number(p,s) p表示位數,s表示小數點後幾位
number(p)
取值範圍 1~38位
integer
2、可變字元型
varchar2(length)
根據實際內容長度改變欄位長度
3、不可變字元型
char(length)
不會根據實際內容長度改變欄位長度,始終為定義長度length
4、日期型別
date
匯入匯出dmp檔案
匯入:在命令列模式下執行“imp”命令,然後根據嚮導匯入
匯出:在命令列模式下執行“exp”命令,然後根據嚮導匯出
資料字典
資料字典:tabs , user_tables , user_constraints
select * from tabs; //查詢全部使用者表
select table_name from user_tables; //查詢全部使用者表
select constraint_name from user_constraints; //查詢約束
sql執行記錄:v$sql
查詢sql執行記錄中,sql中包含“content”的sql語句執行記錄,且按執行時間倒敘排序:
select * from v$sql t where t.SQL_TEXT like ‘%content%’ order by t.LAST_ACTIVE_TIME desc;