MySQL:如何修改一列添加属性"On Delete Cascade"?

MySQL: How to modify a column to add property "On Delete Cascade"?

我正在尝试添加 on delete cascade to a foreign key。我已尝试以下但无法正常工作:

mysql> alter table visits modify fk_targets_visits on delete cascade;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'on delete cascade' at line 1
mysql> alter table visits v modify v.fk_targets_visits on delete cascade; // same error

正确的语法是什么?

你可以这样做:

ALTER TABLE TABLE_NAME
ADD CONSTRAINT YOUR_CONSTRAINT
FOREIGN KEY (YOUR_FK)
REFERENCES TABLE_NAME (YOUR_FK)
ON DELETE CASCADE;