Oracle手边常用命令及操作语句

Oracle手边常用命令及操作语句

作者:白宁超

时间:2016年3月4日11:24:08

摘要:日常使用oracle数据库过程中,常用脚本命令莫不是用户和密码、表空间、多表联合、执行语句等常规操作。另外表的导入导出也很常用,这些脚步命令之前都做有总结,今统一整理出最为常用的语句,使用方便而已。

 

1 oracle数据安装相关

oracle安装后自动生成sys(默认密码sysdba,最高权限)和system(管理员操作权限)用户

常用操作:

导出dmp:

exp userbnc/userbnc@MYORCL file=G:\db\temp\alltable.dmp tables=(S_USER,S_RK,S_OUT,S_CK,S_HW,S_WP,S_KC,S_YG)

导入dmp:

imp userbnc/userbnc@MYORCL file=G:\db\temp\alltable.dmp full=y

执行脚本:

@脚本名.sql

数据库MYORCL全部导出

exp userbnc/userbnc@MYORCL file=G:\db\temp\all.dmp full=y

imp userbnc/userbnc@MYORCL file=G:\db\temp\all.dmp full=y ignore=y

数据库MYORCL中userbnc用户导出

exp userbnc/userbnc@MYORCL file=G:\db\temp\yonghu.dmp owner=(userbnc)

exp userbnc/userbnc@MYORCL file=G:\db\temp\yonghu.dmp owner=(userbnc)

数据库MYORCL中S_USER,S_RK,S_OUT,S_CK,S_HW,S_WP,S_KC,S_YG表导出

exp userbnc/userbnc@MYORCL file=G:\db\temp\alltable.dmp tables=(S_USER,S_RK,S_OUT,S_CK,S_HW,S_WP,S_KC,S_YG)

导出表结构:

exp userbnc/userbnc@MYORCL file=G:\db\temp\alltable.dmp tables=(S_USER,S_RK,S_OUT,S_CK,S_HW,S_WP,S_KC,S_YG) rows=n

处理大表direct=y,速度快

exp userbnc/userbnc@MYORCL file=G:\db\temp\alltable.dmp tables=(S_USER,S_RK,S_OUT,S_CK,S_HW,S_WP,S_KC,S_YG) direct=y

表导不出,则修改:

alter table 表名 allocate extent

可以用tnsping TEST 来获得数据库TEST能否连上。

 

 

查询数据库下表名

select table_name from user_tables

查询数据库下用户名

select username from dba_users;

查询所有角色,oracle究竟有多少角色?

select * from dba_roles;

查询oracle中所有对象权限?

select distinct privilege from dba_tab_privs;

查询数据库的表空间

select tablespace_name from dba_tablespaces;

如何查询一个角色包括的    权限?

查询当前数据库全称?

select * from global_name

2 用户相关操作

创建用户与密码

Password [username]

conn username/password

create user [username] identified by [password] ---------dba权限

grant [username] to sysdba

Alter user [username] identified by [password] ---------dba权限

删除用户以及其用户下对象

如果删除的用户已经创建表,则删除时带参数cascade

账户锁定

例子:scott用户最多登录3次,锁定时间2天

create profile [lock_account] limit failed_login_attempts [3] password_lock_time [2]

解锁账户

Alter user [username] account unlock

例子:用户每隔10天修改登录密码,宽限期2天

create profile [username] limit password_life_time [10] password_grace_time [2]

3 表空间相关操作

创建表空间

create tablespace sp0001 datafile 'G:\db\tablespace\sp001.dbf' size 20m uniform size 128k;

扩展表空间

alter tablespace sp0001 add datafile 'G:\db\tablespace\sp002.dbf' size 300m;

删除表空间

drop tablespace sp0001 including contents and datafiles;

使用表空间

create table student(sid number(10) not null,sname varchar(50), ssex char(1), sphone varchar(50),constraint PK_student primary key (sid)) tablespace sp0001;

只读表空间

alter tablespace sp0001 read only;

使表空间脱机

alter tablespace sp0001 offline;

使表空间联机

alter tablespace sp0001 online;

查询数据库中表空间

select tablespace_name from dba_tablespaces;

查询表空间下所有表

select * from all_tables where tablespace_name='sp0001';

查询当前表所在空间

select tablespace_name,table_name from user_tables where table_name='STUDENT';

4 表操作相关

 

查看表结构

desc [tablename]

alert table 表名 add column 列名
alter table
表名 drop column 列名

授权部分表被查看

grant [select] on [tablename] to [username]

允许授权的对象继续授权

grant [select] on [tablename] to [username] with grant option

回收授权

Revoke [select] on [tablename] from [username]

创建表

create table T_TCM_THESIS (

ID NUMBER not null,

DOCNAME VARCHAR2(512),

YEARS VARCHAR2(128),

DOCTYPE VARCHAR2(128),

SUMMARY VARCHAR2(4000),

AUTHOR VARCHAR2(128),

SOURCE VARCHAR2(2048),

SOURCETYPE VARCHAR2(128),

MEMO VARCHAR2(1024),

TITLE VARCHAR2(512),

ENGLISHTITLE VARCHAR2(512),

AUTHORCOMPANY VARCHAR2(512),

KEYWORD VARCHAR2(512),

CONTENT CLOB,

DIGEST VARCHAR2(4000),

PATH VARCHAR2(256),

DISEASE VARCHAR2(256),

AUDITFLAG NUMBER(1),

constraint PK_TZJ_PINTOOL_THESIS primary key (ID)

);

comment on table T_TCM_THESIS is '中医文献表';

comment on column T_TCM_THESIS.ID is '编号';

comment on column T_TCM_THESIS.DOCNAME is '文献名';

comment on column T_TCM_THESIS.YEARS is '年代';

comment on column T_TCM_THESIS.DOCTYPE is '文献类型';

comment on column T_TCM_THESIS.SUMMARY is '内容提要';

comment on column T_TCM_THESIS.AUTHOR is '作者';

comment on column T_TCM_THESIS.SOURCE is '文献出处';

comment on column T_TCM_THESIS.SOURCETYPE is '文献出处类型';

comment on column T_TCM_THESIS.MEMO is '备注';

comment on column T_TCM_THESIS.TITLE is '标题';

comment on column T_TCM_THESIS.ENGLISHTITLE is '英文标题';

comment on column T_TCM_THESIS.AUTHORCOMPANY is '著者单位';

comment on column T_TCM_THESIS.KEYWORD is '关键词';

comment on column T_TCM_THESIS.CONTENT is '文献内容';

comment on column T_TCM_THESIS.DIGEST is '摘要';

comment on column T_TCM_THESIS.PATH is '文献内容文本文件';

comment on column T_TCM_THESIS.DISEASE is '相关病名';

comment on column T_TCM_THESIS.AUDITFLAG is '是否审核';

创建ID自增

/*创建序列*/

CREATE SEQUENCE EMP_SEQUENCE

INCREMENT BY 1 -- 每次加几个

START WITH 1 -- 从1开始计数

NOMAXVALUE -- 不设置最大值

NOCYCLE -- 一直累加,不循环

NOCACHE -- 不建缓冲区

/*创建触发器*/

create or replace trigger TR_EXA

before insert on T_TCM_THESIS for each row

begin

select to_char(EMP_SEQUENCE.nextval) into :new.id from dual;

end TR_EXA;

查询:

select * from T_TCM_THESIS;

 

/*创建学生表*/

create table student(

xh number(4),

xm varchar2(20),

sex char(2),

birthday date,

sal number(7,2)

);

/*添加学生表列*/

alter table student add (classid number(2));

insert into student values(1,'张三','男','10-9月-2014',3000,12);

/*修改时间类型*/

alter session set nls_date_format='yyyy--mm--dd';

insert into student values(2,'小明','男','2014-09-10',3000,12);

/*插入空值*/

insert into student(xh,xm,sex,birthday) values(3,'小丽','女',null);

/*条件查询*/

select * from student where birthday is not null;

/*设置保存点*/

savepoint aa;

/*删除表数据*/

delete from student;

/*回滚保存点的数据*/

rollback to aa;

/*删除表结构和数据*/

drop table student;

/*取消重复行*/

select distinct * from student;

/*命令行打开执行时间开关*/

set timing on;

/*命令行查询表结构*/

desc student;

/*设置别名*/

select xh "姓名" from student;

/*in多条件查询*/

select * from student where xm in('张三','小明');

/*order by 的使用*/

select * from student order by xh,sal desc;

update student set sal='3000' where xh=3;

 

表自身复制

Insert into [tablename](colname1,colname2…) select * form [tablename]

内置函数的空值代替

Select sal*13+nvl(comm,0)+13 "年工资",ename,comm from emp;

合并查询去除重复行

Select ename,sal,job from emp where sal>2500 union

Select ename,sal,job from emp where job='manager'

 

6 java如何使用数据库

1 加载驱动

Clsss.forName("Sun.jdbc.odbc.TdbcodbcDriver");

2 得到连接

Connection ct =DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:myoral","scott","password");

Statement sm=ct.createStarement();

ResultSet rs=sm.executeQuery("select * from emp");

While(rs.next())

System.out.println("username:"+rs.getString(arg0));

 

 

 

 

你可能感兴趣的:(Oracle手边常用命令及操作语句)