Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

search simple way to alter multi tables in one time in mysql

i have a lot of tables that start with some prefix ,

and i want to alter this tables

what is the simple way to do this (instead run over all tables)

i mean something like :

ALTER TABLE  LIKE tablenameprefix% ADD INDEX `NewIndex1` (`field`);

how can i do this ?

thanks

EDIT :

can i do a kind of loop not in stored procedure ? by select the names of tables from

SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME LIKE 'tableprefix%'
like image 540
Haim Evgi Avatar asked Feb 26 '23 16:02

Haim Evgi


2 Answers

You can't. What you could do though is write a stored procedure that enumerates all tables looking for your prefix and performs the necessary changes.

like image 95
Adrian Grigore Avatar answered Mar 08 '23 16:03

Adrian Grigore


Given that ALTER TABLE syntax doesn't allow multiple table names, you cannot do this. You need to go through all tables in turn:

ALTER [IGNORE] TABLE tbl_name
alter_specification [, alter_specification]

Link: http://dev.mysql.com/doc/refman/5.0/en/alter-table.html

like image 37
Anax Avatar answered Mar 08 '23 15:03

Anax