我有一个博客网站,后端有SQL Server 2012。我正在构建一个移动应用程序,并希望将博客直接发布到后端。
我能够INSERT
在主要表格中发帖,但该帖子没有出现在网站上。
我怀疑有一个或多个表包含构成博客文章的其他数据,但我无法找到它们。
有没有有效的方法来查找其他相关表格?数据库中是否有“隐藏”字段或表?
答案 0 :(得分:1)
第一个查询将为您提供表引用的表的列表,第二个查询将为您提供引用表的表的列表。这仅在表之间设置了引用完整性时才有效。
DECLARE @TableName VARCHAR(MAX) = 'myTable',
@SchemaName VARCHAR(MAX) = 'dbo'
SELECT DISTINCT
s.name AS SchemaName,
o2.name AS TableName
FROM sys.foreign_keys fk
JOIN sys.objects o
ON fk.parent_object_id = o.object_id
JOIN sys.schemas s
ON o.schema_id = s.schema_id
JOIN sys.objects o2
ON fk.referenced_object_id = o2.object_id
JOIN sys.schemas s2
ON o2.schema_id = s2.schema_id
WHERE o.name = @TableName
AND s.name = @SchemaName
ORDER BY SchemaName,
TableName
SELECT DISTINCT
s.name SchemaName,
o.name TableName
FROM sys.foreign_keys fk
JOIN sys.objects o
ON fk.parent_object_id = o.object_id
JOIN sys.schemas s
ON o.schema_id = s.schema_id
JOIN sys.objects o2
ON fk.referenced_object_id = o2.object_id
JOIN sys.schemas s2
ON o2.schema_id = s2.schema_id
WHERE o2.name = @TableName
AND s2.name = @SchemaName
ORDER BY SchemaName,
TableName