Searching in several mysql tables - mysql

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.

Related

Run an ALTER TABLE across all like tables (containing the same schema) in MySQL database

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.

mysql tell difference between table and view

I'm surprised I didn't find this answer out there.
I know very well what the difference between a table and a view is. BUT..how do I DETERMINE whether a db object is a table or view? Since
show tables;
will show both tables and views - and there is no "show views" command.
to determine in my coding (which has to read multiple objects and may not "know" better), I do this:
show create view my_table_or_view
and if I get an error, which I prevent from dying, it's a table. Pretty clumsy, is there a better way?
try this variation instead ...
show full tables;
the Table_type column will give the info you require :)
You can use the following query and if it returns a record it's a table
SELECT *
FROM information_schema.tables
WHERE 'TABLE_TYPE' = 'BASE TABLE'
AND table_name = 'your table name'

MySQL store checksum of tables in another table

CONTEXT:
we have big databases with loads of tables. Most of them (99%) are using innodb.
we want to have a daily process that monitors which table has been modified. As they use innodb the value of Update_time from
SHOW table STATUS from information_schema;
is null.
For that reason we want to create a daily procedure that will store the checksum (and other stuffs for that matters) of each table somewhere (preferably another table). On that, we will do different checks.
PROBLEM:
I'm trying to use
checksum table from db_schema.table_name;
which returns a resultset-table with 2 columns: "table","checksum".
It gives me the value I want but I'm not able to use it in a select or insert statement.
I tried a lot of things like:
select `checksum` from (checksum table from db_schema.table_name);
or other similar queries. But I'm not able to extract the data from the resultset.
Is there a way I can do that?
Thanks in advance for your help.
EDIT: in the end what I want is to build a more complex resultset having different informations in it (table schema, table name, count, checksum, datetime:now()...)
Then I'll use this resultset to compare with the values of yesterday and draw my own statistics. That's why I want to get the checksum from that resultset.
There is no possibility to save the result of CHECKSUM TABLE directly using SQL. Neither can you use prepared statements or cursors in stored procedures to use the checksum result.
You best make a script around it, or download some popular tools doing it for you.
For MyISAM tables using the CHECKSUM=1 table argument, you can simply use INFORMATION_SCHEMA like this:
SELECT TABLE_NAME,CHECKSUM FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test' AND ENGINE='MyISAM'
AND CHECKSUM IS NOT NULL;

query to display the names of databases that contain specific table in mysql?

I have multiple databases in mysql server but want to search the location of a specific table .Can u help me in locating the databases that contain specific table.I am working in command mode. Thank You!
Look up in COLUMNS table in information_schema database.
SELECT `TABLE_SCHEMA`, `TABLE_NAME`
from `COLUMNS`
WHERE
`COLUMN_NAME` = 'YOUR_COLUMN_NAME';
You need root privilege for this. or at least SELECT permission on Information_schema table.
Below link might help you...
How to find all the tables in MySQL with specific column names in them?
Please let me know incase of further queries...

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%'
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.