【发布时间】:2016-10-24 18:20:03
【问题描述】:
所以我有一个按照以下方式布置的表格:
CREATE TABLE `dealerships` (
`dealership_id` BIGINT(20) UNSIGNED NOT NULL,
`zone` CHAR(200) NULL DEFAULT NULL,
`phone` BIGINT(20) UNSIGNED NOT NULL,
`fax` BIGINT(20) UNSIGNED NOT NULL,
`name` CHAR(200) NULL DEFAULT NULL,
`address1` CHAR(200) NULL DEFAULT NULL,
`address2` CHAR(200) NULL DEFAULT NULL',
`servicephone` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
`timezone` CHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (`dealership_id`)
)
COMMENT='This stores dealership information. '
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
现在当我插入时
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES( NULL, ~values);
这会返回一个错误,dealership_id 不能是 NULL 错误 1048。
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES(0, ~values);
这会返回一个错误,即 dealership_id 值 0 已经存在。
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES(default, ~values);
这会返回一个错误,dealership_id 没有默认值错误 1364。
当我简单地说去死,并省略dealership_id
INSERT INTO dealerships (~the rest~), VALUES(~values);
我收到必须为 dealership_id 指定值的错误。
每次我都与autoincrement primary keys 合作。通常插入NULL 可以让索引为我做一些自动神奇的事情。发生了什么事??
平台特定信息:
MariaDB10.1.18 x64
Windows Server 2012 R2
【问题讨论】:
-
您必须将列定义为自动递增。对于第一列,在
NOT NULL之后,您需要`AUTO_INCREMENT`。
标签: mysql mariadb auto-increment