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)
);
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 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.
I am trying to add a column to my table and make it a foreign key but I keep getting this error:
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 'FOREIGN KEY(idplayer) REFERENCES
players(playersid)' at line 1
below is my code before the alter statement:
CREATE TABLE transactions
(
transid INT UNSIGNED NOT NULL AUTO_INCREMENT,
type VARCHAR(20),
fromteam VARCHAR(30),
toteam VARCHAR(30),
idplayer INT UNSIGNED NOT NULL,
PRIMARY KEY(transid)
);
Now I try to alter the idplayer and make it a foreign key:
ALTER TABLE transactions
MODIFY idplayer INT UNSIGNED NOT NULL
FOREIGN KEY(idplayer) REFERENCES players(playersid)
Please an assistance would be great.
First off, you are missing a comma after NOT NULL, Second you need to tell mysql to add the CONSTRAINT. Give this a try:
ALTER TABLE `events`
MODIFY idplayer INT UNSIGNED NOT NULL,
ADD CONSTRAINT `FK_Name` FOREIGN KEY (idplayer) REFERENCES players(playersid);
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)
);
I have one table with name tbl_groupmaster created with SQL as shown below:
create table tbl_groupmaster (
tgm_groupid int(10) unsigned NOT NULL auto_increment,
tgm_groupname varchar(50),
tgm_groupdescription varchar(50),
PRIMARY KEY (tgm_groupid)
)
and I am creating one more table with name tbl_groupmanager, using a foreign key relationship:
create table tbl_groupmanager (
tgmgr_groupmangerid int(10) NOT NULL,
tgm_groupid int(10),
UserNamesID int(10),
tgmgr_groupsize int(10),
tgmgr_groupassigned_date datetime,
tgmgr_grouplead_status enum ('active','inactive'),
PRIMARY KEY (tgmgr_groupmangerid),
FOREIGN KEY (tgm_groupid) REFERENCES tbl_groupmaster(tgm_groupid)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
But I am getting this error:
SQL Error: Can't create table '.\student\tbl_groupmanager.frm' (errno: 150)..
What is this? I am unable to identify my mistake. Please help me to resolve this. Thanks in advance.
The type of the foreign key has to be the same as the referenced key. Change tgm_groupid on table tbl_groupmanager to int(10) unsigned and it will work.
Most probably, MyISAM is default engine in your database and hence tbl_groupmaster is MyISAM.
MyISAM does not support foreign keys.
Your foreign key is not the same datatype as the primary key it references. One is unsigned, one isn't.