导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

如何查询出当前数据表主健是否作为其他数据表的外健

mydreamf 2007-12-21 05:58:51
急!!如题!!高手指教!!
...全文
51 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
w2jc 2007-12-22
学习一下
回复
JL99000 2007-12-22
我也会了
哈哈
exec sp_fkeys '表名'
回复
liangCK 2007-12-21
--表的外键约束

SELECT 外键表ID=b.fkeyid
,外键表名称=object_name(b.fkeyid)
,外键列ID=b.fkey
,外键列名=(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid)
,主键表ID=b.rkeyid
,主键表名=object_name(b.rkeyid)
,主键列ID=b.rkey
,主键列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid)
,级联更新=ObjectProperty(a.id,'CnstIsUpdateCascade')
,级联删除=ObjectProperty(a.id,'CnstIsDeleteCascade')
FROM sysobjects a
join sysforeignkeys b on a.id=b.constid
join sysobjects c on a.parent_obj=c.id
where a.xtype='f' AND c.xtype='U'


--查询一个表的所有外键:

SELECT 主键列ID=b.rkey
,主键列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid)
,外键表ID=b.fkeyid
,外键表名称=object_name(b.fkeyid)
,外键列ID=b.fkey
,外键列名=(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid)
,级联更新=ObjectProperty(a.id,'CnstIsUpdateCascade')
,级联删除=ObjectProperty(a.id,'CnstIsDeleteCascade')
FROM sysobjects a
join sysforeignkeys b on a.id=b.constid
join sysobjects c on a.parent_obj=c.id
where a.xtype='f' AND c.xtype='U'
and object_name(b.rkeyid)='titles'
回复
tim_spac 2007-12-21
顶一下
回复
wzy_love_sly 2007-12-21
1
回复
-狙击手- 2007-12-21
create table ta(id int PRIMARY KEY)

go
create table tb(id int primary key ,col int REFERENCES ta(id))
go

---1

sp_fkeys 'ta'
----2
select * from sysforeignkeys
/*
PKTABLE_QUALIFIER PKTABLE_OWNER PKTABLE_NAME PKCOLUMN_NAME FKTABLE_QUALIFIER FKTABLE_OWNER FKTABLE_NAME FKCOLUMN_NAME KEY_SEQ UPDATE_RULE DELETE_RULE FK_NAME PK_NAME DEFERRABILITY
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ------- ----------- ----------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------
CSDN_TEST dbo ta id CSDN_TEST dbo tb col 1 1 1 FK__tb__col__417BF6D5 PK__ta__3E9F8A2A 7

constid fkeyid rkeyid fkey rkey keyno
----------- ----------- ----------- ------ ------ ------
1098643157 1066643043 1034642929 2 1 1

(所影响的行数为 1 行)

*/

drop table tb,ta
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告