Get all table names of a particular database by SQL query? - sql

I am working on application which can deal with multiple database servers like "MySQL" and "MS SQL Server".
I want to get tables' names of a particular database using a general query which should suitable for all database types. I have tried following:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
But it is giving table names of all databases of a particular server but I want to get tables names of selected database only. How can I restrict this query to get tables of a particular database?

Probably due to the way different sql dbms deal with schemas.
Try the following
For SQL Server:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG='dbName'
For MySQL:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='dbName'
For Oracle I think the equivalent would be to use DBA_TABLES.

Stolen from here:
USE YOURDBNAME
GO
SELECT *
FROM sys.Tables
GO

The following query will select all of the Tables in the database named DBName:
USE DBName
GO
SELECT *
FROM sys.Tables
GO

Just put the DATABASE NAME in front of INFORMATION_SCHEMA.TABLES:
select table_name from YOUR_DATABASE.INFORMATION_SCHEMA.TABLES where TABLE_TYPE = 'BASE TABLE'

USE DBName;
SELECT * FROM sys.Tables;
We can deal without GO in-place of you can use semicolon ;.

In mysql, use:
SHOW TABLES;
After selecting the DB with:
USE db_name

In order if someone would like to list all tables within specific database without using the "use" keyword:
SELECT TABLE_NAME FROM databasename.INFORMATION_SCHEMA.TABLES

This works Fine
SELECT
*
FROM
information_schema.tables;

I did not see this answer but hey this is what I do :
SELECT name FROM databaseName.sys.Tables;

To select the database query below :
use DatabaseName
Now
SELECT * FROM INFORMATION_SCHEMA.TABLES
Now you can see the created tables below in console .
PFA.

For Mysql you can do simple. SHOW TABLES;

select * from sys.tables
order by schema_id --comments: order by 'schema_id' to get the 'tables' in 'object explorer order'
go

In our Oracle DB (PL/SQL) below code working to get the list of all exists tables in our DB.
select * from tab;
and
select table_name from tabs;
both are working. let's try and find yours.

SELECT TABLE_NAME
FROM your_database_name.INFORMATION_SCHEMA.TABLES
ORDER BY TABLE_NAME;

Exec sp_MSforeachtable 'Select ''?'''

USE dbName;
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE (TABLE_SCHEMA = 'dbName' OR TABLE_SCHEMA = 'schemaName')
ORDER BY TABLE_NAME
If you are working with multiple schemata on an MS SQL server, then SELECT-ing TABLE_NAME without also simultaneously selecting TABLE_SCHEMA might be of limited benefit, so I have assumed we are interested in the tables belonging to a known schema when using MS SQL Server.
I have tested the query above with SQL Server Management Studio using an SQL Server database of mine and with MySQL Workbench using a MySQL database, and in both cases it gives the table names.
The query bodges Michael Baylon's two different queries into one that can then run on either database type. The first part of the WHERE clause works on MySQL databases and the second part (after the OR) works on MS SQL Server databases. It is ugly and logically a little incorrect as it supposes that there is no undesired schema with the same name as the database. This might help someone who is looking for one single query that can run on either database server.

UPDATE FOR THE LATEST VERSION OF MSSQL SERVER (17.7)
SELECT name FROM sys.Tables WHERE type_desc = 'USER_TABLE'
Or SELECT * for get all columns.

Yes oracle is :
select * from user_tables
That is if you only want objects owned by the logged in user/schema otherwise you can use all_tables or dba_tables which includes system tables.

Building from Michael Baylon's answer, I needed a list which also included schema information and this is how I modified his query.
SELECT TABLE_SCHEMA + '.' + TABLE_NAME as 'Schema.Table'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = 'dbName'
ORDER BY TABLE_SCHEMA, TABLE_NAME

Simply get all improtanat information with this below SQL in Mysql
SELECT t.TABLE_NAME , t.ENGINE , t.TABLE_ROWS ,t.AVG_ROW_LENGTH,
t.INDEX_LENGTH FROM
INFORMATION_SCHEMA.TABLES as t where t.TABLE_SCHEMA = 'YOURTABLENAMEHERE'
order by t.TABLE_NAME ASC limit 10000;

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_NAME

for postgres it will be:
SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'your_schema' -- probably public

Related

why all columns are invalid after table rename?

I renamed a table and since, in evry 'select' I get the 'Invalid object name' for all columns.
I get from my select the output I need, but why I get an error too ?
this is my simple select...
SELECT [Importance]
,[Color]
,[NotificationName]
FROM [dbo].[Alerts]
It looks like you're using SQL Server Management Studio. I suspect your query will run if you try it, it's just that the intellisense doesn't know that the table name has changed.
Try Ctrl+Shift+R to refresh the cache or alternatively Edit -> Intellisense -> Refresh Local Cache.
You need to do this every time you perform schema changes.
Run below query to confirm that your table has been renamed and has the exact column names specified in your select query:
select * from sys.all_columns where object_id = OBJECT_ID('Alerts')
Alternatively you can execute below query to confirm that the Alerts table exists
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'Alerts'))
BEGIN
PRINT 'Alerts exists'
END
Also make sure you are in the correct database, run below query to find out the database to which Alerts belong :
SELECT TABLE_CATALOG
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'Alerts'
Once you got the database name add USE DATABASENAME at the beginning of your query or you can use DATABASENAME.dbo.Alerts.

Describe table structure

Which query will give the table structure with column definitions in SQL?
sp_help tablename in sql server -- sp_help [ [ #objname = ] 'name' ]
desc tablename in oracle -- DESCRIBE { table-Name | view-Name }
It depends from the database you use. Here is an incomplete list:
sqlite3: .schema table_name
Postgres (psql): \d table_name
SQL Server: sp_help table_name (or sp_columns table_name for only columns)
Oracle DB2: desc table_name or describe table_name
MySQL: describe table_name (or show columns from table_name for only columns)
In MySQL you can use DESCRIBE <table_name>
select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='<Table Name>'
You can get details like column datatype and size by this query
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'student'
DESCRIBE tableName
Check MySQL describe command
For Sybase aka SQL Anywhere the following command outputs the structure of a table:
DESCRIBE 'TABLE_NAME';
Highlight table name in the console and press ALT+F1
For SQL Server use exec sp_help
USE db_name;
exec sp_help 'dbo.table_name'
For MySQL, use describe
DESCRIBE table_name;
For SQL, use the Keyword 'sp_help'
This depends on your database vendor. Mostly it's the "information schema" you should Google for (applies to MySQL, MSSQL and perhaps others).
Sql server
DECLARE #tableName nvarchar(100)
SET #tableName = N'members' -- change with table name
SELECT
[column].*,
COLUMNPROPERTY(object_id([column].[TABLE_NAME]), [column].[COLUMN_NAME], 'IsIdentity') AS [identity]
FROM
INFORMATION_SCHEMA.COLUMNS [column]
WHERE
[column].[Table_Name] = #tableName
In DBTools for Sybase, it's sp_columns your_table_name.

SQL: how to know if a field has "Allow nulls" checked or not checked by SQL command

I want to know which table's field are required or not required so I have to get "Allow nulls" state. How to do that?
I will assume you are talking about SQL Server.
There is a table, INFORMATION_SCHEMA.COLUMNS, that contains meta-data about the columns in the database.
You can do this:
SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
ORDER BY TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME
IS_NULLABLE gives you the "Allow Nulls" value used in the designer.
If your in MySQL use the sql command
DESCRIBE Table;
Where table is the name of the table you want to examine
Try this (SQL Server)
select sysobjects.name, syscolumns.name, syscolumns.isnullable
from sysobjects join syscolumns
on sysobjects.id = syscolumns.id
and sysobjects.xtype = 'U'
and sysobjects.name = 'your table name'

Select all columns from all tables in SQL Server 2008

How can I Select all columns from all tables from the DB, like:
Select * From *
in SQL Server 2008???
The table list it´s very very big, and have so many columns, is it possible to do it without writing the column names?
Or maybe make a select that returns the name of the tables.
This SQL will do this...
DECLARE #SQL AS VarChar(MAX)
SET #SQL = ''
SELECT #SQL = #SQL + 'SELECT * FROM ' + TABLE_SCHEMA + '.[' + TABLE_NAME + ']' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES
EXEC (#SQL)
Try this, works fine
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
then you could add
WHERE TABLE_NAME LIKE '' AND COLUMN_NAME LIKE ''
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID where t.name = 'ProductItem' AND C.name like '%retail%'
ORDER BY schema_name, table_name
It is possible to retrieve the name of all columns from sys.columns
It is possible to retrieve the name of all table from sys.tables
But is impossible to retrieve all the data from all the tables. As soon as more than one table is involved in a query, a JOIN is necessary. Unless join conditions are provided, tables are joined as full Cartesian product, meaning each row from each table is matched with each row from ll other tables. Such a query as you request would produce for 10 tables with 10 records each no less than 10e10 records, ie. 100 billion records. I'm sure you don't want this.
Perhaps if you explain what you what to achieve, not how, we can help better.
To select * from each table, one after another, you can use the undocumented but well known sp_msforeachtable:
sp_msforeachtable 'select * from ?'
If you are going to send to Excel, I would suggest you use the export wizard and simply select all the tables there. In the object browser, put your cursor on the database name and right click. Chose Tasks - Export Data and follow the wizard. WHy anyone would want an entire database in Excel is beyond me, but that's the best way. If you need to do it more than once you can save the export in an SSIS package.
In SQL Server 2016 Management Studio ( Version: 13.0.15900.1), to get all column names in a specified table, below is the syntax:
**Select name from [YourDatabaseName].[sys].[all_columns]
where object_id=(Select object_id from [YourDatabaseName].[sys].[tables]
where name='YourTableName')**

What is the SQL command to return the field names of a table?

Say I have a table called myTable. What is the SQL command to return all of the field names of this table? If the answer is database specific then I need SQL Server right now but would be interested in seeing the solution for other database systems as well.
MySQL 3 and 4 (and 5):
desc tablename
which is an alias for
show fields from tablename
SQL Server (from 2000) and MySQL 5:
select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'tablename'
Completing the answer: like people below have said, in SQL Server you can also use the stored procedure sp_help
exec sp_help 'tablename'
SQL-92 standard defines INFORMATION_SCHEMA which conforming rdbms's like MS SQL Server support. The following works for MS SQL Server 2000/2005/2008 and MySql 5 and above
select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'myTable'
MS SQl Server Specific:
exec sp_help 'myTable'
This solution returns several result sets within which is the information you desire, where as the former gives you exactly what you want.
Also just for completeness you can query the sys tables directly. This is not recommended as the schema can change between versions of SQL Server and INFORMATION_SCHEMA is a layer of abstraction above these tables. But here it is anyway for SQL Server 2000
select [name] from dbo.syscolumns where id = object_id(N'[dbo].[myTable]')
You can use the provided system views to do this:
eg
select * from INFORMATION_SCHEMA.COLUMNS
where table_name = '[table name]'
alternatively, you can use the system proc sp_help
eg
sp_help '[table name]'
PostgreSQL understands the
select column_name from information_schema.columns where table_name = 'myTable'
syntax. If you're working in the psql shell, you can also use
\d myTable
for a description (columns, and their datatypes and constraints)
For those looking for an answer in Oracle:
SELECT column_name FROM user_tab_columns WHERE table_name = 'TABLENAME'
Just for completeness, since MySQL and Postgres have already been mentioned: With SQLite, use "pragma table_info()"
sqlite> pragma table_info('table_name');
cid name type notnull dflt_value pk
---------- ---------- ---------- ---------- ---------- ----------
0 id integer 99 1
1 name 0 0
This is also MySQL Specific:
show fields from [tablename];
this doesnt just show the table names but it also pulls out all the info about the fields.
In Sybase SQL Anywhere, the columns and table information are stored separately, so you need a join:
select c.column_name from systabcol c
key join systab t on t.table_id=c.table_id
where t.table_name='tablename'
MySQL is the same:
select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'tablename'
If you just want the column names, then
select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'tablename'
On MS SQL Server, for more information on the table such as the types of the columns, use
sp_help 'tablename'
For IBM DB2 (will double check this on Monday to be sure.)
SELECT TABNAME,COLNAME from SYSCAT.COLUMNS where TABNAME='MYTABLE'
MySQL
describe tablename
select COLUMN_NAME1,COLUMN_NAME2 from SCHEMA_NAME.TABLE_NAME
where TABLE_NAME.COLUMN_NAME = 'COLUMN_NAME1';