mysql : loop over tables and alter table add index - mysql

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

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.

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

MySQL Workbench - setting ALL table foreign keys on delete action to NO ACTION in one go

I have a large database with many tables, and "on-delete" action is set to cascade for all of them. Is it possible to change this option to "No action" in one go without having to open each and every table and each and every relation properties in Workbench ?
I presume it is not possible in Workbench interface, but I would bet it can be done programmatically using cursors that loop through all tables and change relations
Is necessary to drop the constraint and add it with ON DELETE CASCADE.
You can have the list of all tables with the SQL to:
1-drop all
SELECT concat('ALTER TABLE ' , TABLE_NAME,' DROP FOREIGN KEY ' ,CONSTRAINT_NAME ,';' )
FROM information_schema.REFERENTIAL_CONSTRAINTS
WHERE constraint_schema = '<database>';
2- add all
SELECT concat('ALTER TABLE ' , TABLE_NAME,' ADD FOREIGN KEY (', column_name ,') REFERENCES ', REFERENCED_TABLE_NAME ,' (', REFERENCED_COLUMN_NAME,') ON DELETE CASCADE;' )
FROM information_schema.key_column_usage where
constraint_name <>'PRIMARY'
AND TABLE_SCHEMA = '<database>';
You can copy the result sql of the queries above and execute.
NOTE: Don't execute the first 'Drop all' before you have stored the information to create again given by the 2nd query (in a text file for example).

Copying Tables Without Data in MySQL Retaining Auto Increment

I have ran into trouble when copying my MySQL Tables to a new one, excluding the data, using the query:
CREATE TABLE foo SELECT * FROM bar WHERE 1=0.
The tables are copied, the structure and column names are correctly inserted. But there is a problem with the auto_increment fields and the primary key fields as they are not inserted as they were on the original table. (The fields are not PKs and AI anymore) I am using MySQL 5.5 and PMA 3.5.8.2
I hope someone can help me out.
Thank you SO.
You will probably have to run 2 queries.
CREATE TABLE foo LIKE bar;
ALTER TABLE foo AUTO_INCREMENT = (SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'DatabaseName' AND TABLE_NAME = 'bar');
You would have to replace DatabaseName with the name of your database. This is untested, but I think it will give you what you are looking for.
So I tried testing the above query and the ALTER TABLE statement seems to fail due to the select. There might be a better way, but the way that worked for me was to set the auto increment value to a variable and then prepare the statement and execute it.
For example you would go ahead and create your table first:
CREATE TABLE foo LIKE bar;
Then set your ALTER TABLE statement into a variable
SET #ai = CONCAT("ALTER TABLE foo AUTO_INCREMENT =", (SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'databasename' AND TABLE_NAME = 'bar'));
Finally, you would prepare and execute the statement.
PREPARE query FROM #ai;
EXECUTE query;
DEALLOCATE PREPARE query;
Other than your columns, the table structure: indexes, primary keys, triggers, etc. are not copied by this kind of statement. You either need to run a bunch of alter table statements to add your structure or you need to create the table with all the surrounding structure first, then load it with your select.

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