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..
Related
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.
I am developing a patch version which is by create or update existing table by using raw sql.When I ran these 3 queries like below
first query Success
CREATE TABLE ts_overtime_scheme_histories( id int AUTO_INCREMENT NOT NULL,
name VARCHAR(255),
workdays INT,
break_payable VARCHAR(25),
roundable VARCHAR(25),
round_rule VARCHAR(10), round_minute INT,
type enum('ratio','fixed') DEFAULT 'ratio',
overtime_request_id INT UNSIGNED NOT NULL, PRIMARY KEY(id),
FOREIGN KEY(overtime_request_id) REFERENCES ts_overtime_requests(id) );
Second query success
CREATE TABLE ts_overtime_scheme_details_histories( id INT AUTO_INCREMENT NOT NULL,
hour FLOAT,
ratio FLOAT,
overtime_scheme_history_id INT(11) UNSIGNED NOT NULL,
PRIMARY KEY(id));
And now I am trying to connect second table to the first table. So the first table has foreign key on the second table. So I ran the third query
ALTER TABLE ts_overtime_scheme_details_histories ADD FOREIGN KEY (`overtime_scheme_history_id`) REFERENCES `ts_overtime_scheme_histories` (`id`) ON DELETE CASCADE;
But somehow it failed. The error report is below
General error: 1005 Can't create table `db_test`.`#sql-ea4_28` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: ALTER TABLE ts_overtime_scheme_details_histories
ADD FOREIGN KEY (`overtime_scheme_history_id`) REFERENCES `ts_overtime_scheme_histories` (`id`)
ON DELETE CASCADE;)
Can somebody help me to find what I miss? At first, I suspect the primary and foreign key data length is not similar, but when I double checked, it is correct.
Edit:
table ts_overtime_requests was created using laravel framework.
Column datatype of the foreign key column must match exactly the datatype of referenced key column.
In this case, the reported behavior (Error 1005) is expected because there's a difference in the datatypes of the two columns.
One of the columns is signed integer, the other column is UNSIGNED integer.
Quick fix would be to change the datatype of overtime_scheme_history_id so that is is signed. (Remove the UNSIGNED keyword.)
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.
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.)
Here is my SQL script
CREATE TABLE tracks(
track_id int NOT NULL AUTO_INCREMENT,
account_id int,
track_name varchar(255),
track_path varchar(255),
track_art_path varchar(255),
track_desc text,
primary key(track_id),
FOREIGN KEY (account_id) REFERENCES accounts_profile(accnt_id)
)
I don't see any syntax errors. Everything looks fine. My Database Engine is innoDB. but how come I keep on receiving this error?
#1005 - Can't create table 'beatbeast.tracks' (errno: 150)
It's not showing what line where the error is.
Errno 150 is generally the result of a mismatch between the exact data types of the main table's referenced column, and the referencing column. In your case, tracks.account_id is a signed INT, but the column it references accounts_profile.accnt_id is INT UNSIGNED. So you must create the tracks table using INT UNSIGNED for account_id as well:
CREATE TABLE tracks(
track_id int NOT NULL AUTO_INCREMENT,
account_id int UNSIGNED,
track_name varchar(255),
track_path varchar(255),
track_art_path varchar(255),
track_desc text,
primary key(track_id),
FOREIGN KEY (account_id) REFERENCES accounts_profile(accnt_id)
)
From the documentation:
If MySQL reports an error number 1005 from a CREATE TABLE statement,
and the error message refers to error 150, table creation failed
because a foreign key constraint was not correctly formed.
Check that the datatype of accounts_profile.accnt_id matches tracks.account_id exactly. Currently one is an int, so the other must also be an int.
Furhter, the documentation suggests to call:
SHOW ENGINE INNODB STATUS
after you get the error message for a more detailed explanation.