In MySQL database I have some tables which end with _history.
Now I want to select tables that are like _history.
I have done like below.
show tables like '%_history`
Now I got the desired result.
Now in this result I got some tables which start with temp. Ex: temp_102_history.
Is there a way to exclude the tables that start with temp in the show tables like '%_history statement.
Select table_name
from information_schema.tables
Where table_name like '%_history'
and table_name not like 'temp%'
and table_schema='your database'
You can use information_schema database for this.
Try this
SHOW TABLES
WHERE tables_in_test NOT LIKE 'temp_%'
AND tables_in_test LIKE '%_history'
Replace test with your DB name.
Related
I have multiple tables in my multiple databases.
On different servers, i use MySQL / PostgreSQL / MS SQL.
I keep short table namesbut the comments given to the tables are with full explanation.
I want query that will show me tables ending with "com" and also the comment given to each table (table's comment).
In MySQL, I know:
SELECT table_name FROM information_schema.tables where table_name like "%com"
But this shows all tables from all databases.
For MySQL, check out following:
SELECT table_name FROM information_schema.tables;
will show all table names in all databases;
SELECT table_name,table_comment FROM information_schema.tables
will show all table names + comment in all databases;
interesting thing, you can fire
SELECT * FROM information_schema.tables;
to know what all info you can get of a table.
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b';
will show all table names + comment in "sifr_b" database;
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b' and
table_name like "%com";
will show those table names + comment in "sifr_b" database, that have table name ending with "com";
I have 2 versions of a database (say db_dev and db_beta). I've made some changes in the db_dev database - added some tables, and changed a few columns in some existing tables. I need to find out the list of table names in which changes have been made.
I can easily find out the tables I've added by running the following query on the information_schema database:
SELECT table_name
FROM tables
WHERE table_schema = 'db_dev'
AND table_name NOT IN (SELECT table_name
FROM tables
WHERE table_schema = 'db_beta');
How do I get the table_names whose column_names do not match in the two database versions?
There are many ready made tools available which can give you changed schema by comparing two databases. Here are some tools which can serve your purpose :
Red-Gate's MySQL Schema & Data Compare
Maatkit
MySQL Diff
SQL EDT
Red-Gate's MySQL Compare is best tool for this purpose. Its paid though but they provide 14 days free trial version if you want to do something temporary.
Using information_schema, here is how it works.
First, you know that the information_schema.COLUMNS table contains the columns definition. If one column has been changed, or a table does not exist, it will reflect in the information_schema.COLUMNS table.
Difficult part is that you have to compare all columns of your COLUMNS table. So, you have to select TABLE_CATALOG,TABLE_NAME,COLUMN_NAME,ORDINAL_POSITION,COLUMN_DEFAULT, and so on (which is subject to evolution depending on your MySQL version).
The column list is the result of the following query:
SELECT GROUP_CONCAT(column_name)
FROM information_schema.COLUMNS
WHERE table_schema="information_schema"
AND table_name="COLUMNS" AND column_name!='TABLE_SCHEMA';
After that, we just have to SELECT TABLE_NAME, <column_list> and search for columns which appear once (column inexistent in other table), or where columns have two different definitions (columns altered). So we will have two different count in the resulting query to consider the two cases.
We will so use a prepared statement to retrieve the list of column we want, and grouping the result.
The resulting query does all the process for you:
SELECT CONCAT(
"SELECT DISTINCT TABLE_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA IN('db_dev', 'db_beta')
GROUP BY table_name, COLUMN_NAME
HAVING count(*)=1 OR
COUNT(DISTINCT CONCAT_WS(',', NULL, ",
GROUP_CONCAT(column_name)
,"))=2;")
FROM information_schema.COLUMNS
WHERE table_schema="information_schema"
AND table_name="COLUMNS" AND column_name!='TABLE_SCHEMA'
INTO #sql;
PREPARE stmt FROM #sql;
EXECUTE #sql;
The following solution does not use an sql query like you tried and does not give you a real list of tables, but it shows you all the changes in both databases.
You can do an sql dump of both database structures :
mysqldump -u root -p --no-data dbname > schema.sql
Then you can compare both files, e.g. using the diff linux tool.
I'm looking to replace all occurrences of characters in all columns of all tables in my database.
I got the name of my column like this:
select table_name, column_name from information_schema.columns;
And I would apply an UPDATE REPLACE like this:
update table_name set column_name = replace (column_name, "a", "A");
PS : The replacement of "a" to "A" is just one example, my problem is rather how to link table_name and column_name between my two queries.
I tried with subselect, like:
update (select table_name from information_schema.tables as tables) set (select column_name from columns as information_schema.columns Where table_name = tables) = replace (columns, "a", "A");
But I still get errors when I try. What is the right way to do this?
Thank you in advance.
What you need is a stored procedure that will use the information_schema to find all tables and columns within your database, and execute an update statement for all these tables.
Have a look at the following question which answers exactly what you wish to do: Find and replace in entire mysql database
It will not work like that.
You have 2 options:
1) Iterate through all tables on code side and lunch the update for each one
2) Use a stored procedure that iterate through all tables and lunch the update for each one
When executing SHOW TABLES FROM your_db_name_here inside MySQL, the result has Tables_in_{your_db_name_here} as its indexes. I'd like to change that, like this:
SHOW TABLES as _tables FROM mydb;
or something like
SELECT Tables_in_mydb AS _tables FROM (SHOW TABLES FROM mydb);
Any ideas?
It's usually easier to interact with the INFORMATION_SCHEMA database:
http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
Example:
SELECT TABLE_NAME AS _tables
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'mydb'
I want to make an alias(as in bash) for the following MySQL query:
SHOW COLUMNS FROM table WHERE Field != 'col_name' AND Field != 'col_name';
I read something about views but it seems that I need a SELECT query to use them.
I want to type only something like: showcols in the MySQL prompt and in the background the above query to be executed, is that possible?
PS: I cannot use DESCRIBE because of the length of some enum fields in the table.
You can replace the show columns with a select from the information_schema database.
SELECT column_name FROM INFORMATION_SCHEMA.`columns`
WHERE column_name not IN ('col1','col2');
Now you can create a view based on this select:
CREATE VIEW as SELECT ......