这种情况需要 InnoDB 吗?

Is InnoDB neccessary for this case?

我一直在寻找以下问题的答案:

MySQL: Insert record if not exists in table

我对已接受的答案有一个额外的问题。但是,我没有足够的声誉在页面上发表评论...

在答案中,Mike 像这样创建 table:

CREATE TABLE `table_listnames` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `tele` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB;

根据下面发出的查询,InnoDB 是必须的吗? (也引用自答案)

INSERT INTO table_listnames (name, address, tele)
SELECT * FROM (SELECT 'John', 'Doe', '022') AS tmp
WHERE NOT EXISTS (
    SELECT name FROM table_listnames WHERE name = 'John'
) LIMIT 1;

谢谢。

不,没有必要。

但我更喜欢这样写你的查询:

INSERT INTO table_listnames (name, address, tele)
SELECT 'John', 'Doe', '022' FROM dual
WHERE NOT EXISTS (
    SELECT name FROM table_listnames WHERE name = 'John'
);

或者更好,设置一个唯一约束:

ALTER TABLE table_listnames ADD UNIQUE (name)

然后插入:

INSERT INTO table_listnames (name, address, tele)
VALUES ('John', 'Doe', '022')

如果名为 John 的行已经存在,INSERT 将简单地失败。