Errno:150 in MySQL Database - mysql

Have an error in my SQL code, but could not understand, where it is. Please, help me to solve that. Here is my code:
CREATE TABLE IF NOT EXISTS Records (
record_id int(11) NOT NULL AUTO_INCREMENT,
record_year year(4) NOT NULL,
record_quarter int(1) NOT NULL,
profit_tax int(11) NOT NULL,
PRIMARY KEY (record_id, record_year, record_quarter),
UNIQUE(record_year, record_quarter)
);
CREATE TABLE IF NOT EXISTS ProductsList (
product_id int(11) NOT NULL AUTO_INCREMENT,
product_name varchar(24) NOT NULL,
PRIMARY KEY (product_id)
);
An error is in that table:
CREATE TABLE IF NOT EXISTS RecordsProducts (
recordproduct_id int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (recordproduct_id),
FOREIGN KEY (record_id) REFERENCES Records (record_id),
FOREIGN KEY (product_id) REFERENCES ProductsList (record_id)
);

referencing column should be INDEX
referencing column should have same data type of referenced column
both referencing, referenced table should be InnoDB
CREATE TABLE IF NOT EXISTS RecordsProducts (
...
...
record_id int NOT NULL, <=== check data type
product_id int NOT NULL,
INDEX(record_id), <===== check INDEXed or not
INDEX(product_id), <====
FOREIGN KEY (record_id) REFERENCES Records (record_id),
FOREIGN KEY (product_id) REFERENCES ProductsList (record_id)
) ENGINE = InnoDB; <=== add 'ENGINE=InnoDB' explicitly

Related

Why does it fail to open the referenced table if it already exists?

Somehow I can't create a join table for two entities I already have.
Here is what I run on MySQL just fine, the last one is the one I'm struggling with.
create table instructor_detail_table(
instructor_detail_id int not null primary key auto_increment,
instructor_class varchar(40) not null
);
create table instructor_table(
instructor_id int not null primary key auto_increment,
instructor_name varchar(40) not null,
instructor_detail_id int not null,
foreign key (instructor_detail_id) references instructor_detail_table (instructor_detail_id)
);
create table course_table(
course_id int not null primary key auto_increment,
course_class varchar(40) not null,
instructor_id int not null,
foreign key (instructor_id) references instructor_table (instructor_id)
);
create table student_table(
student_id int not null primary key auto_increment,
student_name varchar(40) not null,
student_age int not null
);
create table student_course_table(
student_id int not null,
course_id int not null,
foreign key (student_id) references student_table (student_id),
foreign key (course_id) references course_table (course_id),
primary key (student_id, course_id)
);
When I try to create the student_course_table MySQL complaints it can't open the referenced table student_table
Thanks in advance
You can have this problem if the storage engine is not the good one.
For example 'MyISAM' instead of 'InnoDB' for the table 'student_table'.
Because MyISAM doesn't support foreign key.

SQL Key Column doesn't exist when ALTER TABLE

ALTER TABLE book
ADD FOREIGN KEY (category_id)
REFERENCES category (category_id)
ON DELETE CASCADE
ON UPDATE CASCADE;
Whenever I run this ALTER TABLE statement, it keeps coming back with, "Error Code: 1072. Key column 'category_id' does not exist."
I'm sure this is an easy fix, but I'm having problems adding a foreign key to one of my tables. I just started learning SQL last week so any help would be appreciated!
My CREATE TABLE statements are as follows:
CREATE TABLE IF NOT EXISTS book
(book_id INT not null PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) not null,
description TEXT not null,
price DEC(18,2) not null
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS category
(category_id INT not null PRIMARY KEY AUTO_INCREMENT,
code1 VARCHAR(20) not null,
name1 VARCHAR(100) not null
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS category
(category_id INT not null PRIMARY KEY AUTO_INCREMENT,
code1 VARCHAR(20) not null,
name1 VARCHAR(100) not null
) ENGINE=InnoDB;
This Table has the Primay Key category_id but below table has no Column
as "category_id" which is being referenced in the Alter Command.Add
Column "category_id in below table and then execute Alter command.
CREATE TABLE IF NOT EXISTS book
(book_id INT not null PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) not null,
description TEXT not null,
price DEC(18,2) not null
) ENGINE=InnoDB;
Here is the complete script then
CREATE TABLE IF NOT EXISTS book
(book_id INT not null PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) not null,
description TEXT not null,
price DEC(18,2) not null,
category_id INT not null
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS category
(category_id INT not null PRIMARY KEY AUTO_INCREMENT,
code1 VARCHAR(20) not null,
name1 VARCHAR(100) not null
) ENGINE=InnoDB;
ALTER TABLE book
ADD FOREIGN KEY (category_id)
REFERENCES category (category_id)
ON DELETE CASCADE
ON UPDATE CASCADE;

mysql creating table foreign key

I've created a table :
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY
,uName VARCHAR(50)
,uSecondName VARCHAR(50)
,eMail VARCHAR(50)
)
After this I even insert some data without any problems. But when I've tried to create new table with FOREIGN KEY referenced to users.id I've got an error:
CREATE TABLE posts(
id INT(6) AUTO_INCREMENT NOT NULL
,pTitle VARCHAR(155) NOT NULL DEFAULT 'not_set'
,pText TEXT
,pAuthor INT(6)
,PRIMARY KEY(id)
,CONSTRAINT fk_PerAuthor FOREIGN KEY (pAuthor)
REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE
);
Did I miss something?

SQL - CONSTRAINT to link two tables

I don't understand how to link two tables together. This is an example:
CREATE TABLE IF NOT EXISTS itemStatus (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
CREATE TABLE IF NOT EXISTS itemData (
id int(11) AUTO_INCREMENT PRIMARY KEY,
title varchar(64) NOT NULL,
status_id int(11) DEFAULT NULL,
CONSTRAINT `fk_id` FOREIGN KEY (`id`) REFERENCES `itemStatus` (`id`),
);
I'm calling the row "status_id" but I don't reference this anywhere, so it can't link the two. For this example, should should my "CONSTRAINT" line read in order to be correct?
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
Take a look at:
http://www.w3schools.com/sql/sql_foreignkey.asp
So it should be:
CREATE TABLE IF NOT EXISTS itemStatus (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
CREATE TABLE IF NOT EXISTS itemData (
id int(11) AUTO_INCREMENT PRIMARY KEY,
title varchar(64) NOT NULL,
status_id int(11) DEFAULT NULL,
CONSTRAINT `fk_id` FOREIGN KEY (`status_id`) REFERENCES `itemStatus` (`id`)
);
FOREIGN KEY (status_id) => Field in the table will REFERENCES itemStatus (id)
Constraint can't have "," when it's the last:
(...) REFERENCES itemStatus (id),
So the structure should be:
CONSTRAINT <<CONSTRAINT_NAME>> FOREIGN KEY (<<COLUMN_IN_THE_TABLE>>) REFERENCES `<<ANOTHER_TABLE>>` (`<<ANOTHER_TABLE_COLUMN_ID>>`)
Looks like you're very close. Try this instead:
CONSTRAINT `fk_id` FOREIGN KEY (`status_id`) REFERENCES `itemStatus` (`id`)
Your constraint is linking two primary keys (id of table 1 with id of table 2).
It should be something like this:
CREATE TABLE IF NOT EXISTS itemData (
id int(11) AUTO_INCREMENT PRIMARY KEY,
title varchar(64) NOT NULL,
status_id int(11) DEFAULT NULL,
CONSTRAINT `fk_id` FOREIGN KEY (`status_id`) REFERENCES `itemStatus` (`id`)
);

trying to create a table relationship with mysql v5.5

Hi all trying to create a relationship between two tables with mysql v5.5 Curious How I go about this...
CREATE TABLE posts(id INT AUTO_INCREMENT,
title varchar(100) NOT NULL,
body varchar(500) NOT NULL,
PRIMARY KEY (id) );
//posts_id not null creating error?
CREATE TABLE comments(id INT AUTO_INCREMENT,
comment varchar(250)NOT NULL,
posts_id NOT NULL,
PRIMARY KEY (id) );
I would like to have a posts_id in comments table share the relationship with the post table id in comments table.
You haven't set posts_id type:
posts_id int(11) NOT NULL
Then your comments table should look like this:
CREATE TABLE IF NOT EXISTS comments (
id int(11) NOT NULL AUTO_INCREMENT,
comment varchar(250) NOT NULL,
posts_id int(11) NOT NULL,
PRIMARY KEY (id),
KEY posts (posts_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
And finally, a foreign relation:
ALTER TABLE comments
ADD CONSTRAINT comments_ibfk_1 FOREIGN KEY (posts_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE
Give it a type, like varchar, or INT
posts_id varchar(20) NOT NULL