MySQL,在除id之外的所有行中插入值

标签 mysql sql

如何在不指定所有列名称的情况下在表中插入除 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/

相关文章:

android - 如何将存储在数据库中的图像作为字符串显示为真实图像?

MYSQL 垂直排序使用不同的列?

php - 使用 PDO/PHP 注册不起作用

sql - 尽量避免加入大表

SQL OVER 子句替换或 Visual Studio 设置更改

sql - 识别sql中不为空值的行

sql - Postgres : sql query by filter date range

php - 选择 ... 其中 id = 任何值。可能吗?

java - Spring JPA 多对多持久性问题

c++ - 使用RDBMS来减少内存消耗?