使用DBATools创建SQL Server数据库

DBATools is an open source PowerShell module with useful commands to do the administrative task in SQL Server. In my earlier articles on DBATools (see TOC at the bottom) we explored installation and performing database backups, restoration, and validation with DBATools.

DBATools是一个开源PowerShell模块,其中包含用于执行SQL Server中管理任务的有用命令。 在我先前关于DBATools的文章(请参见底部的TOC )中,我们探讨了安装以及使用DBATools执行数据库备份,还原和验证。

In this article, let’s explore creating a SQL Server database using DBATools.

在本文中,让我们探索使用DBATools创建SQL Server数据库。

DBATools中的New-DbaDatabase命令 (New-DbaDatabase command in DBATools)

Usually, we create a SQL Server database using a graphical user interface in SSMS or Transact-SQL. We can create a database using open source PowerShell module DBATools command New-DbaDatabase.

通常,我们使用SSMS或Transact-SQL中的图形用户界面创建SQL Server数据库。 我们可以使用开源PowerShell模块DBATools命令New-DbaDatabase创建数据库。

We can search for a list of available functions in DBATools with the following command.

我们可以使用以下命令在DBATools中搜索可用功能的列表。

> Get-Help *Database*

Creating SQL Server Database using DBATools

We can create a SQL Server database using New-DbaDatabase command in DBATools.

我们可以使用DBATools中的New-DbaDatabase命令创建一个SQL Server数据库。

New-DbaDatabase命令的语法 (The syntax for New-DbaDatabase command)

We can get useful stuff for a command using Get-help *function* in DBATools. In the following image, you can see description and syntax for New-DbaDatabase command.

使用DBATools中的Get-help * function *,我们可以获得命令的有用信息。 在下图中,您可以看到New-DbaDatabase命令的描述和语法。

>Get-help New-DbaDatabase

DBATools command help to get syntax

Let’s have a quick recap of SQL database architecture. We have following SQL database files in SQL Server.

让我们快速回顾一下SQL数据库体系结构。 我们在SQL Server中具有以下SQL数据库文件。

  • Primary data file (*.mdf)

    主数据文件(* .mdf)
  • Secondary data file (*.ndf)

    辅助数据文件(* .ndf)
  • Log file (*.ldf)

    日志文件(* .ldf)

Each SQL database is having a Primary file group that contains the primary data file. We can create secondary file groups and have multiple secondary data files in it.

每个SQL数据库都有一个包含主要数据文件的主要文件组。 我们可以创建辅助文件组,并在其中包含多个辅助数据文件。

database architecture

By default, SQL Server creates a copy of a model database if we do not specify any parameter for creating a database. Let’s create a SQL database using DBAtools.

默认情况下,如果我们未指定任何用于创建数据库的参数,则SQL Server将创建模型数据库的副本。 让我们使用DBAtools创建一个SQL数据库。

示例1:创建SQL Server数据库而不指定数据库名称 (Example 1: Create a SQL Server Database without specifying the database name)

In the following DBATools command New-DbaDatabase, we specified the SQL instance name in which we want to create a new SQL database.

在以下DBATools命令New-DbaDatabase中,我们指定了要在其中创建新SQL数据库SQL实例名称。

> New-DbaDatabase -SqlInstance kashish\SQL2019CTP

If we do not specify any database name, DBATools creates a new database with a random name. As we just created this database and did not have any backup, it shows last full, differential and log backup as default value.

如果我们未指定任何数据库名称,则DBATools将创建一个具有随机名称的新数据库。 当我们刚创建此数据库并且没有任何备份时,它会将上次的完整备份,差异备份和日志备份显示为默认值。

SQL Server Database using DBATools command

As stated earlier, if we do not specify any parameter to create a SQL database, it creates a copy of a model database. Let’s compare the properties of the SQL database and the newly created database (random-808569434).

如前所述,如果我们不指定任何参数来创建SQL数据库,它将创建模型数据库的副本。 让我们比较一下SQL数据库和新创建的数据库(random-808569434)的属性。

Database comparison

  1. Both Model and random-808569434 database have similar size, space available and the number of users

    Model数据库和random-808569434数据库的大小,可用空间和用户数量都相似
  2. Collation of both Model and random-808569434 database is Latin1_General_CI_AS

    Model和random-808569434数据库的排序规则均为Latin1_General_CI_AS
  3. Database owner of Model database and random-808569434 is different. We executed DBATools command under default authentication (Windows authentication), and windows user (In this case, Kashish\Test) becomes a database owner

    Model数据库的数据库所有者与random-808569434不同。 我们在默认身份验证(Windows身份验证)下执行DBATools命令,并且Windows用户(在这种情况下为Kashish \ Test)成为数据库所有者

SQL Server requires an exclusive lock on the model database to create a new SQL database. I executed this command while a model database properties window is open. SQL Server could not get exclusive lock and Crate database statement failed with the following error

SQL Server需要在模型数据库上具有排他锁才能创建新SQL数据库。 打开模型数据库属性窗口时,我执行了此命令。 SQL Server无法获得排他锁,并且Crate数据库语句失败,并出现以下错误

Error while creating SQL Server Database

示例2:使用指定的数据库名称创建SQL Server数据库 (Example 2: Create SQL Server Database with specifying a database name)

In the previous example, we did not specify any database name in DBATools command. Let’s specify a database name and execute the command.

在前面的示例中,我们没有在DBATools命令中指定任何数据库名称。 让我们指定一个数据库名称并执行命令。

> New-DbaDatabase -Name SQLTemp1 -SqlInstance .\SQL2019CTP

SQL Server creates a new SQL database with the specified name.

SQL Server使用指定的名称创建一个新SQL数据库。

Specify Database name using DBATools

We can create multiple databases as well by specifying database names separated by a comma.

我们还可以通过指定用逗号分隔的数据库名称来创建多个数据库。

> New-DbaDatabase -Name SQLTemp1, SQLTemp2  -SqlInstance kashish\SQL2019CTP

Database SQLTemp1 already exists in SQL instance kashish\SQL2019CTP therefore we get a warning message for SQLTemp1 database. It creates SQLTemp2 database successfully.

数据库SQLTemp1已存在于SQL实例kashish \ SQL2019CTP中,因此我们收到有关SQLTemp1数据库的警告消息。 它成功创建SQLTemp2数据库。

Specify database name

Let’s drop databases SQLTemp1 and SQLTemp2 and rerun the DBATools command.

让我们删除数据库SQLTemp1SQLTemp2,然后重新运行DBATools命令。

SQL Create Database

We can get a result of a command in a grid view to have a better visual experience. We need to specify by | Out-GridView parameter with DBATools command.

我们可以在网格视图中获得命令的结果,以获得更好的视觉体验。 我们需要指定| 使用DBATools命令的Out-GridView参数。

> New-DbaDatabase -Name SQLTemp1,SQLTemp2  -SqlInstance kashish\SQL2019CTP | Out-GridView

Execute the command, and it opens a new window for output as per the following image.

执行命令,然后根据下图打开一个新窗口以输出。

In the following image, we see the output in a grid format.

在下图中,我们以网格格式看到输出。

output in gird format

示例3:使用辅助文件创建一个SQL Server数据库 (Example 3: Create a SQL Server database with Secondary files )

Suppose we want to create a SQL database with the following requirements.

假设我们要创建一个具有以下要求SQL数据库。

  • Data growth of primary data file (*.mdf) should be 40 MB

    主数据文件(* .mdf)的数据增长应为40 MB
  • We should have a secondary filegroup with a secondary data file having an original size of 30 MB and auto growth 30 MB

    我们应该有一个辅助文件组,其辅助数据文件的原始大小为30 MB,自动增长为30 MB
  • Log file growth should be 30 MB

    日志文件增长应为30 MB

Execute following DBATools PowerShell command. In this command, we specified the following parameters.

执行以下DBATools PowerShell命令。 在此命令中,我们指定了以下参数。

  1. -PrimaryFileGrowth parameter -PrimaryFileGrowth参数自动增长主数据文件
  2. -SecondaryFileGrowth parameter -SecondaryFileGrowth参数辅助数据文件自动增长
  3. -SecondaryFileSize parameter -SecondaryFileSize参数的辅助数据文件的初始大小
  4. -Loggrowth parameter -Loggrowth参数自动增长日志文件
> New-DbaDatabase -Name SQLTemp1  -SqlInstance kashish\SQL2019CTP -PrimaryFileGrowth 40 -SecondaryFileSize 30 -SecondaryFileGrowth 10 -Loggrowth 30

filegrowth while creating SQL Server Database

Let’s open database properties in SSMS and verify the conditions we specified.

让我们在SSMS中打开数据库属性,并验证我们指定的条件。

Auto growth validation

You can notice that we did not specify the secondary file group name as well as the secondary data file name in the command. DBATools automatically assigns the filegroup and data file name for us. It derives the name from the database name. For example, in this example, it creates secondary filegroup SQLTemp1_MainData because database name is SQLTemp1.

您可能会注意到,我们在命令中没有指定辅助文件组名以及辅助数据文件名。 DBATools自动为我们分配文件组和数据文件名。 它从数据库名称派生名称。 例如,在此示例中,由于数据库名称为SQLTemp1,因此它将创建辅助文件组SQLTemp1_MainData。

verify secondary file group

By default, it creates one secondary data file if we specify any parameter related to secondary file such as SecondaryFileGrowth.

默认情况下,如果我们指定与辅助文件相关的任何参数(例如SecondaryFileGrowth) ,它将创建一个辅助数据文件

We might want to create multiple secondary data files as well. We can specify the number of secondary files using -SecondaryFileCount parameter. In the following example, we want to create three secondary data files.

我们可能还想创建多个辅助数据文件。 我们可以使用-SecondaryFileCount参数指定辅助文件的数量。 在下面的示例中,我们要创建三个辅助数据文件。

> New-DbaDatabase -Name SQLTemp1  -SqlInstance kashish\SQL2019CTP -PrimaryFileGrowth 40 -SecondaryFileSize 30 -SecondaryFileGrowth 10 -Loggrowth 30 -SecondaryFileCount 3

specify Number of secondary data file

Now go to database properties in SSMS. We can see four secondary database files in this database. In the query, we specified only three secondary files. It creates one additional secondary data file due to specified secondary data file parameters.

现在转到SSMS中的数据库属性。 我们可以在该数据库中看到四个辅助数据库文件。 在查询中,我们仅指定了三个辅助文件。 由于指定了辅助数据文件参数,它会创建一个附加的辅助数据文件。

Verify secondary data files

示例4:使用DBATools在SQL Server数据库中将默认文件组指定为“辅助” (Example 4: Specify default file group as Secondary in SQL Server Database using DBATools)

By default, SQL Server configures primary filegroup as default filegroup. Usually, we specify default filegroup other than primary filegroup. We can do it using DBATools parameter -DefaultFileGroup.

默认情况下,SQL Server将主文件组配置为默认文件组。 通常,我们指定除主文件组之外的默认文件组。 我们可以使用DBATools参数-DefaultFileGroup来实现。

In the following screenshot, you can see a database created using DBATools without specifying the parameter -DefaultFileGroup.

在以下屏幕截图中,您可以看到使用DBATools创建的数据库,而没有指定参数-DefaultFileGroup。

Verify Default file group in SSMS

Let’s create another database by specifying the parameter -DefaultFileGroup to configure secondary filegroup as default.

让我们通过指定参数-DefaultFileGroup来创建另一个数据库,以将辅助文件组配置为默认文件。

>New-DbaDatabase -Name SQLTemp11  -SqlInstance kashish\SQL2019CTP -PrimaryFileGrowth 40 -SecondaryFileCount 3  -SecondaryFileSize 30 -SecondaryFileGrowth 10 -Loggrowth 30 -DefaultFileGroup Secondary

Verify Default file group in SSMS

示例5:使用指定的排序规则创建一个SQL Server数据库 (Example 5: Create a SQL Server database with the specified collation)

By default, DBATools command creates SQL database with a default collation. If we want to specify a specific collation, we can do it using -Collation parameter.

默认情况下,DBATools命令使用默认排序规则创建SQL数据库。 如果要指定特定的排序规则,则可以使用-Collat​​ion参数。

In this example, we want to create SQL Server Database with a different collation Latin1_General_CS_AI. Execute the following command, and in the output, it shows the database collation same as we specified.

在此示例中,我们要创建具有不同排序规则Latin1_General_CS_AI的 SQL Server数据库 执行以下命令,然后在输出中显示与我们指定的数据库排序规则相同的排序规则

>New-DbaDatabase -Name SQLTemp11  -SqlInstance kashish\SQL2019CTP -PrimaryFileGrowth 40 -SecondaryFileCount 3  -SecondaryFileSize 30 -SecondaryFileGrowth 10 -Loggrowth 30 -DefaultFileGroup Secondary -Collation Latin1_General_CS_AI

Specify a collation in SQL Create Database

Let’s verify it from database properties as well. You can see database collation in database properties under the general page.

让我们也从数据库属性中进行验证。 您可以在常规页面下的数据库属性中看到数据库排序规则。

verify database collation

示例6:使用特定的数据库所有者创建一个SQL Server数据库 (Example 6: Create a SQL Server database with a specific database owner)

Once we create a database, SQL Server sets database owner as a connected user security context. For example, in my case, we connected to SQL Server using windows authentication (ID – Kashish\Test). If we do any specify any database owner, SQL Server sets Windows user Kashish\Test as owner.

创建数据库后,SQL Server会将数据库所有者设置为连接的用户安全上下文。 例如,以我为例,我们使用Windows身份验证(ID – Kashish \ Test)连接到SQL Server。 如果我们指定任何数据库所有者,SQL Server会将Windows用户Kashish \ Test设置为所有者。

Suppose we want to create all SQL Server database having owner SA. In DBATools, we can specify database owner using -owner parameter.

假设我们要创建所有具有所有者SASQL Server数据库。 在DBATools中,我们可以使用-owner参数指定数据库所有者。

Execute the following command to create a database with owner SA.

执行以下命令以创建拥有者SA的数据库

>New-DbaDatabase -Name SQLTemp11  -SqlInstance kashish\SQL2019CTP -PrimaryFileGrowth 40 -SecondaryFileCount 3  -SecondaryFileSize 30 -SecondaryFileGrowth 10 -Loggrowth 30 -DefaultFileGroup Secondary -Collation Latin1_General_CS_AI -owner sa

In the output, it still showed the database owner Kashish\Test that is my windows user. It should set an owner as SA.

在输出中,它仍然显示数据库所有者Kashish \ Test是我的Windows用户。 它应该将所有者设置为SA。

SQL Create database with a specific database owner

If we verify it in the database properties, it shows the correct database owner as specified in DBATools command.

如果我们在数据库属性中进行验证,它将显示DBATools命令中指定的正确数据库所有者。

Verify database owner

示例7:创建具有最大主文件和辅助文件大小SQL Server数据库 (Example 7: Create a SQL Server database with a maximum primary and secondary file size)

We might require setting maximum size for a primary and secondary data file in SQL Server to load a limited amount of data. It might be useful to control excessive growth of a database to avoid any disk related issues.

我们可能需要为SQL Server中的主数据文件和辅助数据文件设置最大大小,以加载有限数量的数据。 控制数据库的过度增长以避免任何与磁盘相关的问题可能很有用。

Note: You should not set maximum file size in production until we exactly know the requirements.

注意:在我们完全了解要求之前,您不应在生产环境中设置最大文件大小。

We can use –PrimaryFileMaxSize and –SecondaryFileMaxSize parameters to set the maximum size of primary and secondary files respectively.

我们可以使用– PrimaryFileMaxSize和– SecondaryFileMaxSize参数分别设置主文件和辅助文件的最大大小。

In the following example, we set the maximum size for both primary and secondary files to 100 MB.

在以下示例中,我们将主文件和辅助文件的最大大小设置为100 MB。

>New-DbaDatabase -Name SQLTemp11  -SqlInstance kashish\SQL2019CTP -PrimaryFileGrowth 40 -SecondaryFileCount 3  -SecondaryFileSize 30 -SecondaryFileGrowth 10 -Loggrowth 30 -PrimaryFileMaxSize 100 -SecondaryFileMaxSize 100

Specify  maximum primary and secondary file size in SQL Server Database

In the following screenshot, we can see that auto growth is set to 100 MB for all data files.

在以下屏幕截图中,我们可以看到所有数据文件的自动增长均设置为100 MB。

Verfiy maximum file size in SQL Create Database

示例8:创建带有确认消息SQL Server数据库 (Example 8: Create a SQL Server database with a confirmation message)

It is an excellent practice to get a confirmation prompt before we do any activity in the database. In DBATools, we can use -Confirm parameter to display a confirmation prompt. It prevents us from executing any command accidentally.

在数据库中进行任何活动之前,获得确认提示是一种极好的实践。 在DBATools中,我们可以使用-Confirm参数来显示确认提示。 它可以防止我们意外执行任何命令。

In the following example, we can see that it gives us a message – Are you sure, you want to perform this actions?

在以下示例中,我们可以看到它向我们提供了一条消息– 您确定要执行此操作吗?

We need to provide input Y or A to execute the DBATools command.

我们需要提供输入Y或A来执行DBATools命令。

>New-DbaDatabase -Name SQLTemp11  -SqlInstance kashish\SQL2019CTP -PrimaryFileGrowth 40 -SecondaryFileCount 3  -SecondaryFileSize 30 -SecondaryFileGrowth 10 -Loggrowth 30 -PrimaryFileMaxSize 100 -SecondaryFileMaxSize 100 -confirm

Confirmation message

结论 (Conclusion)

In this article, we explored creating SQL Server Database using PowerShell module DBATools. We will continue exploring useful command in DBATools in my future articles. If you had comments or questions, feel free to leave them in the comments below.

在本文中,我们探讨了使用PowerShell模块DBATools创建SQL Server数据库。 在以后的文章中,我们将继续探索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-DbaHelpIndex command in 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中的Get-DbaHelpIndex命令

翻译自: https://www.sqlshack.com/creating-a-sql-server-database-using-dbatools/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
软件名称:DBATools For PL/SQL Developer 发布版本:1.1.0 Beta 1 发布日期:2010-01-28 软件简介: PL/SQL Developer是Oracle数据库当前最流行的开发工具之一,它在ORACLE数据库开发设计方面功能强大,使用方便,但是数据库管理方面一直比较欠缺。 DBATools For PL/SQL Developer 是一款PL/SQL Developer的辅助插件,主要功能是提供许多DBA数据库管理的功能,使ORACLE工程师不仅可以使用PL/SQL Developer进行开发设计,同时也可以进行数据库管理及监控,大大提高工作效率。 软件安装说明: 本插件为绿色软件,将DBATools.dll和DBAToolsConfig.xml文件复制到PL/SQL Developer安装目录的PlugIns子目录即可。例如:PL/SQL Developer安装在C:\Program Files\PLSQL Developer目录,那将DBATools.dll和DBAToolsConfig.xml文件复制到C:\Program Files\PLSQL Developer\PlugIns目录下,然后重启PL/SQL Developer就可以使用。 注: 1.本软件只支持PL/SQL Developer7.0及以上的版本。 --------------------------------------------------------------- [2010-01-28]发布 DBATools For PL/SQL Developer 1.1.0 Beta 1版本更新说明 1.+ 对像快捷菜单增加了Oracle加密对像解密功能[仅支持Oracle10g] 2.# 优化了部份数据库监视SQL 3.* 修正了V1.0.0正式版中列表中数字列按字符排序的BUG --------------------------------------------------------------- [2009-01-22]发布 DBATools For PL/SQL Developer 1.0.0 正式版本更新说明 --------------------------------------------------------------- 功能列表: 1.表空间管理 2.初始化参数管理 3.重做日志管理 4.数据库监视 4.1.查看SGA统计信息 4.2.查看排序情况 4.3.查看日志切换情况 4.4.查看锁资源 4.11.查看库缓存命中率 4.6.查看数据缓存命中率 4.7.查看WorkArea情况 4.8.查看当前会话等待事件 4.9.查看数据库大小 4.10.查看等待事件统计信息 5.清空缓冲区 6.导出数据库表结构文档 7.快速打开TNSNAME.ORA文件 8.快速打开listener.ORA文件 9.快速打开Oracle Net Manager 10.自定义配置 10.1.语言配置,支持中英两种语言 10.2.菜单自定义配置 10.3.数据库监视SQL自定义配置 11.快捷菜单 11.1.表 11.1.1.分析表 11.1.2.取记录数 11.1.3.创建物化视图日志 11.1.4.设置并行度 11.2.视图 11.2.1.取记录数 11.3.物化视图 11.3.1.分析 11.3.2.取记录数 11.3.3.刷新 11.3.4.设置并行度 11.4.数据库链接 11.4.1.测试 11.11.列 11.11.1.分析 11.6.索引 11.6.1.分析 11.6.2.重建 11.7.表空间 11.7.1.管理 11.7.2.新增 11.7.3.编辑

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值