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

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格式使用输出。

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.

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

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数据库的结果过滤器。

示例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

示例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在输出中排除系统数据库。

示例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值。

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的输出进行比较

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获取实时值。

示例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.

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

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。

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.

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

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

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

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重新运行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.

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

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.

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

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数据库。

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,因为它处于还原模式,如下所示。

示例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.

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

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

示例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数据库。

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数据库。

结论 (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 获取数据库字段信息

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

  1. sql备份恢复数据库_使用DBATools通过SQL恢复数据库操作验证备份

    sql备份恢复数据库 In this article, we will explore database backup validation by with SQL restore database ...

  2. 织梦自定义html文本,织梦自定义标签dede:sql根据自定义字段填的文章id获取相关文章...

    这篇文章主要为大家详细介绍了织梦自定义标签dede:sql根据自定义字段填的文章id获取相关文章,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴. 有的时候我们需要 ...

  3. sql server(MsSql)字段命名,表命名,视图命名,SQL语句书写参考

    @[TOC](sql server(MsSql)字段命名,表命名,视图命名,SQL语句书写参考) 对我个人来说,字段命名,表命名,视图命名,SQL语句书写都有一套自己的习惯,可以减少维护成本.减少不必 ...

  4. R语言sys方法:sys.info函数获取系统和用户信息、sys.localeConv函数获取当前区域中的数字和货币表示的详细信息、sys.setFileTime函数更改文件的时间

    R语言sys方法:sys.info函数获取系统和用户信息.sys.localeConv函数获取当前区域中的数字和货币表示的详细信息.sys.setFileTime函数更改文件的时间 目录

  5. .Net有关问题。在GridView中添加了一个HyperLinkField,用来显示显示详细信息,请教跳转后的详细信息页面该怎样写代码

    .Net问题...在GridView中添加了一个HyperLinkField,用来显示显示详细信息,请问跳转后的详细信息页面该怎样写代码 在一个页面的GridView中添加了下面的代码: <as ...

  6. sql server修改字段编码格式_关于MySQL如何修改character_set_client的编码问题

    问题引入: 我们经常会遇到一些向MySQL数据库中插入中文,但是select出来的时候,却发现是乱码的情况.如我们向表a出入这样一段记录:i insert into a values('你好hello ...

  7. mysql text字段导出_用命令创建MySQL数据库

    一.连接MYSQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1. 连接到本机上的MYSQL. 首先打开DOS窗口,然后进入目录mysqlbin,再键入命令mysql -u root ...

  8. 数据库 字段长度_全美都在用的大型肿瘤数据库,教你用别人的数据搞定一篇SCI...

    转载来源: 全美都在用的大型肿瘤数据库,教你用别人的数据搞定一篇SCI_辑思编译​editideas.cn 今天介绍一个经典的肿瘤数据库-seer,它是北美最具代表性的大型肿瘤登记注册数据库之一,收集 ...

  9. mysql和sql server能共存吗_让防火墙与SQL Server数据库共存

    欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入 安全与性能是数据库管理员的两块心头肉.而通过防火墙来保护数据库的安全无疑是一种不错的选择.但是有时会防火墙与SQL S ...

最新文章

  1. Chatter 导入
  2. L - Who is the Champion
  3. php设置http请求头信息和响应头信息
  4. C++ STL priority_queue的正确使用方法
  5. python快速排序函数_两种方法在Python中实现快速排序
  6. visio2019安装教程
  7. Java封装代码年龄性别姓名_Java生成随机姓名、性别和年龄的实现示例
  8. 【69】Sqrt(x)
  9. 一款永久免费的CRM软件客户关系管理系统
  10. [GW-CTF2019] babyvm
  11. 欧文分校计算机新sat多少分录取,新SAT多少分能进美国TOP100大学
  12. 使用Mono.Cecil对MSIL进行注入
  13. 23岁需要做到的事情
  14. 半监督学习matlab,基于自适应图的半监督学习方法与流程
  15. Python中break语句和continue语句的用法
  16. win10怎么显示文件后缀名
  17. 【ORACLE】错误代码大全
  18. 为什么90%的程序员学编程都会从C语言开始?
  19. SecureCRT8.3 软件及注册码
  20. 在线接口测试工具(神器)

热门文章

  1. react打包后图片丢失_宜信技术实践|指尖前端重构(React)技术调研分析
  2. JavaScript基础(3)-JS中的面向对象、定时器、BOM、位置信息
  3. 快速掌握用python写并行程序
  4. 《面向对象程序设计》作业(四)
  5. WPF ----在UserControl的xaml里绑定依赖属性
  6. Intellij Idea 15 下新建 Hibernate 项目以及如何添加配置
  7. 用原生javascript做的一个打地鼠的小游戏
  8. 【软件分析与挖掘】Vision of Software Clone Management: Past, Present, and Future (Keynote Paper)...
  9. Android国际化-补充
  10. MVC如何分离Controller与View在不同的项目?