如何使用 AUTO_INCRMENT 列向数据库插入新行而不指定列名?

发布于 2024-09-14 08:54:37 字数 377 浏览 3 评论 0原文

我有一个包含以下列的表:

  • id - INT UNSIGNED AUTO_INCRMENT
  • name - VARCHAR(20)
  • group - VARCHAR(20)

我知道我可以添加这样的行:

INSERT INTO table_name (name, group) VALUES ('my name', 'my group')

我想知道是否有一种方法可以添加行而不指定列名,就像没有 AUTO_INCRMENT 列时一样?

I have a table with the following columns:

  • id - INT UNSIGNED AUTO_INCREMENT
  • name - VARCHAR(20)
  • group - VARCHAR(20)

I know that I can add a row like this:

INSERT INTO table_name (name, group) VALUES ('my name', 'my group')

I wonder if there is a way to add a row without specifying the column names, like when there is no AUTO_INCREMENT column ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以畏孤独 2024-09-21 08:54:39

更好的是,使用 DEFAULT 而不是 NULL。您想要存储默认值,而不是可能触发默认值的 NULL。

但你最好给所有列命名,用一条 SQL 就可以创建你需要的所有 INSERT、UPDATE 和 DELETE。只需检查 information_schema 并构建您需要的查询。无需全部手动完成,SQL 可以帮助您。

Even better, use DEFAULT instead of NULL. You want to store the default value, not a NULL that might trigger a default value.

But you'd better name all columns, with a piece of SQL you can create all the INSERT, UPDATE and DELETE's you need. Just check the information_schema and construct the queries you need. There is no need to do it all by hand, SQL can help you out.

淡墨 2024-09-21 08:54:39

只需添加列名,是的,您可以使用 Null 代替,但在任何插入中都不使用列名是一个非常糟糕的主意。

Just add the column names, yes you can use Null instead but is is a very bad idea to not use column names in any insert, ever.

输什么也不输骨气 2024-09-21 08:54:38

对于某些数据库,您可以直接将 NULL 显式插入到 auto_increment 列中:

INSERT INTO table_name VALUES (NULL, 'my name', 'my group')

For some databases, you can just explicitly insert a NULL into the auto_increment column:

INSERT INTO table_name VALUES (NULL, 'my name', 'my group')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文