Delete MySQL column index without knowing its name - mysql

I have an application which uses Hibernate to support Oracle and MySQL databases. After an update I have to manually delete some columns with indexes/constraints on it. These indexes have Hibernate generated random names.
In Oracle I can do this:
ALTER TABLE table_name DROP (column_name) CASCADE CONSTRAINTS;
Unfortunately this isn't possible for MySQL. Is there a possibility to do something like this
DROP INDEX (SELECT Key_name FROM (SHOW INDEX FROM table_name WHERE Column_name = 'column_name')) ON table_name;
before I drop the column?
EDIT: This should work without user interaction in a SQL script.

You can select indexes for a table form information_schema:
SELECT DISTINCT INDEX_NAME, TABLE_NAME, TABLE_SCHEMA FROM information_schema.STATISTICS;

There is no need to manually delete the indexes, MySQL 5.7 Reference Manual says:
If columns are dropped from a table, the columns are also removed from
any index of which they are a part. If all columns that make up an
index are dropped, the index is dropped as well. If you use CHANGE or
MODIFY to shorten a column for which an index exists on the column,
and the resulting column length is less than the index length, MySQL
shortens the index automatically.

To get all indexes for a particular database (replace <Database_Name> with your database name), use:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>';
To get all indexes for a table (replace <Table_Name> with your table name) of a particular database, use:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>' AND
TABLE_NAME LIKE '<Table_Name>';
To get all indexes of a specific column (replace <Column_Name> with your column name) of a table in a particular database, use:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>' AND
TABLE_NAME LIKE '<Table_Name>' AND
COLUMN_NAME LIKE '<Column_Name>';
In addition to that, you may also use any Wildcard character in the LIKE operator to get specific records, like:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>' AND
TABLE_NAME LIKE 'tbl_prefix_%';

Related

MySQL get list of tables ending with specific name and it's (table's) comment

I have multiple tables in my multiple databases.
On different servers, i use MySQL / PostgreSQL / MS SQL.
I keep short table namesbut the comments given to the tables are with full explanation.
I want query that will show me tables ending with "com" and also the comment given to each table (table's comment).
In MySQL, I know:
SELECT table_name FROM information_schema.tables where table_name like "%com"
But this shows all tables from all databases.
For MySQL, check out following:
SELECT table_name FROM information_schema.tables;
will show all table names in all databases;
SELECT table_name,table_comment FROM information_schema.tables
will show all table names + comment in all databases;
interesting thing, you can fire
SELECT * FROM information_schema.tables;
to know what all info you can get of a table.
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b';
will show all table names + comment in "sifr_b" database;
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b' and
table_name like "%com";
will show those table names + comment in "sifr_b" database, that have table name ending with "com";

how do i validate already table in mysql schema?

Could not validate table exists in MySQL.
Tried below query but no output.
if !(select * from information_schema.tables where table_name='sy_code')
To see the schema of a table, use query
DESCRIBE table_name;
To see all the existing table in our database, use query
SELECT table_name FROM information_schema.tables WHERE table_schema = 'your_db'

ALTER TABLE ALL TABLES ROW_FORMAT=Fixed;

How can I run a command for all tables in a database?
ALTER TABLE table_name ROW_FORMAT=Fixed;
I have more than 40000 tables, I want to change them all in one go.
You cannot alter table more than one table per ALTER statement; but you can use a query on information_schema.tables to generate the alter statements.
Something like
SELECT CONCAT('ALTER TABLE `', table_name, '` ROW_FORMAT=fixed;') AS aQuery
FROM information_schema.tables
WHERE table_schema = 'myschema'
Keep in mind FIXED is not supported for InnoDB tables, and I am not 100% sure if it can even be explicitly set for MyISAM ones (or is entirely dependent on the table's columns' data types.)

Unknown table 'table_name' in information_schema

I want to show index from each table that has table_schema='foo' (database name).
mysql> show index from table_name from information_schema.tables where table_schema='foo';
ERROR 1109 (42S02): Unknown table 'table_name' in information_schema
From the error, I see that the query treats 'table_name' as a table in information_schema. How do I rewrite the query to treat 'table_name' as a column in information_schema.tables?
You're approaching this wrong, and you're making up syntax that doesn't exist.
I suggest the way you want to get the indexes is by reading the INFORMATION_SCHEMA.STATISTICS table, not the TABLES table.
The following query has the same columns as SHOW INDEXES:
SELECT table_name AS `Table`, Non_unique, index_name AS Key_name,
Seq_in_index, Column_name, Collation, Cardinality, Sub_part,
Packed, Nullable, Index_type, Comment, Index_comment
FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_schema = 'foo';
You might think there should be an I_S table called "INDEXES" but in fact the system table for index objects is named "STATISTICS". Go figure.

mysql : loop over tables and alter table add index

I have ~1000 tables that start with the same prefix :
table_prefix_{SOME_ID} (i can take the ids from another table)
what is the fast way to loop over all the tables in mysql and do :
ALTER TABLE `table_prefix_{some_id}` ADD INDEX `fields` (`field`)
Forget looping. Just do this:
select concat( 'alter table ', a.table_name, ' add index `fields` (`field`);' )
from information_schema.tables a
where a.table_name like 'table_prefix_%';
Then take the result set and run it as a SQL script.
BTW, you probably mean create index index_name on table_name( column_name);