使用 PowerShell 更新现有同步组中的同步架构Use PowerShell to update the sync schema in an existing sync group

适用于:是Azure SQL 数据库 APPLIES TO: yesAzure SQL Database

此 Azure PowerShell 示例更新现有“SQL 数据同步”同步组中的同步架构。This Azure PowerShell example updates the sync schema in an existing SQL Data Sync sync group. 同步多个表时,此脚本可帮助你有效地更新同步架构。When you're syncing multiple tables, this script helps you to update the sync schema efficiently. 此示例演示如何使用 UpdateSyncSchema 脚本,该脚本在 GitHub 上以 UpdateSyncSchema.ps1 的形式提供。This example demonstrates the use of the UpdateSyncSchema script, which is available on GitHub as UpdateSyncSchema.ps1.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

本教程需要 Az PowerShell 1.4.0 或更高版本。This tutorial requires Az PowerShell 1.4.0 or later. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 此外,还需要运行 Connect-AzAccount -EnvironmentName AzureChinaCloud 以创建与 Azure 的连接。You also need to run Connect-AzAccount -EnvironmentName AzureChinaCloud to create a connection with Azure.

有关 SQL 数据同步的概述,请参阅使用 Azure SQL 数据同步跨多个云和本地数据库同步数据For an overview of SQL Data Sync, see Sync data across multiple cloud and on-premises databases with Azure SQL Data Sync.

重要

目前,SQL 数据同步不支持 Azure SQL 托管实例。SQL Data Sync does not support Azure SQL Managed Instance at this time.

示例Examples

将所有表添加到同步架构Add all tables to the sync schema

以下示例可刷新数据库架构,并可将中心数据库中的所有有效表添加到同步架构。The following example refreshes the database schema and adds all valid tables in the hub database to the sync schema.

UpdateSyncSchema.ps1 -SubscriptionId <subscriptionId> -ResourceGroupName <resourceGroupName> -ServerName <serverName> -DatabaseName <databaseName> `
    -SyncGroupName <syncGroupName> -RefreshDatabaseSchema $true -AddAllTables $true

添加和删除表和列Add and remove tables and columns

以下示例将 [dbo].[Table1][dbo].[Table2].[Column1] 添加到同步架构并删除 [dbo].[Table3]The following example adds [dbo].[Table1] and [dbo].[Table2].[Column1] to the sync schema and removes [dbo].[Table3].

UpdateSyncSchema.ps1 -SubscriptionId <subscriptionId> -ResourceGroupName <resourceGroupName> -ServerName <serverName> -DatabaseName <databaseName> `
    -SyncGroupName <syncGroupName> -TablesAndColumnsToAdd "[dbo].[Table1],[dbo].[Table2].[Column1]" -TablesAndColumnsToRemove "[dbo].[Table3]"

脚本参数Script parameters

UpdateSyncSchema 脚本具有以下参数:The UpdateSyncSchema script has the following parameters:

参数Parameter 注释Notes
$subscriptionId$subscriptionId 在其中创建同步组的订阅。The subscription where the sync group is created.
$resourceGroupName$resourceGroupName 在其中创建同步组的资源组。The resource group where the sync group is created.
$serverName$serverName 中心数据库的服务器名称。The server name of the hub database.
$databaseName$databaseName 中心数据库名称。The hub database name.
$syncGroupName$syncGroupName 同步组名称。The sync group name.
$memberName$memberName 如果要从同步成员而不是中心数据库加载数据库架构,请指定成员名称。Specify the member name if you want to load the database schema from the sync member instead of from the hub database. 如果要从中心加载数据库架构,则将此参数保留为空。If you want to load the database schema from the hub, leave this parameter empty.
$timeoutInSeconds$timeoutInSeconds 该脚本刷新数据库架构时超时。Timeout when the script refreshes database schema. 默认为 900 秒。Default is 900 seconds.
$refreshDatabaseSchema$refreshDatabaseSchema 指定该脚本是否需要刷新数据库架构。Specify whether the script needs to refresh the database schema. 如果数据库架构已从以前的配置更改(例如,如果添加了新表或新列),则需要在重新配置之前刷新架构。If your database schema changed from the previous configuration (for example, if you added a new table or anew column), you need to refresh the schema before you reconfigure it. 默认值为 false。Default is false.
$addAllTables$addAllTables 如果此值为 true,所有有效的表和列都将添加到同步架构。If this value is true, all valid tables and columns are added to the sync schema. $TablesAndColumnsToAdd 和 $TablesAndColumnsToRemove 的值将被忽略。The values of $TablesAndColumnsToAdd and $TablesAndColumnsToRemove are ignored.
$tablesAndColumnsToAdd$tablesAndColumnsToAdd 指定要添加到同步架构的表或列。Specify tables or columns to be added to the sync schema. 每个表名或列名必须与架构名称完全分隔。Each table or column name needs to be fully delimited with the schema name. 例如:[dbo].[Table1][dbo].[Table2].[Column1]For example: [dbo].[Table1], [dbo].[Table2].[Column1]. 可以指定多个表名或列名,并将其用逗号 (,) 分隔。Multiple table or column names can be specified and separated by a comma (,).
$tablesAndColumnsToRemove$tablesAndColumnsToRemove 指定要从同步架构中删除的表或列。Specify tables or columns to be removed from the sync schema. 每个表名或列名必须与架构名称完全分隔。Each table or column name needs to be fully delimited with schema name. 例如:[dbo].[Table1][dbo].[Table2].[Column1]For example: [dbo].[Table1], [dbo].[Table2].[Column1]. 可以指定多个表名或列名,并将其用逗号 (,) 分隔。Multiple table or column names can be specified and separated by a comma (,).

脚本说明Script explanation

UpdateSyncSchema 脚本使用以下命令。The UpdateSyncSchema script uses the following commands. 表中的每条命令链接到特定于命令的文档。Each command in the table links to command-specific documentation.

命令Command 注释Notes
Get-AzSqlSyncGroupGet-AzSqlSyncGroup 返回有关同步组的信息。Returns information about a sync group.
Update-AzSqlSyncGroupUpdate-AzSqlSyncGroup 更新同步组。Updates a sync group.
Get-AzSqlSyncMemberGet-AzSqlSyncMember 返回有关同步成员的信息。Returns information about a sync member.
Get-AzSqlSyncSchemaGet-AzSqlSyncSchema 返回有关同步架构的信息。Returns information about a sync schema.
Update-AzSqlSyncSchemaUpdate-AzSqlSyncSchema 更新同步架构。Updates a sync schema.

后续步骤Next steps

有关 Azure PowerShell 的详细信息,请参阅 Azure PowerShell 文档For more information about Azure PowerShell, see Azure PowerShell documentation.

可以在 Azure SQL 数据库 PowerShell 脚本中找到更多 SQL 数据库 PowerShell 脚本示例。Additional SQL Database PowerShell script samples can be found in Azure SQL Database PowerShell scripts.

有关 SQL 数据同步的详细信息,请参阅:For more information about SQL Data Sync, see:

有关 SQL 数据库的详细信息,请参阅:For more information about SQL Database, see: