T-SQL query to show table definition?

后端 未结 16 1208
爱一瞬间的悲伤
爱一瞬间的悲伤 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:41
    SELECT ORDINAL_POSITION, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
           , IS_NULLABLE
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'EMPLOYEES'
    
    0 讨论(0)
  • 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')
    
    0 讨论(0)
  • 2020-12-02 06:47

    This will return columns, datatypes, and indexes defined on the table:

    --List all tables in DB
    select * from sysobjects where xtype = 'U'
    
    --Table Definition
    sp_help TableName
    

    This will return triggers defined on the table:

    --Triggers in SQL Table
    select * from sys.triggers where parent_id = object_id(N'SQLTableName') 
    
    0 讨论(0)
  • 2020-12-02 06:48

    Have you tried sp_help?

    sp_help 'TableName'
    
    0 讨论(0)
提交回复
热议问题