在此快速入門中,您可以使用 Azure 入口網站、PowerShell 指令碼或 Azure CLI 指令碼,在 Azure SQL Database 中建立單一資料庫。In this quickstart, you create a single database in Azure SQL Database using either the Azure portal, a PowerShell script, or an Azure CLI script.接著,您可以使用 Azure 入口網站中的 查詢編輯器 來查詢資料庫。You then query the database using Query editor in the Azure portal.
在 SQL 資料庫 下,將 資源類型 設定為 單一資料庫,然後選取 [建立]。Under SQL databases, leave Resource type set to Single database, and select Create.
在 建立 SQL 資料庫 表單的 基本資料 索引標籤上,在 專案詳細資料 下,選取想要的 Azure 訂用帳戶。On the Basics tab of the Create SQL Database form, under Project details, select the desired Azure Subscription.
針對 資源群組 選取 [建立新的],輸入 myResourceGroup,然後選取 [確定]。For Resource group, select Create new, enter myResourceGroup, and select OK.
針對 資料庫名稱 輸入 mySampleDatabase。For Database name enter mySampleDatabase.
在 伺服器 中,選取 [建立新的],並以下列值填寫 新伺服器 表單:For Server, select Create new, and fill out the New server form with the following values:
伺服器名稱:輸入 mysqlserver 並新增一些字元來表示唯一性。Server name: Enter mysqlserver, and add some characters for uniqueness.由於伺服器名稱必須是 Azure 中所有伺服器的全域唯一名稱,而不只是訂用帳戶中的唯一名稱,因此我們無法提供要使用的確切伺服器名稱。We can't provide an exact server name to use because server names must be globally unique for all servers in Azure, not just unique within a subscription.因此,請輸入類似 mysqlserver12345 的值,入口網站會讓您知道您輸入的值是否可以使用。So enter something like mysqlserver12345, and the portal lets you know if it is available or not.
伺服器管理員登入:輸入 azureuser。Server admin login: Enter azureuser.
密碼:輸入符合需求的密碼,然後在 [確認密碼] 欄位中再次輸入。Password: Enter a password that meets requirements, and enter it again in the Confirm password field.
位置:從下拉式清單中選取位置。Location: Select a location from the dropdown list.
選取 [確定]。Select OK.
將 您要使用 SQL 彈性集區 設為 否。Leave Want to use SQL elastic pool set to No.
本快速入門會使用無伺服器資料庫,因此請選取 [無伺服器],然後選取 [套用]。This quickstart uses a serverless database, so select Serverless, and then select Apply.
完成時,選取 [下一步:網路功能],為於頁面底部。Select Next: Networking at the bottom of the page.
在 網路功能 索引標籤的 連線方法 中,選取 [公用端點]。On the Networking tab, for Connectivity method, select Public endpoint.
針對 防火牆規則,將 [新增目前的用戶端 IP 位址] 設定為 是。For Firewall rules, set Add current client IP address to Yes.將 允許 Azure 服務和資源存取此伺服器群組 保留為 否。Leave Allow Azure services and resources to access this server set to No.
完成時,選取 [下一步:其他設定],位於頁面底部。Select Next: Additional settings at the bottom of the page.
在 [其他設定] 索引標籤的 [資料來源] 區段中,針對 [使用現有的資料],選取 [範例]。On the Additional settings tab, in the Data source section, for Use existing data, select Sample.這會建立 AdventureWorksLT 範例資料庫,同時提供一些可供查詢和實驗的資料表和資料,而不是空的空白資料庫。This creates an AdventureWorksLT sample database so there's some tables and data to query and experiment with, as opposed to an empty blank database.
選取頁面底部的 [檢閱 + 建立]:Select Review + create at the bottom of the page:
檢閱 [檢閱 + 建立] 頁面之後,選取 [建立]。On the Review + create page, after reviewing, select Create.
啟動 Azure Cloud ShellLaunch Azure Cloud Shell
Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article.它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。It has common Azure tools preinstalled and configured to use with your account.
若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。To open the Cloud Shell, just select Try it from the upper right corner of a code block.您也可以移至 https://shell.azure.com,從另一個瀏覽器索引標籤啟動 Cloud Shell。You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com.選取 [複製] 即可複製程式碼區塊,將它貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press Enter to run it.
設定參數值Set parameter values
後續的命令會使用下列值來建立資料庫和所需的資源。The following values are used in subsequent commands to create the database and required resources.伺服器名稱在所有 Azure 中必須是全域唯一的名稱,因此會使用 $RANDOM 函式來建立伺服器名稱。Server names need to be globally unique across all of Azure so the $RANDOM function is used to create the server name.取代 IP 位址範圍中的 0.0.0.0 值,以符合您的特定環境。Replace the 0.0.0.0 values in the ip address range to match your specific environment.
# Set the resource group name and location for your server
resourceGroupName=myResourceGroup
location=eastus
# Set an admin login and password for your database
adminlogin=azureuser
password=Azure1234567!
# Set a server name that is unique to Azure DNS (<server_name>.database.windows.net)
serverName=server-$RANDOM
# Set the ip address range that can access your database
startip=0.0.0.0
endip=0.0.0.0
建立資源群組Create a resource group
使用 az group create 命令來建立資源群組。Create a resource group with the az group create command.Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。An Azure resource group is a logical container into which Azure resources are deployed and managed.下列範例會在 eastus 位置建立名為 myResourceGroup 的資源群組:The following example creates a resource group named myResourceGroup in the eastus location:
az group create --name $resourceGroupName --location $location
您可以使用 Windows PowerShell 來建立資源群組、伺服器及單一資料庫。You can create a resource group, server, and single database using Windows PowerShell.
啟動 Azure Cloud ShellLaunch Azure Cloud Shell
Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article.它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。It has common Azure tools preinstalled and configured to use with your account.
若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。To open the Cloud Shell, just select Try it from the upper right corner of a code block.您也可以移至 https://shell.azure.com,從另一個瀏覽器索引標籤啟動 Cloud Shell。You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com.選取 [複製] 即可複製程式碼區塊,將它貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press Enter to run it.
設定參數值Set parameter values
後續的命令會使用下列值來建立資料庫和所需的資源。The following values are used in subsequent commands to create the database and required resources.伺服器名稱在所有 Azure 中必須是全域唯一的名稱,因此會使用 Get-Random Cmdlet 來建立伺服器名稱。Server names need to be globally unique across all of Azure so the Get-Random cmdlet is used to create the server name.取代 IP 位址範圍中的 0.0.0.0 值,以符合您的特定環境。Replace the 0.0.0.0 values in the ip address range to match your specific environment.
# Set variables for your server and database
$resourceGroupName = "myResourceGroup"
$location = "eastus"
$adminLogin = "azureuser"
$password = "Azure1234567!"
$serverName = "mysqlserver-$(Get-Random)"
$databaseName = "mySampleDatabase"
# The ip address range that you want to allow to access your server
$startIp = "0.0.0.0"
$endIp = "0.0.0.0"
# Show randomized variables
Write-host "Resource group name is" $resourceGroupName
Write-host "Server name is" $serverName
建立資源群組Create resource group
使用 New-AzResourceGroup 來建立 Azure 資源群組。Create an Azure resource group with New-AzResourceGroup.資源群組是在其中部署與管理 Azure 資源的邏輯容器。A resource group is a logical container into which Azure resources are deployed and managed.
資料庫建好之後,您可以使用 Azure 入口網站中的 查詢編輯器 (預覽) 連線到資料庫,並查詢資料。Once your database is created, you can use the Query editor (preview) in the Azure portal to connect to the database and query data.
在入口網站中,搜尋並選取 [SQL 資料庫],然後從清單中選取您的資料庫。In the portal, search for and select SQL databases, and then select your database from the list.
在資料庫頁面面上,選取左側功能表中的 [查詢編輯器 (預覽)]。On the page for your database, select Query editor (preview) in the left menu.
輸入您的伺服器系統管理員登入資訊,然後選取 [確定]。Enter your server admin login information, and select OK.
在 [查詢編輯器] 窗格中輸入下列查詢。Enter the following query in the Query editor pane.
SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName
FROM SalesLT.ProductCategory pc
JOIN SalesLT.Product p
ON pc.productcategoryid = p.productcategoryid;
選取 [執行],然後在 [結果] 窗格中檢閱查詢結果。Select Run, and then review the query results in the Results pane.
關閉 [查詢編輯器] 頁面,並在系統提示是否要捨棄未儲存的編輯時選取 [確定]。Close the Query editor page, and select OK when prompted to discard your unsaved edits.
清除資源Clean up resources
請保留資源群組、伺服器和單一資料庫,以繼續進行後續步驟,並了解如何使用不同的方法來連線及查詢您的資料庫。Keep the resource group, server, and single database to go on to the next steps, and learn how to connect and query your database with different methods.
當您使用完這些資源時,您可以刪除所建立的資源群組,而這也會刪除其中的伺服器和單一資料庫。When you're finished using these resources, you can delete the resource group you created, which will also delete the server and single database within it.
使用 Azure 入口網站來刪除 myResourceGroup 和其所有資源:To delete myResourceGroup and all its resources using the Azure portal:
在入口網站中,搜尋並選取 [資源群組],然後從清單中選取 [myResourceGroup]。In the portal, search for and select Resource groups, and then select myResourceGroup from the list.
在 [資源群組] 頁面中,選取 [刪除資源群組]。On the resource group page, select Delete resource group.
在 [輸入資源群組名稱] 底下,輸入 myResourceGroup,然後選取 [刪除]。Under Type the resource group name, enter myResourceGroup, and then select Delete.
若要刪除資源群組及其所有資源,請使用您的資源組名稱來執行下列 Azure CLI 命令:To delete the resource group and all its resources, run the following Azure CLI command, using the name of your resource group:
az group delete --name $resourceGroupName
若要刪除資源群組及其所有資源,請使用您的資源組名稱來執行下列 PowerShell Cmdlet:To delete the resource group and all its resources, run the following PowerShell cmdlet, using the name of your resource group:
Remove-AzResourceGroup -Name $resourceGroupName
後續步驟Next steps
使用不同的工具和語言來連線及查詢您的資料庫:Connect and query your database using different tools and languages: