#1072 - Key column 'Customer_ID' doesn't exist in table - mysql

This is the parent table
CREATE TABLE Customer(
Customer_ID INT(5) not null,
CustName VARCHAR(50) not null,
CustSurname VARCHAR(50) not null,
CustEmail VARCHAR(100) unique not null,
CustMobileNo INT(12) not null,
HomeAddress VARCHAR(255) not null,
Password VARCHAR(10) not null,
constraint c_cuid_pk PRIMARY KEY (Customer_ID))
ENGINE = InnoDB;
When I try to create the property table it shows me
"#1072 - Key column 'Customer_ID' doesn't exist in table"
this is the child table
CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;

In table Property, you must define a column named Customer_ID (well it could be named anything!), then you define the FOREIGN KEY on it.
Your code currently tries to link IndificualCustomer(Customer_ID) to Property(Customer_ID), but Property(Customer_ID) does not exist.
Declaring the constraint just links the columns, but it does not create them.
CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
Customer_ID INT(5) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;

In the first place, you should add a column: Customer_ID to your Property table to create your foreign key constraint:
Property.Customer_ID → IndividualCustomer.Customer_ID
You can not make a constraint on something that does not exist.
Or change the +++ +++ part of your table creation query to a column that exists.
constraint p_cuid_fk FOREIGN KEY (+++Customer_ID+++) --to be changed accordingly
references IndividualCustomer(Customer_ID)
Example: adding the Customer_ID column to your table declaration would give you the following code
CREATE TABLE Property(
Property_ID INT(5) not null,
Customer_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;

CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
Customer_ID INT(5) not null,
PRIMARY KEY (Property_ID),
KEY p_cuid_fk (Customer_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references Customer(Customer_ID))
ENGINE = InnoDB;
You have to add Customer_ID in the table PROPERTY.

Related

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

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.
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)
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));
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.
Error number: 3780 is due to incompatible foreign keys, the datatype of foreign key in each table must be same .
You could also not edit the primary key column in one table if it is a foreign key in another table.
For editing first drop the foreign key constraint in child table and then edit the primary key in the parent table and then again add constraints to child table.
HERE: Geography_ID varchar(25) vs Geography_ID int(11) are incompatible
First drop or alter this
FOREIGN KEY (Geography_ID) REFERENCES Geography_Dim (Geography_ID)
Geography_ID int(11) NOT NULL, and then add new column:
FOREIGN KEY (Geography_ID) REFERENCES Geography_Dim (Geography_ID)
with Geography_ID varchar(25)

Cannot add foreign key constraint for mysql

CREATE TABLE EMPLOYEE
(enum VARCHAR(5) NOT NULL,
first_name VARCHAR(30) NOT NULL,
Last_name VARCHAR(30) NOT NULL,
Salary decimal(8,2) NOT NULL,
CONSTRAINT EMPLOYEE_pkey PRIMARY KEY(enum));
CREATE TABLE LECTURER
(enum VARCHAR(5) NOT NULL,
lecturerNum CHAR(10) NOT NULL,
ranK VARCHAR(2) NOT NULL,
bioDate VARCHAR(200) NOT NULL,
CONSTRAINT pk_enum primary key(enum),
CONSTRAINT LECTURER_fkey FOREIGN KEY (enum) REFERENCES EMPLOYEE(enum),
CONSTRAINT lect_rank CHECK(rank in('L1'or'L2'or'L3')));
CREATE TABLE MANAGER
(enum VARCHAR(5) NOT NULL,
role VARCHAR(10) NOT NULL,
CONSTRAINT pk_enum primary key(enum),
CONSTRAINT MANAGER_fkey FOREIGN KEY(enum)
REFERENCES EMPLOYEE(enum) ON DELETE CASCADE);
CREATE TABLE SUBJECT
(subjectCode CHAR(10) NOT NULL,
SubjectName CHAR(30) NOT NULL,
credit decimal(3,3) NOT NULL,
taughtBy char(10) NOT NULL,
CONSTRAINT SUBJECT_PK PRIMARY KEY(subjectCode),
CONSTRAINT SUBJECT_fkey FOREIGN KEY(taughtBy)
REFERENCES LECTURER(lecturerNum) ON DELETE CASCADE,
CONSTRAINT CREDIT_CHECK CHECK(credit >0));
I did not find any error on my create table, why when I run it at the subject table it says:
ERROR 1215 (HY000): Cannot add foreign key constraint
Why..... can anyone help me with it?
LECTURER.lecturerNum does not have an index, which is required to be able to use it in a FK constraint:
CREATE TABLE LECTURER
(enum VARCHAR(5) NOT NULL,
lecturerNum CHAR(10) NOT NULL,
ranK VARCHAR(2) NOT NULL,
bioDate VARCHAR(200) NOT NULL,
KEY lecturerNum (lecturerNum),
CONSTRAINT pk_enum primary key(enum),
CONSTRAINT LECTURER_fkey FOREIGN KEY (enum) REFERENCES EMPLOYEE(enum),
CONSTRAINT lect_rank CHECK(rank in('L1'or'L2'or'L3')));

Why do I keep getting a 1215 cannot add foreign key constraint error?

USE ccva1;
CREATE TABLE art_piece(
art_inventory_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
artist_id INT(11) NOT NULL,
gallery_id INT(11) NOT NULL,
art_collection VARCHAR(4) NOT NULL,
art_genre VARCHAR(50)NOT NULL,
art_title VARCHAR(50)NOT NULL,
art_medium VARCHAR(50)NOT NULL,
art_acquired VARCHAR(200)NOT NULL,
art_date_acquired INT(10)NOT NULL,
art_appraisal_value VARCHAR(20),
art_size_inches VARCHAR(65),
art_size_centimeters VARCHAR(50),
art_image VARCHAR(500)NOT NULL,
art_modified_by VARCHAR(30) NOT NULL,
art_modified_date VARCHAR(30),
INDEX art_piece_ind(artist_id),
FOREIGN KEY (artist_id)
REFERENCES artist(artist_id)
ON DELETE CASCADE,
INDEX art_piece1_ind(gallery_id),
FOREIGN KEY (gallery_id)
REFERENCES gallery(gallery_id)
ON DELETE CASCADE,
INDEX art_piece2_ind(artist_id),
FOREIGN KEY (art_modified_by)
REFERENCES admin(admin_user_name)
ON DELETE CASCADE )
ENGINE = INNODB;
CREATE TABLE art_exhibition(exhibition_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
gallery_id INT(11) NOT NULL,
exhibition_name VARCHAR(200) NOT NULL,
exhibition_date VARCHAR(60) NOT NULL,
exhibition_time VARCHAR(30) NOT NULL,
exhibition_description VARCHAR(3000) NOT NULL,
exhibition_image VARCHAR(65) NOT NULL,
exhibition_modified_by VARCHAR(30)NOT NULL,
exhibition_modified_date VARCHAR(30),
INDEX gal_ind(gallery_id),
FOREIGN KEY (gallery_id)
REFERENCES gallery(gallery_id),
INDEX artist_ind(artist_id),
FOREIGN KEY (artist_id)
REFERENCES artist(artist_id),
CONSTRAINT admin_unique UNIQUE (admin_user_name)
) ENGINE = INNODB;
You don't have a column called artist_id in art_exhibition.
Assuming you have a table called artist, define a column with the appropriate type, which is presumably int, before defining the foreign key constraint.

MySQL Workbench won't allow me to create foreign keys

I'm trying to create a few tables and one of them has should have foreign keys referencing the other tables, but MySQL Workbench keeps giving me "Error Code: 1215. Cannot add foreign key constraint". This happens if I try to create them during the table creation and if I just create the table and then try to add FK through ALTER. I just can't figure out the problem. I've tried both with and without ENGINE = InnoDB that I saw some people suggest on the web. And yes, tables kommune and person has been created.
CREATE TABLE kommune (
Kommunenr varchar(4) NOT NULL,
Kommunenavn varchar(45) NOT NULL,
PRIMARY KEY (Kommunenr));
CREATE TABLE person (
PersonID varchar(4) NOT NULL,
Fornavn varchar(45) NOT NULL,
Etternavn varchar(45) NOT NULL,
Postnr varchar(4) NOT NULL,
Poststed varchar(45) NOT NULL,
PRIMARY KEY (PersonID));
CREATE TABLE oppdrag (
Oppdragsnr varchar(5) NOT NULL,
Eiendomnr varchar(4) NOT NULL,
Gateadresse varchar(45) NOT NULL,
Postnr varchar(4) NOT NULL,
Poststed varchar(45) NOT NULL,
Kommunenr varchar(4) NOT NULL,
Prisantydning varchar(10) NOT NULL,
Solgt boolean NOT NULL,
PRIMARY KEY (Oppdragsnr),
FOREIGN KEY (Postnr) REFERENCES person(Postnr),
FOREIGN KEY (Poststed) REFERENCES person(Poststed),
FOREIGN KEY (Kommunenr) REFERENCES kommune(Kommunenr));
Check the following lines:
FOREIGN KEY (Postnr) REFERENCES person(Postnr),
FOREIGN KEY (Poststed) REFERENCES person(Poststed),
but in your table structure:
CREATE TABLE person (
PersonID varchar(4) NOT NULL,
Fornavn varchar(45) NOT NULL,
Etternavn varchar(45) NOT NULL,
Postnr varchar(4) NOT NULL,
Poststed varchar(45) NOT NULL,
PRIMARY KEY (PersonID));
Postnr, Poststed are neither unique or not primary key. To make foreign key, the referring column in the base table must be an indexed column

Mysql foreign key constraint is incorrectly formed?

I am receiving an error when attempting to create some tables in mysql with the foreign key
CREATE TABLE session (
code CHAR(2) NOT NULL,
date DATE,
room VARCHAR(30) NULL,
CONSTRAINT session_pk PRIMARY KEY (date),
CONSTRAINT session_fk FOREIGN KEY (code)
REFERENCES module(code));
CREATE TABLE module (
code CHAR(2) NOT NULL,
name VARCHAR(30) NOT NULL,
cost DECIMAL(8,2) NOT NULL,
credits TINYINT NOT NULL,
course_code CHAR(3) NOT NULL,
CONSTRAINT module_pk PRIMARY KEY (code));
Here are the two tables I am trying to create, the syntax I've used matches w3 schools and both data types are the same so I cannot see how this is incorrect, any help would be appreciated thanks :)
You're trying to create a foreign key on table before creating the referencing table.
Interchanging the order of query will work :
CREATE TABLE module (
`code` CHAR(2) NOT NULL,
name VARCHAR(30) NOT NULL,
cost DECIMAL(8,2) NOT NULL,
credits TINYINT NOT NULL,
course_code CHAR(3) NOT NULL,
CONSTRAINT module_pk PRIMARY KEY (`code`));
CREATE TABLE `session` (
`code` CHAR(2) NOT NULL,
`date` DATE,
room VARCHAR(30) NULL,
CONSTRAINT session_pk PRIMARY KEY (`date`),
CONSTRAINT session_fk FOREIGN KEY (`code`)
REFERENCES module(`code`));
Try this
CREATE TABLE module (
code CHAR(2) NOT NULL,
name VARCHAR(30) NOT NULL,
cost DECIMAL(8,2) NOT NULL,
credits TINYINT NOT NULL,
course_code CHAR(3) NOT NULL,
CONSTRAINT module_pk PRIMARY KEY (code));
CREATE TABLE session (
code CHAR(2) NOT NULL,
date DATE,
room VARCHAR(30) NULL,
CONSTRAINT session_pk PRIMARY KEY (date),
CONSTRAINT session_fk FOREIGN KEY (code)
REFERENCES module(code));