oracle 常用命令

时间:2022-07-22
本文章向大家介绍oracle 常用命令,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在Dos命令下输入 sqlplus 可以打开 Oracle 自带的 SQL Plus 并出现登录提示,而输入 sqlplus/nolog则打开 SQL Plus 后不出现登陆提示;。 在 SQL Plus 中输入 conn 可以连接数据库,其中的一种格式为 conn 用户名/密码 如: conn scott/tiger

修改密码

在运行栏里面敲: sqlplus /nolog 回车 接着在弹出框里面敲:conn sys/password as sysdba回车(连接sys用户,加上 as sysdba,是以系统管理员的身份来登录的,如果是普通用户不需要as sysdba) 提示已连接 接着敲:alter user scott identified by tiger;回车(修改密码) 最后敲:alter user scott account unlock;回车(对SCOTT解锁)

数据库导出(exp)

exp常用选项说明

  1. dumpfile: 数据文件名称
  2. logfile: 日志文件名称
  3. tables: 指定导出的表
  4. file: 用于指定导出文件名
  5. rows: 用于指定是否到处表中的数据
  6. inctype: 用于指定执行导出操作的增量类型
  7. fully=y: 导入全部内容
  8. owner: 指定导出的方案

语法

exp 用户名/密码@哪个数据库 tables=(表名) file=本地存放路径

exp test_1209/test_1209@127.0.0.1/orcl tables=(TEST_INDEX) file=d:test_index.dmp

导出表结构

exp test_1209/test_1209@127.0.0.1/orcl tables=(TEST_INDEX) file=d:test_index.dmp rows=n

自用语句

exp aqsc_1221/aqsc_1221@192.168.1.97/orcl rows=y indexes=n compress=n STATISTICS=none GRANTS=n TRIGGERS=n CONSTRAINTS=n buffer=65536 feedback=100000 file=aqsc_1221_0530.dmp log=aqsc_1221_0530.log tables=act_ge_bytearray,act_ge_property,act_hi_actinst,act_hi_attachment,act_hi_comment,act_hi_detail,act_hi_identitylink,act_hi_procinst,act_hi_taskinst,act_hi_varinst,act_id_group,act_id_info,act_id_membership,act_id_user,act_re_deployment,act_re_model,act_re_procdef,act_ru_event_subscr,act_ru_execution,act_ru_identitylink,act_ru_job,act_ru_task,act_ru_variable

注意

在cmd中导出表时,习惯性的在命令的末尾加上了分号(;) [ 但是导出没有报错 ] 导致了备份的文件不是dmp 的扩展名,而变成了 dmp; 因此在导入表的时候,一直会有报错。

数据库导入(imp)

dumpfile 数据文件名称 logfile 日志文件名称 schemas 数据对象集合所属用户 remap_schema 当数据不同源时,需设置此项 remap_tablespace 当两个用户表空间不一致时,需设置此项 version 当数据库版本不同时,需设置此项

所有

  • full=y 是导入文件中全部内容
  • ignore=y相当于,如果没有的表,创建并倒入数据,如果已经有的表,忽略创建的,但不忽略倒入
imp aqsc_0305/aqsc_0305@127.0.0.1/orcl file=aqsc_1221_20190529.dmp log=aqsc_1221_20190529.log full=y

imp aqsc_0305/aqsc_0305@127.0.0.1/orcl file=aqsc_1221_20190529.dmp log=aqsc_1221_20190529.log full=y ignore=y

单表

imp aqsc_1221/aqsc_1221@127.0.0.1/orcl file=aqsc_1221_0618.dmp log=aqsc_1221_0618.log tables=(sys_menu)

创建视图

create view ZT_TRAFFICCOMPANY as select * from FSZHATU.ZT_TRAFFICCOMPANY;

根据结果集创建表

create table zt_car as select * from fszhatu.zt_car 1=2

创建表sql

----
----012.xf_file:项目附件
----drop   table xf_file;
create table xf_file(
	id	VarChar2(50)	Not Null	,	--id:主键
	type	VarChar2(20)		,	--type:类型(手册、文书等)
	sx	Number(9)		,	--sx:顺序(保留接口)
	gcxx_id	VarChar2(50)		,	--gcxx_id:工程信息id(保留接口)
	gcxx_form_id	VarChar2(50)		,	--gcxx_form_id:文书id(保留接口)
	name	VarChar2(50)		,	--name:文件名称
	ext	VarChar2(20)		,	--ext:文件后缀
	path	VarChar2(100)		,	--path:文件路径
	create_date	Date	Not Null	,	--create_date:创建时间
	create_by	VarChar2(50)	Not Null	,	--create_by:创建者标识
	update_date	Date		,	--update_date:更新时间
	update_by	VarChar2(50)		,	--update_by:更新者标识
	remarks	VarChar2(250)		,	--remarks:备注信息
	del_flag	Number(4)			--del_flag:删除标记
)
;

alter table XF_FILE add constraint PK_XF_FILE primary key(ID) ;


comment on table XF_FILE			is	'xf_file:项目附件';

comment on column xf_file.id 			is	'id:主键';
comment on column xf_file.type 			is	'type:类型(手册、文书等)';
comment on column xf_file.sx 			is	'sx:顺序(保留接口)';
comment on column xf_file.gcxx_id 			is	'gcxx_id:工程信息id(保留接口)';
comment on column xf_file.gcxx_form_id 			is	'gcxx_form_id:文书id(保留接口)';
comment on column xf_file.name 			is	'name:文件名称';
comment on column xf_file.ext 			is	'ext:文件后缀';
comment on column xf_file.path 			is	'path:文件路径';
comment on column xf_file.create_date 			is	'create_date:创建时间';
comment on column xf_file.create_by 			is	'create_by:创建者标识';
comment on column xf_file.update_date 			is	'update_date:更新时间';
comment on column xf_file.update_by 			is	'update_by:更新者标识';
comment on column xf_file.remarks 			is	'remarks:备注信息';
comment on column xf_file.del_flag 			is	'del_flag:删除标记';

创建表空间

DATAFILE: 表空间数据文件存放路径 SIZE: 起初设置为200M 空间名称 MOF_TEMP 与数据文件名称不要求相同,可随意命名. AUTOEXTEND ON/OFF 表示启动/停止自动扩展表空间

create tablespace test_data
logging
datafile 'C:/app/Ray/oradata/orcl/TEST_DATA.dbf'
size 200m
autoextend on next 100m
maxsize 2048m
extent management local;

删除表空间

drop tablespace test_data
including contents and datafiles
cascade constraints;

创建用户

create user test_1209 --用户名
identified by test_1209 --密码
default tablespace test_data --默认表空间
grant dba to test_1209 --给权限

用户授权 - 角色

grant dba to test_1209;

语法

grant [权限名(select|insert |update|delete|all)] on [表名] to [用户名]

分配权限

例如:给user1授权查询table1的权限:

grant select on table1 to user1;

上面是用sql分配权限的办法,也可以通过角色来授权,先给角色权限,然后再给用户分配角色

角色权限

Oracle主要系统角色:

  1. DBA: 拥有全部特权,是系统最高权限,只有DBA才可以创建数据库结构。
  2. RESOURCE:拥有Resource权限的用户只可以创建实体,不可以创建数据库结构。
  3. CONNECT:拥有Connect权限的用户只可以登录Oracle,不可以创建实体,不可以创建数据库结构。
  4. 普通用户:一般授予connect, resource权限。
  5. DBA管理用户:授予connect,resource, dba权限。
  6. 有时候可以直接授权Oracle的系统角色,也可以自己创建角色,然后授权

删除用户

drop user test_1209 cascade;

查看所有用户

select * from dba_users;

清空 Activiti 表数据

truncate table act_hi_actinst;
truncate table act_hi_detail;
truncate table act_hi_identitylink;
truncate table act_hi_procinst;
truncate table act_hi_taskinst;
truncate table act_hi_varinst;
truncate table act_ru_event_subscr;
truncate table act_ru_execution;
truncate table act_ru_task;
truncate table act_ru_variable;
truncate table act_ru_event_subscr;
truncate table act_ru_execution;
truncate table act_ru_identitylink;
truncate table act_ru_job;


delete from act_ru_task;
delete from act_ru_identitylink;
delete from act_ru_variable;
delete from act_ru_event_subscr;
delete from act_ru_execution;

索引

创建索引

create index index_hist_sr on ZT_POSITION_HIST (SYNCHRO_DATE, RECEIVE_DATE);
create index index_hist_rd on ZT_POSITION_HIST (RECEIVE_DATE);
create index index_hist_PRM on ZT_POSITION_HIST (R_PLATE_NUMBER);

查看索引个数和类别

select *
from USER_INDEXES
where TABLE_NAME = 'ZT_POSITION_HIST';

查看索引被索引的字段

select *
from user_ind_columns
where table_name = 'ZT_POSITION_HIST'
  and column_name = 'RECEIVE_DATE';

新增

从Oracle数据库中读取数据,自动生成INSERT语句

select 'INSERT INTO TEST_INDEX (ID,A,B,C)
 VALUES(' || '''' || ID ||'''' || ','
|| '''' || A || '''' || ','
|| '''' || B || '''' || ','
|| '''' || C || '''' || ');'
 From TEST_INDEX order by ID