ALTER TABLE ALL TABLES ROW_FORMAT=Fixed; - mysql

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.)

Related

MariaDB set row_format to dynamic to all tables in one command

i want to change the row_format to dynamic on all tables in my database. When the datebase is selected i could do "ALTER TABLE tablename ROW_FORMAT=DYNAMIC;" to do it manually. Unfortunately there are around 100 tables to be changed.
How can i change the row format to dynamic on every tables in a specific DB that has something different to DYNAMIC?
I've been trying it but i cant find a working solution.
You can't ALTER TABLE more than one table at a time, but you can generate all the necessary ALTER TABLE statements this way:
SELECT CONCAT(
'ALTER TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME, '` ',
'ROW_FORMAT=DYNAMIC;'
) AS _alter
FROM INFORMATION_SCHEMA.TABLES
WHERE ENGINE='InnoDB' AND ROW_FORMAT <> 'DYNAMIC';
Capture the output of that and run it as an SQL script.

MySQL - How to alter column datatype in all databases?

On my remote server, I need to update a column in a table in all databases. How can I do this all at once? I have over than hundred databases with the same table.
ALTER TABLE tablename MODIFY columnname VARCHAR(255);
You can write a SQL query to emit a script that does this. The query looks at the information_schema table describing columns in your server, and generates a sequence of ALTER queries. You then run those queries.
SELECT CONCAT(' ALTER TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME,
'` MODIFY `',COLUMN_NAME,'` VARCHAR(255);') ddl
FROM information_schema.`COLUMNS`
WHERE TABLE_NAME = 'tablename'
AND COLUMN_NAME IN ('columnname')
There isn't any oneliner to do ALTER TABLE *.tablename or anything like that.

Unable to truncate multiple table at a time

I am trying to truncate 3 tables using their table names out of 5 tables.
Is there any syntax for it as am unable to find
Can anyone give me reference?
to truncate multiple table
SELECT concat('TRUNCATE TABLE ', TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME in ('table1','table2','table3')
or
run it one by one
truncate table table1

Delete MySQL column index without knowing its name

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_%';

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);