在postgres中键入相同的表格?

时间:2013-11-13 14:41:02

标签: sql postgresql

我正在尝试用postgres创建一个workiarchial table。我正在使用相邻的列表方法。我的问题是,在创建数据模型时,如何引用同一个表的ID?

create table nodes (
    id serial primary key,
    parentid WHAT GOES HERE?
    name varchar,
);

1 个答案:

答案 0 :(得分:2)

你可以这样做:

create table nodes (
    id serial primary key,
    parentid integer references nodes(id),
    name varchar
);

这些被称为“自引用表”