sql 获取数据库字段信息_使用DBATools获取SQL数据库详细信息

sql 获取数据库字段信息

In the series of articles on DBATools, (see TOC at the bottom) we are exploring useful DBATools command to perform administrative SQL tasks. In this article, we will explore useful commands to interact with SQL Server.

在有关DBATools的系列文章中(请参阅底部的TOC ),我们正在探索有用的DBATools命令来执行管理SQL任务。 在本文中,我们将探索与SQL Server交互的有用命令。

We explored installation and performing database backups, restoration, and validation with DBATools.

我们探索了如何使用DBATools安装和执行数据库备份,还原和验证。

Get-DbaDatabase (Get-DbaDatabase)

We use the Get-DbaDatabase command to get SQL database information for each database on the specified SQL instance. Usually, we use SSMS to check the databases information. It is useful to learn to fetch this information using PowerShell. DBATools provides a set of commands to do routine work easily.

我们使用Get-DbaDatabase命令来获取指定SQL实例上每个数据库SQL数据库信息。 通常,我们使用SSMS来检查数据库信息。 学习使用PowerShell来获取此信息非常有用。 DBATools提供了一组命令来轻松进行日常工作。

  • Note: I am using Azure Data Studio to execute DBATools PowerShell Commands.注意:我正在使用Azure Data Studio执行DBATools PowerShell命令。

Get-DbaDatabase的语法 (The Syntax for Get-DbaDatabase)

> Get-help Get-DbaDatabase

The Syntax for Get-DbaDatabase in DBATools

We need to use parameters to get the required data. Let’s explore Get-DbaDatabase commands with examples.

我们需要使用参数来获取所需的数据。 让我们用示例探索Get-DbaDatabase命令。

示例1:获取指定SQL实例中的所有数据库详细信息 (Example 1: Get all databases details in a specified SQL instance)

> Get-DbaDatabase -SqlInstance localhost\SQL2019CTP

It gives output for all databases in the specified SQL instance. If we have a large number of databases, it is best to use the output in a Grid format.

它提供指定SQL实例中所有数据库的输出。 如果我们有大量数据库,则最好以Grid格式使用输出。

Get all databases details in a specified SQL instance

We get useful information such as Database name, status, recovery model, LogReuseWaitStatus, database size in MB, compatibility model, collation, backup details.

我们获得有用的信息,例如数据库名称,状态,恢复模型,LogReuseWaitStatus,以MB为单位的数据库大小,兼容性模型,排序规则,备份详细信息。

> Get-DbaDatabase -SqlInstance localhost\SQL2019CTP | Out-GridView

It opens a new result window and displays results in an interactive format. You can drag and drop columns to display results in a particular order.

它会打开一个新的结果窗口,并以交互格式显示结果。 您可以拖放列以按特定顺序显示结果。

Get all databases details in a specified SQL instance in Grid format

We can use filters in grid format to get desired results. Click on Add Criteria and select the column to filter the results. For example, in the following image, you can see result filter for the ApexSQLMonitor database.

我们可以使用网格格式的过滤器来获得理想的结果。 单击添加条件,然后选择列以过滤结果。 例如,在下图中,您可以看到ApexSQLMonitor数据库的结果过滤器。

Get all databases details in a specified SQL instance and filter results

示例2:仅获取指定SQL实例中的系统数据库详细信息 (Example 2: Get only system databases details in a specified SQL instance)

We can use the parameter -ExcludeUser to exclude all user databases and give output for only system databases (Master, Model, MSDB and TempDB).

我们可以使用参数-ExcludeUser排除所有用户数据库,并仅提供系统数据库(Master,Model,MSDB和TempDB)的输出。

> Get-DbaDatabase -SqlInstance localhost\SQL2019CTP -ExcludeUser | Out-GridView

Get only system databases details in a specified SQL instance

示例3:仅获取指定SQL实例中的用户数据库详细信息 (Example 3: Get only user databases details in a specified SQL instance)

We might want to get details of only user databases. We can use the parameter –ExcludeUser to exclude system databases in the output.

我们可能只想获取用户数据库的详细信息。 我们可以使用参数– ExcludeUser在输出中排除系统数据库。

Get only user databases details in a specified SQL instance

示例4:在指定SQL实例中获取数据库详细信息以及最后使用的信息 (Example 4: Get databases details along with last used information in a specified SQL instance)

We use dynamic management view sys.dm_db_index_usage_stats to get overall access details about indexes in SQL Server.

我们使用动态管理视图sys.dm_db_index_usage_stats来获取有关SQL Server中索引的整体访问详细信息。

The useful columns in the output of this DMV are as follows:

此DMV输出中的有用列如下:

  • Last_user_scan – It is the timestamp of the last user scan for an index

    Last_user_scan –这是上次用户扫描索引的时间戳
  • Last_user_update- It is the timestamp of the last user update for an index

    Last_user_update-它是索引的最后一次用户更新的时间戳

Let’s execute this command in Azure Data Studio for the AdventureWorks2014 database.

让我们在Azure Data Studio中为AdventureWorks2014数据库执行此命令。

SELECT   OBJECT_NAME(S.[OBJECT_ID]) AS [OBJECT NAME], 
         I.[NAME] AS [INDEX NAME], 
         USER_SEEKS, 
         USER_SCANS, 
         USER_LOOKUPS, 
         USER_UPDATES 
FROM     SYS.DM_DB_INDEX_USAGE_STATS AS S 
         INNER JOIN SYS.INDEXES AS I 
           ON I.[OBJECT_ID] = S.[OBJECT_ID] 
              AND I.INDEX_ID = S.INDEX_ID 
WHERE    OBJECTPROPERTY(S.[OBJECT_ID],'IsUserTable') = 1

In the following screenshot, we can see last_user_scan and last_user_update values.

在以下屏幕截图中,我们可以看到last_user_scanlast_user_update值。

Output of DMV to get use details

Suppose we want to get database information using DBATools command Get-DbaDatabase along with the last read & write times for each database in the specified SQL instance. We can use the parameter -IncludeLastUsed to include this information.

假设我们要使用DBATools命令Get-DbaDatabase以及指定SQL实例中每个数据库的最后读写时间来获取数据库信息。 我们可以使用参数-IncludeLastUsed来包含此信息。

  • Note: We have multiple tables in an SQL database. All tables indexes might have different last scan and update dates. DBATools performs a check for all indexes in all databases and returns the latest timestamp of user access and update activity.
  • 注意:我们在SQL数据库中有多个表。 所有表索引的上次扫描和更新日期可能不同。 DBATools对所有数据库中的所有索引执行检查,并返回用户访问和更新活动的最新时间戳。
> Get-DbaDatabase -SqlInstance localhost\SQL2019CTP -IncludeLastUsed  | Out-GridView

It includes LastIndexRead and LastIndexWrite columns to give the required information for each database. You can compare the output from DBATools command Get-DbaDatabase and output of the DMV sys.dm_db_index_usage_stats.

它包括LastIndexRead和LastIndexWrite列,以提供每个数据库所需的信息。 您可以将DBATools命令Get-DbaDatabase的输出与DMV sys.dm_db_index_usage_stats的输出进行比较

IncludeLastUsed paramters in DBATools command
Let’s execute an update command on AdventureWorks204 database.

让我们在AdventureWorks204数据库上执行更新命令。

Update [AdventureWorks2017].[dbo].[DemoTable] set product=1111

Rerun the DBATools command, and you can see updated values because it gets the real-time values from the DMV.

重新运行DBATools命令,您会看到更新的值,因为它从DMV获取实时值。

Get output of DBATools with IncludeLastUsed

示例5:获取只读数据库详细信息指定SQL实例 (Example 5: Get Read-only database details a specified SQL instance)

We might have set a database to Read-only to disable any further updates. We can filter results for the read-only database using -Access ReadOnly parameter.

我们可能已将数据库设置为“只读”以禁用任何进一步的更新。 我们可以使用-Access ReadOnly参数过滤只读数据库的结果。

I do not have a read-only database in my instance. Due to this, the command did not return any output.

我的实例中没有只读数据库。 因此,该命令未返回任何输出。

Get Read-only database details a specified SQL instance

Connect to a database, right-click on it and open properties. In the options page, change the state in column Database Read-only as True.

连接到数据库,右键单击它并打开属性。 在选项页面中,将“ 数据库只读”列中的状态更改为True。

SSMS option to modify DB properties

We need to close all connections to the database to change in a database state. Click on Yes to go ahead with the change and close all other connections.

我们需要关闭与数据库的所有连接以更改数据库状态。 单击是继续进行更改并关闭所有其他连接。

Warning message of open connections

Once database state changes to Read-only, you can see a suffix Read-only in SSMS for that particular database.

数据库状态更改为“只读”后,您可以在SSMS中看到该特定数据库的后缀“只读”。

Database status as Read-only

Rerun the command of DBATools Get-DbaDatabase with parameter -Access ReadOnly. We changed the database status to read-only for the SQLDB database. We get the information about it using DBATools as well.

-Access ReadOnly parameter to get read-only database details

使用参数-Access ReadOnly重新运行DBATools Get-DbaDatabase的命令 我们将SQLDB数据库的数据库状态更改为只读。 我们也可以使用DBATools获得有关它的信息。

示例6:使用DBATools Get-DbaDdatabase命令获取状态为正常的数据库信息的命令 (Example 6: Command to get databases information having status Normal using DBATools Get-DbaDdatabase command)

We might have different states of databases in SQL Server. You can understand all database states (mentioned below) using this article, Understanding different SQL Server database states.

我们在SQL Server中可能具有不同的数据库状态。 您可以使用本文了解不同SQL Server数据库状态了解所有数据库状态(如下所述)。

  • Online

    线上
  • Restoring

    恢复中
  • Recovering

    正在恢复
  • Recovery pending

    恢复中
  • Suspect

    疑似
  • Emergency

    紧急情况
  • Offline

    离线

As of now, we have all databases in online status. For the demonstration purpose, let’s change a database status from Online to Restoring.

到目前为止,我们的所有数据库都处于联机状态。 出于演示目的,让我们将数据库状态从Online更改为Restoreing

To change the status of the SQLDB database, I take a tail-log backup, and it changes the state to Restoring.

要更改SQLDB数据库的状态,我进行了尾日志备份,它将状态更改为“正在还原”。

To take a tail-log backup, right click on a database and go to tasks, backup. In the backup wizard, go to Media and select the option – backup the tail of the log and leave the database in the restoring state.

要进行尾日志备份,请右键单击数据库,然后转到任务备份。 在备份向导中,转到“媒体”,然后选择选项– 备份日志的尾部,并使数据库保持还原状态。

I have already taken a tail-log backup. Therefore, it shows it greyed out in the following screenshot.

我已经进行了尾日志备份。 因此,它在下面的屏幕快照中显示为灰色。

Tail-log backup

Alternatively, you can execute the following backup log command.

或者,您可以执行以下备份日志命令。

 BACKUP LOG [SQLDB] TO  DISK = N'C:\TEMP\SQLDB.bak' WITH  NO_TRUNCATE , NOFORMAT, NOINIT,  NAME = N'SQLDB-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  NORECOVERY ,  STATS = 10
GO

Once the tail-log backup completes, you can see the database in the restoring mode.

尾日志备份完成后,您可以在还原模式下看到数据库。

Database status

Execute the following DBATools command to include only databases having Normal status.

执行以下DBATools命令以仅包括状态为“ Normal”的数据库。

> Get-DbaDatabase -SqlInstance localhost\SQL2019CTP -Status Normal | Out-GridView

In the following output, we do not have SQLDB database that is in Restoring database state.

在以下输出中,我们没有处于还原数据库状态SQLDB数据库。

Command to get databases information having status Normal using DBATools Get-DbaDdatabase command

If we want to get detailed about database having database state Restoring, we can execute the following command.

如果要详细了解具有数据库状态还原的数据库,可以执行以下命令。

> Get-DbaDatabase -SqlInstance localhost\SQL2019CTP -Status Restoring | Out-GridView

Now, we only get one database SQLDB in the output because it is in Restoring mode as shown below.

现在,我们在输出中仅获得一个数据库SQLDB,因为它处于还原模式,如下所示。

Command to get databases information having status Restoring using DBATools Get-DbaDdatabase command

示例7:使用DBATools Get-DbaDdatabase命令获取具有特定恢复模型的数据库 (Example 7: Get a database having a specific recovery model using DBATools Get-DbaDdatabase command)

We can specify different recovery models for separate databases in a SQL instance depending upon the recovery objectives, backup configuration and criticality of a database. The available recovery models in SQL Server as follows:

我们可以根据恢复目标,备份配置和数据库的关键性为SQL实例中的单独数据库指定不同的恢复模型。 SQL Server中可用的恢复模型如下:

  • Full

    充分
  • Bulk-logged

    批量记录
  • Simple

    简单

Suppose we want to get details of databases having a Full recovery model. We can use -RecoveryModel parameter to specify a recovery model. DBATools command checks for the specified recovery model and returns only those databases meeting the criteria.

假设我们要获取具有完全恢复模型的数据库的详细信息。 我们可以使用-RecoveryModel参数来指定恢复模型。 DBATools命令检查指定的恢复模型,并仅返回那些符合条件的数据库。

> Get-DbaDatabase -SqlInstance localhost\SQL2019CTP -RecoveryModel FULL | Out-GridView

In the following screenshot, we can see the database having a FULL recovery model.

在以下屏幕截图中,我们可以看到数据库具有完整恢复模型。

Get a database having a specific recovery model using  DBATools Get-DbaDdatabase command

We can specify multiple values as well with -RecoveryModel parameter. For example, if we want to get details of databases having FULL and Simple recovery model, execute the following command.

我们也可以使用-RecoveryModel参数指定多个值。 例如,如果我们要获取具有完整和简单恢复模型的数据库的详细信息,请执行以下命令。

> Get-DbaDatabase -SqlInstance localhost\SQL2019CTP -RecoveryModel FULL,Simple  | Out-GridView

Get a database having a specific recovery model

示例8:在DBATools Get-DbaDatabase命令中排除特定的数据库 (Example 8: Exclude specific database in DBATools Get-DbaDatabase command)

In earlier examples 2 and 3, we excluded system and user databases in the Get-DbaDatabase command. We might want to exclude a specific database check. For example, I do not want to check the details of the TempDB database and want to exclude this in the output. We can use the -ExcludeDatabase parameter to specify an excluded database.

在较早的示例2和3中,我们在Get-DbaDatabase命令中排除了系统数据库和用户数据库。 我们可能要排除特定的数据库检查。 例如,我不想检查TempDB数据库的详细信息,并希望在输出中排除它。 我们可以使用-ExcludeDatabase参数来指定排除的数据库。

> Get-DbaDatabase -SqlInstance localhost\SQL2019CTP -Excludedatabase TempDB  | Out-GridView

In the output, we can verify that the tempdb database is not present in the output.

在输出中,我们可以验证输出中不存在tempdb数据库。

Exclude specific database in the output

We can exclude multiple databases at the same time using the parameter –Excludedatabase. Specify database names separated by a comma.

我们可以使用参数– Excludedatabase同时排除多个数据库。 指定数据库名称,以逗号分隔。

In the following command, we want to exclude three databases TempDB, SQLDB and SQLTemp1.

在以下命令中,我们要排除三个数据库TempDB,SQLDB和SQLTemp1。

> Get-DbaDatabase -SqlInstance localhost\SQL2019CTP -Excludedatabase TempDB,SQLDB,SQLTemp1  | Out-GridView

In the output, you can see that we do not have TempDB, SQLDB and SQLTemp1 databases in the output of Get-DbaDatabase command.

在输出中,您可以看到Get-DbaDatabase命令的输出中没有TempDB,SQLDB和SQLTemp1数据库。

Exclude specific database in DBATools

结论 (Conclusion)

In this article, we learned to fetch SQL Database information using DBATools PowerShell tool. I will cover more DBATools commands in this series of articles. If you had comments or questions, feel free to leave them in the comments below.

在本文中,我们学习了使用DBATools PowerShell工具获取SQL数据库信息。 在本系列文章中,我将介绍更多DBATools命令。 如果您有任何意见或问题,请随时将其留在下面的评论中。

目录 (Table of contents)

DBATools PowerShell Module for SQL Server
PowerShell SQL Server Validation Utility – DBAChecks
SQL Database Backups using PowerShell Module – DBATools
IDENTITY columns threshold using PowerShell SQL Server DBATools
DBATools PowerShell SQL Server Database Backups commands
SQL Restore Database using DBATools
Validate backups with SQL restore database operations using DBATools
Fix Orphan users in SQL Server using DBATools PowerShell
Creating a SQL Server Database using DBATools
Get SQL Database details using DBATools
适用于SQL Server的DBATools PowerShell模块
PowerShell SQL Server验证实用程序– DBAChecks
使用PowerShell模块SQL数据库备份– DBATools
使用PowerShell SQL Server DBATools的IDENTITY列阈值
DBATools PowerShell SQL Server数据库备份命令
使用DBAToolsSQL Restore Database
使用DBATools通过SQL恢复数据库操作验证备份
使用DBATools PowerShell修复SQL Server中的孤立用户
使用DBATools创建SQL Server数据库
使用DBATools获取SQL数据库详细信息

翻译自: https://www.sqlshack.com/get-sql-database-details-using-dbatools/

sql 获取数据库字段信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值