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:APPYITOPORADATAORCLTEST.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 字段名+字段约束

原文地址:https://www.cnblogs.com/xswt/p/11394032.html