暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle alter table drop约束drop index语法有效吗?

askTom 2017-11-13
914

问题描述

嗨,汤姆,

我正在从表中删除唯一约束,也需要删除唯一索引。是的,有很多关于这个主题的文章,对我来说不清楚的似乎只是文档问题:

我找到了使用语法的建议
alter table ... drop constraint ... drop index

虽然铁路图中似乎不支持这种语法-https://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_3001.htm#i2103997(保持索引和下降索引仅在下降主键... 和下降唯一... 分支中,而不在下降约束... 分支中)。

我正在俯瞰的铁路图有什么扩展吗?我在查错误的文件吗?还是某些未记录的功能?不久,我可以在生产中依靠它吗?

AskTom语法出现的示例:https://asktom.oracle.com/pls/apex/asktom.search?tag=unique-index-200503#followup-6922829900346922650(包括链接https://jonathanlewis.wordpress.com/2012/04/19/drop-constraint/网站) 或https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:1232036500184#followup-426367200346607590(使用保持索引,但从句法角度来看是相同的)

谢谢你的回答!

专家解答

嗯,命令是这样运行的must语法上有效! ;)

严肃地说,我认为这是一个文档错误。与您链接的文章一样,“drop constraint ... drop index...” 也出现在MOS中 (请参见注释370633.1)。

有趣的是,您可以在非主/唯一约束条件上使用此子句。尽管它什么也不做:

create table foo (id varchar(26) not null, name varchar(50) not null);

alter table foo add constraint pk_foo primary key (id);

alter table foo add constraint ck_foo check (id > 0);

select index_name from user_indexes
where  table_name = 'FOO';

INDEX_NAME   
PK_FOO   

alter table foo drop constraint ck_foo drop index;

select index_name from user_indexes
where  table_name = 'FOO';

INDEX_NAME   
PK_FOO 

alter table foo drop constraint pk_foo drop index;

select index_name from user_indexes
where  table_name = 'FOO';

no rows selected


但是,如果您担心这在 “一天” 的生产中无法正常工作,我会选择 “drop primary | unique”。这也允许您在不知道约束名称的情况下删除它:

alter table foo add constraint pk_foo primary key (id);

select index_name from user_indexes
where  table_name = 'FOO';

INDEX_NAME   
PK_FOO   

alter table foo drop primary key drop index;

select index_name from user_indexes
where  table_name = 'FOO';

no rows selected

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论