0% found this document useful (0 votes)
19 views

SQL - How Do I List All The Columns in A Table - Stack Overflow

This document discusses different ways to list all columns in a table for various database systems. It provides SQL commands for MySQL, Oracle, SQL Server, SQLite, and PostgreSQL. For MySQL, it recommends using DESCRIBE table_name. For SQL Server, it recommends either using INFORMATION_SCHEMA.COLUMNS or querying sys.objects and sys.columns.

Uploaded by

soydeyudru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

SQL - How Do I List All The Columns in A Table - Stack Overflow

This document discusses different ways to list all columns in a table for various database systems. It provides SQL commands for MySQL, Oracle, SQL Server, SQLite, and PostgreSQL. For MySQL, it recommends using DESCRIBE table_name. For SQL Server, it recommends either using INFORMATION_SCHEMA.COLUMNS or querying sys.objects and sys.columns.

Uploaded by

soydeyudru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

How do I list all the columns in a table?

Asked 14 years, 1 month ago Modified 4 months ago Viewed 1.2m times

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

314 sql mysql sql-server database oracle

Share Improve this question Follow asked Oct 16, 2009 at 21:10
MattGrommes
12k 10 37 40

Here is the response stackoverflow.com/questions/8739203/… – KaderLAB Feb 15, 2021 at 12:45

13 Answers Sorted by: Highest score (default)

For MySQL, use:

353 DESCRIBE name_of_table;

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

Share Improve this answer Follow edited Dec 13, 2018 at 14:30 answered Oct 16, 2009 at 21:12
walen dave
7,133 2 37 58 12.5k 10 43 59

27 This solution is for MYSQL not MSSQL – TheTechGuy Jul 18, 2013 at 12:14

2 @dmvianna I don't think that necessarily applies to all of Oracle, but to SQL*Plus. – Tripp Kinetics Sep 18, 2014 at
15:48

It should be DESCRIBE name_of_table``; – beahacker Sep 21, 2018 at 9:25

2 for sqlite - use: pragma table_info(table_name) i.e. sqlite> pragma table_info(column1); – GyRo Oct 21, 2018 at 10:33

Editing, since DESCRIBE is not an Oracle PLSQL instruction but a SQL*Plus command, and as such it doesn't work in
most SQL IDEs. – walen Dec 13, 2018 at 14:29

For MS SQL Server:

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

Share Improve this answer Follow edited Apr 27, 2022 at 5:58 answered Oct 16, 2009 at 21:17
Smart Manoj Jeff Meatball Yang
5,293 5 34 60 38.1k 27 91 125
9 The column of interest here would be COLUMN_NAME. – Buggieboy Mar 27, 2013 at 19:53

4 This should work on many DBMSs. information_schema.columns system view is part of ANSI SQL standard (link).
– Bogdan Sahlean Jul 28, 2013 at 20:12

7 good answer but to avoid duplicates I would use: select COLUMN_NAME from information_schema.columns where
table_name = 'tableName' and table_schema = 'databaseName' – But those new buttons though.. Feb 25, 2015
at 15:14

This is SQL-92 ANSI compliant, and ought to work in all database engines. – Gareth Davidson May 13, 2020 at 19:01

For Oracle (PL/SQL)

154 SELECT column_name


FROM user_tab_cols
WHERE table_name = 'myTableName'

For MySQL

SHOW COLUMNS FROM table_name

Share Improve this answer Follow answered Oct 16, 2009 at 21:11
MattGrommes
12k 10 37 40

5 Youd probably want to order the Oracle query by column_id – David Aldridge Oct 18, 2009 at 12:09

8 For Oracle is valid also DESCRIBE name_of_table . – Pigueiras Oct 29, 2013 at 9:42

use <database_name>; show columns in <table_name> like '<column_prefix>%'; Will let you list only the columns
starting with the prefix specified. Omitting the angle brackets of course. – rstackhouse Apr 10, 2014 at 21:10

1 what is user_tab_cols in your query? – Jogi Apr 27, 2016 at 11:54

@Jogi - Google "oracle user_tab_cols" - its built-in to Oracle db. – ToolmakerSteve Feb 22, 2019 at 0:11

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

In PostgreSQL:
63
\d table_name

Or, using SQL:

select column_name, data_type, character_maximum_length


from INFORMATION_SCHEMA.COLUMNS
where table_name = 'table_name';

Share Improve this answer Follow edited Apr 16, 2016 at 19:02 answered May 8, 2015 at 13:20
Jamie Schembri earizon
6,045 4 26 37 2,139 19 29
4 should be \d table_name. \dt table_name lists the relations. – l85m Sep 9, 2015 at 20:23

The second query also works for ms sql. I used it because it added two more important variables that I wanted over
the answer for ms sql. – Trevor Vance Mar 14 at 20:07

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

42 select column_name,data_type,data_length from all_tab_columns where TABLE_NAME = 'xxxx'


AND OWNER ='xxxxxxxxxx'

Share Improve this answer Follow edited Feb 22, 2018 at 16:56 answered Nov 17, 2014 at 16:25
Positive Navid ka_lin
2,541 2 27 41 9,369 6 36 56

stackoverflow.com/questions/8739203/… – zloctb Aug 18, 2015 at 7:09

I tried this in Oracle and it didn't work. The column_name was printed but nothing else. I had to use SELECT
CAST(COLUMN_NAME AS CHAR(40)) || ' ' || DATA_TYPE to get a nice format and obtain multiple columns with
concatenation. – Eamonn Kenny Apr 25, 2019 at 11:29

if query returns empty, try using upper, like here: stackoverflow.com/a/17364929/5691498 – Yogev Levy Nov 29,
2021 at 14:19

SQL Server

32 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.

Share Improve this answer Follow edited Apr 24, 2018 at 18:56 answered Oct 16, 2009 at 21:19
Swatantra Kumar Russ Cam
1,334 5 24 32 124k 33 204 268

1 Neither of these work as written (or at least implied, as I read it) for MS SQL Server. In both cases the table name
column stores the name without any [ ] around it, so the query must not use them, only the plain table name. If
that was not the OP's intention, at least be aware of this. – JonBrave Jul 20, 2016 at 11:03
1 @JonBrave - that's correct, the square brackets were there to imply "insert your table name here" :) – Russ Cam Jul
20, 2016 at 11:22

Being square brackets, I read it as the "insert your table name inside square brackets (because of potentially reserved
word) here", and then got no matches :) Perhaps BNF <Table Name> would have avoided the ambiguity. Anyway, I
realised you might have intended that as I wrote the comment --- it does no harm to warn others just in case.
– JonBrave Jul 20, 2016 at 12:24

1 Only works for MSSQL if there is no '[ ]' and the quotes ' ' are needed around the table name. – XValidated Jan 4,
2018 at 21:21

Call below code in MS SQL Server:

21 sp_columns [tablename]

Share Improve this answer Follow edited Apr 11, 2021 at 7:34 answered Oct 16, 2009 at 21:14
Amin Golmahalleh Bryan
3,723 2 24 36 8,748 7 42 62

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
13
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.

Share Improve this answer Follow edited May 13, 2016 at 1:05 answered May 3, 2013 at 22:23
Nate Anderson Leslie Sage
18.8k 20 100 137 391 2 8

1 Did not work in SSMS 2012. Btw did you mean SQL Server Management Studio 2008? – TheTechGuy Jul 18, 2013 at
12:19

1 Yep, more precisely I meant Microsoft SQL Server Management Studio 2008 R2. I'll edit. – Leslie Sage Aug 12, 2013
at 2:50

For SQL Server

7 sp_help tablename

Share Improve this answer Follow edited May 5, 2021 at 4:52 answered Aug 12, 2013 at 3:13
Prahalad Gaggar
11.4k 16 53 71
Just a slight correction on the others in SQL Server (schema prefix is becoming more important!):

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

Share Improve this answer Follow edited Oct 28, 2022 at 14:56 answered Oct 16, 2009 at 22:59
Aaron Bertrand
274k 37 470 498

Aaron, Thanks for adding this option to the list. Previously I was using this code. SELECT COLUMN_NAME FROM
INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Table_Name' However, it doesn’t always work.
Especially on very large tables, like 50 million rows or more. Not sure why. Your option works just great on all the
tables I have tried so far. Thanks, and kudos. – Aubrey Love Oct 28, 2022 at 14:52

SQL Server

To list all the user defined tables of a database:


3
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')

Share Improve this answer Follow edited Oct 16, 2009 at 21:41 answered Oct 16, 2009 at 21:20
Mircea Grelus
2,915 1 20 14

Heh? This is just wrong...you can only use USE for databases...And the query returns all user defined tables in the
database, which is not what the OP wanted. – Maximilian Mayerl Oct 16, 2009 at 21:25

Example:

3 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

Share Improve this answer Follow edited Feb 27, 2015 at 14:00 answered Feb 27, 2015 at 13:37
mmmmmpie Matthew lowe
2,938 1 19 26 31 1

AWS Athena
To list all columns from a table you can use:

0 SHOW COLUMNS {FROM|IN} database_name.table_name

or

SHOW COLUMNS {FROM|IN} table_name [{FROM|IN} database_name]

The FROM and IN keywords can be used interchangeably.

AWS Athena doc

Share Improve this answer Follow answered Jun 28 at 11:09


mmsilviu
1,231 15 25

You might also like