sql - 检查行是否已经存在,如果存在则告诉引用的表 id

标签 sql postgresql insert reference exists

假设我有一本 table 上杂志:

CREATE TABLE magazine
(
  magazine_id integer NOT NULL DEFAULT nextval(('public.magazine_magazine_id_seq'::text)::regclass),
  longname character varying(1000),
  shortname character varying(200),
  issn character varying(9),
  CONSTRAINT pk_magazine PRIMARY KEY (magazine_id)
);

还有另一个表问题:

CREATE TABLE issue
(
  issue_id integer NOT NULL DEFAULT nextval(('public.issue_issue_id_seq'::text)::regclass),
  number integer,
  year integer,
  volume integer,
  fk_magazine_id integer,
  CONSTRAINT pk_issue PRIMARY KEY (issue_id),
  CONSTRAINT fk_magazine_id FOREIGN KEY (fk_magazine_id)
      REFERENCES magazine (magazine_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
);

当前插入:

INSERT INTO magazine (longname,shotname,issn)
VALUES ('a long name','ee','1111-2222');

INSERT INTO issue (fk_magazine_id,number,year,volume)
VALUES (currval('magazine_magazine_id_seq'),'8','1982','6');

现在一行应该只插入到'magazine',如果它还不存在的话。但是,如果存在,表“issue”需要获取已存在行的“magazine_id”才能建立引用。

我该怎么做?

提前致谢!

最佳答案

如何知道 magazine 表中是否已经有杂志? issn 列是否定义杂志?如果是,那么它应该是一个主键,或者至少是 unique

最简单的方法是在您的客户端应用程序中检查杂志是否存在,如下所示(伪代码):

function insert_issue(longname, shotname, issn, number,year,volume) {
    /* extensive comments for newbies */
    start_transaction();
    q_get_magazine_id = prepare_query(
      'select magazine_id from magazine where issn=?'
    );
    magazine_id = execute_query(q_get_magazine_id, issn);
    /* if magazine_id is null now then there’s no magazine with this issn */
    /* and we have to add it */
    if ( magazine_id == NULL ) {
      q_insert_magazine = prepare_query(
        'insert into magazine (longname, shotname, issn)
          values (?,?,?) returning magazine_id'
      );
      magazine_id = execute_query(q_insert_magazine, longname, shortname, issn);
      /* we have tried to add a new magazine; */
      /* if we failed (magazine_id==NULL) then somebody else just added it */
      if ( magazine_id == NULL ) { 
        /* other, parerelly connected client just inserted this magazine, */
        /* this is unlikely but possible */
        rollback();
        start_transaction();
        magazine_id = execute_query(q_get_magazine_id, issn);
      }
    }
    /* now magazine_id is an id of magazine, */
    /* added if it was not in a database before, new otherwise */
    q_insert_issue = prepare_query(
      'insert into issue (fk_magazine_id,number,year,volume)
         values (?,?,?,?)'
    );
    execute_query(q_insert_issue, magazine_id, number, year, volume);
    /* we have inserted a new issue referencing old, */
    /* or if it was needed new, magazine */
    if ( ! commit() ) {
      rollback();
      raise "Unable to insert an issue";
    }
}

如果您只需要在一个查询中执行此操作,那么您可以将此伪代码实现为数据库中的 pl/pgsql 函数,只需 select insert_issue(?, ?, ?, ?, ?, ?) .

关于sql - 检查行是否已经存在,如果存在则告诉引用的表 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2432371/

相关文章:

sql - 统计比赛参加人数

mysql - INSERT INTO...SELECT,并从 SELECT 更新

php - 如何获取行数作为列

php - 验证用户名是否已存在于 PHP 和 MYSQL 中?

java - 从数据库创建 JTable

json - Postgresql - 从嵌套的 jsonb 元素更新或删除值

SQL分区消除

sql - 将数字字段解释为 SQL 中的字符串

PHP 数组到 mysql BD...缺少数组字段

javascript - JS输入框多吗?