I am a beginner in innoDB and not very good in database. I m getting confused about foreign keys. I just want to know is:
If I delete a PK( fk on another table)
is the FK on child table also deleted?
So how about if I delete FK on child table?
Record in parent table also deleted?
Also, if I ADD a new record to parent table
child table also added?
How about i add to child table?
How about if i update data?
Please help me, I am very confused about foreign key reference in innoDB database . Guide me to the easiest way. Thanks.
Assuming that the foreign key constraints are defined, supported by the storage engine and are enabled, (i.e. MySQL variable foreign_key_checks = 1, Oracle foreign key constraints enabled and not deferred, etc.)
Q: If I delete a PK( fk on another table) , is the FK on child table also deleted ?
A: If the DELETE rule is specified as CASCADE, then a delete on the parent table will also perform delete operation on the child table. If the DELETE rule is specified as RESTRICT, and there's rows in the child table that reference the parent key, then the DELETE operation will raise an error.
(EDIT: For the sake of completeness, we note that "SET NULL" is another option for an UPDATE or DELETE rule.)
Q: If I delete a row from the child table with a reference to a row in the parent table, is the row in the parent table also deleted ?
A: No. Deleting rows in the child table will have no affect on row in the parent table.
Q: If I INSERT a new row to parent table, will a row also be added to the child table>
A: No. Rows are not automatically inserted to child tables. It's valid (relational database-wise) to have a parent row that does not have any child rows referencing it.
Q: If I INSERT a row to the child table ?
A: No. An insert to the child table does not automatically insert a row to the parent table. (Any non-null value of the foreign key column being inserted to the child table will be validated; the database will verify that there already exists a row in the parent table with a matching value. If there's not, then the INSERT will fail with an error.)
Q: What if I UPDATE values of the foreign key column in the child table.
A: Same as with an insert, the new value will be validated before the update operation proceeds.
Q: What if I UPDATE the value of the primary key in the parent table?
A: If the UPDATE rule on the foreign key is specified as CASCADE, then the related rows in the child table will also be updated, to preserve the relationship between the rows. If the UPDATE rule is RESTRICT, and there are related rows in the child table, the update operation will raise an error.
Foreign keys are designed for enforcing referential integrity. They basically disallow inserts/updates in the child table when the new non-null values of the foreign key column doesn't reference a row in the parent.
The UPDATE and CASCADE rules defined on the foreign key constraint determine the behavior of UPDATE and DELETE operations on the parent table.
What your'e asking about is cascading the update/deletion to the child element.
The docs : http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
Example :
CONSTRAINT FOREIGN KEY (jobAbbr) REFERENCESffxi_jobType(jobAbbr) ON DELETE CASCADE ON UPDATE CASCADE
the "ON DELETE CASCADE" means that when the key is deleted, all the FK's are deleted aswell, this is very useful for data integrity. It's however not automatic.
Deleting a foreign key will not delete the key it's based upon either way.
Related
I have a parent table "DROP" with a compound key that is unique
the child table "DROP_MATERIAL" can have 0-many records with the same compound key as a foreign
key with a primary key that is generated and never used
If a DROP column is updated I want to update each of the related child records
Any one have any ideas on how to do this?
I have a MySQL database. I would like to delete all matches where places.match_no > 26 and matches.chart_id = 106.
DELETE matches
FROM kk_matches AS matches
INNER JOIN places ON places.id = matches.place_id
AND places.match_no > 26
WHERE matches.chart_id = 106
This will cause an error:
#1451 - Cannot delete or update a parent row: a foreign key constraint fails...
What to do?
What to do?
Delete the child table dependent row first and then go for deleting parent table row. What you should have actually done is defining CASCADE option while defining your FOREIGN KEY saying ON DELETE CASCADE.
Another option is to have a BEFORE DELETE trigger and in there do DELETE the child table rows
There is another table in your database which has a foreign key to the kk_matches table. Before you can delete a record in the kk_matches table, you need to delete any records in other tables that point to that record in kk_matches. Otherise you would be "orphaning" those other records.
Foreign Keys exist as a means of avoiding these sort of operations that will cause your data to become irrelevant.
You can't delete a row that has a field referenced by a foreign key constraint. You must delete the referencing row first, then delete the row you want (or drop the constraint, if it wasn't intended).
Check this tutorial for more info:
https://www.w3schools.com/sql/sql_foreignkey.asp
, If I have (A,B,C) as PK in parent table and (B,C)[unique] are foreign keys and Primary Key in child table with delete cascade enabled, If I delete a record containing (B,C) from parent table will that delete all the records from child table?
By Definition a Forign Key has to be the same as the PK that it refers to. Your columns in the child table (B,C) do not match the PK of the parent table (A,B,C) and therefore cannot be considered an FK back to that parent.
Cascading deletes will have no impact where a FK-->>PK relationship does not exist, which is the case here, so the answer is no.
I have 3 tables.
The last two tables have a foreign key to the first on the same field.
Both foreign keys are set to UPDATE CASCADE, DELETE CASCADE.
When I delete a child-row in the second table, the parent row in the first table remains unchanged.
But when I delete a child-row in the third table, the first row in the table is deleted!
Foreign keys should not behave in the same way as both daughters?
My guess is that table 2 uses MyISAM tables (which do not support foreign keys) while the other two tables are using the InnoDB engine (which do support foreign keys).
From the documentation:
Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent. The FOREIGN KEY clause is specified in the child table. The parent and child tables must both be InnoDB tables. They must not be TEMPORARY tables.
I have a parent table, and child. The child is newly created table. I connected the child with the parent using foreign key.
The parent already contains data. The child is new empty one. I need to copy data from previous child table (that was previously connected to the parent but I removed the connection because of duplications in values in a column that is supposed to be unique (note: this column that I discovered that it should be unique is not the foreign key of course). However, I type the following statement in order to copy records from the previous child that was connected to the parent:
insert into databasename.newchild select distinct * from databasename.previouschild group by uniquename;
The difference between the new child & the old one, is that I specified a column as unique while it was not unique in the previous child which cause duplicates that shoud not be.
Mysql gives me error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (databsename.newchildtable, CONSTRAINT newchildforeignkeyname FOREIGN KEY (newchildforeignkeyname) REFERENCES parenttable (parentuniquecolumn) ON DELETE CASCADE ON UPDATE CASCADE)
What could be the problem ?