[sql] How can I get column names from a table in Oracle?

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?

This question is related to sql database oracle

The answer is


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


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'];
    }
}

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


SELECT COLUMN_NAME 'all_columns' 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME='user';

SELECT COLUMN_NAME 
FROM YourDatabase.INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'YourTableName'

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  

For SQL Server:

SELECT [name] AS [Column Name]
FROM syscolumns
WHERE id = object_id('TABLE_NAME')

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'

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)

For Oracle

SELECT column_name FROM user_tab_cols WHERE table_name=UPPER('tableName');

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  

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%'

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


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

select * from product where 1 != 1

For Oracle

SELECT column_name FROM user_tab_cols WHERE table_name=UPPER('tableName');

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;

SELECT COLUMN_NAME 'all_columns' 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME='user';

You can do this:

describe EVENT_LOG

or

desc EVENT_LOG

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


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

sp_columns TABLE_NAME

You can do this:

describe EVENT_LOG

or

desc EVENT_LOG

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


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.


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.


For MySQL, use

SELECT column_name 
FROM information_schema.columns 
WHERE 
table_schema = 'Schema' AND table_name = 'Table_Name'

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.


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


For MySQL, use

SELECT column_name 
FROM information_schema.columns 
WHERE 
table_schema = 'Schema' AND table_name = 'Table_Name'

SELECT COLUMN_NAME 
FROM YourDatabase.INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'YourTableName'

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.


describe YOUR_TABLE;

In your case :

describe EVENT_LOG;

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)

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

select * from product where 1 != 1

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)

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


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


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%'

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.


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


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.


You can do this:

describe EVENT_LOG

or

desc EVENT_LOG

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


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'

You can do this:

describe EVENT_LOG

or

desc EVENT_LOG

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


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

sp_columns TABLE_NAME

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


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)

describe YOUR_TABLE;

In your case :

describe EVENT_LOG;

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.


select column_name,* from information_schema.columns
 where table_name = 'YourTableName'
order by ordinal_position

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


For SQL Server:

SELECT [name] AS [Column Name]
FROM syscolumns
WHERE id = object_id('TABLE_NAME')

select column_name,* from information_schema.columns
 where table_name = 'YourTableName'
order by ordinal_position

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;

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!


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.


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


Examples related to sql

Passing multiple values for same variable in stored procedure SQL permissions for roles Generic XSLT Search and Replace template Access And/Or exclusions Pyspark: Filter dataframe based on multiple conditions Subtracting 1 day from a timestamp date PYODBC--Data source name not found and no default driver specified select rows in sql with latest date for each ID repeated multiple times ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database

Examples related to database

Implement specialization in ER diagram phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' cannot be loaded Room - Schema export directory is not provided to the annotation processor so we cannot export the schema SQL Query Where Date = Today Minus 7 Days MySQL Error: : 'Access denied for user 'root'@'localhost' SQL Server date format yyyymmdd How to create a foreign key in phpmyadmin WooCommerce: Finding the products in database TypeError: tuple indices must be integers, not str

Examples related to oracle

concat yesterdays date with a specific time ORA-28001: The password has expired how to modify the size of a column How to create a blank/empty column with SELECT query in oracle? Find the number of employees in each department - SQL Oracle Query to display all tablespaces in a database and datafiles When or Why to use a "SET DEFINE OFF" in Oracle Database How to insert date values into table error: ORA-65096: invalid common user or role name in oracle In Oracle SQL: How do you insert the current date + time into a table?