【问题标题】:Error number: 3780 Referencing column '%s' and referenced column '%s' in foreign key constraint '%s' are incompatible错误号: 3780 外键约束 '%s' 中引用列 '%s' 和引用列 '%s' 不兼容
【发布时间】:2020-08-01 05:30:50
【问题描述】:
DROP DATABASE IF EXISTS ProviderPatients;
CREATE DATABASE ProviderPatients;
USE ProviderPatients;

CREATE TABLE IF NOT EXISTS Date_Dim (
    Date_ID  integer not null,
    Date_ date,
    Full_Date_Des varchar(25) not null,
    Day_Of_Week int(11) not null,
    Calender_Year int(11) not null,
    Weekday_Indicator int(11) not null,
    primary key(Date_ID));

CREATE TABLE IF NOT EXISTS Insurer_DIM (
    Insurer_ID  int(11) not null,
    Insurer_Name varchar(25) not null,
    Line_Of_Buissness varchar(25) not null,
    primary key(Insurer_ID));

CREATE TABLE IF NOT EXISTS Member_DIM (
    Member_ID   int(11) not null,
    Member_Name varchar(25) not null,
    Age         int(11) not null,
    Ethnicity   varchar(25) not null,
    Health_Condition varchar(25) not null,
    primary key(Member_ID));

CREATE TABLE IF NOT EXISTS Geography_Dim (
    Geography_ID varchar(25) not null,
    Country     varchar(25) not null,
    State       varchar(10) not null,
    State_Code  int(11) not null,
    County_Code int(11) not null,
    primary key(Geography_ID));

CREATE TABLE Provider_Dim (
    Provider_ID int(11) not null, 
    Provider_Name VARCHAR(45) NOT NULL, 
    Gender Varchar(25) Not Null,
    NPI     Varchar(25) Not Null,
    Credential Varchar(25) Not Null, 
PRIMARY KEY(Provider_ID));

CREATE TABLE Eval_Fact_Table(
Date_ID int(11) not null,
Member_ID int(11) not null,
Provider_ID int(11) not null,
Insurer_ID int(11) not null,
Geography_ID int(11) not null,
Num_Visits int(11) not null,
Eval_Costint int(11) not null,
Eval_Start date not null,
Eval_End date not null, 
FOREIGN KEY (Date_ID) References Date_Dim (Date_Id) on delete restrict,
FOREIGN KEY (Member_ID) References Member_Dim (Member_ID) on delete restrict,
FOREIGN KEY (Geography_ID) References Geography_Dim (Geography_ID) on delete restrict,
FOREIGN KEY (Provider_ID) References Proveider_Dim (Provider_ID) on delete restrict,
FOREIGN KEY (Insurer_ID) References Insurer_Dim (Insurer_ID)on delete restrict);

错误号:3780;符号:ER_FK_INCOMPATIBLE_COLUMNS; SQLSTATE: HY000

消息:错误代码:3780。在外键约束“eval_fact_table_ibfk_3”中引用列“Geography_ID”和引用列“Geography_ID”不兼容。

【问题讨论】:

    标签: mysql error-code


    【解决方案1】:

    错误Referencing column 'Geography_ID' and referenced column 'Geography_ID' in foreign key constraint 'eval_fact_table_ibfk_3' are incompatible.

    很清楚,列不兼容:

    CREATE TABLE IF NOT EXISTS Geography_Dim (
        Geography_ID varchar(25) not null,
    
    CREATE TABLE Eval_Fact_Table(
    ... truncated
        Geography_ID int(11) not null,
    

    使它们具有相同的类型或删除外键约束。

    你可以阅读更多关于外键约束@​​987654321@,最有趣的部分是

    外键和被引用键中的对应列必须 具有相似的数据类型。

    在您的情况下并非如此:varchar(25)int(11)

    【讨论】:

    • 我也有这个错误,但是两列的数据类型是一样的。 NOT NULL 约束也是可兼容的。你知道,它可能是什么? @rkosegi
    • 在我的情况下,引用列和被引用列都是 varchar,但它们有不同的排序规则导致了问题。一旦我将引用列的排序规则从其 ddl 复制到引用列定义,它就起作用了。请查看此链接以了解如何使用排序规则mysqltutorial.org/mysql-collation
    • 我也是@sebinvincent 更改了表和列排序规则,并为我工作。
    【解决方案2】:

    我尝试了所有其他方法,但我发现这对我很有用。也适用于新的MySQL version(v8.0)

     CREATE TABLE Customers(
        Customers_ID INT(10) PRIMARY KEY AUTO_INCREMENT,
        Customers_name varchar(40) not null,
        Customers_phone INT(10) not null,
        Customers_email varchar(40) not null,
        date_became_customer DATE not null,
        login varchar(40) not null,
        password varchar(40) not null,
        other_details varchar(40) not null,
        Customer_Types_code int(10) not null
    REFERENCES Customer_types(Customer_Types_code));
    

    【讨论】:

      【解决方案3】:

      只需将 Geography_Dim 表上的 Geograpy_ID 更改为 Geography_ID int(11) 或将 Eval_Fact_Table 上的 Geograpy_ID 更改为 Geography_ID varchar(25) 即可解决问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-14
        • 2020-05-22
        • 2021-08-31
        • 2019-06-23
        • 1970-01-01
        • 2020-08-11
        • 2021-06-01
        相关资源
        最近更新 更多