小编Sno*_*all的帖子

强制约束“两个表”

我在 SQL 中对电气原理图建模时遇到了一些麻烦。我想捕获的结构是

  part ??????????? pin
   ?                ?
part_inst ?????? pin_inst
Run Code Online (Sandbox Code Playgroud)

其中“inst”是“instance”的缩写。

例如,我可能part将 LM358 运算放大器pin用作 1OUT、1IN-、1IN+、GND、2IN+、2IN-、2OUT 和 V CC。然后我可能会将这部分放在原理图上,创建 apart_inst和 8 pin_insts。

忽略数据字段,我对模式的最初尝试是

create table parts (
    part_id bigserial primary key
);
create table pins (
    pin_id bigserial primary key,
    part_id bigint not null references parts
);
create table part_insts (
    part_inst_id bigserial primary key,
    part_id bigint not null references parts
);
create table pin_insts (
    pin_inst_id bigserial primary key,
    part_inst_id bigint …
Run Code Online (Sandbox Code Playgroud)

postgresql foreign-key database-design referential-integrity polymorphic-associations

13
推荐指数
1
解决办法
5105
查看次数

限制在边界上的不重叠矩形

我正在尝试对电路板上零件的放置进行建模。没有任何有意义的限制,我的基本架构如下所示:

create table part (
    part_id bigserial primary key,
    name text not null,
    width double precision not null,
    height double precision not null
);
create table board (
    board_id bigserial primary key,
    width double precision not null,
    height double precision not null
);
create table board_part (
    board_id bigint not null references board,
    part_id bigint not null references part,
    position point not null
);
Run Code Online (Sandbox Code Playgroud)

SQL Fiddle可视化

对于bb2any board_parts,我想强制执行以下约束:

  1. b位于黑板上:

    box(b.position, …
    Run Code Online (Sandbox Code Playgroud)

postgresql database-design referential-integrity spatial exclusion-constraint

5
推荐指数
1
解决办法
529
查看次数