sql - 我需要在不同的表中插入 2 条记录,

标签 sql postgresql insert

我需要将 2 条记录插入到 2 个不同的表中。问题是这两条记录将具有相同的 ID。 例如:

我有我的 Mannto 表,其中包含 IdMan 和 oters 字段。我还有我的服务表及其 IdServ。

我该怎么做才能使这一个相等?我正在使用 Postgre。 Mannto 表的 ID 是序列号,我需要将其用作服务表中的外键

我尝试了以下方法,但它不起作用:

Insert into Mannto ( idMan, field 1 field2 ...etc) 
            values ( default, 'f1', 'f2'...etc)

Insert into Service ( idServ, fkMannto, field1...etc) 
             values (default, (in this part I need the same ManntoId called idMan), 'f1')

感谢您提供的任何帮助!

最佳答案

INSERT INTO Mannto ( field1, field2 ...etc) VALUES ( 'f1', 'f2'...etc)
  RETURNING idMan;

http://www.postgresql.org/docs/current/static/sql-insert.html

关于sql - 我需要在不同的表中插入 2 条记录,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7014355/

相关文章:

mysql - 查询以查找表中名称和数字之间的重复项

mysql - 如何在MySQL数据库中动态生成字符串实例?

java - 如何使用 JAVA 代码读取 shape(.shp) 文件并显示其值

sql - 树的迭代深度

mysql - 选择与 MySQL/phpMyAdmin 中的 ID 相同的值的单元格

SQL - 将时间序列事件转换为开/关对(处理可能丢失的开或关)

python - 无法使用 django + nginx 上传媒体文件

javascript - Rethinkdb 追加到数组(如果存在)

node.js - NodeJS- Elasticsearch 以插入要索引的数据

oracle - 插入时可以在表上创建索引