【问题标题】:Relationing tables in postresqlpostgresql中的关系表
【发布时间】:2021-11-16 17:07:49
【问题描述】:

我想在我的 postgresql 数据库中建立一些关系。

我的脚本如下所示:

create table anomaly_analysis
(
    id bigserial not null,
    chart_id bigserial constraint charts_pk references charts id not null,
    analysis_date date not null,
    primary key (id)
);

create table anomaly_analysis_data_points
(
    anomaly_analysis_id bigserial 
         constraint anomaly_analysis_pk
             references anomaly_analysis id not null,
    data_point_id bigserial 
         constraint charts_pk 
             references charts id not null
);

alter table data_series
   add column isAnomaly boolean;

我想在第一个表中添加外键,它将在字段 id 上引用表图表。同样在第二个表中,我想添加外键,这将在我上面创建的表中引用,anomaly_analysis 的 id 上的字段 anomaly_analysis_id 以及表格图表和字段 id 上的 chart_id。

我试过这个查询(这是一行的例子):

chart_id bigserial constraint charts_pk foreign key (charts_id )references charts not null

但这对我也不起作用。我尝试了许多类似的组合,我正在阅读文档和其他网站,但没有奏效。我应该怎么做才能使我的脚本正确?

编辑

我忘记添加日志了。正在使用 flyway 为数据库提供服务。 我收到的例外是:

SQL State  : 42601
Error Code : 0
Message    : ERROR: syntax error at or near "id"
Position: 121
Location   : db/migration /V1.1.15__add_anomaly_analysis_tables.sql (/mypath/V1.1.15__add_anomaly_analysis_tables.sql)
Line       : 1
Statement  : create table anomaly_analysis(
   id bigserial not null,
   chart_id bigserial constraint charts_pk references charts id not null,
   analysis_date date not null,
   primary key (id)
)

【问题讨论】:

  • 您不应将序列用作 FK 列。它应该是一个整数,指向一个序列。
  • 但是我项目中的其他列正在使用 fk 这样的大序列类型,让我想知道的是其他具有类似脚本的表工作正常,只是这个在初始化期间崩溃了。跨度>
  • 序列号是一个整数,其默认值由函数提供。 FK 是一组列,引用另一个表中的一组列。将 FK 设置为生成器函数提供的某个值是没有意义的。生成器函数对另一个表一无所知。
  • chart_id bigint constraint charts_pk references charts (id) not null postgresql.org/docs/current/sql-createtable.html
  • 所以我只需要更改我创建的列的类型,以使我的表格正常工作吗?

标签: postgresql flyway


【解决方案1】:
  • 您应该使用 serial 作为 FK 列。它应该是一个integer指向连续剧。
  • FK 的语法需要括号:.. REFERENCING table_name (column, column,...)
  • anomaly_analysis 表似乎是一个 junction 表,实现了 n-m 关系。它肯定需要一个索引,甚至两个索引。
  • CREATE TABLE ... 的(子)语法非常庞大。看到罚款manual

你可能想要这个:


CREATE TABLE charts( 
        id bigserial NOT NULL PRIMARY KEY
        );

CREATE TABLE anomaly_analysis(
   id bigserial NOT NULL PRIMARY KEY
   , chart_id bigint not null
         CONSTRAINT charts_fk REFERENCES charts (id)
   , analysis_date date NOT NULL
   );

CREATE TABLE anomaly_analysis_data_points(
   anomaly_analysis_id bigint NOT NULL
        CONSTRAINT anomaly_analysis_pk REFERENCES anomaly_analysis(id)
   , data_point_id bigint NOT NULL
        CONSTRAINT charts_fk REFERENCES charts(id)
-- , PRIMARY KEY (anomaly_analysis_id,data_point_id) -- ?? Will force an index to be created
-- , UNIQUE (data_point_id, anomaly_analysis_id) -- Will force an index to be created, too
   );

-- alter table data_series add column isAnomaly boolean; -- huh?

【讨论】:

  • 我之前按照您的指示更正了它,感谢您写下答案,以便我可以投票给您并关闭问题
猜你喜欢
  • 2021-12-22
  • 2013-10-27
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2013-02-08
相关资源
最近更新 更多