跨多个表的公共序列

发布于 2024-12-03 11:06:35 字数 890 浏览 0 评论 0原文

我正在尝试创建一个由多个表拥有的序列,以便在更多表上拥有唯一的标识符。我能想到的唯一解决方法是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱成瘾 2024-12-10 11:06:35

这个问题很老了,但这里有一个简单的答案。

这看起来像是某种继承模型,在我看来,自然的解决方案是使用公共基表。基表至少由单个串行列组成。或者,它可以包含系统中所有对象/节点共有的属性字段。

node(serial node_id, timestamp creation_time default now())

book(integer node_id [foreign key => node.node_id], string title, timestamp published_time)
user(integer node_id [foreign key => node.node_id], string name, timestamp joined_time, string display_name)
car (integer node_id [foreign key => node.node_id], string model, timestamp manufactured_time, real miles)

当您想要添加一个新对象或 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.

node(serial node_id, timestamp creation_time default now())

book(integer node_id [foreign key => node.node_id], string title, timestamp published_time)
user(integer node_id [foreign key => node.node_id], string name, timestamp joined_time, string display_name)
car (integer node_id [foreign key => node.node_id], string model, timestamp manufactured_time, real miles)

When you want to add a new object, or node as it were, then you first insert into node and then use the returned autogenerated serial id 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 the node table and gain the ability to accept any node_id and straightforwardly find the corresponding object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文