gpt4 book ai didi

mysql - 对于这种情况,InnoDB 是必要的吗?

转载 作者:行者123 更新时间:2023-11-29 11:50:30 25 4
gpt4 key购买 nike

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

MySQL: Insert record if not exists in table

对于已接受的答案,我还有一个额外的问题。但是,我没有足够的声誉来评论该页面...

在答案中,Mike 创建了如下表格:

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 将失败。

关于mysql - 对于这种情况,InnoDB 是必要的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34158971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com