ORA-02267: 与引用列的数据类型或排序规则不兼容

ORA-02267: incompatible with the data type or collation of the referenced column

create table doctor
    (
        name          varchar2(20)
      , d_id          varchar2(20)
      , address       varchar2(50)
      , phone_number  number(10)
      , qualification varchar2(20)
      , gender        varchar2(20)
      , constraint pk_doctor primary key(d_id)
    )
;

create table room
    (
        room_id   varchar2(5)
      , room_type varchar2(20)
      , constraint pk_room primary key(room_id)
    )
;

create table patient
    (
        p_id           varchar2(10)
      , p_name         varchar2(20)
      , p_age          number(3)
      , p_gender       varchar2(10)
      , address        varchar2(50)
      , date_admission date
      , phone_number   number(10)
      , room_id        varchar2(5)
      , constraint pk_patient primary key(p_id)
      , constraint fk_p1 foreign key(room_id) references room
    )
;

create table bill
    (
        bill_no        varchar2(10)
      , bill_date      date
      , p_id           varchar2(10)
      , p_name         varchar2(20)
      , p_age          number(3)
      , p_gender       varchar2(10)
      , date_admission date
      , date_discharge date
      , room_charges   number(10)
      , pathology_fees number(10)
      , d_fees         number(10)
      , miscellaneous  number(10)
      , total_amount   number(10)
      , constraint pk_bill primary key(bill_no)
      , constraint fk_b1 foreign key(p_id) references patient
      , constraint fk_b2 foreign key(p_name) references patient
      , constraint fk_b3 foreign key(p_age) references patient
      , constraint fk_b4 foreign key(p_gender) references patient
      , constraint fk_b5 foreign key(date_admission) references patient
    )
;

错误从第 15 行开始 -

create table bill
    (
        bill_no        varchar2(10)
      , bill_date      date
      , p_id           varchar2(10)
      , p_name         varchar2(20)
      , p_age          number(3)
      , p_gender       varchar2(10)
      , date_admission date
      , date_discharge date
      , room_charges   number(10)
      , pathology_fees number(10)
      , d_fees         number(10)
      , miscellaneous  number(10)
      , total_amount   number(10)
      , constraint pk_bill primary key(bill_no)
      , constraint fk_b1 foreign key(p_id) references patient
      , constraint fk_b2 foreign key(p_name) references patient
      , constraint fk_b3 foreign key(p_age) references patient
      , constraint fk_b4 foreign key(p_gender) references patient
      , constraint fk_b5 foreign key(date_admission) references patient
    )

错误报告-

ORA-02267: column type incompatible with referenced column type

02267. 00000 - "column type incompatible with referenced column type"

*Cause:  The data type or collation of the referencing column was

  incompatible with the data type or collation of the referenced

  column.

*Action:  Select a compatible data type for the referencing column.

  Also, the collation of a character column in a foreign key must

  match the collation of the corresponding column in the primary

  key.

我明白错误的意思,但我的数据类型在两个表中都相同,但它仍然显示错误??

请告诉我哪里出错了。我将不胜感激。

您面临的问题来自错误的数据模型,需要进行一些返工。

“账单”table 包含 p_name、p_age、p_gender 和 date_admission 列。 “患者”table 中存在相同的列,因此“账单”中不需要它们。只需从“账单”定义中删除列即可。

每当您需要为账单获取患者姓名时,您始终可以使用 p_id:

获取
select p.p_name, p.p_age
  from patient p
  join bill b
    on b.p_id, p.p_id
 where b.bill_no = 'some_bill_no'

并且保留患者的年龄会导致工程开销,因为您必须每年不断更新一次该字段。我建议您存储患者的 birth_date 并在需要时计算 his/her 年龄。

当然,当它不是具有给出table结构的作业时

From the documentation:

If you identify only the parent table or view and omit the column name, then the foreign key automatically references the primary key of the parent table or view.

所以你实际上在做:

      , constraint fk_b1 foreign key(p_id) references patient
      , constraint fk_b2 foreign key(p_name) references patient(p_id)
      , constraint fk_b3 foreign key(p_age) references patient(p_id)
      , constraint fk_b4 foreign key(p_gender) references patient(p_id)
      , constraint fk_b5 foreign key(date_admission) references patient(p_id)

这显然不是您想要的,并解释了您遇到的错误。

您需要指定匹配的非主键列:

      , constraint fk_b1 foreign key(p_id) references patient
      , constraint fk_b2 foreign key(p_name) references patient(p_name)
      , constraint fk_b3 foreign key(p_age) references patient(p_age)
      , constraint fk_b4 foreign key(p_gender) references patient(p_gender)
      , constraint fk_b5 foreign key(date_admission) references patient(date_admission)

但是,这个不会得到

ORA-02270: no matching unique or primary key for this column-list

因为那四列不是 suitable 目标; none 确实可以是独一无二的,至少是安全的。而且它不允许更改数据——姓名和性别可以,但年龄会,例如。这些可能是记录患者入院时的状态,所以除了纠正错误外,这些不会改变。

duplicate/denormalise 数据没有任何意义。您可以只拥有 PK 参考并根据需要加入主要 table 以获取其他信息。

db<>fiddle

patient 分成一个 table 来识别一个人(用出生日期而不是年龄)和一个单独的 table 来记录那个病人的每次住院 - 有房间和admission/discharge 日期,例如 - 可能更有意义。账单上只写出院日期好像很奇怪