34,590
社区成员
发帖
与我相关
我的任务
分享
create table TestTable(id int,AppType varchar(10))
go
ALTER TABLE [dbo].[TestTable] ADD CONSTRAINT [DF_TestTable_AppType] DEFAULT ('') FOR [AppType]
go
select a.name as DFName,b.Name as ColumnName
from sys.objects a inner join sys.columns b on a.object_id=b.default_object_id
where a.type='D'
and b.object_id=object_id('TestTable') --不加这个条件,库中所有默认约束都可以看到
/*
DFName ColumnName
--------------------------------------------- ----------------------------------
DF_TestTable_AppType AppType
(1 行受影响)
*/
go
drop table TestTable
--获取约束信息
select * from information_schema.constraint_column_usage---可以获取指定数据库中的所有约束的信息以及约束与列的对应关系
go
select * from information_schema.constraint_table_usage---查询结果中只包含表和约束的对应关系,并没有约束对应的列信息
go
select * from information_schema.table_constraints---查询结果中只包含表和约束的对应关系,并没有约束对应的列信息
go
select * from information_schema.key_column_usage---可以获取指定数据库中的所有键约束的列信息,包括主键约束中的主键列,唯一约束中的唯一键列和外键约束中的引用列
go
select * from sys.key_constraints----获取约束信息
select * from sys.foreign_keys--获取表中的外键约束
select * from sys.foreign_key_columns--获取外键约束的列信息
select * from information_schema.referential_constraints--获取外键约束信息
select * from information_schema.check_constraints--获取检查约束信息
select c.name from sysconstraints a
inner join syscolumns b on a.colid=b.colid
inner join sysobjects c on a.constid=c.id
where a.id=object_id('表名')
and b.name='字段名'