how to select all tables with certain postfix in mysql? - mysql

I'm going to select all tables from a database that their names ends with "_language".
For example : "product_language" or "category_language" .
Can you help me?

You can select all tables using information schema using bellow given query
SELECT
TABLE_NAME
from
information_schema.TABLES
where
TABLE_SCHEMA = 'Your_schema_name'
and TABLE_NAME LIKE '%_language';

Reference for MySQL SHOW command
You can simnply go for SHOW command, like
SHOW tables like '%_language';
Then it will display all the tables in MySQL DB with the table names that are ended by the name '_language'
mysql> SHOW TABLES LIKE '%_language';
OUTPUT as
+---------------------+
| Tables |
+---------------------+
| product_language |
| category_language |
+---------------------+

Related

How to get a list of user-created databases in mysql

Im trying to get a list of all the user-created databases (not tables) from mysql, but I always get a list containing 'mysql', 'information_schema' and 'performance_schema'. Is it possible to filter these 3 out of the whole list?
I have tried with the query 'SHOW DATABASES' using the LIKE and NOT LIKE and also the wildcard '%', but not luck so far. I also tried with logical operators like AND and OR:
SHOW DATABASES NOT LIKE 'information_schema' # doesn´t work
SHOW DATABASES LIKE 'information_schema' # Works but outputs 1 record.
SHOW DATABASES WHERE 'Database' NOT LIKE 'information_schema' # Works but doesn´t filter anything.
Do you guys have any idea how can I show only the user-created databases in mysql?
NOTE
In my view this is not a duplicate of When to use single quotes, double quotes, and backticks in MySQL, because the question is how to get a filtered list of user-created databases and NOT about the use of the backticks.
Best,
Try using backtick for the Database
Because this worked for me
SHOW DATABASES WHERE `Database` NOT LIKE 'information_schema'
mysql> show databases where `database` not like 'information_schema';
+--------------------+
| Database |
+--------------------+
| mysql |
| performance_schema |
+--------------------+
2 rows in set (0.00 sec)
mysql> show databases where `database` not in('information_schema');
+--------------------+
| Database |
+--------------------+
| mysql |
| performance_schema |
+--------------------+
2 rows in set (0.00 sec)

Mysql table already exists, can't drop database, can access the invisible table

When I try to access it:
But check this out:
I'm pretty sure that it is because I delete the databases without dropping it first. Can any one help me?
It's possible that maybe information schema doesn't have the proper data? (or does)
Try running the following, and it should give you your answer:
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'Ascii';
SHOW TABLES most likely uses the file structure.
If you run this query:
mysql> SHOW VARIABLES LIKE 'datadir';
+---------------+-----------------------+
| Variable_name | Value |
+---------------+-----------------------+
| datadir | /usr/local/mysql/var/ |
+---------------+-----------------------+
1 row in set (0.00 sec)
Then do ls /usr/local/mysql/var/Ascii/, you should be able to see the table files. There should be an .frm, .MYD, and .MYI

SHOW TABLES statement with multiple LIKE values

mysql> SHOW TABLES like 'cms';
+-------------------------+
| Tables_in_tianyan (cms) |
+-------------------------+
| cms |
+-------------------------+
1 row in set (0.00 sec)
Result
mysql> SHOW TABLES like 'cms' or like 'role';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual...
How can I filter by multiple conditions ?
You need to use the WHERE clause. As shown in the docs, you can only have a single pattern if you use "SHOW TABLES LIKE ...", but you can use an expression in the WHERE clause if you use "SHOW TABLES WHERE ...". Since you want an expression, you need to use the WHERE clause.
SHOW TABLES
FROM `<yourdbname>`
WHERE
`Tables_in_<yourdbname>` LIKE '%cms%'
OR `Tables_in_<yourdbname>` LIKE '%role%';
You can just use a normal SQL WHERE statement to do it.
SHOW TABLES WHERE Tables_in_tianyan LIKE '%cms%'
show tables from mydb
where
Tables_in_mydb like '%statistics%'
or Tables_in_mydb like '%device%';
You take table list using the below code
select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA = 'database_name'
Hope it will help you.
this will help
SELECT
TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME
LIKE 'cms%';

mysql check collation of a table

How can I see what collation a table has? I.E. I want to see:
+-----------------------------+
| table | collation |
|-----------------------------|
| t_name | latin_general_ci |
+-----------------------------+
SHOW TABLE STATUS shows information about a table, including the collation.
For example SHOW TABLE STATUS where name like 'TABLE_NAME'
The above answer is great, but it doesn't actually provide an example that saves the user from having to look up the syntax:
show table status like 'test';
Where test is the table name.
(Corrected as per comments below.)
Checking the collation of a specific table
You can query INFORMATION_SCHEMA.TABLES and get the collation for a specific table:
SELECT TABLE_SCHEMA
, TABLE_NAME
, TABLE_COLLATION
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 't_name';
that gives a much more readable output in contrast to SHOW TABLE STATUS that contains a lot of irrelevant information.
Checking the collation of columns
Note that collation can also be applied to columns (which might have a different collation than the table itself). To fetch the columns' collation for a particular table, you can query INFORMATION_SCHEMA.COLUMNS:
SELECT TABLE_SCHEMA
, TABLE_NAME
, COLUMN_NAME
, COLLATION_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 't_name';
For more details you can refer to the article How to Check and Change the Collation of MySQL Tables
Use this query:
SHOW CREATE TABLE tablename
You will get all information related to table.
Check collation of the whole database
If someone is looking here also for a way to check collation on the whole database:
use mydatabase; (where mydatabase is the name of the database you're going to check)
SELECT ##character_set_database, ##collation_database;
You should see the result like:
+--------------------------+----------------------+
| ##character_set_database | ##collation_database |
+--------------------------+----------------------+
| utf8mb4 | utf8mb4_unicode_ci |
+--------------------------+----------------------+
1 row in set (0.00 sec)

mysql trouble with information_schema.tables

-edit2- 3hrs later and still have the same problem. I am using the noinstall archive package.
-edit- maybe someone can tell me a better way to check if a table exist?
I have a function in my lib to check if a table exist which i asked how to do in the past.
I deleted my database and created it again. My code didnt create the tables properly. After debugging i decided to write the below.
mysql> SELECT table_schema, table_name FROM information_schema.tables WHERE tabl
e_schema = 'mydb' AND table_name='ApprovePost';
+--------------+-------------+
| table_schema | table_name |
+--------------+-------------+
| mydb | ApprovePost |
+--------------+-------------+
1 row in set (0.00 sec)
Weird... mydb was dropped and created again (i wrote drop database mydb; and create database mydb;. It should be gone?). Lets find out what exists
mysql> SELECT table_schema, table_name FROM information_schema.tables WHERE tabl
e_schema = 'mydb';
Empty set (0.00 sec)
Not only do i not know why the first statement shows tables which is wrecking my code, i dont know why this is not showing any tables (in that database).
note: The databases should all be innodb. Also this is a fresh windows install and i may have configured something wrong.
Bonus weirdness.
mysql> drop database mydb;
ERROR 1008 (HY000): Can't drop database 'mydb'; database doesn't exist
mysql> SELECT table_schema, table_name FROM information_schema.tables WHERE tabl
e_schema = 'mydb';
Empty set (0.00 sec)
mysql> SELECT table_schema, table_name FROM information_schema.tables WHERE tabl
e_schema = 'mydb' AND table_name='ApprovePost';
+--------------+-------------+
| table_schema | table_name |
+--------------+-------------+
| mydb | ApprovePost |
+--------------+-------------+
1 row in set (0.00 sec)
Looks like you need to use the FLUSH TABLES command for the INFORMATION_SCHEMA.TABLES to reflect existing tables.
Reference:
TABLE CACHE
Hmmm...
Are results stable? May be some pending transaction..
What is shown by
show tables like '%'
Also it may be an issue with physical files.
Is folder for mydb exists?