你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用于:✅Microsoft Fabric✅Azure 数据资源管理器
在执行命令的数据库中创建或更改 Azure SQL 外部表。
注意
- 如果该表存在,则
.create命令会失败并显示错误。 使用.create-or-alter或.alter修改现有表。 - 不支持更改外部 Azure SQL 表的架构。
支持的 Azure SQL 外部表类型
- SQL Server
- MySQL
- PostgreSQL
- Cosmos DB (宇宙数据库)
权限
.create 至少需要数据库用户权限,.alter 至少需要表管理员权限。
对于 .create、.alter 或 .create-or-alter,使用托管标识身份验证的外部表需要数据库管理员权限。 SQL Server 和 Cosmos DB 外部表支持此方法。
语法
(.create | .alter | .create-or-alter) externaltable TableName(Schema)kind=sql [ table= SqlTableName ] (SqlConnectionString) [with( [ sqlDialect= SqlDialect ] , [ Property, ... ])]
详细了解语法约定。
参数
| 客户 | 类型 | 必需 | 说明 |
|---|---|---|---|
| TableName | string |
✔️ | 外部表的名称。 该名称必须遵循实体名称规则,并且外部表不能与同一数据库中的常规表同名。 |
| 架构 | string |
✔️ | 外部数据架构是包含一个或多个列名和数据类型的逗号分隔列表,其中的每个项遵循以下格式:ColumnName ColumnType:。 |
| SqlConnectionString | string |
✔️ | SQL Server 的连接字符串。 |
| SqlTableName | string |
不包括数据库名称的 SQL 表的名称。 如果表的名称包含句点(“.”),则使用 ['Name.of.the.table'] 表示法。 如果表不在默认 SQL 数据库架构中,请使用 ['SchemaName.TableName'] 表示法。 例如,对于架构“S1”中的表“T1”:['S1。T1'] 除 Cosmos DB 以外的所有类型的表都需要此规范,因为对于 Cosmos DB,集合名称是连接字符串的一部分。 |
|
| SqlDialect | string |
指示 Azure SQL 外部表的类型。 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 INSERT 和 System.Data.SqlClient.SqlBulkCopy) |
createifnotexists |
true/ false |
如果为 true,则会在目标 SQL 表不存在的情况下创建该表;在这种情况下,必须提供 primarykey 属性来指示作为主键的结果列。 默认为 false。 |
primarykey |
string |
如果 createifnotexists 为 true,则生成的列名称将用作 SQL 表的主键(如果是此命令创建的)。 |
身份验证和授权
若要与外部 Azure SQL 表交互,必须在 SqlConnectionString 中指定身份验证方法。 SqlConnectionString 定义要访问的资源及其身份验证信息。
有关详细信息,请参阅 Azure SQL 外部表身份验证方法。
注意
如果外部表用于连续导出,则必须通过用户名/密码或托管标识进行身份验证。
示例
以下示例演示如何创建每种类型的 Azure 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
)
输出
| 数据表名称 | 表类型 | 文件夹 | 文档字符串 | 属性 |
|---|---|---|---|---|
| MySqlExternalTable | SQL | ExternalTables | 文档 | {
} “TargetEntityKind”: “sqltable'”, "TargetEntityName":"MySqlTable", “TargetEntityConnectionString”: “Server=tcp:myserver.database.windows.net,1433;Authentication=Active Directory 集成;初始 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",
)