当名称与 table 名称相同时,内联索引定义失败

Inline index definition fails when name is the same as table name

SQL 服务器对象,即 tables 和索引有自己的命名空间。所以索引和table可以有相同的名字(但不是common/good的做法):

CREATE TABLE t(id INT PRIMARY KEY, col INT);
CREATE INDEX t ON t(col);

SELECT * FROM sys.tables WHERE name = 't';
SELECT * FROM sys.indexes WHERE name = 't';

db<>fiddle demo

不幸的是,我无法使用 inline index definition:

创建相同的结构
CREATE TABLE t(id INT PRIMARY KEY, col INT, INDEX t(col));

Msg 2714 Level 16 State 5 Line 1

There is already an object named 't' in the database.

-- below code is working correctly
CREATE TABLE t(id INT PRIMARY KEY, col INT, INDEX t1(col));

db<>fiddle demo 2

我是否遗漏了一些明显的东西或者是错误?

Do I miss something obvious or is it a bug?

看起来像一个错误。

CREATE TABLE t(id INT PRIMARY KEY, col INT, INDEX t(col));

产出

Microsoft SQL Server 2017 (RTM-GDR) (KB4293803) - 14.0.2002.14 (X64) 
    Jul 21 2018 07:47:45 
    Copyright (C) 2017 Microsoft Corporation
    Developer Edition (64-bit) on Windows 10 Enterprise 10.0 <X64> (Build 17763: ) (Hypervisor)


(1 row affected)

Msg 2714, Level 16, State 5, Line 4
There is already an object named 't' in the database.
Msg 1750, Level 16, State 1, Line 4
Could not create constraint or index. See previous errors.

请在此处添加反馈项:https://feedback.azure.com/forums/908035-sql-server 特别注意这是 SQL 2016 年的回归。