Alter ignore table add column if does not exist using SQL statement

I want to add a new column to mysql table, but I want to ignore adding a column if the column already exists

I am currently using

ALTER IGNORE TABLE `db`.`tablename` ADD COLUMN `column_name` text NULL;

but this causes an error: "ERROR 1060 (42S21): Duplicate column name 'column_name'"

although I use IGNORE, this does not work

I want this to work using a regular SQL statement instead of a stored procedure

+4
source share
1 answer

According to the documentation :

IGNORE - MySQL SQL. , ALTER TABLE , , . IGNORE , , . IGNORE, . . .

, . , , ALTER TABLE ADD IF NOT EXISTS, .

if, , ( INFORMATION_SCHEMA.COLUMNS).

- SQL, .

+3

Source: https://habr.com/ru/post/1530006/


All Articles