Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql postgres:两个表之间的约束_Sql_Database_Postgresql - Fatal编程技术网

Sql postgres:两个表之间的约束

Sql postgres:两个表之间的约束,sql,database,postgresql,Sql,Database,Postgresql,我不确定我做的事情是否正确,但问题是: 我想确保网络x[从1990年到2000年]只有站点[谁的开始/结束时间在1990年到2000年之间] CREATE TABLE network ( id bigint NOT NULL, alternate_code character varying(5), code character varying(5) NOT NULL, description character varying(250), end_time timestam

我不确定我做的事情是否正确,但问题是:

我想确保网络x[从1990年到2000年]只有站点[谁的开始/结束时间在1990年到2000年之间]

CREATE TABLE network
(
  id bigint NOT NULL,
  alternate_code character varying(5),
  code character varying(5) NOT NULL,
  description character varying(250),
  end_time timestamp with time zone,
  historical_code character varying(5),
  last_updated timestamp with time zone DEFAULT current_timestamp,
  start_time timestamp with time zone NOT NULL,
  version bigint,
  CONSTRAINT network_pkey PRIMARY KEY (id),
  CONSTRAINT unq_network_0 UNIQUE (code, start_time, end_time),
  CONSTRAINT network_check CHECK (start_time < end_time)
)
WITH (
  OIDS=FALSE
);


CREATE CONSTRAINT TRIGGER time_check_consistency
  AFTER INSERT OR UPDATE
  ON network
  DEFERRABLE INITIALLY DEFERRED
  FOR EACH ROW
  EXECUTE PROCEDURE network_time_check_consistency();


CREATE OR REPLACE FUNCTION network_time_check_consistency()
  RETURNS trigger AS
$BODY$
begin
    RAISE NOTICE 'Message: % %',:new.id,:new.start_time;
    IF EXISTS(select 1 from station where network_id= :new.id and (start_time < :new.start_time or end_time > :new.end_time)) THEN
    RAISE EXCEPTION 'Invalid network time range (station out of range).';
    END IF;
    RETURN NULL;
END;


CREATE TABLE station
(
  id bigint NOT NULL,
  alternate_code character varying(5),
  code character varying(5) NOT NULL,
  creation_time timestamp with time zone,
  description character varying(255),
  elevation numeric(7,1) NOT NULL,
  end_time timestamp with time zone,
  geology character varying(255),
  historical_code character varying(5),
  last_updated timestamp with time zone DEFAULT current_timestamp,
  latitude numeric(8,6) NOT NULL,
  longitude numeric(9,6) NOT NULL,
  start_time timestamp with time zone NOT NULL,
  termination_time timestamp with time zone,
  vault character varying(255),
  version bigint,
  network_id bigint NOT NULL,
  station_id bigint,
  CONSTRAINT station_pkey PRIMARY KEY (id),
  CONSTRAINT fk_station_network_id FOREIGN KEY (network_id)
      REFERENCES network (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_station_station_id FOREIGN KEY (station_id)
      REFERENCES site (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT unq_station_0 UNIQUE (network_id, code, start_time, end_time),
  CONSTRAINT station_check CHECK (start_time < end_time),
  CONSTRAINT station_latitude_check CHECK ((90::numeric >= latitude)::smallint >= (-90)),
  CONSTRAINT station_longitude_check CHECK ((180::numeric >= longitude)::smallint >= (-180))
)
WITH (
  OIDS=FALSE
);


CREATE CONSTRAINT TRIGGER time_check_consistency
  AFTER INSERT OR UPDATE
  ON station
  DEFERRABLE INITIALLY DEFERRED
  FOR EACH ROW
  EXECUTE PROCEDURE station_time_check_consistency();


CREATE OR REPLACE FUNCTION station_time_check_consistency()
  RETURNS trigger AS
$BODY$
begin
    IF EXISTS(select 1 from channnel where station_id= station_in_id and (start_time < in_starttime or end_time > in_endtime)) THEN
    RAISE EXCEPTION 'Invalid station time range (channel[s] out of range).';
    END IF;

    IF EXISTS(select 1 from network where id= network_in_id and (start_time > in_starttime or end_time < in_endtime)) THEN
    RAISE EXCEPTION 'Invalid station time range (network out of range).';
    END IF;
    RETURN NULL;
END;
我得到:

ERROR:  Invalid network time range (station out of range).
CONTEXT:  edb-spl function inline_code_block line 6 at COMMIT

我认为“可延迟的初始延迟”将延迟检查,直到事务完成且两次都更改,但这不会发生,我的理解正确吗?

请在{station/channel}中添加别名exists子查询中的表。顺便说一下:问题不完整:缺少表站点和通道的定义,并且在函数末尾使用了
$body$language plpgsql丢失。另外:您不希望在
新建之前使用
而且还不清楚
在\u starttime
在\u endtime
中的来源。请发布真实完整的代码。你是在要求完整的模式吗。如果你不知道答案,那就别麻烦了。问题是关于可延迟的初始延迟以及它是如何工作的,我发布的所有其他内容都是额外的,需要进一步澄清。在问题中使用精简模式是可以的,但至少应该是可用的。想象一下,五个人花了15到30分钟试图理解它,但失败了。顺便说一句:你使用的是企业数据库吗?顺便说一句:如果你在我的前两条评论中加上更正,事情似乎很好。(加上模式上一些繁琐的更改)
ERROR:  Invalid network time range (station out of range).
CONTEXT:  edb-spl function inline_code_block line 6 at COMMIT