SQL - Foreign key constraint is incorrectly formed - mysql

I don't know what's the problem in this code... I verified all columns names and data types but this not work
CREATE TABLE empleado (
tipo_dni VARCHAR(50) NOT NULL,
nro_dni INT NOT NULL,
nombre VARCHAR(50) NOT NULL,
apellido VARCHAR(50) NOT NULL,
direccion VARCHAR(50) NOT NULL,
telefono INT NOT NULL,
id_ciudad INT NOT NULL,
PRIMARY KEY (tipo_dni , nro_dni)
);
CREATE TABLE director (
tipo_dni VARCHAR(50) NOT NULL,
nro_dni INT NOT NULL,
PRIMARY KEY (tipo_dni, nro_dni),
FOREIGN KEY (tipo_dni)
REFERENCES empleado (tipo_dni),
FOREIGN KEY (nro_dni)
REFERENCES empleado (nro_dni)
);
ERROR
#1005 - Can't create table `tpfinal`.`director` (errno: 150 "Foreign key constraint is incorrectly formed")
Any idea ?

You primary key is:
PRIMARY KEY (tipo_dni , nro_dni)
This is a composite primary key. The foreign key reference should be composite as well:
FOREIGN KEY (tipo_dni, nro_dni)
REFERENCES empleado (tipo_dni, nro_dni)

Related

Why do I have error code:1822 when my foreign key index matches the primary key in the table it is being referenced? [duplicate]

This question already has answers here:
MySQL Creating tables with Foreign Keys giving errno: 150
(20 answers)
Closed 1 year ago.
create table item
(
isbn varchar(25) not null,
title varchar(150) not null,
publisher_name varchar(50) not null,
classification_code varchar(10) not null,
format_type char(2),
constraint item_pk primary key(isbn)
);
create table copy
(
isbn varchar(25) not null,
copy_id int not null,
acquired_date not null,
constraint copy_pk primary key(isbn, copy_id),
constraint copy_fk foreign key(isbn) references item(isbn)
);
create table borrow (
isbn varchar(25) not null,
copy_id int not null,
user_id varchar(25) not null,
borrowed_datetime datetime not null,
returned_datetime datetime not null,
constraint borrow_pk primary key (isbn, copy_id, user_id, borrowed_datetime),
constraint borrow_fk_1 foreign key(isbn) references copy(isbn),
constraint borrow_fk_2 foreign key(copy_id) references copy(copy_id),
);
So this is my code here from MySQL and every time I try to run it, only Tables item and copy is created. Table borrow is not created due to "Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'borrow_fk_2' in the referenced table 'copy'".
My search engine is InnoDB.
To reference a compound primary key, you must declare a foreign key with the same number of columns as the primary key, and in the same order. In your case, you need the foreign key to be (isbn, copy_id) like this:
create table borrow (
isbn varchar(25) not null,
copy_id int not null,
user_id varchar(25) not null,
borrowed_datetime datetime not null,
returned_datetime datetime not null,
constraint borrow_pk primary key (isbn, copy_id, user_id, borrowed_datetime),
constraint borrow_fk_1 foreign key(isbn, copy_id) references copy(isbn, copy_id)
);

Cannot add foreign key here?

So I'm trying to create tables and I can't for the life of me understand why i keep getting and error saying "Cannot add foreign key to constraint".
The types are the same, the parent is a primary key, and they're NULLness is the same.
The problem is in the line in the create table for CDSingers where it says:
foreign key (track_num) references CDTracks (track_num),
(it's near the end)
It's the only table that won't be created and it's because of this line.
Please help.
(some of the other tables have been ommitted since they aren't connected)
create table CD
(
num int NOT NULL,
producer varchar(100) NOT NULL,
cd_number varchar(100) NOT NULL,
title varchar(100) NOT NULL,
type varchar(100) ,
band_name varchar(100) ,
production_date DATE NOT NULL,
price double CHECK (price >= 0),
foreign key (type) references MusicType (type),
foreign key (band_name) references Band (band_name),
primary key (num),
unique (producer, cd_number)
);
create table CDTracks
(
num int NOT NULL,
track_num int NOT NULL,
song_name varchar(100) NOT NULL,
minute int NOT NULL,
foreign key (num) references CD (num),
primary key (num, track_num)
);
create table Singer
(
id int NOT NULL,
singer_firstname varchar(100) NOT NULL,
singer_lastname varchar(100) NOT NULL,
primary key (id)
);
create table CDSingers
(
num int NOT NULL,
track_num int NOT NULL,
singer_id int NOT NULL,
foreign key (num) references CDTracks (num),
foreign key (track_num) references CDTracks (track_num),
foreign key (singer_id) references Singer (id),
primary key (num, track_num, singer_id)
);
You declare composite foreign keys like this:
foreign key (num, track_num) references CDTracks (num, track_num),

Error Code 1215. Cannot add foreign key constraint for my tables

I'm trying to write a script for SQL for adding 2 tables and adding another table that references both tables through a foreign key. I keep getting an error from the 'Enrolls' table. It says foreign key cannot be create.
Here are the tables.
CREATE TABLE IF NOT EXISTS `homework7`.`Section` (
`CourseNo` INT NOT NULL,
`SectionNo` INT NOT NULL,
`Instructor` VARCHAR(45) NULL,
PRIMARY KEY (`CourseNo`, `SectionNo`),
FOREIGN KEY (`CourseNo`) REFERENCES Course(`CourseNo`));
CREATE TABLE IF NOT EXISTS `homework7`.`Student` (
`SSN` INT NOT NULL,
`FirstName` VARCHAR(45) NULL,
`LastName` VARCHAR(45) NULL,
`Street` VARCHAR(45) NULL,
`City` VARCHAR(45) NULL,
`State` VARCHAR(2) NULL,
`Zip` INT NULL,
PRIMARY KEY (`SSN`));
Here's the one I'm having trouble with.
CREATE TABLE IF NOT EXISTS `homework7`.`Enrolls` (
`SSN` INT NOT NULL,
`CourseNo` Int NOT NULL,
`SectionNo` INT NOT NULL,
PRIMARY KEY (`SSN`, `SectionNo`, `CourseNo`),
FOREIGN KEY (`SSN`) REFERENCES Student(`SSN`),
FOREIGN KEY (`CourseNo`) REFERENCES Section(`CourseNo`),
FOREIGN KEY (`SectionNo`) REFERENCES Section(`SectionNo`));
Also the schema is here.
http://imgur.com/a/fTg5O
So should
Enrolls (CourseNo) reference Course (CourseNo) or Section (CourseNo)?
foreign key must be primary key of other table so for the trouble portion u can use Course(CourseNo) instead of Section(CourseNo) and liff also mention that
CREATE TABLE IF NOT EXISTS `homework7`.`Enrolls` (
`SSN` INT NOT NULL,
`CourseNo` Int NOT NULL,
`SectionNo` INT NOT NULL,
PRIMARY KEY (`SSN`, `SectionNo`, `CourseNo`),
FOREIGN KEY (`SSN`) REFERENCES Student(`SSN`),
FOREIGN KEY (`CourseNo`) REFERENCES Course(`CourseNo`),
FOREIGN KEY (`SectionNo`) REFERENCES Section(`SectionNo`));
Try:
FOREIGN KEY (`CourseNo`, `SectionNo`) REFERENCES Section(`CourseNo`, `SectionNo`)
Foreign keys must reference fields indexed in the table referenced.

Cannot create interconnecting table

i'm trying to create a table with columns that reference toward other tables.
How do i make the foreign keys?
Scheme:
Query: (not working):
CREATE TABLE gebruikers_trainingen (
gebruiker_id INT UNSIGNED NOT NULL,
training_id INT UNSIGNED NOT NULL,
gebruiker_naam VARCHAR(255) NOT NULL,
training_naam VARCHAR(255),
CONSTRAINT fk_idGebruiker FOREIGN KEY (gebruiker_id)
REFERENCES gebruikers(id),
CONSTRAINT fk_idTraining FOREIGN KEY (training_id)
REFERENCES trainingen(id),
CONSTRAINT fk_naamGebruiker FOREIGN KEY (gebruiker_naam)
REFERENCES gebruikers(voornaam),
CONSTRAINT fk_naamTraining FOREIGN KEY (training_naam)
REFERENCES trainingen(naam)
) ENGINE = INNODB;
Getting:
Error Code: 1005 Can't create table 'konecranes.gebruikers_trainingen'
(errno: 150)
EDIT:
Other tables' queries.
CREATE TABLE gebruikers (
id int unsigned NOT NULL,
voornaam varchar(255) NOT NULL,
achternaam varchar(255) NOT NULL,
account_level int unsigned NOT NULL,
PRIMARY KEY (id, voornaam)
) ENGINE = InnoDB;
CREATE TABLE trainingen (
id int unsigned NOT NULL,
naam varchar(255) NOT NULL,
PRIMARY KEY (id, naam)
) ENGINE = InnoDB;
You should add indexes on your foreign keys:
CREATE TABLE gebruikers_trainingen (
gebruiker_id INT UNSIGNED NOT NULL,
training_id INT UNSIGNED NOT NULL,
gebruiker_naam VARCHAR(255) NOT NULL,
training_naam VARCHAR(255) NOT NULL,
INDEX (gebruiker_id, gebruiker_naam),
INDEX (training_id, training_naam),
CONSTRAINT fk_idGebruiker FOREIGN KEY (gebruiker_id, gebruiker_naam)
REFERENCES gebruikers(id, voornaam),
CONSTRAINT fk_idTraining FOREIGN KEY (training_id, training_naam)
REFERENCES trainingen(id, naam)
) ENGINE = INNODB;
Has this table existed before in a different guise?
Mysql 1005 error when creating table using InnoDB engine
Hth Oli
Merging the constraints as follows did work it out. Also thanks too Justin Lurman for helping me out, had to add Indexes aswell.
CONSTRAINT fk_gebruikers FOREIGN KEY (gebruiker_id, gebruiker_naam) REFERENCES gebruikers(id, voornaam),
CONSTRAINT fk_trainingen FOREIGN KEY (training_id, training_naam) REFERENCES trainingen(id, naam)

Mysql, cannot add foreign key constraint

Why does this:
Create table Kaart (
aantal_geel int not null,
aantal_rood int not null,
Primary key (aantal_geel, aantal_rood));
Create table Wedstrijd (
datum date not null,
aantal_geel int not null,
aantal_rood int not null,
Primary key (datum),
Foreign key (aantal_geel) REFERENCES kaart(aantal_geel),
Foreign key (aantal_rood) REFERENCES kaart(aantal_rood));
Gives: Error Code: 1215. Cannot add foreign key constraint
You need to reference the combination of the key columns, not each key column individually:
Create table Wedstrijd
(
datum date not null,
aantal_geel int not null,
aantal_rood int not null,
Primary key (datum),
Foreign key (aantal_geel,aantal_rood)
REFERENCES kaart(aantal_geel,aantal_rood)
);