I already have my forein keys and so on. I only need to add the ON DELETE CASCADE Option. How can I do that? Something with Alter Table? I'm using mysql with phpmyadmin
-
Possible duplicate of [How to add 'ON DELETE CASCADE' in ALTER TABLE statement](http://stackoverflow.com/questions/1571581/how-to-add-on-delete-cascade-in-alter-table-statement) – Dan May 30 '16 at 17:54
3 Answers
I don't know which version of phpMyAdmin you're using, but on the 4.5.2 version, here's how it works if you want to do it without manually writing the SQL query :
- Select your table
- Go to
Structuretab - Go to
Relation view"sub-tab" - Define your constraints
- Save your changes
Note that this is not possible with the MyISAM storage engine (seems to be the default one on my installation) and you have to select the InnoDB one :
- Select your table
- Go to
Operationstab - Change
Storage EnginetoInnoDB - Save
For the older phpMyAdmin versions, I know that it was slightly different, as the Relation view was accessible by a link below the table structure.
- 4,535
- 5
- 28
- 61
The simplest way would be to delete the current constraint and add new one with ON DELETE
To delete: ALTER TABLE mytable DROP FOREIGN KEY 'fk_anothertable'
Then create it again:
ALTER TABLE mytable
ADD CONSTRAINT 'fk_anothertable'
FOREIGN KEY ('aColumn' )
REFERENCES 'anotherTable' ('aColumn' )
ON DELETE CASCADE
- 2,518
- 2
- 18
- 33
Unfortunately, you can only create and drop constraints using alter table, you cannot change an existing one. There is no separate alter constraint command either. Therefore all you can do is drop the foreign key and recreate it adding the on delete cascade clause.
- 33,525
- 10
- 51
- 64