ERROR 1005 (HY000): Can't create table './quotes/actions.frm' (errno: 150) - mysql

When I am heading to create a new table
CREATE TABLE actions ( A_id int NOT NULL AUTO_INCREMENT,
type ENUM('rate','report','submit','edit','delete') NOT NULL,
Q_id int NOT NULL,
U_id int NOT NULL,
date DATE NOT NULL,
time TIME NOT NULL,
rate tinyint(1),
PRIMARY KEY (A_id),
CONSTRAINT fk_Question FOREIGN KEY (Q_id) REFERENCES questions(P_id));
Shows this error:
ERROR 1005 (HY000): Can't create table './quotes/actions.frm' (errno: 150)
----------
With reference to
http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
InnoDB does not currently support foreign keys for tables with user-defined partitioning. This includes both parent and child tables.
Can anyone explain the above lines.
I can't understand why am I seeing this.

150 is usually fixed by CREATEing the tables in a different order. If the does not suffice (eg, circular FKs), use ENABLE/DISABLE FOREIGN KEYS.
PARTITION... True. The current design of PARTITIONing does not allow for arbitrary UNIQUE KEYS or FOREIGN KEYS. This is not likely to be changed until (perhaps) 5.8.

Related

sql error that i cant solve (7)

can anyone help with this error , when i run my code it comes up with error 1005 can't create table my code looks like this can anyone point out the source of this error im using codio mysql if that helps
CREATE TABLE IF NOT EXISTS entries (
entries_id INT NOT NULL AUTO_INCREMENT,
students_id INT UNSIGNED NOT NULL,
Date_of_exam DATETIME NOT NULL,
subjects_id INT UNSIGNED NOT NULL,
PRIMARY KEY (entries_id),
FOREIGN KEY (students_id) REFERENCES students(students_id),
FOREIGN KEY (subjects_id) REFERENCES subjects(subjects_id));
this is the error
mysql> SOURCE task7
ERROR 1005 (HY000): Can't create table 'exams.entries' (errno: 150)
This error related to foreign keys. Check following items:
Name of tables and columns that exist on foreign keys are correct.
Type of foreign keys columns are same.
Data of foreign keys not conflict with them.
Note: You can disable check foreign key on query. Sure enable this option after query.
SET FOREIGN_KEY_CHECKS=0;
CREATE TABLE IF NOT EXISTS entries (
entries_id INT NOT NULL AUTO_INCREMENT,
students_id INT UNSIGNED NOT NULL,
Date_of_exam DATETIME NOT NULL,
subjects_id INT UNSIGNED NOT NULL,
PRIMARY KEY (entries_id),
FOREIGN KEY (students_id) REFERENCES students(students_id),
FOREIGN KEY (subjects_id) REFERENCES subjects(subjects_id)
);
SET FOREIGN_KEY_CHECKS=1;
The error itself is related with the foreign keys (as removing them creates the table with no errors).
Main possible causes:
One of the referenced tables doesn't exists
The field of the referenced tables doesn't exists
The data types of the referenced table doesn't match (I assume is this).
As i can see your primary key on your table is INT, assuming you use INT as your primary keys, students_id is INT UNSIGNED, possibly the cause of your error.

Foreign key error when creating table (errno 150)

I'm trying to create a relationship between two tables using the following queries:
create table card
(id int not null auto_increment,
post_id bigint(20) unsigned not null,
primary key(id));
create table user_comment
(id int not null auto_increment,
comment_author longtext,
comment_post_id bigint(20) unsigned not null,
primary key (id),
foreign key (comment_post_id) references card(post_id));
However it gives me the following error message:
ERROR 1005 (HY000) at line 9: Can't create table 'test.user_comment'
(errno: 150)
If I execute the command for showing innodb status:
show engine innodb status;
It shows this message:
LATEST FOREIGN KEY ERROR
Error in foreign key constraint of table test/user_comment: foreign key
(comment_post_id) references card(post_id)): Cannot find an index in
the referenced table where the referenced columns appear as the first
columns, or column types in the table and the referenced table do not
match for constraint.
But I still can't figure out how to fix the problem.
Any ideas?
From the docs:
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.
Create an index on card (post_id).
Just use this command once before creating the table:
SET FOREIGN_KEY_CHECKS=0;

MySQL errno 150 without solution yet

although this is a repeated question,
I have been searching through most of the similar posts, but found nothing useful.
Here's my SQL script for MySQL.
CREATE DATABASE IF NOT EXISTS store;
USE store;
CREATE TABLE IF NOT EXISTS Box (
coord VARCHAR (255),
box_id INT UNSIGNED NOT NULL,
img_path VARCHAR (256),
PRIMARY KEY (coord, box_id)
);
CREATE TABLE IF NOT EXISTS Tool (
serial VARCHAR (50),
tool_id INT,
descr VARCHAR (256),
box_id INT UNSIGNED NOT NULL,
tool_state BOOLEAN,
PRIMARY KEY (tool_id),
FOREIGN KEY (box_id) REFERENCES Box(box_id)
);
Output is: ERROR 1005 (HY000) at line 9: Can't create table 'store.Tool' (errno: 150)
Any suggestion
From: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
If you re-create a table that was dropped, it must have a definition
that conforms to the foreign key constraints referencing it.It must
have the right column names and types, and it must have indexes on the
referenced keys, as stated earlier. If these are not satisfied, MySQL
returns error number 1005 and refers to error 150 in the error
message.
I guess you have to use the same amount of foreign key, in your code you use 2 PK in table Box, so either you use only box_id as your PK or add foreign key to table Tool..

Errno 150 in MySQL with Foreign Key Matching Datatype

I'm making the following SQL query:
CREATE TABLE discography (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
musician_id int(11) NOT NULL,
title VARCHAR(255) NOT NULL,
album_cover_url varchar(255) DEFAULT NULL,
year INT(11),
FOREIGN KEY (musician_id) REFERENCES musician (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The id on the musician field that the foreign key musician_id is referencing looks like this:
id | int(11) | NO | PRI | NULL | auto_increment |
It thus seems like I'm using a matching datatype for musician_id. Yet I'm getting the following error:
ERROR 1005 (HY000): Can't create table 'discography' (errno: 150)
Indicating an issue with the foreign key. What's up with that?
Check out the MySQL manual about foreign key constrains:
If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.
A few ideas:
1. Better drop the tables and create it new with a well formed syntax.
2. Make sure to add ENGINE=InnoDB; to your CREATE TABLE - command.
3. Make sure InnoDB is enabled on your MySQL server. To verify this,
try this command:
SHOW `VARIABLES LIKE` 'have_innodb'; - if it returns
a YES, then InnoDB is enabled.
4. Check your command for upper- and lowercases in table- and
fieldnames.
5. Check this not only one the table you want to create, but also on
the tables the foreign keys are referring to.
6. Make sure your referred tables are properly indexed.

Error Code: 1005 can't create table

please see my create statement:
CREATE TABLE INTERNAL_MEDICINE_DETAIL(DIAGNOSE_ITEM VARCHAR(15) NOT NULL,
EXM_RESULT VARCHAR(50),
IDENTIFY_ID INT AUTO_INCREMENT PRIMARY KEY,
SUMMARY_ID INT NOT NULL,
FOREIGN KEY(SUMMARY_ID) REFERENCES INTERNAL_MEDICINE_SUMMARY(SUMMARY_ID),
FOREIGN KEY(DIAGNOSE_ITEM) REFERENCES INTERNAL_MEDICINE_ITEM_DEF(DIAGNOSE_ITEM))
The referenced two tables are created successfully:
CREATE TABLE INTERNAL_MEDICINE_ITEM_DEF(DIAGNOSE_ITEM VARCHAR(15) NOT NULL,
ITEM_DESCRIPTION VARCHAR(50) NOT NULL,
IDENTIFY_ID INT AUTO_INCREMENT PRIMARY KEY)
CREATE TABLE INTERNAL_MEDICINE_SUMMARY(SUMMARY_ID INT AUTO_INCREMENT PRIMARY KEY,
SUMMARY VARCHAR(1000),
R_IDENTIFYID INT NOT NULL,
FOREIGN KEY(R_IDENTIFYID) REFERENCES BASICINFO(IDENTIFY_ID))
I have seen the mean of error code 1005 on MySQL web site, it says:
Cannot create table. If the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. If the error message refers to error –1, table creation probably failed because the table includes a column name that matched the name of an internal InnoDB table.
my error is 150 currently, but I cannot find the difference of the two foreign keys defined in INTERNAL_MEDICINE_DETAIL table with the two fields defined in INTERNAL_MEDICINE_SUMMARY AND INTERNAL_MEDICINE_ITEM_DEF respectively.
So, could you please help me and tell me the reason?
From the InnoDB foreign key constraints documentation:
InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.
You'll need to create an index on INTERNAL_MEDICINE_ITEM_DEF that has DIAGNOSE_ITEM as its first column. (No need to change anything for INTERNAL_MEDICINE_SUMMARY since the referenced column is that table's primary key.)