If there is a column in MySQL, use the alter to delete columns

If the column exists, how to use ALTER to delete a column in the MySQL table?

I know that I can use ALTER TABLE my_table DROP COLUMN my_column, but if my_column does not exist, an error will be thrown. What is the alternative syntax to conditionally drop a column?

I use MySQL version 4.0.18.

For MySQL, No: MySQL Feature Request.

Anyway, it may be a very bad idea to allow this: if EXISTS indicates that you are using (for you) a database of unknown structure to run a destructive operation In some cases, this is acceptable for fast and dirty local work, but if you want to run such a statement against production data (in migrations, etc.), then you are playing with fire. /p>

But if you insist, it’s not difficult to check for existence or catch errors on the client first.

MariaDB also supports the following starting from 10.0.2:

DROP [COLUMN] [IF EXISTS] col_name

I. That is

ALTER TABLE my_table DROP IF EXISTS my_column;

But depending on the non-standard function supported by only one of the several branches of MySQL, this may be a bad idea.

If the column exists, how to use ALTER to delete the column in the MySQL table?

I know that I can use ALTER TABLE my_table DROP COLUMN my_column, but if my_column does not exist, an error will be thrown. What is the alternative syntax to conditionally drop a column?

I am using MySQL version 4.0.18.

For MySQL, there is no: MySQL Feature Request.

Anyway, it may be a very bad idea to allow this: if EXISTS indicates that you are using (to you) an unknown structure to run a destructive operation on the database. In some cases, this is for quick and dirty Dirty local work is acceptable, but if you want to run such a statement against production data (in migrations, etc.), then you will play with fire.

But if you insist, It is not difficult to check for existence or catch errors on the client first.

MariaDB also supports the following content starting from 10.0.2:

DROP [COLUMN] [IF EXISTS] col_name

I. That is

ALTER TABLE my_table DROP IF EXISTS my_column;

But only one of the several branches that rely on MySQL supports non-standard features, which may be a bad idea.

Leave a Comment

Your email address will not be published.