检查行是否已存在,如果存在,则告诉引用的表 id
假设我有一本表格杂志:
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');
现在,如果一行尚不存在,则只能将其插入到“杂志”中。但是,如果存在,表“issue”需要获取已存在的行的“magazine_id”才能建立引用。
我该怎么做?
提前谢谢!
Let's assume I have a table magazine:
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)
);
And another table issue:
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
);
Current INSERTS:
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');
Now a row should only be inserted into 'magazine', if it does not already exist. However if it exists, the table 'issue' needs to get the 'magazine_id' of the row that already exists in order to establish the reference.
How can i do this?
Thx in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您如何知道杂志是否已在
magazine
表中?issn
列是否定义一本杂志?如果是,那么它应该是主键,或者至少是唯一的。最简单的方法是检查客户端应用程序中是否存在杂志,如下所示(以伪代码形式):
如果您只需在一个查询中执行此操作,那么您可以将此伪代码实现为数据库中的 pl/pgsql 函数,并且只需 <代码>选择 insert_issue(?, ?, ?, ?, ?, ?)。
How do you know if a magazine is already in
magazine
table? Doesissn
column define a magazine? If yes then it should be a primary key, or at leastunique
.The easiest way would be to do a check for magazine existence in your client application, like this (in pseudocode):
If you just have to do this in one query then you can implement this pseudocode as pl/pgsql function in database and just
select insert_issue(?, ?, ?, ?, ?, ?)
.如果您使用的是 PostgreSQL 9.1 或更高版本,您可以执行以下操作:
Writeable CTE's FTW!
If you are on PostgreSQL 9.1 or higher you can do something like:
Writeable CTE's FTW!
我不确定你是否可以用 SQL 来做到这一点。我知道Oracle可以用于触发器,但我不认为SQL可以。如果我错了,有人纠正我。
I'm not sure that if you can do this with SQL. I know that Oracle can be used for triggers, but i don't think SQL is able to. Someone correct me if I'm wrong.