跨多个表的公共序列
我正在尝试创建一个由多个表拥有的序列,以便在更多表上拥有唯一的标识符。我能想到的唯一解决方法是在 INSERTing 时使用 nextval(blabla_id_seq) ,但显然在使用 COPY 时它不起作用(或我现在无法想到的其他一些情况)。
那么有人知道这个问题的解决方案或其他解决方法吗?问题的目的主要是教育性的。
干杯, Don
LE 我可以为两个(或更多)表(父表和子表)实现全局主键吗?目前我尝试过
DROP TABLE IF EXISTS child;
DROP TABLE IF EXISTS parent;
CREATE TABLE parent (
id serial PRIMARY KEY
, categ varchar(20) NOT NULL
);
CREATE TABLE child (
else varchar (30) NOT NULL
, id integer -- i have also tried with no id in child table, on;y when using
--"id serial" does id become primary key
, CONSTRAINT PK__child PRIMARY KEY (id)
) INHERITS (parent);
COPY parent (categ)
FROM 'E:\\1\\_parent.csv'
WITH CSV;
COPY child(categ,altceva)
FROM 'E:\\1\\_child.csv'
WITH CSV;
INSERT INTO child (id,categ,altceva)
--VALUES(nextval('parent_id_seq')+3,'kid7','blabla');
VALUES(5,'kid7','blabla');
,但我可以插入重复项
I'm trying to create a sequence owned by multiple tables, to have a unique identifier over more tables. The only workaround i can think about right know is using a nextval(blabla_id_seq) when INSERTing, but obviously it wont work when using COPY (or some other situations which i can't think of right now).
So anybody know a solution or another workaround for this ? Purpose of question is mostly educational.
Cheers,
Don
LE And can i implement a global primary key for two (or more) table , parent and child? Currently i tried
DROP TABLE IF EXISTS child;
DROP TABLE IF EXISTS parent;
CREATE TABLE parent (
id serial PRIMARY KEY
, categ varchar(20) NOT NULL
);
CREATE TABLE child (
else varchar (30) NOT NULL
, id integer -- i have also tried with no id in child table, on;y when using
--"id serial" does id become primary key
, CONSTRAINT PK__child PRIMARY KEY (id)
) INHERITS (parent);
COPY parent (categ)
FROM 'E:\\1\\_parent.csv'
WITH CSV;
COPY child(categ,altceva)
FROM 'E:\\1\\_child.csv'
WITH CSV;
INSERT INTO child (id,categ,altceva)
--VALUES(nextval('parent_id_seq')+3,'kid7','blabla');
VALUES(5,'kid7','blabla');
but i can insert duplicates
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题很老了,但这里有一个简单的答案。
这看起来像是某种继承模型,在我看来,自然的解决方案是使用公共基表。基表至少由单个串行列组成。或者,它可以包含系统中所有对象/节点共有的属性字段。
当您想要添加一个新对象或
node
时,首先插入到node
中,然后使用返回的自动生成的序列id
将更多详细信息插入子类型表中。这不需要任何特殊的考虑,如事务、检查、触发器等,也不会因为唯一性错误而导致创建失败。
作为扩展,您可以在
node
表中添加一个string 类型
字段,并获得接受任何node_id
并直接找到相应对象的能力。This question is old and all, but here's a simple answer.
This looks like some sort of inheritance model and the natural solution to that in my mind is to use a common base table. The base table consists at the least of a single serial column. Optionally it may contain fields for attributes that are common for all the objects/nodes in the system.
When you want to add a new object, or
node
as it were, then you first insert intonode
and then use the returned autogenerated serialid
to insert further details into the subtyped tables.This doesn't require any special considerations like transactions, checks, triggers and so on, nor will creation ever fail due to uniqueness errors.
As an extension, you can add a
string type
field to thenode
table and gain the ability to accept anynode_id
and straightforwardly find the corresponding object.