Mysql - find a table in all database - sql

Is there any command that I can locate a certain table from all databases? Since I forgot where the table is. Thanks.

Use the MySQL specific:
SHOW TABLES
...or use the ANSI standard, INFORMATION_SCHEMA.TABLES:
SELECT table_name,
table_schema AS dbname
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name='searched table name'

Related

INFORMATION_SCHEMA.TABLES behavior?

I have this query below. This query finds on database if that particular table is exists in the database. My question is, by using INFORMATION_SCHEMA.TABLES, is it going to find that table name to the other database? Or in just particular database where you are connected???
select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = N'WebServiceCredentials'
As per the MSDN:
Returns one row for each table in the current database for which the
current user has permissions
You can view all the tables for all databases, but not using INFORMATION_SCHEMA.TABLES
You can using system tables.
You can run this query:
sp_msforeachdb 'select "?" AS dbname, * from [?].sys.tables'

SHOW COLUMNS Command for HSQLDB

Is there an HSQLDB equivalent for the MYSQL SHOW COLUMNS from TABLE command?
HSQLDB does not have separate commands for showing tables, columns or other database objects.
You use SELECT * FROM INFORMATION_SCHEMA.COLUMNS and various other tables in the INFORMATION_SCHEMA for such purposes.
http://hsqldb.org/doc/2.0/guide/databaseobjects-chapt.html#dbc_information_schema
This is the query that you need.
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '<your schema>' AND TABLE_NAME = '<name table or view>';

SQL Select list of tables in a database

I am using SQL Server 2005. I am trying to SELECT a list of tables in one of my database. Here is my structure of my SQL Server:
- <IP>(SQL Server 9.0 -userlogin)
- Databases
- Company
- Tables
- dbo.UserDB
- dbo.detailsDB
- dbo.goodsDB
I would like to retrieve the values of dbo.UserDB, dbo.detailsDB, dbo.goodsDB
But i do not know what is the exact sql query needed.
I have tried many ways like
SELECT * FROM userlogin.Tables; and
SELECT * FROM userlogin.Company.Tables;, but none of them works.
I have seen quite a few posts which suggests using show databases and show tables, but they don't seem to work as well.
Is it possible to select a list of table names in a database in the first place?
Thank you for any help in advance.
Thanks for the MSDNA link that #TomTom provided, I can now list my tables in my database.
However, I would want to list specific tables where TABLE_NAME contains "user".
How can I do it? I am trying on the following sql but it is not displaying result at the moment:
SELECT DISTINCT TABLE_NAME
FROM Company.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%"user"%';
GO
Try the iNFORMATION_SCHEMA.
http://msdn.microsoft.com/en-us/library/ms186778.aspx
They contain all the schema information you need.
Use the new sys system catalog in SQL Server 2005 and up:
SELECT Name
FROM sys.tables
WHERE is_ms_shipped = 0 -- only list *your* tables - not the system / MS table
You can read more about system catalog views and how to query them in the MSDN Books Online - and be warned - there's lots more to read and learn!
You could use this
Use ***database name***
SELECT *
FROM sys.tables
WHERE name like '%user%'
Sorry, have seen that #marc_s has provided the answer before me!
You can use INFORMATION_SCHEMA as told by #TomTom:
USE <<YOUR_DB_NAME>>
SELECT TABLE_SCHEMA + '.' + TABLE_NAME, *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_SCHEMA + '.' + TABLE_NAME
You can customize above query as per your requirements.
For your other question to find tables where the name contains 'user', execute the following statement:
USE <<YOUR_DB_NAME>>
Exec sp_tables '%user%'
Try:
SELECT *
from sysobjects
where type = 'U' AND
NAME LIKE '%user%'
GO

find the tables has binary data

How to find what are all tables has binary data and display the table name ?
For MySql you want the INFORMATION_SCHEMA COLUMNS table:
http://dev.mysql.com/doc/refman/5.1/en/columns-table.html
If you need to find all of the tables which have binary columns, then you can create a query joining with the INFORMATION_SCHEMA TABLES table:
http://dev.mysql.com/doc/refman/5.1/en/tables-table.html
That depends a lot on what database you are using. Many databases (at least MySQL and PostgreSQL, maybe all) has a database called information_schema (or something similar). This is a database describing the structure of your databases along with all tables, their fields and what types of data the fields hold. So this would be a good starting point.
I'd need to know which database, but this (or something very similar) should work on Oracle:
select *
from all_tab_columns
where data_type in ('BLOB', 'RAW')
;
you need to select the system tables (in msssql 2000 - syscolumns) or system management views (in mssql 2005 or 2008 - sys.columns) to find the columns with system_type_id you need to find, and then find the corresponding table joining sys.columns and sys.objects by object_id field.
Can you ask the DBA or the DB-developper?
if no, what DB (Oracle, MySql, Microsoft, other?) are you using ..
EDITED for MySQL DB
Use
select table_schema
, table_name
, column_name
, data_type
from information_schema
where data_type like '%blob%'
or data_type in ('binary','varbinary')

How can I do the equivalent of "SHOW TABLES" in T-SQL?

I would like to do a lookup of tables in my SQL Server 2005 Express database based on table name. In MySQL I would use SHOW TABLES LIKE "Datasheet%", but in T-SQL this throws an error (it tries to look for a SHOW stored procedure and fails).
Is this possible, and if so, how?
I know you've already accepted an answer, but why not just use the much simpler sp_tables?
sp_tables 'Database_Name'
This will give you a list of the tables in the current database:
Select Table_name as "Table name"
From Information_schema.Tables
Where Table_type = 'BASE TABLE' and Objectproperty
(Object_id(Table_name), 'IsMsShipped') = 0
Some other useful T-SQL bits can be found here: http://www.devx.com/tips/Tip/28529
Try this:
USE your_database
go
Sp_tables
go
Try this
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'Datasheet%'
One who doesn't know the TABLE NAME will not able to get the result as per the above answers.
TRY THIS
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='dbo';
Try this :
select * from information_schema.columns
where table_name = 'yourTableName'
also look for other information_schema views.
And, since INFORMATION_SCHEMA is part of the SQL-92 standard, a good many databases support it - including MySQL.
Try following
SELECT table_name
FROM information_schema.tables
WHERE
table_name LIKE 'Datasheet%'
Try it :
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%'
MS is slowly phasing out methods other than information_schema views. so for forward compatibility always use those.
I know this is an old question but I've just come across it.
Normally I would say access the information_schema.tables view, but on finding out the PDO can not access that database from a different data object I needed to find a different way. Looks like sp_tables 'Database_Name is a better way when using a non privileged user or PDO.