【问题标题】:SQL insert query combined with selectSQL插入查询结合选择
【发布时间】:2017-09-18 10:52:28
【问题描述】:

我想在表格中插入一个数据集。插入查询应该将数据插入到两个表中。

这是表格:

表 A:

ID, customerId, ..[some other columns]...

表 B:

customerId, name

查询应该插入到表 A 和表 B 中。如何将值插入到表 B 中,关系为 customerId

编辑:DBMS 是 sqlite。

定义:

适应:

  • id -> 整数不为空
  • customerId -> 整数不为空
  • 一些文本列....

客户:

  • customerId -> 整数非空
  • 名称 -> 文本不为空

【问题讨论】:

  • 您使用的是哪个 DBMS?后格雷斯?甲骨文?
  • 写两条插入语句,每个表一个。如果其中一个表恰好是客户表,那么这个插入必须是第一个。
  • 向我们展示表定义。
  • DBMS 是 sqlite。定义:适应:id -> integer not null,customerId -> integer not null,一些文本列; // 客户:customerId -> integer not null, name -> text not null
  • 编辑问题

标签: sql sqlite select insert


【解决方案1】:

我不确定,我完全理解你的问题,但我建议你看看技术叫:

交易 (https://en.wikipedia.org/wiki/Database_transaction)

从技术上讲,这仍然是两个(或更多)插入查询,但它可以让您保持数据集的完整性。事务在大多数 DBMS 中实现。

【讨论】:

    猜你喜欢
    • 2014-10-17
    • 2023-04-09
    • 1970-01-01
    • 2013-03-14
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    相关资源
    最近更新 更多