数据库分析和理解表

时间:2016-05-05 07:30:21

标签: sql sql-server database

有一个数据库。 我后来被包括在内。 我怎么知道这个数据库?有工具吗?

一个屏幕上的所有表格? 关系? 等

注意:DB SQL SERVER

谢谢。

1 个答案:

答案 0 :(得分:1)

有几个表和命令可以帮助你。

获取数据库的ER图:

  

创建新的数据库图表并包含您想要查看的表格   。的关系。

要了解数据库中的所有表:

SELECT name Table FROM sys.objects WHERE type = 'U'

要了解数据库中的所有存储过程:

SELECT name Procedure FROM sys.objects WHERE type = 'P'

了解与表相关的架构,约束和依赖关系:

EXEC sp_help <tablename>

几年前我写的另一个程序,用于检查对一个或多个关键字的依赖性:

/****** Object:  StoredProcedure [dbo].[FindObjectsByKeywords]    Script Date: 8/25/2015 2:36:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Kunal Chitkara
-- Description: Find dependencies of the delimited keywords entered in the first input. 
--              Second input specifies the delimiter used, default being comma. 
--              This procedure is dependent upon the utility function
-- Test Run:    EXEC FindObjectsByKeywords @searchString = 'UserId,InstanceID', @delimiter = ','
-- =============================================
ALTER PROCEDURE [dbo].[FindObjectsByKeywords]
    @searchString NVARCHAR(MAX) = '',
    @delimiter NCHAR(1) = ','
AS
BEGIN

    BEGIN TRY

        IF (@searchString = '')
            RAISERROR ('Invalid input', 16, 2)

        DECLARE @iterator INT = 1,
                @searchCount INT = 0

        SELECT value SearchString, ROW_NUMBER() OVER (ORDER BY value) Sort_Order
        INTO #SearchStrings
        FROM [dbo].[stringlist_to_table] (@searchString, @delimiter)

        SELECT @searchCount = MAX(Sort_Order)
        FROM #SearchStrings

        DECLARE @Result TABLE
        (
            Dependency NVARCHAR(256),
            Type NVARCHAR(120),
            KeyWords NVARCHAR(MAX)
        )

        WHILE (@iterator <= @searchCount)
        BEGIN
            SELECT @searchString = SearchString
            FROM #SearchStrings
            WHERE Sort_Order = @iterator

            INSERT INTO @Result (Dependency, Type, KeyWords)
            SELECT DISTINCT so.name, sysObj.type_desc, @searchString
            FROM sysobjects so
            INNER JOIN syscomments sc ON sc.id = so.id
            INNER JOIN sys.objects sysObj ON sysObj.object_id = so.id
            WHERE CHARINDEX(@searchString, sc.text) > 0
            ORDER BY name

            SET @iterator = @iterator + 1;
        END

        IF EXISTS (SELECT * FROM @Result)
        BEGIN
            SELECT
                P.Dependency,
                P.Type,
                STUFF
                (
                    (
                        SELECT ',' + KeyWords
                        FROM @Result M
                        WHERE M.Dependency = P.Dependency AND M.Type = P.Type
                        ORDER BY KeyWords
                        FOR XML PATH('')
                    ), 1, 1, ''
                ) AS KeyWords
            FROM @Result P
            WHERE P.Dependency != 'FindObjectsByKeywords'
            ORDER BY KeyWords, Dependency

        END
        ELSE
            PRINT 'No dependencies of the input keywords found.'

        DROP TABLE #SearchStrings
    END TRY
    BEGIN CATCH
        IF (ERROR_STATE() = 2)
        RAISERROR ('Invalid input. Make sure you enter at least one keyword. More keywords can be entered in the first delimited parameter', 16, 1);
    END CATCH
END