214

I need to query the database to get the column names, not to be confused with data in the table. For example, if I have a table named EVENT_LOG that contains eventID, eventType, eventDesc, and eventTime, then I would want to retrieve those field names from the query and nothing else.

I found how to do this in:

But I need to know: how can this be done in Oracle?

Community
  • 1
  • 1
Paxenos
  • 2,756
  • 4
  • 21
  • 20

26 Answers26

247

You can query the USER_TAB_COLUMNS table for table column metadata.

SELECT table_name, column_name, data_type, data_length
FROM USER_TAB_COLUMNS
WHERE table_name = 'MYTABLE'
baretta
  • 7,385
  • 1
  • 25
  • 25
  • 22
    Note that this is specific to Oracle. – ConcernedOfTunbridgeWells May 27 '14 at 11:56
  • 11
    Is there any reason why this could return "no rows selected"? (In Oracle.) – Ian R. O'Brien Jun 10 '14 at 14:34
  • 25
    If you have 'no rows selected' then you could try to change `USER_TAB_COLUMNS` to `all_tab_columns`. To be 100% sure about result you could speficy owner. – Dracontis Feb 25 '15 at 09:01
  • 4
    You can add "ORDER by column_id" in case you want to retrieve them in the same order they were created in the table. Here are some other relevant column data from the table https://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_2094.htm#I1020277 – Bernauer Apr 13 '16 at 07:49
  • 3
    The Oracle table USER_TAB_COLUMNS describe the columns owns by the current user. To access to all columns of all owner, you can query DBA_TAB_COLUMNS. – Renaud Kern Sep 06 '17 at 09:01
  • 7
    Make sure the table name is uppercase. – Hugh Seagraves Oct 09 '18 at 16:16
  • Why none of these work in Python? And only if WHERE clause is added. I needed to use `field_name = [field[0] for field in cur.description]` from cursor object instead of pure sql query. – Peter.k Jan 04 '19 at 03:10
  • Silly Me. The name of the table happens to be case specific. All Caps worked SO MUCH BETTER. – Aggie Jon of 87 Feb 04 '20 at 14:25
67

In SQL Server...

SELECT [name] AS [Column Name]
FROM syscolumns
WHERE id = (SELECT id FROM sysobjects WHERE type = 'V' AND [Name] = 'Your table name')

Type = 'V' for views Type = 'U' for tables

Eppz
  • 3,178
  • 2
  • 19
  • 26
44

You can do this:

describe EVENT_LOG

or

desc EVENT_LOG

Note: only applicable if you know the table name and specifically for Oracle.

Sergey Golovchenko
  • 18,203
  • 15
  • 55
  • 72
26

For SQL Server 2008, we can use information_schema.columns for getting column information

SELECT *
FROM   information_schema.columns
WHERE  table_name = 'Table_Name'
ORDER  BY ordinal_position  
Jom
  • 1,877
  • 5
  • 29
  • 46
  • 1
    Note that the `INFORMATION_SCHEMA` tables are the ANSI standard metadata tables. Any good, modern RDBMS will have them. – Bacon Bits Dec 14 '14 at 02:33
20

For SQLite I believe you can use something like the following:

PRAGMA table_info(table-name);

Explanation from sqlite.org:

This pragma returns one row for each column in the named table. Columns in the result set include the column name, data type, whether or not the column can be NULL, and the default value for the column. The "pk" column in the result set is zero for columns that are not part of the primary key, and is the index of the column in the primary key for columns that are part of the primary key.

See also: Sqlite.org Pragma Table Info

ThE uSeFuL
  • 1,456
  • 1
  • 16
  • 29
shieldstroy
  • 1,307
  • 1
  • 10
  • 24
19

That information is stored in the ALL_TAB_COLUMNS system table:

SQL> select column_name from all_tab_columns where table_name = 'DUAL';

DUMMY

Or you could DESCRIBE the table if you are using SQL*PLUS:

SQL> desc dual
Name                               Null?    Type
----------------------------------------------------- -------- ---------------------- -------------
DUMMY                               VARCHAR2(1)
Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
  • Official documentation on [**`ALL_TAB_COLUMNS`**](https://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_2094.htm) via the official Oracle *Database Reference*. This view describes the columns of the tables, views, and clusters accessible to the current user. – Mr. Polywhirl Dec 16 '15 at 18:41
  • But we cant give schema name in front of table , right ? for example if I want like this `select column_name from all_tab_columns where table_name = 'dbo.usertbl';` – AhammadaliPK Jul 01 '20 at 05:54
9

The other answers sufficiently answer the question, but I thought I would share some additional information. Others describe the "DESCRIBE table" syntax in order to get the table information. If you want to get the information in the same format, but without using DESCRIBE, you could do:

SELECT column_name as COLUMN_NAME, nullable || '       ' as BE_NULL,
  SUBSTR(data_type || '(' || data_length || ')', 0, 10) as TYPE
 FROM all_tab_columns WHERE table_name = 'TABLENAME';

Probably doesn't matter much, but I wrote it up earlier and it seems to fit.

Sasha
  • 1,190
  • 7
  • 10
8

For Oracle

SELECT column_name FROM user_tab_cols WHERE table_name=UPPER('tableName');
jampez77
  • 5,012
  • 7
  • 32
  • 52
5
describe YOUR_TABLE;

In your case :

describe EVENT_LOG;
Rohit
  • 109
  • 1
  • 8
4

Even this is also one of the way we can use it

select * from product where 1 != 1
Robert
  • 5,278
  • 43
  • 65
  • 115
Jaisankar
  • 453
  • 1
  • 5
  • 10
3
select column_name,* from information_schema.columns
 where table_name = 'YourTableName'
order by ordinal_position
  • 1
    does this query work, with specifying a col name and star- " select column_name, * " .. It is not working in Oracle. – Teja Mar 10 '15 at 00:32
3

For MySQL, use

SELECT column_name 
FROM information_schema.columns 
WHERE 
table_schema = 'Schema' AND table_name = 'Table_Name'
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
ssamuel68
  • 932
  • 13
  • 10
2

For SQL Server:

SELECT [name] AS [Column Name]
FROM syscolumns
WHERE id = object_id('TABLE_NAME')
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
bstricks
  • 823
  • 8
  • 14
2
SELECT COLUMN_NAME 'all_columns' 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME='user';
Lucas Zamboulis
  • 2,494
  • 5
  • 24
  • 27
expert one
  • 37
  • 1
  • Welcome on Stack Overflow @expert one. Even if your answer seems to be accurate, please add more details and explanations than juste 'code'. It would be clearer for everyone. – Jean-Rémy Revy May 27 '14 at 12:04
1

You could also try this, but it might be more information than you need:

sp_columns TABLE_NAME
WEFX
  • 8,298
  • 8
  • 66
  • 102
1
SELECT A.COLUMN_NAME, A.* FROM all_tab_columns a 
WHERE table_name = 'Your Table Name'
AND A.COLUMN_NAME = 'COLUMN NAME' AND a.owner = 'Schema'
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Henry
  • 19
  • 1
1

Mysql

SHOW COLUMNS FROM a_table_named_users WHERE Field REGEXP 'user_id|user_name|user_pass'

This will return a result something like this:

Field     |  Type        |   Null   |   Key   |   Default   |   Extra  
user_id      int(8)          NO         PRI       NULL          auto_increment
user_name    varchar(64)     NO         MUL       NULL
user_pass    varchar(64)     NO                   NULL

Then to pull out the values you can simply

fetch row[0]

This is also great for passing input dynamically since the REGEXP needs the '|' for multiple inputs, but is also a way to keeps data separated and easy to store/pass to classes/functions.

Try throwing in dummy data as well for security when sending it out and compare what was returned when receiving any errors.

Dbo
  • 127
  • 4
1

In Oracle, there is two views that describe columns:

  • DBA_TAB_COLUMNS describes the columns of all tables, views, and clusters in the database.

  • USER_TAB_COLUMNS describes the columns of the tables, views, and
    clusters owned by the current user. This view does not display the
    OWNER column.

Renaud Kern
  • 1,116
  • 10
  • 25
0

The answer is here: http://php.net/manual/en/function.mysql-list-fields.php I'd use the following code in your case:

$result = mysql_query("SHOW COLUMNS FROM sometable");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$fields = array();
if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
        $fields[] = $row['Field'];
    }
}
user2558588
  • 77
  • 1
  • 2
0

you can run this query

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 c.name LIKE '%%' --if you want to find specific column write here 
ORDER BY schema_name, table_name;
Etibar - a tea bar
  • 1,912
  • 3
  • 17
  • 30
0

This will return all tables:

SELECT table_name, column_name, data_type, data_length
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'YOUR_TABLE_NAME'
DSH
  • 1,038
  • 16
  • 27
-1

Try this

select * from sys.all_columns c join sys.objects o on c.object_id=o.object_id where o.name = 'TABLENAME' and c.name like '%COLUMN NAME%'
pals17
  • 1
  • 1
-2

Just select first row from the table , for oracle : select * from <table name> where rownum = 1;

blue-sky
  • 51,962
  • 152
  • 427
  • 752
-2

Came across this question looking for access to column names on Teradata, so I'll add the answer for their 'flavour' of SQL:

SELECT ColumnName
FROM DBC.Columns
WHERE DatabaseName='DBASE_NAME'
AND TableName='TABLE_NAME';

The info is stored in the DBC dbase.

Getting data types is a little bit more involved: Get column type using teradata system tables

Community
  • 1
  • 1
carnust
  • 611
  • 1
  • 8
  • 10
-3

I did it like this

SELECT 
    TOP 0
    *
FROM
    Posts

It works even in http://data.stackexchange.com whose service tables I am not aware of!

Val
  • 1
  • 8
  • 40
  • 64
-5
SELECT COLUMN_NAME 
FROM YourDatabase.INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'YourTableName'
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100