【问题标题】:MySQL, insert values in all rows except idMySQL,在除 id 之外的所有行中插入值
【发布时间】:2018-06-13 14:21:17
【问题描述】:

如何在不指定所有列名的情况下在表中插入除 id(我已设置为 PK)之外的值?

【问题讨论】:

标签: mysql sql


【解决方案1】:

如果您的id 是自动递增的,那么如果您插入NULL,MySQL 仍然会自动递增。因此,您可以执行以下操作:

create table t (
    id int auto_increment primary key,
    x int
);

insert into t
    select null, 2;

insert into t
    select null, 3;

也就是说,我建议(几乎)始终将所有列包含在 insert 中。所以我强烈推荐:

insert into t (x)
    select 2;

insert into t (x)
    select 3;

【讨论】:

  • 感谢@Gordon Linoff。只是出于好奇,您为什么建议“始终将所有列包含在 INSERT 中”?
猜你喜欢
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
相关资源
最近更新 更多