行已成功插入,但在表中未显示

人工智能 2026-07-10

我添加了这一行,MySQL显示为

1行受影响

但是即使在插入前后分别统计了行数,结果仍然相同。我认为这意味着该行没有被插入。那为什么MySQL没有显示任何错误或警告信息。

insert into retail.customers_prac values("CO1001","Female",32,"London","2026-03-03","YES");

这是我的表结构:

CREATE TABLE `customers_prac` (
  `customer_id` text,
  `gender` text,
  `age` int DEFAULT NULL,
  `city` text,
  `signup_date` text,
  `loyalty_member` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

解决方案

The issue is autocommit being OFF.

问题在于autocommit被设为OFF。

Your table uses InnoDB, which is transactional. If autocommit = 0 in your session, the INSERT runs inside an implicit transaction. MySQL correctly reports "1 row(s) affected" because the row was inserted into the transaction — but it's not committed yet.

你的表使用InnoDB,具备事务性。如果你当前会话中的autocommit=0,INSERT将在一个隐式事务中执行。MySQL会正确显示“1 row(s) affected”,因为该行已经被插入到事务中——但还没有提交。

If you checked the count from a different session/connection (or restarted your client), the uncommitted row wouldn't be visible due to transaction isolation. And if your session ended without a COMMIT, the row was rolled back.

如果你在不同的会话/连接中检查计数(或重新启动客户端),由于事务隔离,未提交的行将不可见。而且如果你的会话在没有执行COMMIT的情况下结束,该行会被回滚。

Fix — either:

修复方法如下:

  1. Explicitly commit after inserting:

  2. 在插入后显式提交:

  INSERT INTO retail.customers_prac VALUES("CO1001","Female",32,"London","2026-03-03","YES");
  COMMIT;
  1. Or enable autocommit:

  2. 或者启用autocommit:

  SET autocommit = 1;

To verify this is the cause:

要验证这是原因:

  SELECT @@autocommit;

If it returns 0, that's your problem.

如果返回0,那就是你的问题。

备选方案

If I test executing your insert twice, then it succeeds:

如果我将你的 insert 执行两次,就能成功:

insert into customers_prac values("CO1001","Female",32,"London","2026-03-03","YES");
insert into customers_prac values("CO1001","Female",32,"London","2026-03-03","YES");
select * from customers_prac;

and it selects the two records successfully, see https://www.db-fiddle.com/f/hzDZt6WS7RjmmqSxYBjcSL/0

并能成功选择这两条记录,见 https://www.db-fiddle.com/f/hzDZt6WS7RjmmqSxYBjcSL/0

Therefore your insert is expected to insert the records. However, you do it at retail.customers_prac, which is your retail database. You may be on another database, try running

因此你的 insert 预计会 insert 这些记录。然而,你是在 retail.customers_prac 位置执行的,这对应的是你的 retail 数据库。你可能在另一个数据库上,试着运行

select database();

if it's different from retail, you may be inserting into a table in another database and counting the records in your current one.

如果它和 retail 不同,你可能是在另一个数据库中向某个表插入数据,并在当前数据库统计记录数。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章