How to fetch databases by name? - mysql

I have 4 databases:
auth ; auth_backup ; auth1 ; auth1_backup ;
I want to fetch only the ones with the "_backup" extension.Is there a way?
Just with php and sql.And if its possible with mysql not mysqli(i know its deprecate but i have to use it
Until now i tried:
$sql4="SHOW DATABASES LIKE '%backup%'";
$query4=mysql_query($sql4,$connect);
$row1=mysql_fetch_assoc($query4)
but in this way,it doesnt work what i am trying to do.So i need something somehow to fetch only the specific dbs.Also if its possible to drop some specific dbs,this will do it too..

Try the following statement:
SHOW DATABASES WHERE `Database` LIKE '%_backup'
This will fetch the databases for you with the name _backup in it.

Related

MySQL Queries - Display information created by 'sys'

I'm learning to work with databases and have been learning to write queries based on the tables created.
I'm trying to display information about all the views that were created by the sys user.
I've tried Information_Schema but it returns results that I've created and not the 'sys'.
I'm not sure if I'm attempting it correctly, but I'm getting a result with;
EXEC sp_tables
#table_owner ='sys';
but as it still displays everything and not just 'view' - when I try;
EXEC sp_tables
#table_owner ='sys',
#table_type = 'view';
It returns no data.
Not sure how to work this one out. Tried many things but all were wrong.
I don't know if I got this right but have tried :
SELECT * FROM INFORMATION_SCHEMA.Views where TABLE_schema='sys'

Update everything in a database that is like something in mysql

I have a database and i have a home URL that I want to change into another one. The problem is that this home URL is used in a lot of table and in a lot of column.
So is there a MySQL request that do something like :
UPDATE * SET * = REPLACE(*, 'old-url', 'new-url') WHERE * LIKE '%old-url%'
?
Have a good day ! :)
You could use the plugin "Go Live Update URLS". You just enter the old & new URL and select the tables you want to replace the value.
https://nl.wordpress.org/plugins/go-live-update-urls/
Back-up your database before executing the replacement!

MySQL: Is there a way to return a list of functions / procedures filtered by a string found within the code?

Basically I have the unenviable position of updating our entire system to stop using a certain table and instead use another one. I've already done this for all of our code, now I need to do it for all of our functions and procedures.
I know that I can get a list of the functions / procedures in a database as such:
SELECT * FROM INFORMATION_SCHEMA.ROUTINES
I also know that I can look at the code for an individual function / procedure as such:
SHOW CREATE FUNCTION function_name
SHOW CREATE PROCEDURE procedure_name
However, I don't want to have to look through each function and procedure one by one, as we have over 200 of them.
I'm wondering if there is anything like...
SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE code_column_name LIKE '%search_string%'
There doesn't seem to be any column in INFORMATION_SCHEMA.ROUTINES that contains the code, but... is there a way to do this on a different table perhaps?
I would use UNIX grep for that.
If you output the results of show create function/procedure to a flat file on disk then run grep on it. Or turn it directly thru grep.
Here is one way to do it:
echo 'show create function foo' | mysql -h*host* -u*user* -p*pass* *schema* | grep *obsolete-tablename*
or dump the entire database to disk and then grep it.
mysqldump -h*host* -u*user* -p*pass* *schema --routines > mydump.sql
grep *obsolete-tablename mydump.sql
Try:
SELECT * FROM mysql.proc WHERE body LIKE '%search_string%'
You asked about MySQL but I will just mention that I use this for stored procedures in MS SQL Server:
SELECT object_name(id)
FROM syscomments
WHERE text LIKE '%wibble%'
The INFORMATION_SCHEMA.ROUTINES table (which you mention) also contains similar stuff, but the text is cut off after 4000 chars, so you find less matches; not very helpful.

MYSQL - two tables in different databases

I need to check phone number to see if they match but the issue is, that one table is in database A and another is in database B.
I am wondering is there away to do a search like this:
update `chk_dup`, new set chk_dup.dup='Y' WHERE chk_dup.phone = new.phone;
But I guess I would need to do something like this:
update `A.chk_dup`, B.new set A.chk_dup.dup='Y' WHERE A.chk_dup.phone = B.new.phone;
I any one knows how to search two tables in completely different databases that would help.
I think in your second one you had a syntax error, try this one:
UPDATE `A`.`chk_dup`, `B`.`new`
SET `A`.`chk_dup`.`dup`='Y'
WHERE `A`.`chk_dup`.`phone` = `B`.`new`.`phone`;

Idiorm (MySQL) - Raw query - SHOW TABLES LIKE

I use Idiorm as ORM for MySQL with PHP.
I need to check if a table is created or not.
In SQL
This works in phpMyAdmin
SHOW TABLES LIKE 'ro_globals'
What I tried in Idiorm
ORM::raw_query("SHOW TABLES LIKE 'ro_globals'")->count()
Call to undefined method admin::count()
Is it possible to make this work with Idiorm? If so, how?
The correct anwser is:
ORM::forTable()->rawQuery('SHOW TABLES')->findMany()->count();
or with underscore:
ORM::for_table()->raw_query('SHOW TABLES')->find_many()->count();
Prefixed tables:
ORM::for_table()->raw_query('SHOW TABLES LIKE ' . $prefix . '%')->find_many()->count();
I got the answer in a comment by Saharsh. It's not a Idiorm problem, it could be solved with SQL.
Result
CREATE TABLE IF NOT EXISTS