Error number: 3780 Referencing column '%s' and referenced column '%s' in foreign key constraint '%s' are incompatible

末鹿安然 提交于 2020-12-13 03:47:08

问题


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);

Error number: 3780; Symbol: ER_FK_INCOMPATIBLE_COLUMNS; SQLSTATE: HY000

Message:Error Code: 3780. Referencing column 'Geography_ID' and referenced column 'Geography_ID' in foreign key constraint 'eval_fact_table_ibfk_3' are incompatible.


回答1:


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

is quite clear, columns 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,

Make them of same type or remove foreign key constraint.

You can read more about foreign key constraints in documentation, most interesting part is

Corresponding columns in the foreign key and the referenced key must have similar data types.

That is not true in your case : varchar(25) vs. int(11)




回答2:


Just change Geograpy_ID on Geography_Dim table to Geography_ID int(11) or change Geograpy_ID on Eval_Fact_Table to Geography_ID varchar(25) to solve the problem.




回答3:


I tried all other ways but I found this useful as it worked for me. It is applicable also to the new 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));


来源:https://stackoverflow.com/questions/61293224/error-number-3780-referencing-column-s-and-referenced-column-s-in-foreign

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!