日期:2014-05-17 浏览次数:21237 次
主键用 sys.key_constraints
外键用 sys.foreign_keys
USE AdventureWorks
GO
SELECT OBJECT_SCHEMA_NAME(T.[object_id], DB_ID()) AS [架构名] ,
T.[name] AS [表名] ,
AC.[name] AS [列名] ,
TY.[name] AS [系统数据类型] ,
TY.is_user_defined AS [是否用户自定义类型],--1 = 用户定义类型,0 = SQL Server 系统数据类型
AC.[max_length] [最大长度],
AC.[precision] [精确度],--如果列包含的是数值,则为该列的精度;否则为0
AC.[scale] [数值范围],--如果列包含的是数值,则为列的小数位数;否则为0
AC.[is_nullable] [是否允许为空],
AC.[is_ansi_padded][是否使用ANSI_PADDING]--1 = 如果列为字符、二进制或变量类型,则该列使用ANSI_PADDING ON 行为
FROM sys.[tables] AS T
INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
INNER JOIN sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id]
AND AC.[user_type_id] = TY.[user_type_id]
WHERE T.[is_ms_shipped] = 0
ORDER BY T.[name] ,
AC.[column_id]
exec sp_pkeys '表名' --主键
exec sp_fkeys '表名' --外键