Difference between ADD FOREIGN KEY and ADD CONSTRAINT fk_foreign_key
mysql, sql
Solution
Both queries are more or less the same, though according to the manual the `FOREIGN KEY` should be present in both queries.
[CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]
The benefit of `CONSTRAINT [symbol]` is that you can name the constraint rather than MySQL generating the name for you; this symbol must be unique within the database and can be used to later remove the constraint.
Generated symbol names can be seen by checking the table structure after creation:
SHOW CREATE TABLE TABLE_A;
Problem
Does the following in MySQL: ``` alter table TABLE_A ADD CONSTRAINT fk_id (id) REFERENCES TABLE_B(id) ON DELETE CASCADE ON UPDATE CASCADE; ``` do the same as ``` alter table TABLE_A ADD FOREIGN KEY (id) REFERENCES TABLE_B(id); ``` except that it also adds a BTREE index on the relationship? Or do they complement each other?