T-SQL query to show table definition?

后端 未结 16 1265
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 06:16

What is a query that will show me the full definition, including indexes and keys for a SQL Server table? I want a pure query - and know that SQL Studio can give this to me

16条回答
  •  生来不讨喜
    2020-12-02 06:46

    There is no easy way to return the DDL. However you can get most of the details from Information Schema Views and System Views.

    SELECT ORDINAL_POSITION, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
           , IS_NULLABLE
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'Customers'
    
    SELECT CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
    WHERE TABLE_NAME = 'Customers'
    
    SELECT name, type_desc, is_unique, is_primary_key
    FROM sys.indexes
    WHERE [object_id] = OBJECT_ID('dbo.Customers')
    

提交回复
热议问题