Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 创建索引_Sql Server - Fatal编程技术网

Sql server 创建索引

Sql server 创建索引,sql-server,Sql Server,我正在做索引,我对它还是有点陌生。我想在现有的Northwind视图上创建一个索引,名为“按类别列出的产品”,基于CategoryName和ProductName列。但我遇到了一个问题,按类别划分的产品不允许我这样做,因为其中有一个问题。名称中有一个空格,但是by被突出显示。是否有任何方法使其消失,以便我停止接收错误 CREATE INDEX IDX_categoryproducts ON dbo.Products by catergory (CategoryName,ProductName)

我正在做索引,我对它还是有点陌生。我想在现有的Northwind视图上创建一个索引,名为“按类别列出的产品”,基于CategoryName和ProductName列。但我遇到了一个问题,按类别划分的产品不允许我这样做,因为其中有一个问题。名称中有一个空格,但是by被突出显示。是否有任何方法使其消失,以便我停止接收错误

 CREATE INDEX IDX_categoryproducts
ON dbo.Products by catergory (CategoryName,ProductName)

编辑:刚刚注意到您正在使用
Northwind
。您需要更改视图定义以使其可索引,如下所示

ALTER VIEW [dbo].[Products by Category] 
WITH SCHEMABINDING
AS
SELECT Categories.CategoryID,
       Categories.CategoryName, 
       Products.ProductID,
       Products.ProductName, 
       Products.QuantityPerUnit, 
       Products.UnitsInStock, 
       Products.Discontinued
FROM dbo.Categories INNER JOIN dbo.Products ON Categories.CategoryID = Products.CategoryID
WHERE Products.Discontinued <> 1

GO

CREATE UNIQUE CLUSTERED INDEX ix ON [dbo].[Products by Category] (CategoryID, ProductID)

CREATE NONCLUSTERED INDEX IDX_categoryproducts ON dbo.[Products by Category] (CategoryName,ProductName)
alterview[dbo].[按类别划分的产品]
诡计多端
作为
选择Categories.CategoryID,
Categories.CategoryName,
Products.ProductID,
Products.ProductName,
Products.QuantityPerUnit,
产品.单位库存,
产品。停产
从dbo.Categories内部连接Categories.categoriyid=Products.categoriyid上的dbo.Products
凡产品。停产1
去
在[dbo].[Products by Category](CategoryID,ProductID)上创建唯一的聚集索引ix
在dbo上创建非聚集索引IDX_categoryproducts。[按类别划分的产品](CategoryName,ProductName)

编辑:刚刚注意到您使用的是
Northwind
。您需要更改视图定义以使其可索引,如下所示

ALTER VIEW [dbo].[Products by Category] 
WITH SCHEMABINDING
AS
SELECT Categories.CategoryID,
       Categories.CategoryName, 
       Products.ProductID,
       Products.ProductName, 
       Products.QuantityPerUnit, 
       Products.UnitsInStock, 
       Products.Discontinued
FROM dbo.Categories INNER JOIN dbo.Products ON Categories.CategoryID = Products.CategoryID
WHERE Products.Discontinued <> 1

GO

CREATE UNIQUE CLUSTERED INDEX ix ON [dbo].[Products by Category] (CategoryID, ProductID)

CREATE NONCLUSTERED INDEX IDX_categoryproducts ON dbo.[Products by Category] (CategoryName,ProductName)
alterview[dbo].[按类别划分的产品]
诡计多端
作为
选择Categories.CategoryID,
Categories.CategoryName,
Products.ProductID,
Products.ProductName,
Products.QuantityPerUnit,
产品.单位库存,
产品。停产
从dbo.Categories内部连接Categories.categoriyid=Products.categoriyid上的dbo.Products
凡产品。停产1
去
在[dbo].[Products by Category](CategoryID,ProductID)上创建唯一的聚集索引ix
在dbo上创建非聚集索引IDX_categoryproducts。[按类别划分的产品](CategoryName,ProductName)

谢谢,但它不起作用,因为我试图在视图上创建索引,而架构不允许这样做。如何更正模式,使其允许我创建索引。@sha-这是另一个问题。您需要确保视图满足要编制索引的要求。是使用SCHEMABINDING创建的
,并且您创建的第一个索引需要是唯一的聚集索引。创建视图[按类别列出的产品]使用SCHEMABINDING?谢谢Martin我正在处理SCHEMABINDING现在我明白了我没有放入的内容再次感谢您的帮助。谢谢,但这不起作用,因为我正在尝试在视图上创建索引,而模式不允许它这样做。如何更正模式,使其允许我创建索引。@sha-这是另一个问题。您需要确保视图满足要编制索引的要求。是使用SCHEMABINDING创建的
,并且您创建的第一个索引需要是唯一的聚集索引。使用SCHEMABINDING创建视图[Products by Category]吗?谢谢您,Martin,我正在使用SCHEMABINDING,现在我明白了我没有输入的内容,再次感谢您的帮助。by是以Products by Category的名义创建的。类别名称位于“按类别划分的产品”中。“by”位于“按类别划分的产品”名称中。类别名称位于“按类别划分的产品”中。