DB:11.2.0.3.0

oracle 表迁移方法 (一)中,只是move了一张普通的表,如果表的字段带有主键约束呢 ?

[oracle@db01 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Mon Nov 3 18:40:16 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select name from v$datafile;

NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/system01.dbf
/u01/app/oracle/oradata/orcl/sysaux01.dbf
/u01/app/oracle/oradata/orcl/undotbs01.dbf
/u01/app/oracle/oradata/orcl/users01.dbf

创建dahao表空间
SQL> create tablespace dahao datafile '/u01/app/oracle/oradata/orcl/dahao01.dbf' size 100m;

Tablespace created.

创建yoon用户
SQL> create user yoon identified by yoon default tablespace dahao;

User created.

授权
SQL> grant dba to yoon;

Grant succeeded.

当前用户
SQL> show user
USER is "YOON"

创建测试表yoon
SQL> create table yoon as select * from scott.emp;

Table created.

查看当前表索引
SQL> select index_name from user_indexes;

no rows selected

创建empno主键约束
SQL> alter table yoon add constraint pk_empno primary key (empno);

Table altered.

SQL> conn / as sysdba
Connected.

SQL> select name from v$datafile;

NAME
------------------------------------------------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/system01.dbf
/u01/app/oracle/oradata/orcl/sysaux01.dbf
/u01/app/oracle/oradata/orcl/undotbs01.dbf
/u01/app/oracle/oradata/orcl/users01.dbf
/u01/app/oracle/oradata/orcl/dahao01.dbf

创建yoon表空间
SQL> create tablespace yoon datafile '/u01/app/oracle/oradata/orcl/yoon01.dbf' size 100m;

Tablespace created.

SQL> conn yoon/yoon; 
Connected.

创建索引
SQL> create index idx_deptno on yoon(deptno);

Index created.

设置yoon表为只读
SQL> alter table yoon.yoon read only;

Table altered.

迁移yoon表
SQL> alter table yoon.yoon move tablespace yoon;

Table altered.

修改用户默认表空间
SQL> alter user yoon default tablespace yoon;

User altered.

查看用户对应默认表空间
SQL> select username,default_tablespace from dba_users;

USERNAME                       DEFAULT_TABLESPACE
------------------------------ ------------------------------
SYS                            SYSTEM
SYSTEM                         SYSTEM
SCOTT                          USERS
GGS                            USERS
YOON                           YOON

查看索引状态
SQL> select INDEX_NAME,TABLE_OWNER,TABLE_NAME,STATUS from user_indexes where index_name='PK_EMPNO' ;

INDEX_NAME                     TABLE_OWNER                    TABLE_NAME                     STATUS
------------------------------ ------------------------------ ------------------------------ --------
PK_EMPNO                       YOON                           YOON                           UNUSABLE

SQL> select INDEX_NAME,TABLE_OWNER,TABLE_NAME,STATUS from user_indexes where index_name='IDX_DEPTNO' ;

INDEX_NAME                     TABLE_OWNER                    TABLE_NAME                     STATUS
------------------------------ ------------------------------ ------------------------------ --------
IDX_DEPTNO                     YOON                           YOON                           UNUSABLE

重建索引
SQL> alter index IDX_DEPTNO rebuild ;

Index altered.

SQL> select INDEX_NAME,TABLE_OWNER,TABLE_NAME,STATUS from user_indexes where index_name='PK_EMPNO' ;

INDEX_NAME                     TABLE_OWNER                    TABLE_NAME                     STATUS
------------------------------ ------------------------------ ------------------------------ --------
PK_EMPNO                       YOON                           YOON                           UNUSABLE

SQL> select INDEX_NAME,TABLE_OWNER,TABLE_NAME,STATUS from user_indexes where index_name='IDX_DEPTNO' ;

INDEX_NAME                     TABLE_OWNER                    TABLE_NAME                     STATUS
------------------------------ ------------------------------ ------------------------------ --------
IDX_DEPTNO                     YOON                           YOON                           VALID

设置yoon表为读写
SQL> alter table yoon read write;

Table altered.

插入数据
SQL> insert into yoon (empno) values (7934);
insert into yoon (empno) values (7934)
*
ERROR at line 1:
ORA-01502: index 'YOON.PK_EMPNO' or partition of such index is in unusable state

重建索引
SQL> alter index PK_EMPNO rebuild;

Index altered.

SQL> insert into yoon (empno) values (7934);
insert into yoon (empno) values (7934)
*
ERROR at line 1:
ORA-00001: unique constraint (YOON.PK_EMPNO) violated

经上述测试发现,通过move迁移表至另一个表空间,索引失效,主键约束不失效.

oracle 表迁移方法 (二) 约束不失效的更多相关文章

  1. oracle 表迁移方法 (一)

    在生产系统中,因业务需求,56张表中清空54张表数据,另外两张表数据保留,数据量大约10G左右:1.大部分人想法就是expdp/impdp,的确是这样,哈哈 2.rman 3.以下方法,move 虚拟 ...

  2. 头像文件上传 方法一:from表单 方法二:ajax

    方法一:from表单 html 设置form表单,内包含头像预览div,内包含上传文件input 设置iframe用来调用函数传参路径 <!--表单提交成功后不跳转处理页面,而是将处理数据返回给 ...

  3. Oracle表、列、约束的操作

    获得有关表的信息 可以直接DESCRIBE DESC[RIBE] table_name; 可以通过数据字典 SELECT * FROM user_tables WHERE table_name =xx ...

  4. Oracle表空间迁移Move Tablespace

    move一个表到另外一个表空间时,索引不会跟着一起move,而且会失效.(LOB类型例外) move分为: *普通表move *分区表move *LONG,LOB大字段类型move来进行测试和说明. ...

  5. Oracle表空间数据文件移动的方法

    最近遇到这样的一个问题,Oracle存放表空间文件的盘符 空间不够了,必须把部分表空间迁移出去, [转]http://www.jb51.net/article/77026.htm 实现把用户表空间中的 ...

  6. Oracle—表、约束、索引、表空间、分区、序列、统计信息

    表.约束.索引.表空间.分区.序列.统计信息 一.表及其操作 1.创建表 create table 表名 ( 字段名1 字段类型 默认值 是否为空 , 字段名2 字段类型 默认值 是否为空, 字段名3 ...

  7. oracle表空间表分区详解及oracle表分区查询使用方法(转+整理)

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  8. Oracle 11gR2 用exp无法导出空表解决方法

    Oracle 11gR2 用exp无法导出空表解决方法 在11gR2中有个新特性,当表无数据时,不分配segment以节省空间.Oracle 当然在运行export导出时,空表则无法导出,可是还是有解 ...

  9. ORACLE 表空间扩展方法

    ORACLE 表空间扩展方法 环境: linux系统 工具:PL/SQL DEVELOPER 第一步:查看表空间的名字及文件所在位置: select tablespace_name, file_id, ...

随机推荐

  1. 【LeetCode】15. 3Sum 三个数和为0

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  2. 如何去掉有背景图的a标签的边框

    有两种情况: 1.<a href="#" style="background:url(../images/download.png);"></ ...

  3. Java 获取*.properties配置文件中的内容 ,常见的两种方法

    import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...

  4. oracle:jdbcTest

    JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.l ...

  5. C++中rapidxml用法及例子

    rapidxml是一个快速的xml库,比tinyxml快了50-100倍.本文给出创建.读取.写入xml的源码. 由于新浪博客不支持文本文件上传,在使用下面代码需要先下载 rapidxml,关于这个库 ...

  6. jqmobi 转换语言

    当第一次打开APP时,检测手机默认的语言,设置APP的语言跟手机默认一样:当点击了APP里面的设置语言的按钮,存储当前设置的语言 :关闭APP:再一次打开APP时,检测存储在APP里面的语言,转换语言 ...

  7. Java 对字符反转操作。

    //把一段字符串反转后大小写互换位置 public class test_demo { public static void main(String[] args)throws Exception { ...

  8. JavaScript字符串转日期格式

    var dependedVal="2005-3-4"; //根据日期字符串转换成日期 var regEx = new RegExp("\\-","gi ...

  9. js动态改变图片热区坐标,手机端图片热区自适应

    <img id='banner1' src="images/banner.jpg" usemap="#banner" border="0&quo ...

  10. 运行ipython后显示WARNING: IPython History requires SQLite, your history will not be saved

    在CentOS6.5下将自带的python2.6升级到python2.7,并安装了ipython,启动ipython后显示如下信息: WARNING: IPython History requires ...