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.
Related
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.)
I am getting error when I create table with foreign key
create table _users(_id int(20) unsigned NOT NULL AUTO_INCREMENT,
_user_fullname varchar(50)not null,
_user_username varchar(160) not null,
_user_password varchar(200) not null,_user_remember_me tinyint,
_user_email varchar(30),
_user_mobile varchar(15),
_user_age varchar(10)
,primary key(_id,_user_email,_user_mobile));
_users table created successfully..there were no error..
But When I want to create employee table :
CREATE TABLE employee ( _Id INT NOT NULL AUTO_INCREMENT,
_user_mobile VARCHAR(15) not null,
_name varchar(15),
_org varchar(10),
PRIMARY KEY (_Id),
foreign key (_user_mobile) references _users(_user_mobile));
Its showing error:
ERROR 1005 (HY000): Can't create table 'DB.employee' (errno: 150)
What am I doing wrong??
Hey In this case you just need to do one thing ,
you just need to add index to the reference column of the user table and then run the create table for employee
ALTER TABLE `_users` ADD INDEX (`_user_mobile`);
After running above query just run the below query :-
CREATE TABLE `employee`(
`_Id` INT(11) NOT NULL AUTO_INCREMENT,
`_user_mobile` VARCHAR(15) NOT NULL,
`_name` VARCHAR(15),
`_org` VARCHAR(10),
PRIMARY KEY (`_Id`),
FOREIGN KEY (`_user_mobile`) REFERENCES `_users`(`_user_mobile`) );
In this way you will get rid of the error 1005 of mysql which says that you need to have index on the reference column of parent table.
150 is a foreign key error:
C:\>perror 150
MySQL error code 150: Foreign key constraint is incorrectly formed
Getting the exact error message is very tricky. You need to run this query:
show engine innodb status
... and search in the output:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
160627 14:09:32 Error in foreign key constraint of table test/employee:
foreign key (_user_mobile) references _users(_user_mobile)):
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.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
Once you know that, it'd be easy to add the missing index:
ALTER TABLE `_users`
ADD UNIQUE INDEX `_user_email` (`_user_email`);
But I wouldn't if I were you. It's weird to use mobile phone number as key. Instead, just simplify the primary key:
create table _users(_id int(20) unsigned NOT NULL AUTO_INCREMENT,
_user_fullname varchar(50)not null,
_user_username varchar(160) not null,
_user_password varchar(200) not null,_user_remember_me tinyint,
_user_email varchar(30),
_user_mobile varchar(15),
_user_age varchar(10)
,primary key(_id));
... and use in the linked table:
CREATE TABLE employee ( _Id INT NOT NULL AUTO_INCREMENT,
_user_id int(20) unsigned not null,
_name varchar(15),
_org varchar(10),
PRIMARY KEY (_Id),
foreign key (_user_id) references _users(_id));
The problem is in the foreign key part. If you remove that, table will be created without a problem.
If you need to use that foreign key, you need to use InnoDB as the storage engine of MySQL. InnoDB allows a foreign key constraint to reference a non-unique key as can be seen in here.
In my database I need to create a table which has two foreign keys, I am unable to figure out the source of error although I tried .Any body help me in solving this problem.
The mysql command I gave to create the table
create table book_vegetable(
id int NOT NULL AUTO_INCREMENT,
producer_offer_id int NOT NULL,
consumer_id int NOT NULL,
booked_qty varchar,
PRIMARY KEY(id),
FOREIGN KEY(producer_offer_id)
REFERENCES producer_offer(id),
FOREIGN KEY(consumer_id) REFERENCES user(id)
);
The error I am getting
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'PRIMARY KEY(id),FOREIGN KEY(producer_offer_id) REFERENCES
producer_offer(id),FOR' at line 1
Your first problem was caused by a missing length specification on the booked_qty varchar column.
The usual suspects for error 150:
mismatch in primary vs. foreign key type (for example int - bigint). make sure they match exactly
different table engines (for example if you try to reference a MyIsam table in a InnoDB table)
This works in MySQL, just tested (note that producer offer and user are just a mock tables).
create table producer_offer (id int(10) not null auto_increment, primary key(id));
create table user (id int(10) not null auto_increment, primary key(id));
create table book_vegetable (
id int(10) NOT NULL AUTO_INCREMENT,
producer_offer_id int(10) NOT NULL,
consumer_id int(10) NOT NULL,
booked_qty varchar(255),
PRIMARY KEY(id),
FOREIGN KEY(producer_offer_id) REFERENCES producer_offer(id),
FOREIGN KEY(consumer_id) REFERENCES user(id)
);
So I've been researching this for hours and been having no luck in finding something suitable for what I'm trying to do.
I'm currently having two issues: Creating a table with multiple foreign keys referencing the same primary key and splitting these attributes into multiple columns.
In the end, I would like my table to look something like this, where my O-Director_ID is the primary key in one column that correlates to all the other ID's (in this case FAD_ID, SAD_ID and SUD_ID) in the other column that are all foreign keys corresponding to the primary key from another table (Member_ID from crew_member, which already exists).
Here's what I'm currently trying to do, just to create multiple foreign keys (I don't even know how to go about creating that second column yet):
mysql> create table Other_Directing (
-> O_Director_ID int (4) not null auto_increment,
-> FAD_ID int (5) not null,
-> SAD_ID int (5) not null,
-> SUD_ID int <5> not null,
-> primary key (O_Director_ID),
-> foreign key (FAD_ID) references crew_member(Member_ID),
-> foreign key (SAD_ID) references crew_member(Member_ID),
-> foreign key (SUD_ID) references crew_member(Member_ID)
-> );
However even this doesn't work, I get this error message:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'<5> not null,
primary key (O_Director_ID),
foreign key (FAD_ID) references crew_'
at line 5
Thank you and I hope this makes sense.
Regards,
Michael
change SUD_ID int <5> not null,
to this: SUD_ID int (5) not null,
Does your project really necessitate non-default sizes on your int columns? If so, you have a typo... one of your sizes is in <>'s instead of ()'s: -> SUD_ID int <5> not null should be -> SUD_ID int (5) not null
If you're sure you do want the sizes due to memory constraints:
create table Other_Directing (
O_Director_ID int not null auto_increment,
FAD_ID int not null,
SAD_ID int not null,
SUD_ID int not null,
primary key (O_Director_ID),
foreign key (FAD_ID) references crew_member(Member_ID),
foreign key (SAD_ID) references crew_member(Member_ID),
foreign key (SUD_ID) references crew_member(Member_ID)
);
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.)