简体   繁体   English

SQL Server中的MySQL INDEX()语法等效

[英]MySQL INDEX() syntax equivalent in SQL Server

I am following a PHP workbook and one of the exercises asks me to create a table with columns using the following MySQL code 我正在阅读PHP工作簿,其中一项练习要求我使用以下MySQL代码创建带有列的表

CREATE TABLE messages ( 
    message_id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
    parent_id INT UNSIGNED NOT NULL DEFAULT 0, 
    forum_id TINYINT UNSIGNED NOT NULL, 
    user_id MEDIUMINT UNSIGNED NOT NULL, 
    subject VARCHAR(100) NOT NULL,
    body LONGTEXT NOT NULL, 
    date_entered DATETIME NOT NULL, 
    PRIMARY KEY (message_id), 
    INDEX (parent_id), 
    INDEX (forum_id),
    INDEX (user_id),
    INDEX (date_entered) 
);

The problem is at the place I work they use Microsoft SQL Server so the syntax is different. 问题出在我工作的地方,他们使用Microsoft SQL Server,因此语法不同。

What is the equivalent SQL Server syntax I can use for 我可以用于的等效SQL Server语法是什么

INDEX (parent_id), 
INDEX (forum_id),
INDEX (user_id),
INDEX (date_entered)

What is the equivalent SQL Server syntax I can use for 我可以用于的等效SQL Server语法是什么

 INDEX (parent_id), 
 INDEX (forum_id),
 INDEX (user_id),
 INDEX (date_entered)

Looking in the manual i notice the BNF form. 手册中,我注意到BNF表格。

<column_index> ::=   
 INDEX index_name [ CLUSTERED | NONCLUSTERED ]  
    [ WITH ( <index_option> [ ,... n ] ) ]  
    [ ON { partition_scheme_name (column_name )   
         | filegroup_name  
         | default   
         }  
    ]   
    [ FILESTREAM_ON { filestream_filegroup_name | partition_scheme_name | "NULL" } ]  

So SQL Server 2008+ should also support INDEX keyword in the CREATE TABLE statement.. 因此,SQL Server 2008+还应该在CREATE TABLE语句中支持INDEX关键字。

But the valid syntax for the INDEX keyword is using 但是INDEX关键字的有效语法正在使用

CREATE TABLE test ( 
   id INT
 , INDEX index_name (id)
);

But there are other things wrong like datatypes or keywords.. 但是还有其他错误,例如数据类型或关键字。

The correct SQL code for SQL server is SQL Server的正确SQL代码是

CREATE TABLE messages ( 
 message_id INT identity(1, 1), 
 parent_id INT NOT NULL DEFAULT 0, 
 forum_id TINYINT NOT NULL, 
 user_id INT NOT NULL, 
 subject VARCHAR(100) NOT NULL,
 body TEXT NOT NULL, 
 date_entered DATETIME NOT NULL, 
 PRIMARY KEY (message_id), 
 INDEX parent_id (parent_id), 
 INDEX forum_id (forum_id),
 INDEX user_id (user_id),
 INDEX date_entered (date_entered) 
);

You can use syntax that should work in any SQL engine, eg 您可以使用在任何SQL引擎中都可以使用的语法,例如

CREATE TABLE messages ( 
    message_id INT, 
    parent_id INT NOT NULL DEFAULT 0, 
    forum_id TINYINT NOT NULL, 
    user_id INT NOT NULL, 
    subject VARCHAR(100) NOT NULL,
    body TEXT NOT NULL, 
    date_entered DATETIME NOT NULL, 
    PRIMARY KEY (message_id)
)

Then create other indexes individually: 然后分别创建其他索引:

CREATE INDEX parent_id_idx ON messages (parent_id))

And so on. 等等。

This syntax should be supported by all SQL engines. 所有SQL引擎都应支持此语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM