How do I list all the columns in a table?

SqlMysqlSql ServerDatabaseOracle

Sql Problem Overview


For the various popular database systems, how do you list all the columns in a table?

Sql Solutions


Solution 1 - Sql

For MySQL, use:

DESCRIBE name_of_table;

This also works for Oracle as long as you are using SQL*Plus, or Oracle's SQL Developer.

Solution 2 - Sql

For Oracle (PL/SQL)

SELECT column_name
FROM user_tab_cols
WHERE table_name = 'myTableName'

For MySQL

SHOW COLUMNS FROM table_name

Solution 3 - Sql

For MS SQL Server:

select COLUMN_NAME from information_schema.columns where table_name = 'tableName'

Solution 4 - Sql

(5 years laters, for the Honor of PostgreSQL, the most advanced DDBB of the Kingdom)

In PostgreSQL:

\d table_name

Or, using SQL:

select column_name, data_type, character_maximum_length
    from INFORMATION_SCHEMA.COLUMNS 
    where table_name = 'table_name';

Solution 5 - Sql

I know it's late but I use this command for Oracle:

select column_name,data_type,data_length from all_tab_columns where TABLE_NAME = 'xxxx' AND OWNER ='xxxxxxxxxx'

Solution 6 - Sql

SQL Server

SELECT 
	c.name 
FROM
	sys.objects o
INNER JOIN
	sys.columns c
ON
	c.object_id = o.object_id
AND o.name = 'Table_Name'

or

SELECT 
	COLUMN_NAME 
FROM 
	INFORMATION_SCHEMA.COLUMNS
WHERE 
	TABLE_NAME  = 'Table_Name'

The second way is an ANSI standard and therefore should work on all ANSI compliant databases.

Solution 7 - Sql

Call below code in MS SQL Server:

sp_columns [tablename]

Solution 8 - Sql

Microsoft SQL Server Management Studio 2008 R2:

In a query editor, if you highlight the text of table name (ex dbo.MyTable) and hit ALT+F1, you'll get a list of column names, type, length, etc.

ALT+F1 while you've highlighted dbo.MyTable is the equivalent of running EXEC sp_help 'dbo.MyTable' according to this site

I can't get the variations on querying INFORMATION_SCHEMA.COLUMNS to work, so I use this instead.

Solution 9 - Sql

For SQL Server

sp_help tablename

Solution 10 - Sql

SQL Server

To list all the user defined tables of a database:

use [databasename]
select name from sysobjects where type = 'u'

To list all the columns of a table:

use [databasename]
select name from syscolumns where id=object_id('tablename')

Solution 11 - Sql

Just a slight correction on the others in SQL Server (schema prefix is becoming more important!):

SELECT name
  FROM sys.columns 
  WHERE [object_id] = OBJECT_ID('dbo.tablename');

Solution 12 - Sql

Example:

select Table_name as [Table] , column_name as [Column] , Table_catalog as [Database], table_schema as [Schema]  from information_schema.columns
where table_schema = 'dbo'
order by Table_name,COLUMN_NAME

Just my code

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMattGrommesView Question on Stackoverflow
Solution 1 - SqldaveView Answer on Stackoverflow
Solution 2 - SqlMattGrommesView Answer on Stackoverflow
Solution 3 - SqlJeff Meatball YangView Answer on Stackoverflow
Solution 4 - SqlearizonView Answer on Stackoverflow
Solution 5 - Sqlka_linView Answer on Stackoverflow
Solution 6 - SqlRuss CamView Answer on Stackoverflow
Solution 7 - SqlBryanView Answer on Stackoverflow
Solution 8 - SqlLeslie SageView Answer on Stackoverflow
Solution 9 - SqlPrahalad GaggarView Answer on Stackoverflow
Solution 10 - SqlMircea GrelusView Answer on Stackoverflow
Solution 11 - SqlAaron BertrandView Answer on Stackoverflow
Solution 12 - SqlMatthew loweView Answer on Stackoverflow