query to replace term in all tables of database - mysql

I want to search for http://example.com and replace with https://example.com.
I know I can target a specific table and column with this approach:
UPDATE table_name SET post_content = REPLACE(column_name, 'http://example.com', 'https://example.com');
But how do I run a query which targets all tables/columns: the entire database?

Do a DB dump and open it as a text file. Find and replace. Save and then re-import.
As far as I know, I don't think you can use REPLACE on all tables in one query.

There a two ways to do it. The first is to create SQL UPDATE via the information_schema and execute it as prepared statement. this is much work.
you must look at each column if you can do a replace, so you must ignore INTS and ENUMs etc.
The second way is not a real SQL change, but it works: Generate a full SQL-Dump from your database and make the changes in this file via editor or via commandline with AWK or SED. After this you can import the changed file

Related

Changing names of tables in a MySql Database (Oracle sql developer) [duplicate]

So basically I have a joomla database in MySQL which has a bunch of tables that have the prefix 'jmla_'. I would like to rename all of these tables by replacing the 'jmla_' prefix with a 'jos_' prefix. Any ideas about how to do this with a simple SQL script or SQL query?
SELECT concat ('rename table ',table_name,' to ',table_name,'_old;')
FROM information_schema.tables
WHERE table_name like 'webform%'
and table_schema='weiss_db_new'
will work.
Run this statement:
SELECT 'rename table '||table_name||' to '||'jos'||substr(table_name,5)||';'
FROM information_schema.tables
WHERE table_name like 'jmla%'
This creates a script that will rename all the tables. Just copy & paste the output into your SQL client.
(You will need to change the || to MySQL's non-standard concatenation operator in case you are not running it in ANSI mode)
RENAME TABLE jmla_whatever to jos_whatever;
You'll have to write a script to cover all your tables - you can populate your script with the output of show tables. See http://dev.mysql.com/doc/refman/5.0/en/rename-table.html for details.
Export using phpmyadmin to .sql file
Use any text editor (I prefer vim, work excellent on large files) with function "find and replace" to open file
Do "find and replace", putting your actual prefix in find box, and updated in replace box
Import file using phpmyadmin.
Remember to drop old database before importing by phpmyadmin. This may be done, checking suitable options during export.
Have you considered using Akeeba Admin Tools Component for Joomla! and then using the Database Prefix Editor to change table prefixes, works really well. Akeeba Admin tools can be used to secure your Joomla! installation
Akeeba can be obtained here

MySQL Workbench How can I run the same query using each line of the file as input?

So, this is going to go back a bit... The last time I had to do something like this I was using Watcom Sybase SQLAnywhere 5.0 for OS/2. To preemptively answer the question: Yes, I have indoor plumbing! :)
On that platform I was able to write "parameter-ized" queries and then execute those queries over a comma-separated file of records. e.g.:
UPDATE table_name SET column_name = "%parameter_one%" WHERE other_column_name = "%parameter_two%";
There were some other set up lines above it to equate the parameter names with the fields from the file, but you get the gist. This query above would be saved as an .sql file and then executed in the interactive SQL browser as
READ my_parameterized_query.sql my_file_full_of_data.csv
Is it possible to do this or something similar with MySQL Workbench? ...or do I need to learn Python?
No, this is not possible with MySQL Workbench, but is a nice idea. I'll take this into our FR list, if you don't mind.
I'm afraid you have to write a little application to do what you want here. It doesn't matter which language you choose, unless you want to create a plugin in WB (which requires Python).
As a start: use a prepared statement and bind the values to the parameters for each line in the file.

Replace instances of newlines in entire database

In MySQL Workbench I want to replace all instances of newlines with the explicit string "\\n" in my entire database, except where the string already exists. How would I form the query? Thanks.
[edit]
It just occurred to me that it might be easier to dump the database and use Notepad++ to replace the strings. But I was hoping there were a way to do this from within MySQL Workbench itself.
You will need to do this for each table and each column that you want to do the replacement. For example:
update table set column = REPLACE(column,'\n','\\n')

In MySQL, how do I batch rename tables within a database?

So basically I have a joomla database in MySQL which has a bunch of tables that have the prefix 'jmla_'. I would like to rename all of these tables by replacing the 'jmla_' prefix with a 'jos_' prefix. Any ideas about how to do this with a simple SQL script or SQL query?
SELECT concat ('rename table ',table_name,' to ',table_name,'_old;')
FROM information_schema.tables
WHERE table_name like 'webform%'
and table_schema='weiss_db_new'
will work.
Run this statement:
SELECT 'rename table '||table_name||' to '||'jos'||substr(table_name,5)||';'
FROM information_schema.tables
WHERE table_name like 'jmla%'
This creates a script that will rename all the tables. Just copy & paste the output into your SQL client.
(You will need to change the || to MySQL's non-standard concatenation operator in case you are not running it in ANSI mode)
RENAME TABLE jmla_whatever to jos_whatever;
You'll have to write a script to cover all your tables - you can populate your script with the output of show tables. See http://dev.mysql.com/doc/refman/5.0/en/rename-table.html for details.
Export using phpmyadmin to .sql file
Use any text editor (I prefer vim, work excellent on large files) with function "find and replace" to open file
Do "find and replace", putting your actual prefix in find box, and updated in replace box
Import file using phpmyadmin.
Remember to drop old database before importing by phpmyadmin. This may be done, checking suitable options during export.
Have you considered using Akeeba Admin Tools Component for Joomla! and then using the Database Prefix Editor to change table prefixes, works really well. Akeeba Admin tools can be used to secure your Joomla! installation
Akeeba can be obtained here

Dump MySQL Table(s) as XML using SQL command (like table_to_xml() or query_to_xml() in postgresql?)

is there a way to retrieve the result of a query / or to dump a whole table into an XML fragment which can be retrieved by using an XML query? I know there is something like this for PostgreSQL (9.0), table_to_xml()and query_to_xml().
I also know that mysqldump --xml can export XML, but I do seek for something that allows me to issue a simple query. The application I’m working on should allow some users to dump a certain table into an XML file on their machine, therefor I need to issue a query and obtain a String or something (is there an XML type in MySQL?).
I need the result to be XML and a Result Set of a query, not a file on server or something.
The query resulting in a SQL script similar to a MySQL dump for a single table would have three parts:
SHOW CREATE TABLE tblname - to generate CREATE TABLE statement.
DESCRIBE tblname - to retrieve column names for the construction of INTO(...) part of the INSERT queries.
SELECT * FROM tblname - to retrieve values for the construction of VALUES(...) part of the INSERT queries. Each row in the result set will correspond to an INSERT statement. INSERT statements will be generated in the loop handling the result set.
If this is to be done from MySQL, it can be wrapped into a stored procedure.
Found this in a question here at stackoverflow, as linked in the comments. Proposes to manually build XML in a query, like
SELECT concat("<this-is-xml>", field1, "</this-is-xml>") FROM ...
Of course, xml-charcter escaping and so on has to be done manually.
There seems to be no native way to directly get the result of a query as xml.
There is also a library (lib_mysqludf_xql) for mysql which provides XML functionality for MySQL.
INTO OUTFILE will dump the results to an XML file, so you could then send that to a client.