oracle常用命令(1)


oracle常用命令

一、登錄

1、管理員身份登錄:sqlplus/nolog--->conn/as sysdba

2、普通用戶登錄:sqlplus/nolog---->conn 用戶名/密碼

3、管理員切換到普通用戶:conn 用戶名/密碼

4、普通用戶切換到管理人員:conn sys as sysdba,然后輸入密碼回車

二、角色

1、oracle預訂角色

Connect角色---》普通用戶

CONNECT SESSION-修改會話

Create cluster--建立簇族

Create database link--建立數據庫連接

Create sequence--建立序列

Create session--建立會話

Create synonym--建立同義詞

Create view--建立視圖

Resource角色--》開發人員

Create cluster--建立簇族

Create procedure--建立過程

Create sequence--建立序列

Create table--建表

Create trigger--建立觸發器

Create type--建立類型

Dba角色--》管理員【具備所有系統權限,但是無法啟動和關閉數據庫】

備份任何表、執行任何操作、查詢任何表

2、自定義角色

創建角色:create role角色名 not identified(建立公用角色)/create role 角色名 identified by密碼(建立私用角色)

3、角色授權:

3.1創建用戶沒有分配任何權限的時候,oracle自帶的上sqlplus上登錄該用戶會報錯

3.2 grant 權限名稱 to 角色名【注意:系統權限unlimited tablespace 和對象權限with grant option選項不能賦予給角色 】

4、顯示角色信息

4.1查詢角色信息:select * from role_sys_privs where role = '角色名稱'

4.2 查詢所有的角色:select * from dba_roles

4.3查詢角色系統權限:select privilege,admin_option from role_sys_privs whererole='角色名'

4.4查詢角色對象具有的權限:select privilege,admin_option from dba_tab_privs where role='角色名'

4.5查詢用戶具有角色以及默認角色:select grant_role from dba_sys_privs where grantee='用戶名'

4.6將角色分配給用戶:grant 角色名 to 用戶名 with admin option[加上with admin option是為了用戶可以將system分配給它的角色分配給別的其他用戶]

4.7刪除角色:drop role角色名稱

三、分配權限

3.1系統權限

(1)查看oracle系統權限:select * from sys_privilege_map

(2)分配權限:grant 權限名稱1,權限名稱2 to 用戶名 with admin option

(3)回收系統權限:revoke create session from 用戶名

3.2對象權限

前提:Smith用戶訪問scott.emp表

(1)常用的對象權限:alter、delete、select、insert、update、index、reference引用、execute

(2)查看對象權限:select distinct privilege from scott.emp

(3)授予對象權限:grant select on emp to monkey --授權查詢權限給monkey/grant delete on emp to monkey--授權刪除權限給monkey【也可以一次性授予權限:grant all on em to monkey】

(4)授予更具體的權限(比如對列的操作):grant update on emp(sal) to monkey---授權monkey用戶emp表sal列的修改權限

(5)授予execute權限[用戶要執行其他的方案的包、過程、函數必須要有execute權限]

(6)授予index權限[用戶在表scott.emp表上建立索引,需要index權限]

(7)使用with grant option選項【該選項用於轉授對象權限,只能授予用戶,不能授予角色】

四、用戶

4.1創建用戶:create user 用戶名 identifiled by 密碼

4.2刪除用戶:drop user 用戶名

4.3查看所有用戶:select  * from dba_users/all_users或者select * from user_users

4.4查看用戶角色

(1)當前用戶被激活的角色:select * from session_roles

(2)當前用戶被授予的角色:select * from user_role_privs

(3)全部用戶被授予的角色:select * from dba_role_privs

(4)查看某個用戶擁有的角色:select * from dba_role_privs where grantee='用戶名'

 五、oracle基本操作

 表空間--》用戶--》表

1、建表空間:create tablespace 表間名 datafile '數據文件名【路徑可以沿用系統自帶數據文件,但是記得修改文件名】' size 表空間大小

  • 知識點普及

  Oracle數據庫:數據庫的物理結構是由數據庫的操作系統文件所決定,每一個Oracle數據庫是由三種類型的文件組成:數據文件  

  [select * from v$logfile]、日志文件和控制文件[查看控制文件存放地址:select name from v$controlfile]。數據庫的文件為數據庫信息提供真正的物理存儲(就是數據文件)

  oracle物理數據文件【查看數據文件:select name v$datafile

  一個oracle數據庫有一個或多個數據文件

  一個數據文件只與一個數據庫連接

  一旦建立,數據文件只增不減

  一個表空間由一個或多個數據文件組成

  建用戶:create user 用戶名 identified by 密碼 default tablespace 表空間

  給用戶授權:grant connect ,resource to 用戶名[grant dba to 用戶名]

  建表:create table 表名

(1)、案例:

create tablespace data_test datafile 'D:\APP\YITOP\ORADATA\ORCL\TEST.DBF' size 2000M;

 

Tablespace created【建表空間和數據文件】

create user wangt identified by wtwrws666 default tablespace data_test;【建用戶】

grant connect,resource to wangt;grant dba to wangt;

conn wangt/wtwrws666[從管理員切換到普通用戶]

建表:語法格式alter table 表名 add constraint 約束名 約束內容【alter table info add constraint ck_info_age check(age>=0and age<=100)

六、增刪改查

1、改

  修改表

 

  重命名表:rename 表名 to 新表名

 

  向表中添加注釋:comment on table 表名 is ‘注釋文字’

 

  更新表數據:update 表名 set 列名=’新值’ where 條件

  修改列

 

  修改列名:alter table 表名 rename column 列名 to 新列名

 

  向列中添加注釋:comment on column 表名.列名 is ‘注釋文字’

 

  修改列的屬性:alter table 表名 modify 列名 varchar2(字符長度)/number(精度)/char(數據類型/default ‘默認值’);

 

2、增

  插入數據到表中:insert into 表名 values(‘數據’,’’,’’,’’)

3、刪

  3.1刪除表字段

  alter table 表名 drop column 字段名

  3.2刪除表

  • 刪除某條數據delete from 表名 where 條件
  • 刪除整張表的數據:truncate table 表名
  • 刪除整張表:drop table 表名
  • 刪除有外鍵約束的表:drop table student cascade constraints;
  • 刪除擁有對象的用戶:drop table 用戶名 cascade;

4、查

語法:select[distinct] * |列名稱 [別名],列名稱 [列名], from表名稱[別名][where 過濾條件(s)][order by 字段[ASC|DESC],……]

七、約束

(1)alter table 表名 add constraint 約束名(約束名簡寫_表名)約束(字段名)

例如:添加約束鍵:alter table INFOS add constraint ck_infos_check check(age>=0 and age<=100)

   添加外鍵:alter table scores add constraint fk_scores_infos_stuid foregin key(stuid) references infos(stuid) references infos(stuid)

   主鍵約束:alter table 表名 add constraint 約束名稱 primary key(列名)

   外鍵約束: alter table 表名1 add constraint 約束名稱 foreign key(列名) references 表名2(列名)

   檢查約束:alter table 表名 add constraint 約束名稱 check(檢查條件)

   不為空:alter table 表名 add constraint 約束名稱  not null(列名)

   唯一約束:alter table 表名 add constraint 約束名稱 unique(列名)

    默認值約束:alter table 表名 add constraint 約束名稱 default ‘默認值’

   給列添加約束

    alter table 表名 add 字段名+字段約束


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM