对于各种流行的数据库系统,如何列出表中的所有列?


当前回答

(5年后,为了王国最先进的DDBB PostgreSQL的荣誉)

在PostgreSQL:

\d table_name

或者,使用SQL:

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

其他回答

Oracle (PL/SQL)

SELECT column_name
FROM user_tab_cols
WHERE table_name = 'myTableName'

MySQL

SHOW COLUMNS FROM table_name

对于mssql Server:

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

SQL Server

使用实例列出数据库中所有用户定义的表。

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

列出一个表的所有列:

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

例子:

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

只是我的代码

我知道已经很晚了,但是我在Oracle上使用这个命令:

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