On delete cascade - where I have to add it - php

I needed an example of cascade delete. My question is, where do I add it? In the table I create PK, or in other tables where this PK is sitting there as FK?
Can I use "alter table" to add "on delete cascade" to existing table? An example please?
#edit
MYSQL, using phpMyAdmin
#edit 2
Does it look good?
alter table wplaty
drop foreign key pesel,
add constraint pesel foreign key (pesel)
references baza_osob(pesel) on delete cascade on update restrict;
My parent table = baza_osob
My child table = wplaty
PK is pesel, FK is pesel as well.
#edit3
Got error:
#1025 - Error on rename of '.\projekt\wplaty' to '.\projekt#sql2-1300-6c' (errno: 152)

cascade directives go into the "child" tables, e.g.
create table parent (
id int primary key
)
create table child (
id int primary key
parent_id int,
foreign key (parent_id) references parent (id)
on delete cascade
)
Never tried doing an alter on a foreign key to change its on settings, but worst case, you simply drop the existing FK and redefine it with the new on settings in place.

You actually don't add the 'ON DELETE CASCADE' on a table. It's part (or not) of each Foreign Key definition.

You need to enable this option for the foreign key. If you did not add this option when you created foreign key, then you should recreate it.
alter table <table> drop foreign key <fk name>;
alter table <table> add constraint <fk name> foreign key (<column name>)
references <ref tble>(<ref column) on delete cascade on update restrict;
In one statement:
alter table <table>
drop foreign key <old fk name>,
add constraint <new fk name> foreign key (<column name>)
references <ref tble>(<ref column) on delete cascade on update restrict;

Related

needed in a foreign key constraint error by doctrine

I have database project migration from Symfony 3.2 -> 3.4
I made new project and copy entity to the new 3.4 environment.
then,
php bin/console doctrine:schema:update --force
it shows the error.
An exception occurred while executing 'DROP INDEX IDX_EFE42E9BC5E35EFC ON placeinfos_placecats':
SQLSTATE[HY000]: General error: 1553 Cannot drop index 'IDX_EFE42E9BC5E35EFC': needed in a foreign key constraint
So, I am checking the placeinfos_placecats table,
mysql> show create table placeinfos_placecats;
| placeinfos_placecats | CREATE TABLE `placeinfos_placecats` (
`placeinfo_id` int(11) NOT NULL,
`placecat_id` int(11) NOT NULL,
PRIMARY KEY (`placeinfo_id`,`placecat_id`),
KEY `IDX_EFE42E9BC5E35EFC` (`placecat_id`),
CONSTRAINT `FK_EFE42E9B6762112` FOREIGN KEY (`placeinfo_id`) REFERENCES `PlaceInfo` (`id`) ON DELETE CASCADE,
CONSTRAINT `FK_EFE42E9BC5E35EFC` FOREIGN KEY (`placecat_id`) REFERENCES `PlaceCat` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
There is constraint IDX_EFE42E9BC5E35EFC.
And then, what the doctrine tries to do,
php bin/console doctrine:schema:update --dump-sql
DROP INDEX IDX_EFE42E9BC5E35EFC ON placeinfos_placecats;
DROP INDEX IDX_EFE42E9B6762112 ON placeinfos_placecats;
ALTER TABLE placeinfos_placecats DROP PRIMARY KEY;
ALTER TABLE placeinfos_placecats ADD place_info_id INT NOT NULL, ADD place_cat_id INT NOT NULL, DROP placeinfo_id, DROP placecat_id;
ALTER TABLE placeinfos_placecats ADD CONSTRAINT FK_EFE42E9B6947D603 FOREIGN KEY (place_info_id) REFERENCES place_info (id) ON DELETE CASCADE;
ALTER TABLE placeinfos_placecats ADD CONSTRAINT FK_EFE42E9B4463AE2A FOREIGN KEY (place_cat_id) REFERENCES place_cat (id) ON DELETE CASCADE;
CREATE INDEX IDX_EFE42E9B6947D603 ON placeinfos_placecats (place_info_id);
CREATE INDEX IDX_EFE42E9B4463AE2A ON placeinfos_placecats (place_cat_id);
ALTER TABLE placeinfos_placecats ADD PRIMARY KEY (place_info_id, place_cat_id);
ALTER TABLE fos_user DROP FOREIGN KEY FK_957A6479B6E0899D;
ALTER TABLE fos_user CHANGE userkey user_key VARCHAR(255) DEFAULT NULL;
ALTER TABLE fos_user ADD CONSTRAINT FK_957A6479B6E0899D FOREIGN KEY (metainfo_id) REFERENCES meta_info (id);
ALTER TABLE media__gallery_media DROP FOREIGN KEY FK_80D4C5414E7AF8F;
ALTER TABLE media__gallery_media DROP FOREIGN KEY FK_80D4C541EA9FDD75;
ALTER TABLE media__gallery_media ADD CONSTRAINT FK_80D4C5414E7AF8F FOREIGN KEY (gallery_id) REFERENCES media__gallery (id) ON DELETE CASCADE;
ALTER TABLE media__gallery_media ADD CONSTRAINT FK_80D4C541EA9FDD75 FOREIGN KEY (media_id) REFERENCES media__media (id) ON DELETE CASCADE;
DROP INDEX IDX_5C6DD74E12469DE2 ON media__media;
ALTER TABLE media__media DROP category_id;
What does it mean???
Just dropping and adding constraint again??
Why doctrine wants to do like this ?
Then ,how should I solve it??
I just deleted the table manually via adminer. Withot "foreign key check".
Then, I did bin/console doctrine:schema:update -f and its ok!
I suppose this closely connected with Foreign Key restrictions. So, you can somehow exclude this check in doctrine.
But be careful. I guess method below isn't really good way for prod.
Actually, you could delete a special KEYNAME via adminer!
IDX_EFE42E9BC5E35EFC, for example...

MySQL cascade delete on self-referencing parent-child table?

I have this MySql database table:
tbl_project
id
project_name
parent_id
If a project is a 'parent', parent_id is 0.
Now I'm trying to add a self referencing foreign key
CONSTRAINT `FK_tbl_project_tbl_project` FOREIGN KEY (`parent_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE
When I try to insert a new record,
SQL Error (1452): Cannot add or update a child row: a foreign key constraint fails (mydbname.#sql-3539_d7d, CONSTRAINT FK_tbl_project_tbl_project FOREIGN KEY (parent_id) REFERENCES tbl_project (id) ON DELETE CASCADE)
Basically I just want all the children to be deleted when I delete the parent. What am I missing here?
When you enter any value into the parent_id column of a new row, the foreign key requires that there is a row where id has that value, because that's how foreign keys work.
If you want to use a foreign key with your self-referencing relationship, you should use NULL instead of 0 for the parent_id of a row that has no parent to reference. Foreign keys ignore NULL.

How to add "ON UPDATE SET NULL" to foreign key constraint?

How can I use the Laravel Schema Builder to add "ON UPDATE SET NULL" to my foreign key constraint?
I do not understand why to set as null on update; but this is the idea:
You can delete the existent constraint.
ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;
And create it again
ALTER TABLE tbl_name
ADD CONSTRAINT my_new_FK
FOREIGN KEY (column_in_this_table)
REFERENCES other_table (other_table_pk_column)
ON DELETE SET NULL;
You can find more information about constraints and the alter table statement in the mysql documentation.
Using FOREIGN KEY Constraints
ALTER TABLE Syntax

Mysql foreign key

so i am trying again and again to make a column as a foreign key but it gives me Constraint name error? what Constraint name should i put? i am using PHPmyadmin SQL , I tried giving it names such as trackid and so on but still i am getting an error
ALTER TABLE `ss_ordered_carts`
ADD CONSTRAINT `track_id` FOREIGN KEY (`track_id`)
REFERENCES `ProjectDatabase`.`ss_orders` (`track_id`)
ON DELETE RESTRICT ON UPDATE CASCADE;
MySQL said: Documentation
1452 - Cannot add or update a child row: a foreign key constraint fails (projectdatabase.
'#sql-1414_6c7'>, CONSTRAINT track_id FOREIGN KEY (track_id) REF
ERENCES ss_orders (track_id) ON UPDATE CASCADE)
If you have data in child table for the referred column field, to add foreign key on that field,
first you have to disable foreign key checking.
And then alter to add foreign key.
And lastly reset the constraint checking.
Step 1: Disable the foreign key checks.
set foreign_key_checks=0;
Step 2: Now add the foreign key constraint.
ALTER TABLE `ss_ordered_carts`
ADD CONSTRAINT `track_id` FOREIGN KEY (`track_id`)
REFERENCES `ProjectDatabase`.`ss_orders` (`track_id`)
ON DELETE RESTRICT ON UPDATE CASCADE;
Step 3: Now reset the foreign key checks.
set foreign_key_checks=1;
It is possible that you have table ss_ordered_carts filled with wrong data, that cannot be related. Check it using this query -
SELECT * FROM ss_ordered_carts t1
LEFT JOIN ss_orders t2
ON t1.track_id = t2.track_id
WHERE
t2.track_id IS NULL;
Then modify table's data, and after, create foreign key.
Try this.
mysql> SET foreign_key_checks = 0;
mysql> ALTER TABLE ss_ordered_carts
ADD CONSTRAINT track_id
FOREIGN KEY(track_id)
REFERENCES ss_orders(track_id)
mysql> SET foreign_key_checks = 1;
May be, you are getting the specified error as there exists data in the tables.
This may also occur if child table contain some data with the foreign key that that are not in parent table. In this case try,
Delete from child where child_id NOT IN (select parent_id from parent where parent.id = child.id)

phpmyadmin doesnt allow me to add primary/foreign key relationship

That is the primary table field (tasks table):
task_id int(10) UNSIGNED No None AUTO_INCREMENT
This is my foreign table field (url_error_stats table):
task_id int(10) UNSIGNED No None
url_error_stats doesnt present the "relation view" option to connect between the keys..why?
SQL query:
ALTER TABLE url_error_stats ADD FOREIGN KEY ( task_id )
REFERENCES aws_backlinks.tasks (
task_id ) ON DELETE CASCADE ON UPDATE CASCADE ;
MySQL said:
1452 - Cannot add or update a child row: a foreign key constraint
fails (aws_backlinks., CONSTRAINT #sql-6f0_3bd_ibfk_1 FOREIGN KEY
(task_id) REFERENCES tasks (task_id) ON DELETE CASCADE ON UPDATE
CASCADE)
you have to use innodb and index the primary key if you want to create the foreign keys. and I will recommend you to use NAVICAT . its much easier to create foreign keys and quick too. but for a quick phpmyadmin guide see
Setting up foreign keys in phpMyAdmin?
Another reason can be the unrelated data in your tables. I mean you may have a foreign key that doesn't exist in parent table.
in this , click on url_error_stats table, then on right hand side it will show all the fields list, so now check on checkbox of that particular field which u wanted to be foreign and click on the link relation view (which is provided by phpmyadmin below to the table fields with blue color hyperlink).
it will open relationship screen , there You can select the field of the primary table.
Thanks

Categories