查询表的外键引用


[sqlserver]

select * from (
	select fk.name 外键约束名, ftable.name 子表名, cn.name 外键列名, rtable.name 父表名 from sysforeignkeys
	join sysobjects fk
	on sysforeignkeys.constid = fk.id
	join sysobjects ftable
	on sysforeignkeys.fkeyid = ftable.id
	join sysobjects rtable
	on sysforeignkeys.rkeyid = rtable.id
	join syscolumns cn
	on sysforeignkeys.fkeyid = cn.id and sysforeignkeys.fkey = cn.colid
)a where a.[父表名]='CODE_TASK_CODE'


[oracle]


SELECT F.TABLE_NAME, F.CONSTRAINT_NAME, F.COLUMN_NAME, F.POSITION, P.TABLE_NAME, P.COLUMN_NAME, P.POSITION
  FROM USER_CONSTRAINTS R, USER_CONS_COLUMNS F, USER_CONS_COLUMNS P
WHERE 
   R.CONSTRAINT_TYPE = 'R'
   AND R.CONSTRAINT_NAME = F.CONSTRAINT_NAME
   AND R.R_CONSTRAINT_NAME = P.CONSTRAINT_NAME
   AND F.POSITION = P.POSITION
 --and r.CONSTRAINT_NAME='FK_MSG_ATTA_2' --外键名
 and (F.TABLE_NAME='MSG_ATTACHFILES' or P.TABLE_NAME='MSG_ATTACHFILES')--表名
ORDER BY F.TABLE_NAME, F.CONSTRAINT_NAME, F.POSITION;


















你可能感兴趣的:(查询表的外键引用)