I have a database which has 180 tables in it. Going through all of them one by one would be wasting my time.
The problem I am facing, is that I want to search a specific keyword from all tables and columns. So let's say that I got a database d with tables t1, t2, and so on, all tables have different column names and the string which I want to see must be LIKE '%connect%'.
To clarify, the %connect% must be inside the table contents (i.e. inside a row of a table).
If this is not possible by a single query, maybe you can point me into the right direction how to do this programmatically.
Table's names:
select t.table_name from information_schema.tables t where t.table_name like '%connect%';
Column's names:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table';
Related
I want to select the nth element of the table list provided by the SHOW TABLES query.
If I use the following code to list them alphabetically:
WITH tableslist as (SHOW TABLES)
SELECT Tables_in_DBname FROM tableslist ORDER BY Tables_in_DBname
I get an error regarding the syntax near 'SHOW TABLES).
If I only do the SHOW TABLES query I get a table with the column Tables_in_DBname.
The main goal of this would be to populate a checkbox in VBA with the table names in the database, so in case I am looking in the completely wrong direction to go about this please correct me.
I am able to populate the cbo with the fields of specific tables, but I could not find a way to list the tables from the database as fields, so I am attempting to make an ordered list to select the nth element from.
The server type is MariaDB.
You can use information_schema.tables to get the list of tables:
SELECT table_name FROM information_schema.tables
WHERE table_schema = '<name of database>' ORDER BY table_name;
This allows you to build more complex queries that inspect the tables in the database.
I need to select all indexes from a given table.
But, it looks like MySQL creates multiple indexes with the same name with all the possible column combinations when there is more than one column in the index.
So SHOW INDEXES returns multiple times the same value.
Is there a way to do a select distinct to get the index names from a given table?
And if possible that is not MySQL specific.
Please try this query:
SELECT DISTINCT INDEX_NAME FROM information_schema.statistics
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'
How to make query to information_schema.tables to get list of available tables which is exist in one DB but not exist in another one, something like diff but more suitable. I just need sql query.
So i have Db's like A,B,C,D and all these DB's should has the same tables, how I can check it ?
try
select *
from INFORMATION_SCHEMA.tables
group by table_name
having count(table_schema) < 4
if you have 4 DB's. If more you have to adjust the having clause.
This query give you all unique tables in all databases.
SELECT *,count(TABLE_NAME)
FROM
`TABLES` group by table_name
having count(TABLE_NAME)=1
And if you want repeated table names then use this
SELECT *,count(TABLE_NAME)
FROM
`TABLES` group by table_name
having count(TABLE_NAME)>1
I have a database with about 200 tables, and need to do a query on all tables containing a certain column (creation_date), but not all tables have that column. SELECT * FROM * WHERE creation_date>=42 obviously doesn't work, but what would be the best way of doing it?
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, TABLE_NAME,
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
[AND table_schema = 'db_name']
[AND column_name ='creation_date']
copied directly from MySQL - you need to loop thru these system tables and the list will contain those ...
then you can build your SQL statements and be sure that they work ...
You could build a dynamic SQL query from metadata. I would approach this like:
Get a list of tables
For each table,
See if the table has the column creation_date
If it does, add your query on this table to the dynamic query
Union the results together
You might also be able to create a view on the multiple tables. Then you can just query the view.
You can try to include the table you want to query like so:
SELECT * FROM table1 a, table2 b WHERE a.creation_date>=42
You cannot use a wildcard in the from. In the way as shown above you can specify on which tables the where clause must apply. You can than leave out the ones that don't have the column.
So in the example query, table1 (alias a) has the column, and table2 (alias b) does not.
Here's a fun one.
Occasionally we may want to find the tables consisting of certain fields, this is because our schema's are so large, and our affiliation with external entities for database definitions causes a bit of confusion when composing certain queries.
Sometimes I need to know the answer to the question of: "What tables in database X contains the field name of Y?"
Querying schemas isn't my forté, and neither is finding search criteria to yield something mildly useful.
Cheers
I would use this query:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'ColumnName'
I use this:
select * from sys.tables
where object_id in
(
select object_id from sys.columns where [name] = 'FieldName'
)