115

Which query will give the table structure with column definitions in SQL?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Anto Varghese
  • 3,131
  • 6
  • 31
  • 38
  • 3
    You should specify your database vendor as the command(s) differ. –  Jul 29 '10 at 12:08
  • 2
    possible 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
  • 3
    It'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 Answers13

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 (or sp_columns table_name for only columns)
  • Oracle DB2: desc table_name or describe table_name
  • MySQL: describe table_name (or show 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
28

In MySQL you can use DESCRIBE <table_name>

Anax
  • 9,122
  • 5
  • 34
  • 68
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
  • 1
    Note 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'
neeraj
  • 141
  • 1
  • 2
8
DESCRIBE tableName

Check MySQL describe command

Amarghosh
  • 58,710
  • 11
  • 92
  • 121
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
2

For SQL, use the Keyword 'sp_help' enter image description here

maneesh
  • 919
  • 8
  • 6
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
1

In DBTools for Sybase, it's sp_columns your_table_name.

Quan VO
  • 1,258
  • 11
  • 19