--GENERIC WAY OF ADDING FK's with on update cascade and on delete cascade
ALTER TABLE [dbo].[FkTable] WITH CHECK ADD CONSTRAINT [FK_FkTable_FkColName_PkTable_PkColName] FOREIGN KEY([FkColName])
REFERENCES [dbo].[PkTable] ([PkColName])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[FkTable] CHECK CONSTRAINT [FK_FkTable_FkColName_PkTable_PkColName]
GO
--AND NOW QUERY THEM
SELECT OBJECT_NAME(f.parent_object_id) AS 'FkTable' ,
COL_NAME(fc.parent_object_id,fc.parent_column_id) AS 'FkColName' ,
f.name AS 'FkName' ,
OBJECT_NAME (f.referenced_object_id) as 'PkTable' , COL_NAME(fc.referenced_object_id,
fc.referenced_column_id) AS 'PKColName'
FROM .sys.foreign_keys AS f
INNER JOIN .sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!