我发现当我尝试进行mysqli查询时,除非我以某种方式对参数进行排序,否则它将失败。
//Example that causes error (focus on order of AUTO_INCREMENT and UNSIGNED)
<?php
$con = new mysqli("localhost", "user", "pass", "db");
$query = "CREATE TABLE colors (
id INT(2) AUTO_INCREMENT UNSIGNED PRIMARY KEY,
color1 varchar(20) NOT NULL,
color2 varchar(20) NOT NULL
)";
$con->query($query);
?>
但是,当AUTO_INCREMENT
和UNSIGNED
切换位置时,查询成功。当PRIMARY KEY
参数不是列出的最后一个参数时,我似乎也发生了同样的事情。订单是否重要?
答案 0 :(得分:1)
是的,请查看MySQL文档中的CREATE TABLE
语法:
http://dev.mysql.com/doc/refman/5.7/en/create-table.html
具体为column_definition的这一部分:
column_definition:
data_type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT 'string']
[COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
[STORAGE {DISK|MEMORY|DEFAULT}]
[reference_definition]
| data_type [GENERATED ALWAYS] AS (expression)
[VIRTUAL | STORED] [UNIQUE [KEY]] [COMMENT comment]
[NOT NULL | NULL] [[PRIMARY] KEY]
格式和顺序很重要。该文档可以帮助您确定参数的正确顺序。