Skip to main content
deleted 6 characters in body
Source Link
the_nuts
  • 362
  • 3
  • 11

If you name the index, the query will fail if the index already exists (tested in MySQL 8.0):

ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC);

Error Code: 1061. Duplicate key name 'col_idx';

So you can just catch the exception and ignore it, for example in PHP:

try {
    $db->query('ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC) VISIBLE;');
} catch (PDOException $ex) {
    if ($exception$ex->errorInfo[2] == 1061) {
        // Index already exists
    } else {
        // Another error occurred
    }
}

If you name the index, the query will fail if the index already exists (tested in MySQL 8.0):

ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC);

Error Code: 1061. Duplicate key name 'col_idx';

So you can just catch the exception and ignore it, for example in PHP:

try {
    $db->query('ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC) VISIBLE;');
} catch (PDOException $ex) {
    if($exception->errorInfo[2] == 1061) {
        // Index already exists
    } else {
        // Another error occurred
    }
}

If you name the index, the query will fail if the index already exists (tested in MySQL 8.0):

ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC);

Error Code: 1061. Duplicate key name 'col_idx';

So you can just catch the exception and ignore it, for example in PHP:

try {
    $db->query('ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC) VISIBLE;');
} catch (PDOException $ex) {
    if ($ex->errorInfo[2] == 1061) {
        // Index already exists
    } else {
        // Another error occurred
    }
}
added 16 characters in body
Source Link
the_nuts
  • 362
  • 3
  • 11

If you name the index, the query will fail if the index already exists (tested in MySQL 8.0):

ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC);

Error Code: 1061. Duplicate key name 'col_idx';

So you can just catch the exception and ignore it, for example in PHP:

try {
    $db->query('ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC) VISIBLE;');
} catch (PDOException $ex) {
    if($exception->errorInfo[2] == 1061) {
        // Index already exists, 
 do nothing  } else {
        // Another error occurred
    }
}

If you name the index, the query will fail if the index already exists (tested in MySQL 8.0):

ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC);

Error Code: 1061. Duplicate key name 'col_idx';

So you can just catch the exception and ignore it, for example in PHP:

try {
    $db->query('ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC) VISIBLE;');
} catch (PDOException $ex) {
    // Index already exists, do nothing
}

If you name the index, the query will fail if the index already exists (tested in MySQL 8.0):

ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC);

Error Code: 1061. Duplicate key name 'col_idx';

So you can just catch the exception and ignore it, for example in PHP:

try {
    $db->query('ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC) VISIBLE;');
} catch (PDOException $ex) {
    if($exception->errorInfo[2] == 1061) {
        // Index already exists 
    } else {
        // Another error occurred
    }
}
Source Link
the_nuts
  • 362
  • 3
  • 11

If you name the index, the query will fail if the index already exists (tested in MySQL 8.0):

ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC);

Error Code: 1061. Duplicate key name 'col_idx';

So you can just catch the exception and ignore it, for example in PHP:

try {
    $db->query('ALTER TABLE `my_table` ADD INDEX `col_idx` (`col` DESC) VISIBLE;');
} catch (PDOException $ex) {
    // Index already exists, do nothing
}