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%'
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.
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
When I wanted to change multiple table's engine from MyISAM to InnoDB, instead of writing a loop I just made a full DB dump and opened it in a text editor. In the text editor I just simply changed all MyISAM words to InnoDB.
I know that this ain't proper solution but for me it was easier then writing a routine for this.
You would have to write a loop, according to the documentation you just specify the table name.
I made a stupid mistake of putting all Wordpress tables with my product tables, fortunately all Wordpress tables start with a wp_ prefix and all my other product tables have no this wp_ prefix.
I created another database named wordpress, now I want to move all tables start with wp_ to that database.
Here is what I did:
SELECT CONCAT('ALTER TABLE olddb.', table_name, ' RENAME wordpress.', table_name, ';')
FROM information_schema.tables
WHERE table_schema='olddb' AND table_name LIKE 'wp%'
INTO OUTFILE '/tmp/move_to_wordpress';
SOURCE /tmp/move_to_wordpress;
That's it.
Related
I have a database that contains a table per client, each table has the same columns in them. We're talking a few thousand client tables.
I need to add new columns to each of these tables for new development but cannot find a way to recurse all the tables in the database to add the columns. I know MS SQL has something sp_MSforeachtable which does maybe what I'm asking but I don't know if MySQL has anything similar?
As per this previous answer in SO you could do something like this :
select concat('ALTER TABLE `',table_name,'` ADD `test` INT NOT NULL AFTER `column_x`;')
from information_schema.tables
where table_schema = 'your_db_name'
Then remove the few tables not about client (here's hoping client is the only entity to have "private table") or if possible add a condition on the table_name (like AND table_name LIKE "client_%"), and execute the whole batch.
The 2nd solution, to use procedure, is too complex (for me) for this use case, but maybe someone more skilled than me in PLSQL won't agree.
I need to Add & Delete merged tables in the UNION=() line. According to the MySQL docs it says:
DROP the MERGE table and re-create it.
Use ALTER TABLE tbl_name UNION=(...) to change the list of
underlying tables.
The only "DROP" I'm aware of is DROP TABLE tablename; Are these instructions suggesting that I drop the MRG_MyISAM table, then recreate it with an empty UNION=() field? To then be followed by an ALTER TABLE tbl_name UNION=(...) with all the tables I need to have connected?
If possible, could you post an example of the commands?
Thanks
Oh boy, am I late here. But this page is in the top google search results for "alter table tbl_name union=(...)". So I guess it needs an answer
So here's the answer.
To change the union list of underlying tables for merge table you only need to execute this statement
alter table tbl_name union=(`t1`,`t2`,`t3`);
where t1,t2,t3 is a list of tables you want to have in a union.
You can drop merge table and recreate it with a new list of underlying tables.
Drop statement execute on merge table will only delete the merge table itself and won't affect underlying tables.
But altering it should be sufficient. And you don't need to recreate it with empty union, if you ever do that, just use list of tables that you want to have in it.
For more, please refer to documentation:
https://dev.mysql.com/doc/refman/5.7/en/merge-storage-engine.html
I am working on MySql Workbench and everytime I rename a table/column, the existing FKs and IDXs names don't change. I ended up with a model with all the FK/IDX names messed up. Is there any way to regenerate all names according to the new tables and columns names or do I have to change them one by one?
Straight on the model i haven't found any regeneration of FKs etc. A workaround is the following, but you have to forward engineer your model first in order to have a schema:
Use and customize the following statement:
SELECT TABLE_NAME, INDEX_NAME, COLUMN_NAME,
CONCAT('ALTER TABLE `your_schema`.`',TABLE_NAME,'` DROP INDEX `',INDEX_NAME,'`, ADD INDEX `fk_',TABLE_NAME,'_',COLUMN_NAME,'_idx` (`',COLUMN_NAME,'` ASC);') as statement
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'your_schema';
Customize the WHERE above in order to exclude e.g. PRIMARY indexes or anything else.
You can also set the prefix and suffix of index names that is appropriate for you in the CONCAT expression.
The first 3 columns in the above SELECT will be used in order to create the fourth expression. You can use the result of the fourth expression to batch-create the needed statements.
It may not seems the fastest solution but, once you add your final statement to your snippets, the next time will be quick and easy to regenerate your schema indexes.
I have a Moodle installation that I migrated to another server and I need to change several references to the old domain.
How can I replace a string for another string in MySQL for a given database searching all tables, all fields, and all rows?
I don't need to change the field names, just the values.
Related: How can I use mySQL replace() to replace strings in multiple records?
But the marked as answer solution implies I strongly type the table name and I need to fire this into an entire database, not manually work on each table running the script N times.
This may seem a bit ... ugly. But maybe simply dump the database to a sql/txt file, replace all strings and recreate the database using the modified dump.
You could run the following code to create all the udpate statements you would need to run to do your updates. It would update every field in every table within your database. You would need to run this code, and copy the results and run them.
WARNING - Be sure to test this in a test environment. You don't want any unpleasant surprises. Modify as needed.
SELECT concat('UPDATE ', TABLE_NAME, ' SET ', COLUMN_NAME, ' = REPLACE(', COLUMN_NAME, ', ''STRING_TO_REPLACE'', ''REPLACE_STRING'')')
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE TABLE_SCHEMA = 'YOUR_DATABASE_NAME'
AND DATA_TYPE IN ('char', 'varchar')
;
I limited this to only char and varchar fields. You may need to add additional data types.
I would consider querying INFORMATION_SCHEMA.COLUMNS and dynamically searching the columns and tables. Try something like creating a cursor of all the columns and tables in the db:
DECLARE col_names CURSOR FOR
SELECT column_name, table_name
FROM INFORMATION_SCHEMA.COLUMNS
Then iterate through each of the columns in each of the tables and run dynamic/prepared sql to update where the string exists.
Here are a couple of good posts to get you in the right direction:
https://stackoverflow.com/a/4951354/1073631
https://stackoverflow.com/a/5728155/1073631
There is a MySQL database with several(but we don't know how much) tables. The only thing I know about them, is that all of them have a prefix 'pref'. So how can I search in every table, if a don't know their names ? Can you help me with the query ?
Sorry for my bad english
Do you want to take out all the tables names starting with prefix 'pref'.
If YEs, you can run following query:
SELECT table_name FROM information_schema.tables WHERE table_name LIKE 'pref%'
You can query information_schema.tables to know all the tables starting with 'pref' and query them individually.
Use a stored procedure if this is a frequent task.