带主键和多插入速记身份

发布于 2024-11-19 07:53:06 字数 1314 浏览 2 评论 0原文

我想要一个简写(如果可能的话),用于将多个记录插入到具有主键和 IsIdentity 属性的表中。例如,假设我有一个名为“People”的表,其中包含以下列:
- ID(主键和身份[即自动增量])
- 姓名(不为空)
- Email (not null)

不包含自动递增 ID 列的插入语句是完全有效的,例如:

INSERT INTO People VALUES ('George', '[email protected]')

但是,如果我想在同一个语句中插入多个值,理想情况可以这样做,我不必显式指定列名称

INSERT INTO People VALUES (
   (auto, 'George', '[email protected]'),
   (auto, 'Mary', '[email protected]')
)

我能找到的最好的解决方案是这样的:

INSERT INTO People (
   SELECT 'George', '[email protected]',
   UNION ALL
   SELECT 'Mary', '[email protected]'
)

我想你可能会说,这是一个有点无意义的追求,但我希望查询本身可以随着表设计而扩展。例如,如果列名称发生更改,或者添加了更多列,我不必在代码中的任何地方进行更改。

干杯:)

I would like a shorthand (if possible) for inserting multiple records into a table which has a primary key and IsIdentity property. For example, say I have a table called 'People' with the following columns:
- ID (Primary Key, and Identity [i.e. autoincrementing])
- Name (not null)
- Email (not null)

An insert statement excluding the auto-incrementing ID column is perfectly valid, such as:

INSERT INTO People VALUES ('George', '[email protected]')

But if I want to insert multiple values in the same statement, ideally that could be done something like this where I don't have to explicitly specify the column names:

INSERT INTO People VALUES (
   (auto, 'George', '[email protected]'),
   (auto, 'Mary', '[email protected]')
)

The best solution I could find was something like this:

INSERT INTO People (
   SELECT 'George', '[email protected]',
   UNION ALL
   SELECT 'Mary', '[email protected]'
)

I suppose you could argue, this is a somewhat meaningless pursuit, but I wanted the query itself to be extensible along with the table design. For example, if a column name changed, or more columns were added I wouldn't have to change this everywhere in the code.

Cheers :)

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

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

发布评论

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

评论(2

我乃一代侩神 2024-11-26 07:53:06

您可以像这样插入多行:

INSERT INTO `People` (`name`,`email`) VALUES ('George', '[email protected]'),('Mary', '[email protected]');

编辑:

mysql> create table `test`(
    -> `id` int(10) unsigned not null AUTO_INCREMENT,
    -> `name` varchar(255) not null default 'N/A/',
    -> `email` varchar(255) not null default 'N/A/',
    -> PRIMARY KEY(`id`) 
    -> )ENGINE=MyISAM DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO `test` VALUES (null, 'Name 1', 'Email 1'),(null, 'Name 2','Email 2');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from `test`;
+----+--------+---------+
| id | name   | email   |
+----+--------+---------+
|  1 | Name 1 | Email 1 |
|  2 | Name 2 | Email 2 |
+----+--------+---------+
2 rows in set (0.00 sec)

只要您的主键设置为自动增量,您就可以将该字段置空,它会自动将值设置为自动增量值。

You can insert multiple rows like this:

INSERT INTO `People` (`name`,`email`) VALUES ('George', '[email protected]'),('Mary', '[email protected]');

EDIT:

mysql> create table `test`(
    -> `id` int(10) unsigned not null AUTO_INCREMENT,
    -> `name` varchar(255) not null default 'N/A/',
    -> `email` varchar(255) not null default 'N/A/',
    -> PRIMARY KEY(`id`) 
    -> )ENGINE=MyISAM DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO `test` VALUES (null, 'Name 1', 'Email 1'),(null, 'Name 2','Email 2');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from `test`;
+----+--------+---------+
| id | name   | email   |
+----+--------+---------+
|  1 | Name 1 | Email 1 |
|  2 | Name 2 | Email 2 |
+----+--------+---------+
2 rows in set (0.00 sec)

As long as your primary key is set to auto increment, you can nullify the field and it will auto set the value to the auto increment value.

饭团 2024-11-26 07:53:06

这取决于您使用的数据库。一些数据库允许多个值集:

insert into People values
(auto, 'George', '[email protected]'),
(auto, 'Mary', '[email protected]')

That depends on what database you are using. Some databases allow multiple value sets:

insert into People values
(auto, 'George', '[email protected]'),
(auto, 'Mary', '[email protected]')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文