1. 复制表结构及其数据:
create table table_name_new as select * from table_name_old
2. 只复制表结构:
create table table_name_new as select * from table_name_old where 1=2;
3. 只复制表数据:
如果两个表结构一样:
insert into table_name_new select * from table_name_old
如果两个表结构不一样:
insert into table_name_new(column1,column2...) select column1,column2... from table_name_old
创建临时表空间、表空间、用户及赋权限
创建临时表空间
create temporary tablespace infosys_temp tempfile 'd:\oracle\product\10.2.0\oradata\infosys_temp.dbf' size 50m autoextend on next 50m maxsize 2048m extent management local;
创建表空间
create tablespace infosys_data logging datafile 'd:\oracle\product\10.2.0\oradata\infosys_data.dbf' size 50m autoextend on next 50m maxsize 2048m extent management local;
创建用户并指定表空间
create user infosys identified byinfosys default tablespace infosys_data temporary tablespace infosys_temp;
给用户授予dba权限
grant dba to infosys;
导入导出
导出:exp username/password@orcl file=d://daochufile.dmp full=y ;//orcl为oracle用户实例名,将用户username中的数据全部导出
owner=(username1,username2) ,导出指定用户的表
tables=(t1,t2),指定表明
tables=(t1) query=\" where field=='condition'\"; //查询某个表的特定数据, 在window上经测试 这个报错,(参数'query' 不允许有多个值),未解决
导入:imp username/password@orcl file=d://daorufile.dmp full=y;
注意:当导入导出的表中存在BLOB、CLOB字段类型时,假如导入和导出的数据库表空间名称不同时会报错:找不到表空间.
oracle 自动备份:
linux:
#!/bin/sh
export ORACLE_BASE=/home/oracle
export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1
export ORACLE_SID=orcl
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
export DATA_DIR=/home/bakup/data
export LOGS_DIR=/home/bakup/logs
export DELTIME=`date -d "7 days ago" +%Y%m%d`
export BAKUPTIME=`date +%Y%m%d%H%M%S`
mkdir -p $DATA_DIR
mkdir -p $LOGS_DIR
echo "Starting bakup..."
echo "Bakup file path $DATA_DIR/$BAKUPTIME.dmp"
exp shop/lyisABC0987@orcl file=$DATA_DIR/$BAKUPTIME.dmp log=$LOGS_DIR/$BAKUPTIME.log
echo "Delete the file bakup before 7 days..."
rm -rf $DATA_DIR/$DELTIME*.dmp
rm -rf $LOGS_DIR/$DELTIME*.log
echo "Delete the file bakup successfully. "
echo "Bakup completed."
添加到任务调度
crontab -u oracle -e
* 3 * * * /home/bakup/bakup.sh
即每天凌晨3点进行备份
如需每天备份多次,可设置不同时间段备份:
例如:* 3,13,18 * * * /home/bakup/bakup.sh,即每天3点、13点、18点进行备份。
说明:文件备份目录,用户oracle必须有更改权限,否则无法备份。
window:
使用vbs脚本
Do
if Hour(Now)=0 then
Set BatRunner = createobject("wscript.shell")
BatRunner.run "cmd /c exp username/paswd@orcl file=D://filename"& Date &".dmp owner=(username) "
wscript.sleep 24*3600*1000
End if每天 凌晨进行备份