sql server displayed all tables foreign keys
sql server displayed all tables foreign keys
SELECT
'ALTER TABLE ' +OBJECT_NAME(f.parent_object_id) AS TableName ,
' ADD CONSTRAINT ' + f.name AS ForeignKey,
' FOREIGN KEY (['+ COL_NAME(fc.parent_object_id,fc.parent_column_id) +'])'AS ColumnName,
' REFERENCES [' + OBJECT_NAME (f.referenced_object_id)+'] ' AS ReferenceTableName,'('+
COL_NAME(fc.referenced_object_id,fc.referenced_column_id) + ')' AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
TableName ForeignKey ColumnName ReferenceTableName ReferenceColumnName
ALTER TABLE profile ADD CONSTRAINT FK_User_UserId FOREIGN KEY ([userId]) REFERENCES [User] (id)
ALTER TABLE UserAddress ADD CONSTRAINT FK_UserAddress_UserId FOREIGN KEY ([userId]) REFERENCES [User] (id)
ALTER TABLE UserContact ADD CONSTRAINT FK_UserContact_UserId FOREIGN KEY ([userId]) REFERENCES [User] (id)
No comments:
Post a Comment