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
Related
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 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.
I had a table named movies which had the fields id as primary key, and two varchars: title and genre.
I created a new table named genres with the int field id as primary key and desription varchar. I changed the field genre in my movies table so I could create a foreign key referencing a genre.
However, Mysql Workbench says there's an error when creating the foreign key.
Here's the statement:
ALTER TABLE `managemovies`.`movies`
ADD CONSTRAINT `genre_reference`
FOREIGN KEY (`genre` )
REFERENCES `managemovies`.`genres` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
, ADD INDEX `genre_reference_idx` (`genero` ASC) ;
Error:
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (`managemovies`.`#sql-3ba_2b`, CONSTRAINT `genre_reference` FOREIGN KEY (`genre`) REFERENCES `genres` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement: [... same statement than above ... ]
ERROR: Error when running failback script. Details follow.
ERROR 1046: No database selected
SQL Statement:
CREATE TABLE `movies` [...]
[... the errors above repeated again ...]
clear your table contents and try adding foreign key.
if your table contain data which not matching the foreign key field value you will see this error ...
It looks like your table movies has data in genre column which is not present in genres.id column.
Your statement should work after removing the invalid data.
Hope it helps
Vishad
I have faced same issue i got resolved later..
for this answer is simple you just need to add atleast a row of values in both tables then try to add the foreign key
I am trying to set up a foreign key in Mysql workbench. I used the same name for the foreign key as the primary key of the table I am trying to set a relationship with. I already have one relation set up this way in another table, but When I try and apply the alterations to this table, the script gives me an error:
Error 1005: Can't create table 'X.#sql-718_a' (errno: 121)
SQL Statement:
ALTER TABLE `X`.`X_use`
ADD CONSTRAINT `XyzID`
FOREIGN KEY (`XyzID` ) REFERENCES `X`.`Xyz` (`XyzID` )
ON DELETE NO ACTION O
N UPDATE NO ACTION ,
ADD INDEX `XyzID` (`XyzID` ASC) ,
However, if I change the foreign key name to "AbcID" I have no problem setting up the foreign key relation. Why is that and why can't I have the primary key name from one table be the same for the foreign key for this table? I have set up relations like that previously but for this table I cannot.
Constraint names have to be unique within the database.
That (errno: 121) in the error message means that MySQL encountered a duplicate key exception. The usual cause of this is that there is already a constraint of the same name in the database.
This "unique name" requirement is one reason why the normative pattern is to include the table name when constructing the name of the foreign key constraint. e.g. FK_table_cols e.g. FK_X_use_XyzID.
Why must the constraint name be unique within the database? That's a question for the dbms designers.
But consider this: when the database encounters a constraint violation, it throws an error that contains the name of the constraint. When that constraint name references only one constraint in the database, that makes locating the problem a bit easier.
You can not use same constrain name through out the database as described in ACID properties of DB.