Can not Add External Index Constraints - mysql

This is the exact error that WAMP returns when I run the child code from an external file called entries.txt
ERROR 1215 (HY000): Can not Add External Index Constraints
I need to be able to connect the parent tables to the child table so that links can be made between the tables easily.
The question states:
Create and populate a third table called entries, again using query scripts.
This table should contain foreign keys to allow sensible links to be
made with the other two tables, together with the dates of each exam.
Parent Tables:
CREATE TABLE IF NOT EXISTS students(
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
password CHAR(40) NOT NULL,
reg_date DATETIME NOT NULL,
PRIMARY KEY (student_id),
UNIQUE (email));
CREATE TABLE IF NOT EXISTS subjects(
subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
subject_name CHAR(30) NOT NULL,
level_of_entry VARCHAR(10) NOT NULL,
exam_board CHAR(20) NOT NULL,
PRIMARY KEY (subject_id));
Child Table:
CREATE TABLE IF NOT EXISTS entries(
date_of_exam DATETIME NOT NULL,
first_name VARCHAR,
middle_name VARCHAR,
last_name VARCHAR,
subject_name CHAR,
level_of_entry VARCHAR,
exam_board CHAR,
INDEX idx_first_name(first_name),
INDEX idx_middle_name(middle_name),
INDEX idx_last_name(last_name),
INDEX idx_subject_name(subject_name),
INDEX idx_level_of_entry(level_of_entry),
INDEX idx_exam_board(exam_board),
PRIMARY KEY (date_of_exam),
CONSTRAINT fk_first_name FOREIGN KEY (first_name) REFERENCES students(first_name),
CONSTRAINT fk_middle_name FOREIGN KEY (middle_name) REFERENCES students(middle_name),
CONSTRAINT fk_last_name FOREIGN KEY (last_name) REFERENCES students(last_name),
CONSTRAINT fk_subject_name FOREIGN KEY (subject_name) REFERENCES subjects(subject_name),
CONSTRAINT fk_level_of_entry FOREIGN KEY (level_of_entry) REFERENCES subjects(level_of_entry),
CONSTRAINT fk_exam_board FOREIGN KEY (exam_board) REFERENCES subjects(exam_board)
)ENGINE=InnoDB;

A foreign key in the child table must reference the primary key of the parent table only. You don't need to create clones of every attribute column of the parent tables, just the primary key column. If you need to read the other attributes, you'd write a query with a JOIN.
CREATE TABLE IF NOT EXISTS entries(
student_id INT UNSIGNED NOT NULL,
subject_id INT UNSIGNED NOT NULL,
date_of_exam DATETIME NOT NULL,
PRIMARY KEY (student_id, subject_id, date_of_exam),
CONSTRAINT fk_student FOREIGN KEY (student_id) REFERENCES students(student_id),
CONSTRAINT fk_subject FOREIGN KEY (subject_id) REFERENCES subjects(subject_id),
)ENGINE=InnoDB;

Related

Failed to add the foreign key constraint in MySQL: error 3780

I am getting the error:
Error Code: 3780. Referencing column 'category' and referenced column 'category_id' in foreign key constraint 'product_ibfk_1' are incompatible.
drop table if exists Provider;
drop table if exists Category;
drop table if exists Product;
create table Provider
(
privider_id serial not null primary key,
login_password varchar(20) not null
constraint passrule3 check(login_password sounds like '[A-Za-z0-9]{6,20}'),
fathersname varchar(20) not null,
name_of_contact_face varchar(10) not null,
surname varchar(15),
e_mail varchar(25) unique
constraint emailrule2 check(e_mail sounds like '[A-Za-z0-9]{10,10})\#gmail.com\s?')
);
create table Category
(
title varchar(20),
category_id serial not null primary key
);
create table Product
(
barecode serial not null primary key,
provider_id bigint not null,
manufacturer varchar(25) not null,
category_id bigint not null,
dimensions varchar(10) not null,
amount int not null,
date_of_registration datetime not null,
#constraint 'provider_for_product'
foreign key (provider_id) references Provider (provider_id) on delete restrict on update cascade,
foreign key (category_id) references Category (category_id) on delete restrict on update cascade
);
The datatypes of the two columns referenced in a foreign key constraint need to match
In MySQL, SERIAL is an alias for BIGINT UNSIGNED AUTO_INCREMENT.
To make a foreign key that references this column, it must be BIGINT UNSIGNED, not a signed BIGINT.
You might like to view a checklist of foreign key mistakes I contributed to: https://stackoverflow.com/a/4673775/20860
I also cover foreign key mistakes in more detail in a chapter of my book, SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming.

How to solve the errno: 150 "Foreign key constraint is incorrectly formed" even though my constraint is correct?

I am trying to create three tables :
Hotel Table
CREATE TABLE Hotels(
HotelID VARCHAR(30) NOT NULL,
Name VARCHAR(30) NOT NULL,
City VARCHAR(30) NOT NULL,
Rating VARCHAR(30) NOT NULL,
Description VARCHAR(500) NOT NULL,
PRIMARY KEY (HotelID)
)
Rooms table
CREATE TABLE Rooms (
HotelID VARCHAR(30) NOT NULL,
Room_Number INT NOT NULL,
Price_Rate INT NOT NULL,
Type VARCHAR(30) NOT NULL,
PRIMARY KEY(HotelID, Room_Number),
FOREIGN KEY(HotelID) REFERENCES hotels(HotelID)
)
BookingInfo Table
CREATE TABLE BookingInfo (
HotelID VARCHAR(30) NOT NULL,
UserID VARCHAR(30) NOT NULL,
Room_Number INT NOT NULL,
Arrival_Date DATE NOT NULL,
Departure_Date DATE NOT NULL,
PRIMARY KEY(UserID, HotelID, Room_Number, Arrival_Date),
FOREIGN KEY(HotelID) REFERENCES hotels(HotelID),
FOREIGN KEY(UserID) REFERENCES users(UserID),
FOREIGN KEY (Room_Number) REFERENCES rooms(Room_Number)
)
But when I try to create the third table I am getting errno: 150 "Foreign key constraint is incorrectly formed".
Moreover, when I delete FOREIGN KEY (Room_Number) REFERENCES rooms(Room_Number) the table gets created successfully. But I don't understand why it is not accepting FOREIGN KEY (Room_Number) REFERENCES rooms(Room_Number).
I have also tried to alter the table and change data type but still didn't get any success. Can anyone please point out my the mistake in this codes.
The primary key of Rooms is two columns: (HotelID, Room_Number).
The foreign key in BookingInfo is one column: (Room_Number).
A foreign key should reference the whole primary key of the referenced table. If that primary key has more than one column, all of the columns must be referenced by the foreign key, and in the same order.
Caveat that is not standard SQL and specific to InnoDB: InnoDB allows a foreign key to reference part of a multi-column key in the referenced table, as long as the column is the leftmost column of that key. But in your case, you are referencing the second from the left column of that key, Room_Number. So it's not allowed. And in my opinion, it's a bad idea anyway to reference part of the key, because it leads to weird data anomalies.
REFERENCES rooms(Room_Number) room_number has to be a key or the first node of a multi column key
'In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order.' https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
Thanks a lot for the guidance. This code has worked for me :
CREATE TABLE BookingInfo (
HotelID VARCHAR(30) NOT NULL,
UserID VARCHAR(30) NOT NULL,
Room_Number INT NOT NULL,
Arrival_Date DATE NOT NULL,
Departure_Date DATE NOT NULL,
PRIMARY KEY(UserID, HotelID, Room_Number, Arrival_Date),
FOREIGN KEY(UserID) REFERENCES users(UserID),
FOREIGN KEY(HotelID,Room_Number) REFERENCES rooms(HotelID,Room_Number)
)

Error Code: 1822 when data types are matching, with composite key

Getting an
Error Code: 1822. Failed to add the foreign key constraint. Missing
index for constraint 'subject_ibfk_1' in the referenced table
'enrolment'
when attempting to create the subject table. The problem is, the error does not occur on the previous table student. The data types are the same, and the primary keys are defined.
This error occurs for both the enrolment and grade tables.
create table enrolment(
stud_id char(9) not null,
subj_code char(8) not null,
semester tinyint unsigned not null,
year smallint unsigned not null,
comment text,
primary key (stud_id, subj_code, semester, year)
);
create table grade(
stud_id char(9) not null,
subj_code char(8) not null,
semester tinyint unsigned not null,
year smallint unsigned not null,
grade tinyint unsigned,
primary key (stud_id, subj_code, semester, year)
);
create table student(
stud_id char(9) not null,
stud_name char(30),
stud_phone char(12),
stud_date_of_birth date,
stud_city char(26),
stud_address char(30),
stud_postcode char(4),
primary key (stud_id),
foreign key (stud_id)
references grade(stud_id),
foreign key (stud_id)
references enrolment(stud_id)
);
create table subject(
subj_code char(8) not null,
subj_title char(40),
primary key (subj_code),
foreign key (subj_code)
references enrolment(subj_code),
foreign key (subj_code)
references grade(subj_code)
);
The problem is due to the fact that the foreign key, subj_code, is part of a multi-column primary key (PK) in the referenced table enrolment:
primary key (stud_id, subj_code, semester, year)
where this column (subj_code) is not the leftmost one.
Table student does not have this problem because its foreign key column stud_id is the leftmost column of the PK in the referenced table.
To resolve this you can create a new index for the referened column:
ALTER TABLE enrolment ADD INDEX subj_code_idx (subj_code);
Note: You have to do the same for referenced table grade in the other foreign key.
Demo here
FOREIGN KEY of one table is a PRIMARY key of another table. its a mapping key or column between two table.
First of all: you don't make subject_id as primary key in CLO table.
subject_id is act as a primary key in subject table and foreign key in clo table
ALTER TABLE `clo` DROP PRIMARY KEY, ADD PRIMARY KEY(`clo_id`);
ALTER TABLE `subject` DROP PRIMARY KEY, ADD PRIMARY KEY(`subject_id`);
ALTER TABLE clo ADD FOREIGN KEY (subject_id) REFERENCES subject(subject_id)

MySQL Foreign Keys Issue

I have 3 table: CD, Song and Song_Details which is a relationship between CD & Song:
create table Song(
ID int not null auto_increment,
Title varchar(255) not null,
Length float not null,
primary key (ID, Title)
);
create table CD(
Title varchar(255) not null,
CD_Number int not null,
primary key (Title, CD_Number)
);
Create table Song_Details(
CD_Title varchar(255) not null,
Song_Title varchar(255) not null,
Track_Number int not null,
primary key(CD_Title, Song_Title),
foreign key(CD_Title) references CD(Title),
foreign key(Song_Title) references Song(Title)
);
I have managed to find out that this line in Song_Details:
foreign key(Song_Title) references Song(Title) is throwing the Error 1215(HY000): Cannot add foreign key constraint;
Could anyone help me see based on my table, what could be causing this issue?
Two things. The auto_increment key would normally be the foreign key. Second, you need to make your reference to all the keys defined as the primary or unique key for the table (I don't advise making foreign key references to non-unique keys although MySQL does all that).
So:
create table Song (
Song_ID int not null auto_increment,
Title varchar(255) not null,
Length float not null,
primary key (ID),
unique (title)
);
create table CD (
CD_Id int auto_increment primary key,
Title varchar(255) not null,
CD_Number int not null,
unique (Title, CD_Number)
);
Create table Song_Details(
CD_ID varchar(255) not null,
Song_Id varchar(255) not null,
Track_Number int not null,
primary key(CD_ID, Song_ID),
foreign key(CD_ID) references CD(CD_ID),
foreign key(Song_ID) references Song(Song_ID)
);
Notes:
Use the primary key relationships for the foreign key definitions.
I like to have the primary keys include the table name. That way, the primary key can have the same name as the corresponding foreign keys.
Don't put the titles in more than one place. They belong in the entity tables. Autoincremented ids can then be used to access the titles.

Error Code 1022 in MySQL

I'm pretty new to database processing. I'm trying to create table
ASSIGNMENT (ProjectID, EmployeeNumber, HoursWorked)
ProjectID and EmployeeNumber are Composite Primary Keys of table ASSIGNMENT and Foreign Keys of tables PROJECT and EMPLOYEE (see below)
Here is my data:
CREATE TABLE IF NOT EXISTS ASSIGNMENT(
ProjectID Int(4) NOT NULL AUTO_INCREMENT,
EmployeeNumber Int(4) NOT NULL,
HoursWorked Int(4) NOT NULL,
PRIMARY KEY (ProjectID),
UNIQUE (EmployeeNumber),
KEY ProjectFK (ProjectID),
KEY EmployeeFK (EmployeeNumber),
CONSTRAINT ProjectFK FOREIGN KEY (ProjectID) REFERENCES PROJECT(ProjectID) ON DELETE CASCADE,
CONSTRAINT EmployeeFK FOREIGN KEY (EmployeeNumber) REFERENCES EMPLOYEE(EmployeeNumber)
);
I've seen a lot of examples like this one but what makes this instance unique is that MySQL doesn't allow two Primary Keys in one table so I made EmployeeNumber a UNIQUE key.
Here is the data for the entire schema:
CREATE SCHEMA IF NOT EXISTS WPC;
CREATE TABLE IF NOT EXISTS DEPARTMENT(
Department Char(30) NOT NULL DEFAULT 'Human Resources',
BudgetCode Int(20) NOT NULL,
OfficeNumber Int(10) NOT NULL,
Phone Char(12) NULL,
PRIMARY KEY (Department));
CREATE TABLE IF NOT EXISTS EMPLOYEE(
EmployeeNumber Int(4) NOT NULL AUTO_INCREMENT,
FirstName Char(25) NOT NULL,
LastName Char(25) NOT NULL,
Department Char(30) NOT NULL,
Phone Char(17) NULL,
Email VarChar(100) NOT NULL,
PRIMARY KEY (EmployeeNumber),
UNIQUE KEY Email (Email),
KEY DepartmentFK (Department),
CONSTRAINT DepartmentFK FOREIGN KEY (Department) REFERENCES DEPARTMENT(Department) ON DELETE RESTRICT ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS PROJECT(
ProjectID Int(4) AUTO_INCREMENT,
ProjectName Char(20) NOT NULL,
Department Char(30) NOT NULL,
MaxHours Int(14) NOT NULL DEFAULT 100,
StartDate Char(10) NOT NULL,
EndDate Char(10) NULL,
PRIMARY KEY (ProjectID),
KEY ProjectFK (Department),
CONSTRAINT ProjectFK FOREIGN KEY (Department) REFERENCES DEPARTMENT(Department) ON DELETE RESTRICT ON UPDATE CASCADE
)
ENGINE=InnoDB AUTO_INCREMENT=1000;
SET ##AUTO_INCREMENT_INCREMENT=100;
CREATE TABLE IF NOT EXISTS ASSIGNMENT(
ProjectID Int(4) NOT NULL AUTO_INCREMENT,
EmployeeNumber Int(4) NOT NULL,
HoursWorked Int(4) NOT NULL,
PRIMARY KEY (ProjectID),
UNIQUE (EmployeeNumber),
KEY ProjectFK (ProjectID),
KEY EmployeeFK (EmployeeNumber),
CONSTRAINT ProjectFK FOREIGN KEY (ProjectID) REFERENCES PROJECT(ProjectID) ON DELETE CASCADE,
CONSTRAINT EmployeeFK FOREIGN KEY (EmployeeNumber) REFERENCES EMPLOYEE(EmployeeNumber)
);
ProjectID and EmployeeNumber are composite primary keys in the table ASSIGNMENT. They are also Foreign Keys that reference tables PROJECT and EMPLOYEE.
Everything with the database is fine except for the ASSIGNMENT table. When I run the script for ASSIGNMENT I get this response:
Error Code: 1022. Can't write; duplicate key in table 'assignment'.
The names used for Foreign Key constraints must be unique in the database. You're attempting to use the same name for FKs on different tables.
You're using the name ProjectFK on the PROJECT table, and attempting to use the same name again on the ASSIGNMENT table.
Change the FK constraint name on one of the tables.