开发者

When dropping a constraint will the supporting indexes also be dropped?

I am trying to memorize some sql syntax and I have gotten the ALTER TABLE ADD CONSTRAINT syntax down开发者_如何学运维. I believe I am correct when I say that when you use this syntax to add a FOREIGN KEY or PRIMARY KEY constraint, that sql server automatically creates indexes to support the constraint operations. (Is that true...or is it only true on the PK but not the FK?)

If so, when you use the ALTER TABLE DROP CONSTRAINT syntax...are the supporting indexes automatically dropped as well? Can these implicit supporting indexes be explicitly dropped? If so is the CONSTRAINT automatically removed?

I am just wanting to know how it works "under the covers". Googling has not helped. I imagine I could have queried some sys tables to discover the truth but thought I would try here instead.

Thanks for your help.

Seth


When you add a primary key, a unique index is in fact added. Whether that addition caused the new index to be clustered depends on whether you specified that it be non-clustered or not. If in adding a primary key constraint, you do not specify that it is clustered nor non-clustered, it will be clustered if a clustered constraint or index does not already exist on the table otherwise it will be non-clustered.

When you add a foreign key, no index is automatically created.

When you drop a constraint, any indexes created as a result of the constraint creation will be dropped. However, if you attempt to drop a unique or primary key constraint and there are foreign key constraints that reference it, you will get an error.

Indexes created as a result of constraint creation cannot be deleted using DROP INDEX.


A primary key constraint will add a clustered index on to the table, if one does not exist yet otherwise a unique non clustered index will be created for it.

Dropping a primary key constraint will also drop the underlying index.

A foreign key constraint will not add an index.

Dropping a foreign key constraint will do nothing to an index.

Foreign keys have nothing to do with indexes.


FKs don't automatically get an index in SQL Server, if you want one you need to add it! When dropping the FK, you don't drop an index, you'll need to drop the index on its own.


The index enforcing a UNIQUE constraint will be dropped, one index supporting a FK constraint will not be dropped automatically. It won't be created automatically either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜