如何在不指定所有列名称的情况下在表中插入除 id(我将其设置为 PK)之外的值?
最佳答案
如果你的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 into t (x)
select 2;
insert into t (x)
select 3;
关于MySQL,在除id之外的所有行中插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50839600/