In order to DROP a column with a fkey in mySQL, you need the name of the key, as identified in Dropping a column with a foreign key. Then, to identify a key, there's a way to do so in SQL as posted in an answer to How do I see all foreign keys to a table or column?.
I want to remove a column from a table that may have one or more foreign keys programmatically, e.g. without having to manually copy over the name of the foreign key(s) or know the name of said key(s). I don't know how many keys have been created; so the name of a particular key may be tablename_ibfk_5
in one instance and tablename_ibfk_7
in another.
More than one key may be attached to the column-to-remove, so a WHILE
loop seems the most logical solution. Then it also involves creating a procedure. Here's where I got stuck: Apparently the WHILE
loop refuses to function outside a procedure, while the EXISTS
query will throw a nondescript syntax error when used either as a cursor or directly inside said query.
As a solution to this question, I'd accept resolving the problem with my current best attempt, or coming up with a (much) simpler way of doing something that should be straightforward: removing a column and its associated objects.