I want to assign foreign key(email) references user(email), but It shows error. I cant figure out what is wrong.
ERROR 1005: Can't create table 'schema.#sql-1bf8_f' (errno: 121)
SQL Statement:
ALTER TABLE `schema`.`vendor_ambassador`
ADD CONSTRAINT `email`
FOREIGN KEY (`email`)
REFERENCES `schema`.`user` (`email`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
I've have the same problem. These were my steps to resolve this problem:
Check if master table are created first.
Check the primary key on the master table are implemented and indexes are created.
Check data type within related column on master and detail table are same.
Ensure that column on foreign key in detail table are not set into primary key.
Check the foreign key or constraint name on other tables within database. The foreign key or constraint name should be unique.
If this doesn't help, check using MySQL Command: Show innodb engine status; to read the problems.
I hope this solves your problem.
Related
I have a table named "countries" and another table named "country_continents" in my DB. I want to make my continent_id column in countries , a foreign key referencing the id of country_continents, but I am getting an error message. This is the SQL to create the foreign key and the error:
ALTER TABLE countries
ADD CONSTRAINT fk_continent_id
FOREIGN KEY (continent_id)
REFERENCES country_continents(id);
ERROR:
#1215 - cannot add foreign key constraint
At first, I was getting the :
"Error: relational features are disabled"
so I ran the command ALTER TABLE countries ENGINE=InnoDB; and ALTER TABLE country_continents ENGINE=InnoDB; but now I'm getting the #1215 error.
This is the Structure for "country_continents":
This is the Structure for "countries":
Any ideas on whats happening? Thanks in advance.
I think the error is due to:
in your countries table, continent_id is not unsigned.
Edit that and tell me if it worked
I have successfully added my first FK in heidisql using the foreign key tab and adding all the appropriate sections.
I have tried to do the same to my second related column both by using the FK tab and by running a query but I keep getting an error.
SQL Error (1005): Can't create table sprout.#sql-430_3 (errno: 150 "Foreign key constraint is incorrectly formed")
sprout is my db name so I have no idea why it is saying cant create table sprout (because I'm not referencing it in my query).
sql query for my first FK(generated via heidisql):
ALTER TABLE `purchase_history`
ADD CONSTRAINT `bus_id` FOREIGN KEY (`bus_id`) REFERENCES `business` (`bus_id`);
sql query for my second FK(generated via heidisql)
ALTER TABLE `purchase_history`
ADD CONSTRAINT `bus_name_fk` FOREIGN KEY (`bus_name`) REFERENCES `business` (`bus_name`);
sql query I wrote to try and add second FK
Alter table purchase_history
Add constraint bus_name_fk
Foreign key (bus_name)
references business(bus_name);
Can someone help explain to me how my constraint is incorrectly formed? To my understanding I was able to add another constraint to the the table.
This is too long for a comment.
Huh? Why are you adding two foreign constraints to the same table . . . but using different columns? That doesn't really make sense. In general, the foreign key should be referencing the primary key of the other table, which I presume is bus_id.
Then, if you want the business name, you can use a join to get the name.
I have searched other answers and tried them, but no luck. Here is what I get:
Error
SQL query:
ALTER TABLE `watching` ADD FOREIGN KEY ( `anime_Score` )
REFERENCES `anime_15-12-2015`.`score` (`Score_ID`);
MySQL said: Documentation
1452 - Cannot add or update a child row: a foreign key constraint fails (anime_15-12-2015/#sql-fdc_14a, CONSTRAINT
#sql-fdc_14a_ibfk_2 FOREIGN KEY (anime_Score) REFERENCES score
(Score_ID))
Basicly I have a database called anime_15-12-2015 and in that database I have 3 tables: watching, type and score.
I was able to add foreign key to type but no luck with score...
If you need more info, reply what and I'll answer as soon as I can.
Thanx to everyone that tries to help me!
Check in the column 'anime_Score' of Table 'watching' if exist a value which no exist in the Table 'score' (Score_ID).
I use MySQL with InnoDB engine. I double-checked type of columns. But always have:
Error Code: 1215. Cannot add foreign key constraint
I tried:
ALTER TABLE `mail`.`boxes`
ADD CONSTRAINT FK_id
FOREIGN KEY (id)
REFERENCES `mail`.`users` (id)
ON UPDATE NO ACTION
ON DELETE NO ACTION;
and
ALTER TABLE `mail`.`boxes`
ADD FOREIGN KEY (id)
REFERENCES `mail`.`users` (id)
Nothing works(((
Please, help, what I am doing wrong (except choosing MySQL :-) )?
If table contains data then you are not able to add foreign key you drop table object and recreate
use below reference for the same
Basics of Foreign Keys in MySQL?
To check what exactly the problem is, use:
SHOW ENGINE INNODB STATUS\G
There is section "last foreign key error". Look at: http://dev.mysql.com/doc/refman/5.0/en/innodb-monitors.html
My guess is that data type od mail.boxes (id) and mail.users (id) is not the same. (E.g. smallint in one table and integer in second one).
Data in table on which you're trying to create FK could possibly also be problem (are your mailbox ids the same as id of existing users?)
So I am trying to add a primary key to one of the tables in my database. Right now it has a primary key like this:
PRIMARY KEY (user_id, round_number)
Where user_id is a foreign key.
I am trying to change it to this:
PRIMARY KEY (user_id, round_number, created_at)
I am doing this in phpmyadmin by clicking on the primary key icon in the table structure view.
This is the error I get:
#1025 - Error on rename of './database/#sql-2e0f_1254ba7' to './database/table' (errno: 150)
It is a MySQL database with InnoDB table engine.
There is probably another table with a foreign key referencing the primary key you are trying to change.
To find out which table caused the error you can run SHOW ENGINE INNODB STATUS and then look at the LATEST FOREIGN KEY ERROR section.
As was said you need to remove the FKs before. On Mysql do it like this:
ALTER TABLE `table_name` DROP FOREIGN KEY `id_name_fk`;
ALTER TABLE `table_name` DROP INDEX `id_name_fk`;
For those who are getting to this question via google... this error can also happen if you try to rename a field that is acting as a foreign key.
To bypass this in PHPMyAdmin or with MySQL, first remove the foreign key constraint before renaming the attribute.
(For PHPMyAdmin users: To remove FK constrains in PHPMyAdmin, select the attribute then click "relation view" next to "print view" in the toolbar below the table structure)
If you are trying to delete a column which is a FOREIGN KEY, you must find the correct name which is not the column name. Eg: If I am trying to delete the server field in the Alarms table which is a foreign key to the servers table.
SHOW CREATE TABLE alarm;
Look for the CONSTRAINT `server_id_refs_id_34554433` FORIEGN KEY (`server_id`) REFERENCES `server` (`id`) line.
ALTER TABLE `alarm` DROP FOREIGN KEY `server_id_refs_id_34554433`;
ALTER TABLE `alarm` DROP `server_id`
This will delete the foreign key server from the Alarms table.
I had this problem, it is for foreign-key
Click on the Relation View (like the image below) then find name of the field you are going to remove it, and under the Foreign key constraint (INNODB) column, just put the select to nothing! Means no foreign-key
Hope that works!
If you are adding a foreign key and faced this error, it could be the value in the child table is not present in the parent table.
Let's say for the column to which the foreign key has to be added has all values set to 0 and the value is not available in the table you are referencing it.
You can set some value which is present in the parent table and then adding foreign key worked for me.