获取两个日期之间的外键

Get foreign key between two dates

有必要根据生肖来分析汇率的波动。 我想在下表中插入外键(zodiak_id):

create table CURRENCY (
  CUR_ID number NOT NULL,  
  CUR_DATE DATE not null,
  CUR_NAME varchar2(3) not null,
  VALUE NUMBER not null,
  Zodiac_id number,
  constraint PK_CUR primary key (CUR_ID),
  CONSTRAINT FK_ZOD FOREIGN KEY (Zodiac_id) REFERENCES Zodiac(Zodiac_id)
  );

of this table:

create table Zodiac (
  Zodiac_id number not null,
  Zodiac_name VARCHAR2(15) not null,
  START_PERIOD  date,
  END_PERIOD  date,  
  constraint PK_Zod primary key (Zodiac_id)
  );

我写的sql如下:

insert into CURRENCY cr(Zodiac_id)
select Zodiac_id from ZODIAC z
where cr.CUR_DATE >= z.START_PERIOD and cr.CUR_DATE <= z.END_PERIOD;

但出现此错误:SQL 错误:ORA-00904:"CR"。"CUR_DATE":标识符无效

在此先感谢您的帮助。

这是因为您只将 zodiac_id 插入 CURRENCY table,但 cur_date 是必填字段。您在 create table 语句中用 "not null" 约束标记了它。

试试这个;

 insert into CURRENCY  (Zodiac_id)
 select Z.Zodiac_id from Zodiac Z
 inner join CURRENCY CR ON CR.Zodiac_id=Z.Zodiac_id
 WHERE CR.CUR_DATE BETWEEN Z.START_PERIOD AND Z.END_PERIOD