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.
Related
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
I just started learning SQL so I am a bit confused.
If I have Table A that has a primary key : CustomerID & Table B with foreign key CustomerID
I added the foreign key constraint by using CASCADE so that the foreign key should update or delete automatically when primary key is deleted or updated.
However, it only works for delete. When I add a new record in the primary field table, that record is not shown in the foreign key table, why is that ?
Corresponding rows are updated or deleted in the referencing table
when that row is updated or deleted in the parent table. CASCADE
cannot be specified if a timestamp column is part of either the
foreign key or the referenced key. ON DELETE CASCADE cannot be
specified for a table that has an INSTEAD OF DELETE trigger. ON UPDATE
CASCADE cannot be specified for tables that have INSTEAD OF UPDATE
triggers.
As mention in MSDN. They have mentioned the only update and delete operation of primary key table will affect the Foreign key table's column. If any insert made to primary key, it will not affected the foreign key. Since the Main objective in primary key and foreign key relationship
"An each every record is available in the foreign key table, it should contain corresponding record should be present in the primay key table and vice versa is not applicable".
If you insert any record to foreign key table that it will throws foreign referential integrity error. It will not allows you to insert a record in the foreign table unless and until you will corresponding record in the primary key table.
for information take look in following in MSDN links
http://msdn.microsoft.com/en-us/library/ms179610.aspx
Note:
if you want to achieve this functionality then you have write a logic in Stored procedure or trigger.
No,is not automatic add in your foreign key table , it's not make sense , for example if you have two table ,"City" and "People" , People in the City , so there must be a FK refer for People , if you add record in City e.g. New York , How is database know who's need to insert to People table?How many people? , and what this people properties? e.g. People Name?
So database can't do that automatic , you have to do it manually!
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
There are two tables one is a parent i.e., groups table which has foreign key to a child table i.e., users. I am not able to edit foreign key column in parent table where as I have given it to cascade to child table. It gives a error as follows:
Error Code : 1452
Cannot add or update a child row: a foreign key constraint fails (`tms`.`groups`, CONSTRAINT `FK_groups` FOREIGN KEY (`GroupName`) REFERENCES `users` (`groupname`) ON DELETE CASCADE ON UPDATE CASCADE)
Thanks,
-Jeevan
I assume a group contains many users, and a users belongs to one group.
Then you have declared the foreign key in the wrong direction. Actually users.groupname must reference tms.groups. Drop the current foreign key and rebuild it the other way around (in the users table).
This happens if you try to reference a non existing entry in database. In short, you inserted into groups and tried to reference an user entry which doesn't exist yet.
Navicat does not show primary keys which are also foreign key on table report as foreign keys. Why?
I gave the image explaining the situation:
A foreign key is a constraint that applies only to the referencing table. In your case, the translate_talent_id field has a foreign key constraint that references another field of another table.
On the other hand, translator_id is probably referenced by foreign keys in other tables. However, such foreign keys won't appear (or have any effect) on the the referenced table (trl_translator in this case). That is why your MySQL client is not showing any foreign keys on translator_id.