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. 图的最短路算法 Bellman-Ford

    BF求图的最短路径的时间复杂度是O(MN),这样的时间复杂度并不比迪杰斯特拉算法好,但是BF算法支持图中存在负权的情况,但图中不能存在负圈,因为如果存在负圈,最短路是不存在的,因此BF算法的另一个重要 ...

  2. 记录网上资源URL

    FQ(使用路由器): http://kennylee26.iteye.com/blog/1887753 http://www.iteye.com/search?type=all&query=s ...

  3. JavaScript之数组循环 forEach 循环输出数组元素

    var arrayAll = []; arrayAll.push(1); arrayAll.push(2); arrayAll[arrayAll.length] = 3; arrayAll[array ...

  4. MFC读取XML文件并解析

    现在经常会对XML文件进行操作,怎么在MFC下去读和解析XML文件呢?直接上代码: 首先得等在stdafx.h中加入这句,以引入MSXML命名空间 #import <msxml3.dll> ...

  5. Mybatis SQL语句查询

    MyBatis中使用in查询时的注意事项 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach一共有三种类型,分别为List,[](array),Map三种. ...

  6. Android IOS WebRTC 音视频开发总结(二十)-- 自由职业

    咋看标题感觉与WebRTC和音视频无关,其实有着很大的关联,文章来自博客园RTC.Blacker,转载请说明出处. 背景: 一方面因为对开发人员比较了解,不喜欢约束,喜欢自由自在,所以我们向往自由职业 ...

  7. C# 和SQL Server 类型转换

    /// <summary> /// 数据库中与C#中的数据类型对照 /// </summary> /// <param name="type"> ...

  8. Muzei Live Wallpaper壁纸应用安卓源码项目

    这个刚刚在安卓教程网那里看到的,这个是Muzei Live Wallpaper壁纸应用源码,这是已经在Playstore上线了的android壁纸应用,点击壁纸界面可以磨砂透明效果以及一般背景效果之间 ...

  9. JDBC数据库连接(MySQL为例)

    1.什么是JDBC?有什么作用? Java Data Base Connectivity  Java数据库连接协议 是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问. 他提 ...

  10. VS2008安装SP1补丁后智能提示从中文变为英文的解决办法

    如果你安装了中文的VS2008,打了SP1补丁之后出现问题,那是微软的Bug,请下载此补丁修正: VS90SP1-KB957507-CHS-x86.exe 点击下载