MySQL Duplicate foreign key constraint name - mysql

I am trying to connect following tables, according to this model:
When I add a foreign key first time, say profile_id from PASSENGER_PROFILE is a FK to CREDIT_CARD_DETAILS, that runs fine.
However when I try to do the same to the TICKET_INFO table, I am getting an
ERROR 1826: Duplicate foreign key constraint name
The same thing when I add flight_id as a primary key, it works fine on FLIGHT_DETAILS but same error when I try to add it to the TICKET_INFO table.
All those keys are defined as primary keys in their respective tables (flight_id is PK in FLIGHT table and profile_id is PK in PASSENGER_PROFILE).
So I can't figure out why the MySQL Workbench displays an error as it should be possible to define the same PK as a FK in multiple tables.
Any suggestion appreciated.

You should have posted your create table-.
but in general
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID)
but if i try to add
CONSTRAINT **FK_PersonOrder** FOREIGN KEY (OrderID)
REFERENCES Order(OrderID)
The name FK_PersonOrder is the same, and will cause the error you described
As you didn't post the complete error message, you have to look in youir create tables and eleminate all double names for foerign kegnb, the have to be unique

Related

Does Heidi sql allow you to add more than one foreign key?

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.

child tables and foreign keys

I know there have been myriads of questions concerning primary and foreign keys. Looking through them, I cannot seem to find a simple answer to my question. My understanding of primary and foreign keys is that a foreign key is a column designated in a child table that refers back to a primary key as a column in a parent table. Is that correct, or do I have it backwards? If that is indeed correct, I am trying to find out why I am having difficulty creating a foreign key in a child table as such:
salesorders.sonumber (pk) < customer.sonumber (fk)
I am using Navicat with MariaDB (same as MySQL) and the error I get is:
1452 - Cannot add or update a child row: a foreign key constraint fails ('customer_orders','#sql7a8_3'; CONSTRAINT 'sonumber' FOREIGN KEY ('sonumber') REFERENCES 'salesorder' ('sonumber') ON DELETE NO ACTION ON UPDATE CASCADE)
Customer_orders is the database name. I am naming the foreign key 'sonumber' which is the same as the column name in the child table (customer) and the parent table (salesorders). Is that incorrect? Should I give the foreign key another name?
gitpicker
The foreign key should have its own unique name, yes. sonumber_fk or something similar is a simple naming schema.
You also need to make sure that every entry in the Foreign Key column has a corresponding entry in the Referenced table, otherwise you will not be able to create the foreign key.

How to add foreign key to MySQL table?

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?)

I get Cannot add or update a child row: a foreign key constraint fails error

I have a table where I keep
id|user_id|subject_id
I have another two table users and subjects.
user_id is a foriegn key and refer the id in users table id column.
I use php admin and I could create the relation.
Same way, I tried to create relation for the subject_id foriegn key.
But I get the following error.
#1452 - Cannot add or update a child row: a foreign key constraint fails (`version2`.<result 2 when explaining filename '#sql-25b4_1e1'>, CONSTRAINT `#sql-25b4_1e1_ibfk_1` FOREIGN KEY (`id`) REFERENCES `wp_cons_table` (`subject_id`))
all tables are ino db and columns have int(5) data type.
I don't know why I get the error.
Can someone figure the reson to this error.
The specific link it's failing on is described at the end of your error:
FOREIGN KEY (`id`) REFERENCES `wp_cons_table` (`subject_id`)
It would be useful to have clearer information about the tables but essentially there are values already in your child table that do not exist in the parent table.
If there is any data that would violate the constraint then you won't be allowed to create it. Delete the mismatched child data or create the parents and you should be fine.
See also: alter table add foreign key fails

Setting up foreign key in Mysql Workbench

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.