適用於: ✅Microsoft網狀架構✅Azure 數據總管
在執行命令的資料庫中,建立或改變 Azure SQL 外部數據表 。
注意
- 如果數據表存在,
.create命令將會失敗併發生錯誤。 使用.create-or-alter或修改.alter現有的數據表。 - 不支援變更外部 Azure SQL 資料表的架構。
支援的 Azure SQL 外部資料表類型
- SQL 伺服器
- MySQL
- PostgreSQL
- Cosmos DB(宇宙資料庫)
權限
至少 .create 需要 資料庫用戶 權力,而且 .alter 至少 需要數據表管理員 許可權。
對 .create、 .alter或 .create-or-alter 使用受控識別驗證的外部數據表,需要 資料庫管理員 許可權。 SQL Server 和 Cosmos DB 外部資料表支援此方法。
語法
(.create | .alter | .create-or-alter) externaltable TableName( 架構)kind=sql [ table= SqlTableName ] (SqlConnectionString) [ [ SqlDialect ] withsqlDialect=, 屬性, ... ]])
深入瞭解 語法慣例。
參數
| 姓名 | 類型 | 必要 | 描述 |
|---|---|---|---|
| 表名 | 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 = 格式的索引鍵/值屬性組。 請參閱 選擇性屬性。 |
警告
應模糊處理包含機密資訊的連接字串和查詢,以便從任何 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 伺服器
.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",
)