Batch replacements in MySQL Database - mysql

I've learned how to do simple search and replace operations in my MySQL databases using phpMyAdmin...
UPDATE my_table SET my_column = replace(my_column,'Spain','Spanish')
I just wondered if there are ways to target more than one fields and/or tables at a time. For example, if you have three tables named One, Two and Three and each one has a field named Article, could you do a search-and-replace operation in all three tables simultaneously? Or could you even search fields with different names, like One.Article, Two.Article, Three.Content?
Solutions don't have to be limited to phpMyAdmin. I'm interested in learning about other popular DB administration programs.

Yes, UPDATE supports multi-table syntax:
UPDATE One, Two, Three
SET One.Article = replace(One.Article,'Spain','Spanish'),
Two.Article=replace(Two.Article,'Spain','Spanish'),
Three.Content=replace(Three.Content,'Spain','Spanish');

Related

How to manage schema changes on many identical schema-based databases with mysql?

I'm developping a web platform to manage student registrations in schools of my region. For that I have 17 databases running on MySQL (5.7.19) where there is one which is the main database and the 16 others represent schools. Schools databases (must) have the exactly the same schema, each containing data corresponding to the associated school. I separated this way to avoid latency as each school can register many applications (16k on average), so the requests could get heavier over time.
Now I have a serious problem: when I change the schema of a school's database, I have to manually do it for those of other schools to keep the schema consistency because my sql requests are made independently of the school. For example, if i add a new field in table_b of database_school5, i have to manually do the same on table_b of all remaining databases.
What can I do to manage theses changes efficiently? Is there an automatic solution? Is there an adapted DBMS for this problem?
Somebody told me that PostgreSQL can achieve this easily with INHERITANCE, but this only concerns the tables, unless I've done some poor research.
I want every time I make a change to a database schema, whether it is adding a table, adding a field, removing a field, adding a constraint, etc., the changes are automatically transferred to the other databases.
Thanks in advance.
SELECT ... FROM information_schema.tables
WHERE schema_name LIKE 'database_school%'
AND table_name != 'the 17th table'
AND schema_name != 'database_school5' -- since they have already done it.
That will find the 16 names. What you put into ... is a CONCAT(...) to construct the ALTER TABLE ... statements.
Then you do one of these:
Plan A: Manually copy/paste those ALTERs into mysql commandline tool to perform them.
Plan B: Wrap all of it in a Stored Procedure that will loop through the results of the SELECT and prepare+execute each one.

What's the difference between `use my_db` and `my_db.my_table` in mysql?

We have lots of database in one instance.
Our query language is:
use db1; select * from table1;
use db2; select * from table2;
But when we change to this style, the MySQL server CPU load dropped a lot:
select * from db1.table1;
select * from db2.table2;
What's the different between them?
Which one should we use?
MySQL can work with multiple databases in a single query. Each table belongs to a single database. Its full name is: database.table.
If you are sticking to a single database, then you probably don’t want to work with the database prefix. USE database allows you to dispense with the prefix.
If you are using multiple databases simultaneously, then you’re never going to be in the right database. At the very lease, you will need to prefix the other database tables.
I don’t know how much work MySQL does when you apply the USE database statement, but I imagine that it’s not very important in the overall scheme of things. Under normal circumstances you are working with a single database so issuing the USE statement once should be the end of it.
Certainly if you’re constantly switching between the two, and especially if you’re joining tables from different databases, then you should prefix the tables.

selecting multiple tables in mysql and combine the results [duplicate]

The table names in my mysql database are dynamically generated. Is there some way to select data from tables which have a name matching a pattern? I guess it will look like:
select * from 'table_id_%'
No, you can't do that with MySQL. Tables in a query can't be dynamically specified - you have to build the list in your application (or do several single-table queries).
You can use INFORMATION_SCHEMA TABLES table to find tables you want, here is documentation: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html . TABLES table has column NAME which represents names of tables. After finding table names you can run any sql queries you like.
That's not possible in the way you'd like to do it. However you could probably use prepared statements which are basically query-templates where you specificy the parameters (AFAIK also table names) that get replaced depending on your needs without copy and pasting the same query over and over again for different tables.

How to compare two databases' altered tables

I have inserted new columns in one database, and I now want to add the same type of columns to a second database. I need to know which columns are in the first database that are not in the second. I have many tables in each database that needs column difference comparing. I searched the web and I can only find ways to see the difference of the contents of columns in two tables. I don't need to compare the contents, just different columns in all the tables in each database. Each database has the same tables.
Thanks!
I found that you can do a database dump that just has the structure from phpmyadmin.
Are you doing this manually? You could just use SHOW CREATE to see the structure of the tables, and then something like the diff command in Linux to compare them.
For a commercial product answer: I use Red Gate's SQL Compare which works great. It can compare the entire schema of two databases. It can also update your target database to match your source database.
Use redgate SQL compare to comapre schema of two tables.
sql-dbdiff works well too. Its an open source.

What is the MySQL equivalent of a PostgreSQL 'schema'?

I have a PostgreSQL database whose tables are divided amongst a number of schemas. Each schema has a different set of access controls; for example, one schema might be read-only to regular users, while they are allowed to create tables on another. Schemas also act as namespaces, so users don't have to worry about duplicating existing tables when they create new ones.
I want to create a similar setup using MySQL. Does it have an equivalent concept? If not, how can I most closely simulate it? I would prefer not to use multiple databases.
Database should be the closest one.
Prefixing table names is what's done with most MySQL-driven apps.