你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

创建和更改 SQL 外部表

在执行命令的数据库中创建或更改 SQL 外部表

注意

  • 如果该表存在,则 .create 命令会失败并显示错误。 使用 .create-or-alter.alter 修改现有表。
  • 不支持更改外部 SQL 表的架构。

支持的 SQL 外部表类型

  1. Microsoft SQL Server
  2. MySQL
  3. PostgreSQL
  4. Cosmos DB

权限

.create 至少需要数据库用户权限,.alter 至少需要表管理员权限。

.create-or-alter 使用托管标识身份验证的外部表需要 AllDatabasesAdmin 权限。 目前,这仅适用于 Microsoft SQL Server 外部表。

语法

(.create | .alter | .create-or-alter) externaltableTableName(Schema)kind=sql [ table=SqlTableName ] (SqlConnectionString) [with( [ sqlDialect=SqlDialect ] , [ Property, ... ])]

详细了解语法约定

参数

名称 类型 必需 说明
TableName string ✔️ 外部表的名称。 该名称必须遵循实体名称规则,并且外部表不能与同一数据库中的常规表同名。
架构 string ✔️ 外部数据架构是包含一个或多个列名称和数据类型的逗号分隔列表,其中的每个项遵循以下格式:ColumnName:ColumnType。
SqlTableName string 不包括数据库名称的 SQL 表的名称。 例如,"MySqlTable" 而不是 "db1.MySqlTable"。 如果表的名称包含句点 ("."),请使用 ['Name.of.the.table'] 表示法。

所有类型的表都需要遵循此规范,但 Cosmos DB 除外,因为对于 Cosmos DB,集合名称是连接字符串的一部分。
SqlConnectionString string ✔️ SQL Server 的连接字符串。
SqlDialect string 指示 SQL 外部表的类型。 默认使用 Microsoft SQL Server。 对于 MySQL,请指定 MySQL。 对于 PostgreSQL,请指定 PostgreSQL。 对于 Cosmos DB,请指定 CosmosDbSql
属性 string 采用 PropertyName=PropertyValue 格式的键值属性对。 请参阅可选属性

警告

应该对包含机密信息的连接字符串和查询进行模糊处理,使其在任何 Kusto 跟踪中都被忽略。 有关详细信息,请参阅经过模糊处理的字符串文本

可选属性

属性 类型 说明
folder string 表的文件夹。
docString string 一个用来记录表的字符串。
firetriggers true/false 如果为 true,则指示目标系统激发 SQL 表上定义的 INSERT 触发器。 默认为 false。 (有关详细信息,请参阅 BULK INSERTSystem.Data.SqlClient.SqlBulkCopy
createifnotexists true/ false 如果为 true,则会在目标 SQL 表不存在的情况下创建该表;在这种情况下,必须提供 primarykey 属性来指示作为主键的结果列。 默认为 false
primarykey string 如果 createifnotexiststrue,则生成的列名称将用作 SQL 表的主键(如果是此命令创建的)。

身份验证和授权

若要从 Azure 数据资源管理器与外部 SQL 表交互,必须将身份验证方法指定为 SqlConnectionString 的一部分。 SqlConnectionString 定义要访问的资源及其身份验证信息。

有关详细信息,请参阅 SQL 外部表身份验证方法

注意

如果外部表用于连续导出,则必须通过用户名/密码或托管标识进行身份验证。

示例

以下示例演示如何创建每种类型的 SQL 外部表。

SQL Server

.create external table MySqlExternalTable (x:long, s:string) 
kind=sql
table=MySqlTable
( 
   h@'Server=tcp:myserver.database.windows.net,1433;Authentication=Active Directory Integrated;Initial Catalog=mydatabase;'
)
with 
(
   docstring = "Docs",
   folder = "ExternalTables", 
   createifnotexists = true,
   primarykey = x,
   firetriggers=true
)  

输出

TableName TableType 文件夹 DocString 属性
MySqlExternalTable Sql ExternalTables Docs {
"TargetEntityKind": "sqltable`",
"TargetEntityName":"MySqlTable",
"TargetEntityConnectionString": "Server=tcp:myserver.database.windows.net,1433;Authentication=Active Directory Integrated;Initial Catalog=mydatabase;",
"FireTriggers": true,
"CreateIfNotExists": true,
"PrimaryKey": "x"
}

MySQL

.create external table MySqlExternalTable (x:long, s:string) 
kind=sql
table=MySqlTable
( 
   h@'Server=myserver.mysql.database.windows.net;Port = 3306;UID = USERNAME;Pwd = PASSWORD;Database = mydatabase;'
)
with 
(
   sqlDialect = "MySql",
   docstring = "Docs",
   folder = "ExternalTables", 
)  

PostgreSQL

.create external table PostgreSqlExternalTable (x:long, s:string) 
kind=sql
table=PostgreSqlTable
( 
   h@'Host = hostname.postgres.database.azure.com; Port = 5432; Database= db; User Id=user; Password=pass; Timeout = 30;'
)
with 
(
   sqlDialect = "PostgreSQL",
   docstring = "Docs",
   folder = "ExternalTables", 
)  

Cosmos DB

.create external table CosmosDBSQLExternalTable (x:long, s:string) 
kind=sql
( 
   h@'AccountEndpoint=https://cosmosdbacc.documents.azure.com/;Database=MyDatabase;Collection=MyCollection;AccountKey=' h'R8PM...;'
)
with 
(
   sqlDialect = "CosmosDbSQL",
   docstring = "Docs",
   folder = "ExternalTables", 
)