Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

INSERT と AUTO_INCREMENT

AUTO_INCREMENT な Column を含む Table に INSERT したいときの注意。
例えば、

CREATE TABLE animals (id INT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (id));

だったら、次はダメ。

INSERT INTO animals VALUES ('dog');          # ダメ
  --> ERROR 1136 (21S01): Column count doesn't match value count at row 1
INSERT INTO animals VALUES ('', 'dog');      # よい
INSERT INTO animals (name) VALUES ('dog');   # よい