如何查询表的列描述?

时间:2011-03-17 05:11:07

标签: c# sql-server sql-server-2005

是否有任何查询来获取带有primarykey外键引用的列列表?

SELECT column_name, data_type, CHARacter_maximum_length
FROM DBTIME.information_schema.columns
WHERE table_name = 'Table Name' 

从这个查询中我得到数据类型和列的最大长度。除此之外,我需要知道它的外键主键参考。请帮忙......

2 个答案:

答案 0 :(得分:0)

SELECT * FROM information_schema.columns WHERE table_name = 'Table Name' 
select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where TABLE_NAME='Table Name'// It will list all the primary key and foreign key references.

答案 1 :(得分:0)

SELECT
    K_Table = FK.TABLE_NAME,
    FK_Column = CU.COLUMN_NAME,
    PK_Table = PK.TABLE_NAME,
    PK_Column = PT.COLUMN_NAME,
    Constraint_Name = C.CONSTRAINT_NAME
FROM
    INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK
    ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK
    ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
    ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
    SELECT
        i1.TABLE_NAME,
        i2.COLUMN_NAME
    FROM
        INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
    INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2
        ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
    WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT
    ON PT.TABLE_NAME = PK.TABLE_NAME