MySQL 如何列出表中的所有列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1580450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I list all the columns in a table?
提问by MattGrommes
For the various popular database systems, how do you list all the columns in a table?
对于各种流行的数据库系统,如何列出一个表中的所有列?
回答by dave
For MySQL, use:
对于 MySQL,请使用:
DESCRIBE name_of_table;
This also works for Oracle as long as you are using SQL*Plus, or Oracle's SQL Developer.
只要您使用 SQL*Plus 或 Oracle 的 SQL Developer,这也适用于 Oracle。
回答by MattGrommes
For Oracle (PL/SQL)
对于 Oracle (PL/SQL)
SELECT column_name
FROM user_tab_cols
WHERE table_name = 'myTableName'
For MySQL
对于 MySQL
SHOW COLUMNS FROM table_name
回答by Jeff Meatball Yang
For MS SQL Server:
对于 MS SQL 服务器:
select * from information_schema.columns where table_name = 'tableName'
回答by earizon
(5 years laters, for the Honor of PostgreSQL, the most advanced DDBB of the Kingdom)
(5年后,为了王国最先进的DDBB PostgreSQL的荣誉)
In PostgreSQL:
在 PostgreSQL 中:
\d table_name
Or, using SQL:
或者,使用 SQL:
select column_name, data_type, character_maximum_length
from INFORMATION_SCHEMA.COLUMNS
where table_name = 'table_name';
回答by ka_lin
I know it's late but I use this command for Oracle:
我知道已经晚了,但我对 Oracle 使用此命令:
select column_name,data_type,data_length from all_tab_columns where TABLE_NAME = 'xxxx' AND OWNER ='xxxxxxxxxx'
回答by Russ Cam
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 shouldwork on allANSI compliant databases.
第二种方式是 ANSI 标准,因此应该适用于所有符合 ANSI 标准的数据库。
回答by Bryan
MS SQL Server:
微软 SQL 服务器:
sp_columns [tablename]
sp_columns [表名]
回答by Leslie Sage
Microsoft SQL Server Management Studio 2008 R2:
Microsoft SQL Server 管理工作室 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.
在查询编辑器中,如果您突出显示表名的文本(例如 dbo.MyTable)并点击ALT+ F1,您将获得列名、类型、长度等的列表。
ALT+F1while you've highlighted dbo.MyTable
is the equivalent of running EXEC sp_help 'dbo.MyTable'
according to this site
ALT+F1当您突出显示时dbo.MyTable
,相当于根据此站点运行EXEC sp_help 'dbo.MyTable'
I can't get the variations on querying INFORMATION_SCHEMA.COLUMNS to work, so I use this instead.
我无法让查询 INFORMATION_SCHEMA.COLUMNS 的变化起作用,所以我改用它。
回答by Luv
For SQL Server
对于 SQL Server
sp_help tablename
回答by Mircea Grelus
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')