ITPub博客

首页 > 数据库 > Oracle > oracle11g alter table set unused column指定表某列不可用之系列一

oracle11g alter table set unused column指定表某列不可用之系列一

Oracle 作者:bitifi 时间:2015-11-04 11:21:20 0 删除 编辑
<h2> 结论 </h2> 1,alter table set unused column指定表的某列为不可用<br /> 2,alter table set unused column生效后不可用列已经不再属于表,但空间不会释放,需要重组方可释放<br /> 3,alter table set unused column的信息可用user_unused_col_tabs查询<br /> 4,alter table set unused column适用于高并发OLTP环境,在业务繁忙时,先运行此语句,待业务压力小时,然后用alter table drop unused columns真正删除不可用的列;<br /> &nbsp; alter table drop column删除列消耗的时间明显要高于alter table set unused column<br /> 5,alter table set unused column指定某个LONG列不可用,不能添加其它LONG列<br /> 6,同一个表中不能同时存在2个LONG,其它LOB列不受其限制<br /> 7,通过alter table set unsed(表中列的列表,以逗号分割),可以同时指定多个列不可用<br /> 8, alter table set unused column cascade constraints配置某列不可用会递归删除定义的约束,发现指不指定cascade constraints选项都会递归删除其约束<br /> 9, 通过alter table set unsed(表中列的列表,以逗号分割),可以同时指定多个列不可用<br /> <br /> <br /> <h2> 测试 </h2> 1,数据库版本<br /> SQL&gt; select * from v$version where rownum=1;<br /> <br /> <br /> BANNER<br /> --------------------------------------------------------------------------------<br /> Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production<br /> <br /> <br /> 2,获取暂时不使用的列的表信息<br /> SQL&gt; select count(*) from user_unused_col_tabs;<br /> <br /> <br /> &nbsp; COUNT(*)<br /> ----------<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0<br /> <br /> <br /> 3,创建某个测试表<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int,b int,c int);<br /> <br /> <br /> Table created. &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br /> <br /> <br /> <br /> <br /> 4,配置指定列为不可用<br /> <br /> <br /> 查阅官方手册,配置表的列为不可用,其语法请参考drop_column_clause 节<br /> SQL&gt; alter table t_unused_col set unused column b;&nbsp;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column c;&nbsp;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select count(*) from user_unused_col_tabs;<br /> <br /> <br /> &nbsp; COUNT(*)<br /> ----------<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br /> 5,配置指定不可用后不再显示其列<br /> SQL&gt; desc user_unused_col_tabs;<br /> &nbsp;Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Null? &nbsp; &nbsp;Type<br /> &nbsp;----------------------------------------- -------- ----------------------------<br /> &nbsp;TABLE_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NOT NULL VARCHAR2(30)<br /> &nbsp;COUNT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NUMBER<br /> <br /> <br /> 6,可以在如下字典查询到不可用列的相关信息<br /> SQL&gt; select table_name,count from user_unused_col_tabs;<br /> <br /> <br /> TABLE_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;COUNT<br /> ------------------------------------------------------------ ----------<br /> T_UNUSED_COL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2<br /> <br /> <br /> SQL&gt; insert into t_unused_col values(1,1,1);<br /> insert into t_unused_col values(1,1,1)<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br /> ERROR at line 1:<br /> ORA-00913: too many values<br /> <br /> <br /> <br /> <br /> SQL&gt; insert into t_unused_col values(1);<br /> <br /> <br /> 1 row created.<br /> <br /> <br /> SQL&gt; commit;<br /> <br /> <br /> Commit complete.<br /> <br /> <br /> SQL&gt; select * from t_unused_col;<br /> <br /> <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A<br /> ----------<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br /> <br /> <br /> 7,删除测试表中不可用的列<br /> SQL&gt; alter table t_unused_col drop unused columns;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> 8,重新添加B与C列<br /> SQL&gt; alter table t_unused_col add b int;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add c int;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> 9,配置某列为不可用后,可以添加同名的列<br /> SQL&gt; alter table t_unused_col set unused column b;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add b int;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> 10,配置某列为不可用,其列占用的空间不会释放<br /> SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int,b int,c int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> SQL&gt; insert into t_unused_col select level,level,level from dual connect by level&lt;=100000;<br /> <br /> <br /> 100000 rows created.<br /> <br /> <br /> SQL&gt; commit;<br /> <br /> <br /> Commit complete.<br /> <br /> <br /> SQL&gt; select segment_name,bytes/1024/1024 from user_segments where segment_name='T_UNUSED_COL';<br /> <br /> <br /> SEGMENT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BYTES/1024/1024<br /> ------------------------------ ---------------<br /> T_UNUSED_COL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column b;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select segment_name,bytes/1024/1024 from user_segments where segment_name='T_UNUSED_COL';<br /> <br /> <br /> SEGMENT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BYTES/1024/1024<br /> ------------------------------ ---------------<br /> T_UNUSED_COL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3<br /> <br /> <br /> 11,删除表中不可用的列后,其空间仍不会释放<br /> SQL&gt; alter table t_unused_col drop unused columns;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select segment_name,bytes/1024/1024 from user_segments where segment_name='T_UNUSED_COL';<br /> <br /> <br /> SEGMENT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BYTES/1024/1024<br /> ------------------------------ ---------------<br /> T_UNUSED_COL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3<br /> <br /> <br /> 12,只有重组表后,才会释放删除不可用列的空间<br /> SQL&gt; alter table t_unused_col move;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select segment_name,bytes/1024/1024 from user_segments where segment_name='T_UNUSED_COL';<br /> <br /> <br /> SEGMENT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BYTES/1024/1024<br /> ------------------------------ ---------------<br /> T_UNUSED_COL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2<br /> <br /> <br /> 13,配置某个LONG类型的列为不可用后,必须删除其列后方可添加新的LONG列<br /> SQL&gt; alter table t_unused_col add b clob;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column b;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add x long;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column x;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add y long;<br /> alter table t_unused_col add y long<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br /> ERROR at line 1:<br /> ORA-01754: a table may<br /> contain only one column of<br /> type LONG<br /> <br /> <br /> <br /> <br /> SQL&gt; alter table t_unused_col drop unused columns;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add y long;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int,b long,c long);<br /> create table t_unused_col(a int,b long,c long)<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br /> ERROR at line 1:<br /> ORA-01754: a table may<br /> contain only one column of<br /> type LONG<br /> <br /> <br /> 14,可见alter table drop columns消耗的时间明显要大于alter table set unused columns,所以后者适用于高并发的OLTP环境,减少锁持有情况发生;<br /> 到业务压力不大时,可以采用alter table drop unused columns<br /> SQL&gt; create table t_unused_col(a int,b int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> SQL&gt; insert into t_unused_col select level,level from dual connect by level&lt;=1000000;<br /> <br /> <br /> 1000000 rows created.<br /> <br /> <br /> SQL&gt; commit;<br /> <br /> <br /> Commit complete.<br /> <br /> <br /> <br /> <br /> SQL&gt; set timing on time on<br /> 06:34:03 SQL&gt; alter table t_unused_col drop column b;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> Elapsed: 00:03:06.30<br /> <br /> <br /> <br /> <br /> 06:37:42 SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> Elapsed: 00:00:03.94<br /> <br /> <br /> 06:37:56 SQL&gt; create table t_unused_col(a int,b int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> Elapsed: 00:00:01.98<br /> <br /> <br /> 06:38:12 SQL&gt; insert into t_unused_col select level,level from dual connect by level&lt;=1000000;<br /> <br /> <br /> <br /> <br /> 1000000 rows created.<br /> <br /> <br /> Elapsed: 00:00:15.59<br /> 06:40:58 SQL&gt; 06:40:58 SQL&gt; commit;<br /> <br /> <br /> 06:41:11 SQL&gt; alter table t_unused_col set unused column b;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> Elapsed: 00:00:00.77<br /> <br /> <br /> 06:41:32 SQL&gt; alter table t_unused_col drop unused columns;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> Elapsed: 00:02:22.51<br /> 06:44:13 SQL&gt; commit;<br /> <br /> <br /> Commit complete.<br /> <br /> <br /> Elapsed: 00:00:00.00<br /> <br /> <br /> <br /> <br /> 15,通过alter table set unsed(表中列的列表,以逗号分割),可以同时指定多个列不可用<br /> SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int,b int,c int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused (b,c);<br /> <br /> <br /> Table altered.<br /> <br /> <br /> 16,alter table set unused column cascade constraints配置某列不可用会递归删除定义的约束,发现指不指定cascade constraints选项都会递归删除其约束<br /> SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int,b int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add constraint chk_b check(b&gt;1);<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select constraint_name,constraint_type,status from user_constraints where table_name='T_UNUSED_COL';<br /> <br /> <br /> CONSTRAINT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CO STATUS<br /> ------------------------------ -- ----------------<br /> CHK_B &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C &nbsp;ENABLED<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column b cascade constraints;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select constraint_name,constraint_type,status from user_constraints where table_name='T_UNUSED_COL';<br /> <br /> <br /> no rows selected<br /> <br /> <br /> SQL&gt; drop table T_UNUSED_COL purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int primary key,b int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> SQL&gt; select constraint_name,constraint_type,status from user_constraints where table_name='T_UNUSED_COL';<br /> <br /> <br /> CONSTRAINT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CO STATUS<br /> ------------------------------ -- ----------------<br /> SYS_C0011644 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; P &nbsp;ENABLED<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column a;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select constraint_name,constraint_type,status from user_constraints where table_name='T_UNUSED_COL';<br /> <br /> <br /> no rows selected<br /> <br /> <br /> <b style="color:#666666;font-family:宋体, Arial;line-height:26px;white-space:normal;"> <h2 style="margin:0px;padding:0px;"> <span style="text-indent:2em;">个人简介</span> </h2> <b style="text-indent:24px;"><br /> <div style="text-indent:2em;"> 8年oracle从业经验,具备丰富的oracle技能,目前在国内北京某专业oracle服务公司从事高级技术顾问。 </div> <div style="text-indent:2em;"> </div> <div style="text-indent:2em;"> 服务过的客户: </div> <div style="text-indent:2em;"> 中国电信 </div> <div style="text-indent:2em;"> 中国移动 </div> <div style="text-indent:2em;"> 中国联通 </div> <div style="text-indent:2em;"> 中国电通 </div> <div style="text-indent:2em;"> 国家电网 </div> <div style="text-indent:2em;"> 四川达州商业银行 </div> <div style="text-indent:2em;"> 湖南老百姓大药房 </div> <div style="text-indent:2em;"> 山西省公安厅 </div> <div style="text-indent:2em;"> 中国邮政 </div> <div style="text-indent:2em;"> 北京302医院 &nbsp; &nbsp;&nbsp; </div> <div style="text-indent:2em;"> 河北廊坊新奥集团公司 </div> <div style="text-indent:2em;"> <br /> &nbsp;项目经验: </div> <div style="text-indent:2em;"> 中国电信3G项目AAA系统数据库部署及优化<br /> &nbsp; &nbsp; &nbsp; 中国联通4G数据库性能分析与优化 </div> <div style="text-indent:2em;"> 中国联通CRM数据库性能优化 </div> <div style="text-indent:2em;"> 中国移动10086电商平台数据库部署及优化 </div> <div style="text-indent:2em;"> 湖南老百姓大药房ERR数据库sql优化项目 </div> <div style="text-indent:2em;"> 四川达州商业银行TCBS核心业务系统数据库模型设计和RAC部署及优化 </div> <div style="text-indent:2em;"> 四川达州商业银行TCBS核心业务系统后端批处理存储过程功能模块编写及优化 </div> <div style="text-indent:2em;"> 北京高铁信号监控系统RAC数据库部署及优化 </div> <div style="text-indent:2em;"> 河南宇通客车数据库性能优化 </div> <div style="text-indent:2em;"> 中国电信电商平台核心采购模块表模型设计及优化 </div> <div style="text-indent:2em;"> 中国邮政储蓄系统数据库性能优化及sql优化 </div> <div style="text-indent:2em;"> 北京302医院数据库迁移实施 </div> <div style="text-indent:2em;"> 河北廊坊新奥data guard部署及优化 </div> <div style="text-indent:2em;"> 山西公安厅身份证审计数据库系统故障评估 </div> <div style="text-indent:2em;"> 国家电网上海灾备项目4 node&nbsp;rac+adg&nbsp;<br /> &nbsp; &nbsp; &nbsp; &nbsp;贵州移动crm及客服数据库性能优化项目<br /> &nbsp; &nbsp; &nbsp; &nbsp;贵州移动crm及客服务数据库sql审核项目<br /> &nbsp; &nbsp; &nbsp; &nbsp;深圳穆迪软件有限公司数据库性能优化项目 </div> <div style="text-indent:2em;"> <br /> 联系方式: </div> <div style="text-indent:2em;"> 手机:18201115468 </div> <div style="text-indent:2em;"> qq &nbsp; : &nbsp; 305076427 </div> <div style="text-indent:2em;"> qq微博: wisdomone1 </div> <div style="text-indent:2em;"> 新浪微博:wisdomone9 </div> <div style="text-indent:2em;"> qq群:275813900 &nbsp; &nbsp; </div> <div style="text-indent:2em;"> itpub博客名称:wisdomone1 &nbsp; &nbsp;<a href="http://blog.itpub.net/9240380/" style="text-decoration:none;color:#565656;">http://blog.itpub.net/9240380/</a> </div> </b></b><br /> <br /> <br /> <br />

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30496894/viewspace-1822203/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2015-09-21

  • 博文量
    211
  • 访问量
    329861