Are foreign keys indexed automatically in SQL Server?(外键是否在 SQL Server 中自动编入索引?)
问题描述
以下 SQL 语句会自动在 Table1.Table1Column 上创建索引,还是必须显式创建?
Would the following SQL statement automatically create an index on Table1.Table1Column, or must one be explicitly created?
数据库引擎为 SQL Server 2000
Database engine is SQL Server 2000
CREATE TABLE [Table1] (
. . .
CONSTRAINT [FK_Table1_Table2] FOREIGN KEY
(
[Table1Column]
) REFERENCES [Table2] (
[Table2ID]
)
)
推荐答案
SQL Server 不会自动在外键上创建索引.同样来自 MSDN:
SQL Server will not automatically create an index on a foreign key. Also from MSDN:
外键约束没有仅链接到 PRIMARY KEY另一个表中的约束;它可以也被定义为引用UNIQUE 约束的列另一张桌子.外键约束可以包含空值;但是,如果组合的任何列FOREIGN KEY 约束包含 null值,验证所有值组成外键约束被跳过.确保;确定复合 FOREIGN 的所有值KEY 约束被验证,指定所有参与的非空列.
A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table. A FOREIGN KEY constraint can contain null values; however, if any column of a composite FOREIGN KEY constraint contains null values, verification of all values that make up the FOREIGN KEY constraint is skipped. To make sure that all values of a composite FOREIGN KEY constraint are verified, specify NOT NULL on all the participating columns.
这篇关于外键是否在 SQL Server 中自动编入索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:外键是否在 SQL Server 中自动编入索引?


基础教程推荐
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01