oracle

to_char(sysdate,'YYYY/MM/DD HH24:MI:SS')

trunc(u.change_time)=to_date('2018-01-18','yyyy-mm-dd')

 

sqlplus "/as sysdba"不用登陆进入数据库

startup oracle例程启动

lsnrctl start

lsnrctl restart

lsnrctl status

lsnrctl stop

 导入

新建连接

1,连接的名字不能太短

2,设置dba权限

3,imp hbcmatc/123456@tst file=D:\sql-workspace\edu2.dmp  tablespaces=hbcmatc fromuser=edu2 touser=hbcmatc


imp edu2017/123456@tst file=D:\sql-workspace\edu2017180314.dmp tablespaces=edu fromuser=edu2017 touser=edu2017

 导出

11G中有个新特性,当表无数据时,不分配segment,以节省空间
解决方法:

1、insert一行,再rollback就产生segment了。

该方法是在在空表中插入数据,再删除,则产生segment。导出时则可导出空表。

2、设置deferred_segment_creation 参数

show parameter deferred_segment_creation

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
deferred_segment_creation boolean TRUE
SQL> alter system set deferred_segment_creation=false;

系统已更改。

SQL> show parameter deferred_segment_creation

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
deferred_segment_creation boolean FALSE

该参数值默认是TRUE,当改为FALSE时,无论是空表还是非空表,都分配segment。

需注意的是:该值设置后对以前导入的空表不产生作用,仍不能导出,只能对后面新增的表产生作用。如需导出之前的空表,只能用第一种方法。

先查询一下当前用户下的所有空表

select table_name from user_tables where NUM_ROWS=0;

用以下这句查找空表

select 'alter table '||table_name||' allocate extent;' from user_tables where num_rows=0
把查询结果导出,执行导出的语句
alter table LP_PHASEMARKINDICATORSYS allocate extent;
alter table LP_PHASEPASSINDICATORSYS allocate extent;
alter table LP_TASK allocate extent;
alter table LP_TASKDATELIMIT allocate extent;
alter table LP_TASKJUDGE allocate extent;
alter table LP_TASKSCOPE allocate extent;
alter table LP_USERLEARNPATHPASS allocate extent;
alter table LP_USERTASK allocate extent;
alter table LP_USERTASKAUDIT allocate extent;
alter table LT_LEARNTASK allocate extent;

显示deferred_segment_creation状态
show parameter deferred_segment_creation;
deferred_segment_creation boolean TRUE
将deferred_segment_creation改为false
alter system set deferred_segment_creation=false;
系统已更改。
然后再执行
exp 用户名/密码@数据库名(实例名) file=/home/oracle/exp.dmp log=/home/oracle/exp_smsrun.log

 

导出空表

数据库表导出语句:
exp edu2017/123456@tst file=d:\edu20170531.dmp tables=(table1,table2)
1.先分析表,显示当前数据库行为0的表名
select table_name from user_tables where num_rows=0;
2.将空表表名用逗号隔开并显示,复制粘贴逗号隔开的空表表名,填入table1,table2的位置
select listagg(table_name, ',') within group( order by table_name) from user_tables where num_rows=0;

 

 查询表空间相关信息

SELECT T.TABLESPACE_NAME,D.FILE_NAME,D.AUTOEXTENSIBLE,D.BYTES,D.MAXBYTES,D.STATUS 
FROM DBA_TABLESPACES T,DBA_DATA_FILES D 
WHERE T.TABLESPACE_NAME =D.TABLESPACE_NAME 
ORDER BY TABLESPACE_NAME,FILE_NAME;

查询当前表空间 

select username,default_tablespace from user_users

查询所有表空间

select tablespace_name from dba_tablespaces

查询DBA用户username和表空间

select username,default_tablespace from dba_users

创建表空间

create tablespace hbcmatc datafile 'D:\sql-workspace\hbcmatc.dbf'
size 1024M autoextend on next 100M MAXSIZE UNLIMITED

删除表空间以及数据

drop tablespace hbcmatc including contents and datafiles

删除连接以及用户

https://jingyan.baidu.com/article/d2b1d10294f2be5c7e37d4ad.html

以oracle为数据源,配置tomcat连接池和数据源

http://blog.csdn.net/u011768325/article/details/38655455

数据源的由来以及JNDI数据源的配置

https://www.cnblogs.com/xdp-gacl/p/3951952.html

 

ORACLE删除当前用户下所有的表的方法

1、如果有删除用户的权限,则可以:

drop user user_name cascade;

加了cascade就可以把用户连带的数据全部删掉。

删除后再创建该用户。
--创建管理员用户
create user 用户名 identified by 密码 default tablespace space_data(表空间名称) temporary tablespace space_temp(临时表空间名称);
--授权
grant connect,dba to 用户名;
--修改限额
ALTER USER "用户名" QUOTA UNLIMITED ON SPACE_DATA(表空间名称);

--查看所有用户对象
select uo.object_name,uo.object_type from user_objects uo where uo.object_type<>'LOB' order by uo.object_type desc

 

2、如果没有删除用户的权限,则可以执行:

select 'drop table '||table_name||';' 
from cat 
where table_type='TABLE'

将会输出一批删除表的sql语句,这些SQL语句执行一下就可以了。(需要有drop table的权限)

3、

revoke unlimited tablespace from hbcmatc;

alter user hbcmatc quota 0 on users;

alter user hbcmatc quota unlimited on hbcmatc;

更改表空间名称

alter tablespace hbcmatc rename to users;

 查询约束名为'  '的数据

SELECT * FROM USER_CONSTRAINTS
WHERE CONSTRAINT_NAME = '  ';

 

 

 

执行sql语句出错

1.

CREATE TABLE "EDU2017"."EDU_TECHPLAN_REUSE"
( "TEACHPLAN_REUSE_ID" NUMBER(20,0) NOT NULL ENABLE,
"TEACHPLAN_ID" NUMBER(20,0) NOT NULL ENABLE,
"PLANCLASSTERM_ID" NUMBER(20,0) NOT NULL ENABLE,
"STARTDATE" DATE NOT NULL ENABLE,
"ENDDATE" DATE NOT NULL ENABLE,
"CREATECLASSSTATUS" NUMBER(1,0),
CONSTRAINT "EDU_TECHPLAN_REUSE_PK" PRIMARY KEY ("TEACHPLAN_REUSE_ID"),
CONSTRAINT "FK_TEACHPLAN" FOREIGN KEY ("TEACHPLAN_ID")
REFERENCES "EDU2017"."EDU_TEACHPLAN" ("TEACHPLAN_ID") ENABLE,
CONSTRAINT "FK_PLANCLASSTERMS" FOREIGN KEY ("PLANCLASSTERM_ID")
REFERENCES "EDU2017"."EDU_PLANCLASSTERMS" ("PLANCLASSTERMS_ID") ENABLE );

有的地方有','有的地方没有','缺少关键字

2.

alter table "EDU_TRAINNOTICE" add (TEACHPLAN_REUSE_ID NUMBER(20));
COMMENT ON COLUMN "EDU_TRAINNOTICE"."TEACHPLAN_REUSE_ID" IS '教学计划复用ID,如果为空则此原计划对应的班期没有复用~~';

comment之前加;

3.

alter table EDU_TRAINBUDGET add (TEACHPLAN_REUSE_ID NUMBER(20));
COMMENT ON COLUMN "EDU_TRAINBUDGET "."TEACHPLAN_REUSE_ID" IS '教学计划复用ID,如果为空则此原计划对应的班期没有复用 ~~';

"EDU_TRAINBUDGET "引用的表名如果中间夹杂空格也会报错

4. 表空间不足 或者 oracle错误959未发现该表空间则新建并命名该表空间

 

expdp

导出数据

创建directory

create or replace directory dmpdir as 'D:\sql-workspace\'

1)按用户导

expdp scott/tiger@orcl schemas=scott dumpfile=expdp.dmp DIRECTORY=dpdata1;

2)并行进程parallel

expdp scott/tiger@orcl directory=dpdata1 dumpfile=scott3.dmp parallel=40 job_name=scott3

3)按表名导

expdp scott/tiger@orcl TABLES=emp,dept dumpfile=expdp.dmp DIRECTORY=dpdata1;

4)按查询条件导

expdp scott/tiger@orcl directory=dpdata1 dumpfile=expdp.dmp Tables=emp query='WHERE deptno=20';
5)按表空间导

expdp system/manager DIRECTORY=dpdata1 DUMPFILE=tablespace.dmp TABLESPACES=temp,example;
6)导整个数据库

expdp system/manager DIRECTORY=dpdata1 DUMPFILE=full.dmp FULL=y;
五、还原数据

1)导到指定用户下

impdp scott/tiger DIRECTORY=dpdata1 DUMPFILE=expdp.dmp SCHEMAS=scott;
2)改变表的owner

impdp system/manager DIRECTORY=dpdata1 DUMPFILE=expdp.dmp TABLES=scott.dept REMAP_SCHEMA=scott:system;
3)导入表空间

impdp system/manager DIRECTORY=dpdata1 DUMPFILE=tablespace.dmp TABLESPACES=example;
4)导入数据库

impdb system/manager DIRECTORY=dump_dir DUMPFILE=full.dmp FULL=y;
5)追加数据
impdp system/manager DIRECTORY=dpdata1 DUMPFILE=expdp.dmp SCHEMAS=system TABLE_EXISTS_ACTION

posted @ 2017-10-16 19:12  音为  阅读(260)  评论(0编辑  收藏  举报