在两个日期之间获取外键

时间:2015-01-04 21:27:53

标签: sql oracle11g

有必要根据黄道带的符号分析汇率的波动。 我想在下表中插入外键(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)
  );

我写的sqls如下:

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”:无效的标识符

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

试试这个;

 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

答案 1 :(得分:0)

这是因为您只将zodiac_id插入CURRENCY表,但cur_date是必填字段。你用&#34; not null&#34;标记了它。 create table语句中的约束。