Which query will give the table structure with column definitions in SQL?
Asked
Active
Viewed 5.8e+01k times
115
-
3You should specify your database vendor as the command(s) differ. – Jul 29 '10 at 12:08
-
2possible duplicate of [Which SQL command can I use to see the structure of a table on SQL Server?](http://stackoverflow.com/questions/3159236/which-sql-command-can-i-use-to-see-the-structure-of-a-table-on-sql-server) – Damien Overeem Mar 20 '14 at 10:17
-
3It's not a duplicate since there the database is specified. – schmijos Feb 27 '15 at 16:15
-
2@schmijos: actually it's implied, at best. "sql" does NOT mean "MS Sql Server" and it's irritating that so many people seem to think it's acceptable to use the generic term to specify a specific vendor. "sql" means Structured Query Language and Microsoft is not the only "sql". – Bane Jan 30 '18 at 17:02
-
2@bane I actually thought the same and tried to answer the generic "SQL" question. But the OP deleted my answer. That's was reason for the implication. And in the meantime the question was edited from MSSQL to SQL. So I don't get it anymore. – schmijos Feb 01 '18 at 14:07
-
@schmijos +1 -- I guess a case of when history-rewriting is a bad thing. :\ – Bane Feb 02 '18 at 15:01
13 Answers
77
It depends from the database you use. Here is an incomplete list:
- sqlite3:
.schema table_name
- Postgres (psql):
\d table_name
- SQL Server:
sp_help table_name
(orsp_columns table_name
for only columns) - Oracle DB2:
desc table_name
ordescribe table_name
- MySQL:
describe table_name
(orshow columns from table_name
for only columns)

schmijos
- 8,114
- 3
- 50
- 58
75
sp_help tablename in sql server -- sp_help [ [ @objname = ] 'name' ]
desc tablename in oracle -- DESCRIBE { table-Name | view-Name }

Pranay Rana
- 175,020
- 35
- 237
- 263
22
select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='<Table Name>'
You can get details like column datatype and size by this query

Pranay Rana
- 175,020
- 35
- 237
- 263

Pankaj Upadhyay
- 419
- 3
- 6
-
1Note that this command runs extremely slow in case you have many tables. In my company we have >5k tables, each table has 10,20, some has 100 cols, and it results it read timeout operation. `describe` is prefered – Duc Trung Mai Mar 25 '21 at 02:31
14
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'student'

Meninx - メネンックス
- 6,331
- 16
- 30

neeraj
- 141
- 1
- 2
5
For Sybase aka SQL Anywhere the following command outputs the structure of a table:
DESCRIBE 'TABLE_NAME';

Ozair Kafray
- 13,351
- 8
- 59
- 84
5
For SQL Server use exec sp_help
USE db_name;
exec sp_help 'dbo.table_name'
For MySQL, use describe
DESCRIBE table_name;

Hari_pb
- 7,088
- 3
- 45
- 53
4
Highlight table name in the console and press ALT+F1

Roman Marusyk
- 23,328
- 24
- 73
- 116

Krishna Teja
- 107
- 2
- 7
-
10OP asked for the query not for a vague steps in a unspecified console. – Wranorn Dec 14 '17 at 08:11
1
This depends on your database vendor. Mostly it's the "information schema" you should Google for (applies to MySQL, MSSQL and perhaps others).

Karel Petranek
- 15,005
- 4
- 44
- 68
1
Sql server
DECLARE @tableName nvarchar(100)
SET @tableName = N'members' -- change with table name
SELECT
[column].*,
COLUMNPROPERTY(object_id([column].[TABLE_NAME]), [column].[COLUMN_NAME], 'IsIdentity') AS [identity]
FROM
INFORMATION_SCHEMA.COLUMNS [column]
WHERE
[column].[Table_Name] = @tableName

Gabriele Petrioli
- 191,379
- 34
- 261
- 317