How can I rename all the indexes in MySQL - mysql

I have a MySQL database where the names of indexes are shared between tables. Which is fine in MySQL, because index names only have to be unique within a table, and not within a database. But I have to export this database to a system that requires index name be globally unique.
Is there some command or script I can run to assign unique names to the indexes? I don't care if they are randomly generated.

As of the latest version of MySQL, there is no way to rename indexes. Your only option would be to DROP the index then CREATE a new one or just CREATE a new one.

I think it's worth highlighting what #zerkms alluded to in a comment above - you can rename an index (since MySQL 5.7):
alter table mytable RENAME INDEX old_index_name TO new_index_name

There is no RENAME or ALTER index command on MySQL.
You need to either DROP the index or CREATE a new one.

MySQL supports no syntax in ALTER TABLE to rename an index (or key, which is a synonym).
You can ALTER TABLE DROP KEY and ALTER TABLE ADD KEY.
There is no ALTER INDEX command in MySQL. You can only DROP INDEX and then CREATE INDEX with the new name.

Related

Is it possible to alter a MySQL unique index to non-unique without recreating it

Is it possible to alter a MySQL unique index to non-unique without recreating it (DROP and CREATE)?
I'm using MySQL version 8.0.19.
No, in MySQL it is not possible to change the definition of an index without dropping it and recreating it. There is no ALTER INDEX statement.
Correction: You can rename an index without rebuilding it. You can make an index visible/invisible without rebuilding it.
If you've meant that you want to avoid dropping the unique index, then please read Bill Karwin's answer, which properly describes that it's not possible.
However, if you've meant that you want to recreate an index without dropping and recreating the table, then it's certainly possible, like:
DROP INDEX indexname on tablename;
and then
CREATE UNIQUE indexname ON tablename(columnname);

Navicat rename field in model and sync to server without drop

I created a model from a database schema's tables. It's working correctly, but when I rename a column in a table and after that I sync to the server. Navicat recognize the changes and show the SQL queries which needed to be executed. The queries are the following:
disable foreign key checks
drop indexes
create column with new name
drop old column
recreate previously dropped indexes with the currently created column
enable foreign key checks
How can I force Navicat to not drop columns, just rename them?
Thanks in advance, kukko.
Have a look at object editors in dbForge Studio for MySQL. There is possibility to rename the field with dependent object modifications.
Here it is another simple way to rename the column:
select column in Database Explorer
press F2 (Rename command) and enter new name
choose Rename or Refactor (with dependent objects modifications)
By simply executing the statement to rename the column:
ALTER TABLE your_tablename CHANGE column_name new_name datatype other_options_like_nullable;
For example:
ALTER TABLE myTable CHANGE badname newname INT NOT NULL DEFAULT 0;

Clarification on MySQL 5.6 using IN PLACE alter table for adding/dropping the same index

From the docs:
An ALTER TABLE statement that contains DROP INDEX and ADD INDEX
clauses that both name the same index uses a table copy, not Fast
Index Creation.
This is a bit unclear to me. Is it talking about the NAME of the index? Can someone give an example of a query in which MySQL resorts to a table copy?
Indeed, it sounds like this line is about:
An (One, single) ALTER TABLE statement
that contains (both) a DROP INDEX and an ADD INDEX clause
and both clauses name the same index
and states that such a statement uses a table copy, not Fast Index Creation.
Such a statement would be:
ALTER TABLE MyTable
DROP INDEX MyIndex
ADD INDEX MyIndex(MyColumn);
The documentation is not really clear about the reason behind this, but I think the database want to create an index first and then drop the other index, so the statement by itself can more easily be made atomic. (Creating the index might fail.) If the index name itself is used in the storage as well, that order of first creating then dropping would give a conflict.
After all, fast index creation is a relatively new feature, so they might improve this over time.

How do I keep a MySQL Fulltext Index up to date?

Creating an index MySQL
After I have added the FULLTEXT index, how do I keep it up to date?
Added it this way : ALTER TABLE search_index ADD FULLTEXT(si_fulltext)
Tried to update it like this :
ALTER TABLE search_index MODIFY FULLTEXT(si_fulltext)
ALTER TABLE search_index CHANGE FULLTEXT(si_fulltext)
Thanks,
Joe
Unless you disabled the index, the server should be keeping the index up to date as data changes in the column. If you'd like to explore what is in the index, you can use the myisam_ftdump utility:
http://dev.mysql.com/doc/refman/5.0/en/myisam-ftdump.html
It's updated automatically whenever data is inserted, updated or deleted form search_index.

Alter a live table to make a key non-unique

I saw some other questions related to this, but they were not MySQL.
The database is a live database, so I don't want to delete and recreate the table. I simply want to make a column no longer unique, which is less permissive in nature so it shouldn't cause any problems.
If your column was defined unique using UNIQUE clause, then use:
ALTER TABLE mytable DROP INDEX constraint_name
, or, if your constraint was implicitly named,
ALTER TABLE mytable DROP INDEX column_name
If it was defined unique using PRIMARY KEY clause, use:
ALTER TABLE mytable DROP PRIMARY KEY
Note, however, that if your table is InnoDB, dropping PRIMARY KEY will result in implicit recreation of your table and rebuilding all indexes, which will lock the table and may make it inaccessible for quite a long time.
These are instructions for phpmyadmin app (if you are using phpMyAdmin) ::
In a some cases, the developer (you) may not want to drop it but rather just modify the "uniqueness" to "not-unique".
Steps :
Go to the table in context, where you want to make the modification
Click on the "Structure" tab (mostly next to Browse)
Look for the "+Indexes" link, just under the columns. Yeah... now click it
Now you can see all the "Indexes" and you can now click on the "DROP" button or link to modify.
Answer was found here :
Source : https://forums.phpfreaks.com/topic/164827-phpmyadmin-how-to-make-not-unique/
Just DROP the unique index. There shouldn't be a problem with the fact that it is a live DB. If it is a really large table, you may block some queries temporarily while the index is removed. But that should only happen if you were adding an index.
ALTER TABLE table_name DROP INDEX index_name;
Although the accepted answer is incorrect (see comments), the suggested workaround is possible. But it is not correct too, at least for a "live table", as asked.
To lower the impact you should create a new index at first, and then delete the old one:
ALTER TABLE mytable ADD INDEX idx_new (column);
ALTER TABLE mytable DROP INDEX idx_old;
This avoids using the table (column) without index at all, which can be quite painful for clients and the MySQL itself.
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast (MySQL Manual).
If the unique key that you want to make non-unique is used by a foreign key constraint, then you'll get an error when dropping it. You will have to recreate it on the same line:
alter table mytable drop KEY myUniqueKey, add key myUniqueKey (myColumn);