Error Code: 1215. Cannot add foreign key constraint - mysql

Can someone tell me why I am getting this error message? The error pops up when I am trying to create the RENTAL table.
CREATE TABLE CAR_CLASS
(CAR_CLASS_ID INT(3) PRIMARY KEY,
CAR_CLASS CHAR(20),
RENTAL_RATE DECIMAL(4,2) );
CREATE TABLE CAR
(CAR_ID CHAR(25) PRIMARY KEY,
CAR_CLASS_ID INT(3),
CAR_COLOR CHAR(20),
FOREIGN KEY (CAR_CLASS_ID) REFERENCES CAR_CLASS(CAR_CLASS_ID) );
CREATE TABLE CUSTOMER_INFO
(CUSTOMER_ID CHAR(30) PRIMARY KEY,
CUSTOMER_FIRST CHAR(30),
CUSTOMER_LAST CHAR(30),
CUSTOMER_CC_NUMBER CHAR(16));
CREATE TABLE RENTAL
(RENTAL_ID INT(3) PRIMARY KEY,
RENTAL_DATE_OUT DATE,
RENTAL_DATE_IN DATE,
CAR_CLASS_ID INT(3),
CAR_ID CHAR(25),
CUSTOMER_ID CHAR(30),
FOREIGN KEY (CAR_CLASS_ID) REFERENCES CAR_CLASS(CAR_CLASS_ID),
FOREIGN KEY (CAR_ID) REFERENCES CAR(CAR_ID),
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER(CUSTOMER_ID) );

Your other table is called CUSTOMER_INFO, while your foreign key references just CUSTOMER. Change your last CREATE TABLE to this:
CREATE TABLE RENTAL
(RENTAL_ID INT(3) PRIMARY KEY,
RENTAL_DATE_OUT DATE,
RENTAL_DATE_IN DATE,
CAR_CLASS_ID INT(3),
CAR_ID CHAR(25),
CUSTOMER_ID CHAR(30),
FOREIGN KEY (CAR_CLASS_ID) REFERENCES CAR_CLASS(CAR_CLASS_ID),
FOREIGN KEY (CAR_ID) REFERENCES CAR(CAR_ID),
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER_INFO(CUSTOMER_ID) );

the name of the table is wrong while creating a RENTAL table.
Look at the last line of your code.
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER(CUSTOMER_ID).
instead of customer, it should be CUSTOMER_INFO.
All the best :)

Related

Can't create table `cms`.`invoice` (errno: 150 "Foreign key constraint is incorrectly formed")

What did I do wrong in the INVOICE table?
This is the report :
2021-01-25 13:49:23 0x6820 Error in foreign key constraint of table hmsarea.invoice:
FOREIGN KEY (VISIT_ID)
REFERENCES VISIT(VISIT_ID),
CONSTRAINT FK_INVOICEAPT FOREIGN KEY (APPOINTMENT_ID)
REFERENCES APPOINTMENT(APPOINTMENT_ID),
CONSTRAINT FK_INVOICETRT FOREIGN KEY (TREATMENT_ID)
REFERENCES TREATMENT(TREATMENT_ID)
):
CREATE TABLE VISIT (
VISIT_ID INT,
PATIENT_ID INT,
VISIT_DATE VARCHAR(50),
CONSTRAINT FK_V1 FOREIGN KEY (DOCTOR_ID)
REFERENCES DOCTOR(DOCTOR_ID),
CONSTRAINT FK_V2 FOREIGN KEY (PATIENT_ID)
REFERENCES PATIENT(PATIENT_ID)
);
CREATE TABLE APPOINTMENT (
APPOINTMENT_ID INT,
APPOINTMENT_DATE VARCHAR(50),
PATIENT_ID INT,
PRIMARY KEY (APPOINTMENT_ID),
CONSTRAINT FK_APP1 FOREIGN KEY (DOCTOR_ID)
REFERENCES DOCTOR(DOCTOR_ID),
CONSTRAINT FK_APP2 FOREIGN KEY (PATIENT_ID)
REFERENCES PATIENT(PATIENT_ID)
);
CREATE TABLE TREATMENT (
TREATMENT_ID INT,
TREATMENT_TYPE VARCHAR(100),
PATIENT_ID INT,
TREATMENT_DATE VARCHAR(50),
PRIMARY KEY (TREATMENT_ID),
CONSTRAINT FK_TR1 FOREIGN KEY (DOCTOR_ID)
REFERENCES DOCTOR(DOCTOR_ID),
CONSTRAINT FK_TR2 FOREIGN KEY (PATIENT_ID)
REFERENCES PATIENT(PATIENT_ID)
);
CREATE TABLE INVOICE (
INVOICE_NO INT,
INVOICE_TOTAL DECIMAL(5,2) NOT NULL,
INVOICE_DATE VARCHAR(25),
PRESCRIPTION_ID INT,
VISIT_ID INT,
APPOINTMENT_ID INT NULL,
TREATMENT_ID INT NULL,
PATIENT_INT INT,
PATIENT_FIRSTNAME VARCHAR(25),
PRIMARY KEY (INVOICE_NO),
CONSTRAINT FK_INVOICE1 FOREIGN KEY (VISIT_ID)
REFERENCES VISIT(VISIT_ID),
CONSTRAINT FK_INVOICE2 FOREIGN KEY (APPOINTMENT_ID)
REFERENCES APPOINTMENT(APPOINTMENT_ID),
CONSTRAINT FK_INVOICE3 FOREIGN KEY (TREATMENT_ID)
REFERENCES TREATMENT(TREATMENT_ID)
);
fiddle
Failed to add the foreign key constraint. Missing index for constraint 'FK_INVOICEVISIT' in the referenced table 'VISIT'
VISIT.VISIT_ID is not indexed.
Key column 'PATIENT_ID' doesn't exist in table
The column name is PATIENT_INT.
All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead
RECEIPT_NO is defined as NULL explicitly.

MySQL- [HY000][1005] Can't create table 'dbc18b3536647.results' (errno: 150)

I am having problem with one of my table creation for my database. The specific table that has the error is 'results',
CREATE TABLE results (
ranking INTEGER,
team_name VARCHAR(100),
driver_name VARCHAR(100),
CONSTRAINT pk_results
PRIMARY KEY (ranking, team_name, driver_name),
CONSTRAINT fk_results_team_name
FOREIGN KEY (team_name)
REFERENCES teams(name),
CONSTRAINT fk_results_driver_name
FOREIGN KEY (driver_name)
REFERENCES drivers(name)
);
and I know that it has the problem with only this part:
CONSTRAINT fk_results_driver_name
FOREIGN KEY (driver_name)
REFERENCES drivers(name)
I have not inserted any data in my database, however all the other foreign keys worked just fine, except for this one. Please help me inspect the code. The 'result' table is at the very end of my code.
This is all of my code for the program. Thanks in advance!
CREATE TABLE seasons (
year INTEGER,
CONSTRAINT pk_seasons_year
PRIMARY KEY (year)
);
CREATE TABLE tracks (
name VARCHAR(100),
city VARCHAR(100),
length INTEGER,
num_of_turns INTEGER,
CONSTRAINT pk_tracks_name
PRIMARY KEY (name)
);
CREATE TABLE teams (
name VARCHAR(100),
date_created DATE, -- YYYY-MM-DD
nationality VARCHAR(100),
website VARCHAR(150),
CONSTRAINT pk_teams_name
PRIMARY KEY (name)
);
CREATE TABLE races (
id INTEGER,
season_year INTEGER,
date DATE,
laps INTEGER,
teams_line_up VARCHAR(100),
teams_name_classification VARCHAR(100),
ranking_classification INTEGER,
CONSTRAINT pk_races
PRIMARY KEY (id, season_year, teams_line_up, teams_name_classification,
ranking_classification),
CONSTRAINT fk_races_lineup
FOREIGN KEY (teams_line_up)
REFERENCES teams(name),
CONSTRAINT fk_races_teamname_classification
FOREIGN KEY (teams_name_classification)
REFERENCES teams(name)
);
CREATE TABLE cars (
model VARCHAR(100),
height INTEGER,
length INTEGER,
width INTEGER,
weight INTEGER,
CONSTRAINT pk_cars_model
PRIMARY KEY (model)
);
CREATE TABLE sponsors (
name VARCHAR(100),
website VARCHAR(150),
address VARCHAR(200),
team_sponsored VARCHAR(50),
CONSTRAINT pk_sponsors_name
PRIMARY KEY (name),
CONSTRAINT fk_sponsors_team_sponsored
FOREIGN KEY (team_sponsored)
REFERENCES teams (name)
);
CREATE TABLE members (
ssn INTEGER,
name VARCHAR(100),
nationality VARCHAR(100),
team_name VARCHAR(100),
season_year INTEGER,
CONSTRAINT pk_members
PRIMARY KEY (ssn, team_name),
CONSTRAINT fk_members_team_name
FOREIGN KEY (team_name)
REFERENCES teams(name),
CONSTRAINT fk_members_season
FOREIGN KEY (season_year)
REFERENCES seasons(year)
);
CREATE TABLE team_chief (
ssn INTEGER,
name VARCHAR(100),
nationality VARCHAR(100),
team_name VARCHAR(100),
season_year INTEGER,
CONSTRAINT pk_team_chief
PRIMARY KEY (ssn, team_name),
CONSTRAINT fk_team_chief_ssn
FOREIGN KEY (ssn)
REFERENCES members(ssn),
CONSTRAINT fk_team_chief_name
FOREIGN KEY (team_name)
REFERENCES teams(name),
CONSTRAINT fk_team_chief_season
FOREIGN KEY (season_year)
REFERENCES seasons(year)
);
CREATE TABLE drivers (
ssn INTEGER,
name VARCHAR(100),
nationality VARCHAR(100),
team_name VARCHAR(100),
season_year INTEGER,
CONSTRAINT pk_drivers
PRIMARY KEY (ssn, name, team_name),
CONSTRAINT fk_drivers_ssn
FOREIGN KEY (ssn)
REFERENCES members(ssn),
CONSTRAINT fk_drivers_name
FOREIGN KEY (team_name)
REFERENCES teams(name),
CONSTRAINT fk_drivers_season
FOREIGN KEY (season_year)
REFERENCES seasons(year)
);
CREATE TABLE head_drivers (
ssn INTEGER,
name VARCHAR(100),
nationality VARCHAR(100),
team_name VARCHAR(100),
season_year INTEGER,
CONSTRAINT pk_head_drivers
PRIMARY KEY (ssn, team_name),
CONSTRAINT fk_head_drivers_ssn
FOREIGN KEY (ssn)
REFERENCES members(ssn),
CONSTRAINT fk_head_drivers_name
FOREIGN KEY (team_name)
REFERENCES teams(name),
CONSTRAINT fk_head_drivers_season
FOREIGN KEY (season_year)
REFERENCES seasons(year)
);
CREATE TABLE reserve_drivers (
ssn INTEGER,
name VARCHAR(100),
nationality VARCHAR(100),
team_name VARCHAR(100),
season_year INTEGER,
CONSTRAINT pk_reserve_drivers
PRIMARY KEY (ssn, team_name),
CONSTRAINT fk_reserve_drivers_ssn
FOREIGN KEY (ssn)
REFERENCES members(ssn),
CONSTRAINT fk_reserve_drivers_name
FOREIGN KEY (team_name)
REFERENCES teams(name),
CONSTRAINT fk_reserve_drivers_season
FOREIGN KEY (season_year)
REFERENCES seasons(year)
);
CREATE TABLE results (
ranking INTEGER,
team_name VARCHAR(100),
driver_name VARCHAR(100),
CONSTRAINT pk_results
PRIMARY KEY (ranking, team_name, driver_name),
CONSTRAINT fk_results_team_name
FOREIGN KEY (team_name)
REFERENCES teams(name),
CONSTRAINT fk_results_driver_name
FOREIGN KEY (driver_name)
REFERENCES drivers(name)
);
The column that you reference in a foreign key has to be indexed. So you need to add an index on the name column in the drivers table:
ALTER TABLE drivers ADD INDEX (name);
Note that while it's not a strict requirement, foreign keys usually reference the primary key of the other table.

Failed to add the foreign key constraint. Missing index for constraint Error Code: 1822

So I'm trying to create a table 'Prerequisite_to', which is basically a relation that shows what classes are considered as a prerequisite for a specific class. Here are my SQL tables defections:
CREATE TABLE Class(
infs CHAR(4) NOT NULL,
course_number CHAR(3) NOT NULL,
PRIMARY KEY (infs,course_number));
CREATE TABLE Prerequisite_to(
infs CHAR(4),
course_number CHAR(3),
PRIMARY KEY (infs,course_number),
FOREIGN KEY (infs) REFERENCES Class(infs),
FOREIGN KEY (course_number) REFERENCES Class(course_number)
)
However, when I execute the script I get this error:
17:29:43 CREATE TABLE Prerequisite_to( infs CHAR(4), course_number CHAR(3), PRIMARY KEY (infs,course_number), FOREIGN KEY (infs) REFERENCES Class(infs), FOREIGN KEY (course_number) REFERENCES Class(course_number) ) Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'prerequisite_to_ibfk_2' in the referenced table 'Class' 0.00038 sec
You have a composite primary key, so you need a composite foreign key:
CREATE TABLE Prerequisite_to(
infs CHAR(4),
course_number CHAR(3),
PRIMARY KEY (infs,course_number),
FOREIGN KEY (infs, course_number) REFERENCES Class(infs, course_number)
);
Just for the record, I'm not a fan of composite primary keys. I also think prerequisites need two course references. So:
CREATE TABLE Classes (
class_id int auto_increment primary key,
infs CHAR(4) NOT NULL,
course_number CHAR(3) NOT NULL,
unique (infs, course_number)
);
CREATE TABLE Prerequisites (
preresequisite_id int auto_increment primary key,
class_id int,
prerequisite_class_id int,
FOREIGN KEY (class_id) REFERENCES Classes(class_id),
FOREIGN KEY (prerequisite_class_id) REFERENCES Classes(class_id)
);

Cannot add foreign key constraint MySQL Workbench

I'm getting "Error Code: 1215. Cannot add foreign key constraint" when trying to create the "orders" table.
credit_cards table:
CREATE TABLE credit_cards (
customer VARCHAR(30),
card_no CHAR(16),
PRIMARY KEY (customer, card_no),
FOREIGN KEY (customer) REFERENCES customers(username));
orders table:
CREATE TABLE orders (
order_no INT AUTO_INCREMENT,
customer VARCHAR(30) NOT NULL,
date_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
credit_card CHAR(16) NOT NULL,
PRIMARY KEY (order_no),
FOREIGN KEY (customer)
REFERENCES customers (username),
FOREIGN KEY (credit_card)
REFERENCES credit_cards (card_no));
The report from SHOW ENGINE INNODB STATUS says that the problem is FOREIGN KEY (credit_card) REFERENCES credit_cards(card_no))
I've read a bunch of resolved questions and still can't figure it out. I'm using MySQL Workbench. Thanks.
You need to reference all the keys in a primary key (or unique key) relationship. I would recommend:
CREATE TABLE credit_cards (
credit_card_id int auto_increment primary key,
customer VARCHAR(30),
card_no CHAR(16),
UNIQUE KEY (customer, card_no),
FOREIGN KEY (customer) REFERENCES customers(username));
orders table:
CREATE TABLE orders (
order_no INT AUTO_INCREMENT,
customer VARCHAR(30) NOT NULL,
date_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
credit_card_id int NOT NULL,
PRIMARY KEY (order_no),
FOREIGN KEY (customer)
REFERENCES customers (username),
FOREIGN KEY (credit_card_id)
REFERENCES credit_cards (credit_card_id));

Error 150 : table couldn't be created

Where am I going wrong? I get the error 150 debit_card table couldn't be created. Something to do the foreign keys i know, just don't know what exactly. Help?
create table customer(
cust_id int auto_increment,
first_name varchar(25),
last_name varchar(25),
street varchar(25),
city varchar(25),
state char(2),
zip varchar(10),
CONSTRAINT customer_pk primary key(cust_id)
);
create table card_account(
acct_no char(16),
exp_date date,
card_type ENUM("Debit","Credit") not null,
CONSTRAINT card_account_pk primary key(acct_no,exp_date)
);
create table debit_card(
bank_no char(9) not null,
acct_no char(16),
exp_date date,
CONSTRAINT debit_card_pk primary key(acct_no,exp_date),
CONSTRAINT debit_card_acct_no_fk foreign key(acct_no) references card_account(acct_no) on delete cascade,
CONSTRAINT debit_card_exp_date_fk foreign key(exp_date) references card_account(exp_date) on delete cascade
);
Your card_account table has a composite primary key. You cannot use those columns separately in a foreign key constraint.
But you can add a foreign key referencing to the composite primary key:
create table debit_card(
bank_no char(9) not null,
acct_no char(16),
exp_date date,
CONSTRAINT debit_card_pk primary key(acct_no,exp_date),
CONSTRAINT debit_card_acct_no_fk foreign key(acct_no,exp_date) references card_account(acct_no,exp_date) on delete cascade
);