快速入门:使用 SSMS 连接到并查询 Azure SQL 数据库或 Azure SQL 托管实例Quickstart: Use SSMS to connect to and query Azure SQL Database or Azure SQL Managed Instance
Azure SQL 数据库
Azure SQL 托管实例
本快速入门介绍如何使用 SQL Server Management Studio (SSMS) 连接到 Azure SQL 数据库或 Azure SQL 托管实例并运行一些查询。In this quickstart, you'll learn how to use SQL Server Management Studio (SSMS) to connect to Azure SQL Database or Azure SQL Managed Instance and run some queries.
先决条件Prerequisites
完成本快速入门需要以下各项:Completing this quickstart requires the following items:
SQL Server Management Studio (SSMS)。SQL Server Management Studio (SSMS).
Azure SQL 数据库中的数据库。A database in Azure SQL Database. 可以根据下述快速入门之一,在 Azure SQL 数据库中创建数据库,然后对其进行配置:You can use one of these quickstarts to create and then configure a database in Azure SQL Database:
操作Action SQL 数据库SQL Database SQL 托管实例SQL Managed Instance Azure VM 上的 SQL ServerSQL Server on Azure VM 创建Create PortalPortal PortalPortal 门户Portal CLICLI CLICLI PowerShellPowerShell PowerShellPowerShell PowerShellPowerShell 配置Configure 服务器级别 IP 防火墙规则Server-level IP firewall rule 从 VM 进行连接Connectivity from a VM 从现场进行连接Connectivity from on-site 连接到 SQL ServerConnect to SQL Server 加载数据Load data 根据快速入门加载的 Adventure WorksAdventure Works loaded per quickstart 还原 Wide World ImportersRestore Wide World Importers 还原 Wide World ImportersRestore Wide World Importers 从 GitHub 所提供的 BACPAC 文件还原或导入 Adventure WorksRestore or import Adventure Works from BACPAC file from GitHub 从 GitHub 所提供的 BACPAC 文件还原或导入 Adventure WorksRestore or import Adventure Works from BACPAC file from GitHub 重要
本文中脚本的编写目的是使用 Adventure Works 数据库。The scripts in this article are written to use the Adventure Works database. 使用托管实例时,必须将 Adventure Works 数据库导入一个实例数据库,或者修改本文中的脚本,以便使用 Wide World Importers 数据库。With a managed instance, you must either import the Adventure Works database into an instance database or modify the scripts in this article to use the Wide World Importers database.
如果只是想要在不安装 SSMS 的情况下运行某些即席查询,请参阅快速入门:使用 Azure 门户的查询编辑器查询 Azure SQL 数据库中的数据库。If you simply want to run some ad-hoc queries without installing SSMS, see Quickstart: Use the Azure portal's query editor to query a database in Azure SQL Database.
获取服务器连接信息Get server connection information
获取连接到数据库所需的连接信息。Get the connection information you need to connect to your database. 需要提供完全限定的服务器名称或主机名称、数据库名称和登录信息才能完成本快速入门。You'll need the fully qualified server name or host name, database name, and login information to complete this quickstart.
登录 Azure 门户。Sign in to the Azure portal.
导航到要查询的“数据库”或“托管实例”。Navigate to the database or managed instance you want to query.
在“概述”页上,在“Server 名称”旁查看 SQL 数据库中的数据库的完全限定服务器名称,或在“Host”旁边查看 VM 上 SQL 托管实例中或 SQL Server 实例中的托管实例的完全限定服务器名称(或 IP 地址) 。On the Overview page, review the fully qualified server name next to Server name for your database in SQL Database or the fully qualified server name (or IP address) next to Host for your managed instance in SQL Managed Instance or your SQL Server instance on your VM. 若要复制服务器名称或主机名称,请将鼠标悬停在其上方,然后选择“复制”图标。To copy the server name or host name, hover over it and select the Copy icon.
备注
有关 Azure VM 上的 SQL Server 的连接信息,请参阅连接到 SQL ServerFor connection information for SQL Server on Azure VM, see Connect to SQL Server
连接到数据库Connect to your database
备注
在 2021 年 12 月,将不再使用 MFA 通过 Azure Active Directory 对 18.6 版之前的 SSMS 版本进行身份验证。In December 2021, releases of SSMS prior to 18.6 will no longer authenticate through Azure Active Directory with MFA.
若要继续将 MFA 与 Azure Active Directory 身份验证结合使用,需要 SSMS 18.6 或更高版本。To continue utilizing Azure Active Directory authentication with MFA, you need SSMS 18.6 or later.
在 SSMS 中连接至服务器。In SSMS, connect to your server.
重要
服务器在端口 1433 上进行侦听。A server listens on port 1433. 若要从公司防火墙后连接到服务器,该防火墙必须打开此端口。To connect to a server from behind a corporate firewall, the firewall must have this port open.
打开 SSMS。Open SSMS.
此时会显示“连接到服务器”对话框。The Connect to Server dialog box appears. 输入以下信息:Enter the following information:
设置Setting 建议的值Suggested value 说明Description 服务器类型Server type 数据库引擎Database engine 所需的值。Required value. 服务器名称Server name 完全限定的服务器名称The fully qualified server name 类似于 servername.database.chinacloudapi.cn 。Something like: servername.database.chinacloudapi.cn . 身份验证Authentication SQL Server 身份验证SQL Server Authentication 本教程使用 SQL 身份验证。This tutorial uses SQL Authentication. 登录Login 服务器管理员帐户用户 IDServer admin account user ID 用于创建服务器的服务器管理员帐户的用户 ID。The user ID from the server admin account used to create the server. 密码Password 服务器管理员帐户密码Server admin account password 用于创建服务器的服务器管理员帐户的密码。The password from the server admin account used to create the server.
备注
本教程使用 SQL Server 身份验证。This tutorial utilizes SQL Server Authentication.
选择“连接到服务器”对话框中的“选项” 。Select Options in the Connect to Server dialog box. 在“连接到数据库”下拉菜单中,选择“mySampleDatabase” 。In the Connect to database drop-down menu, select mySampleDatabase . 完成“先决条件”部分中的快速入门即可创建一个名为 mySampleDatabase 的 AdventureWorksLT 数据库。Completing the quickstart in the Prerequisites section creates an AdventureWorksLT database named mySampleDatabase. 如果 AdventureWorks 数据库的工作副本名称不同于 mySampleDatabase,请改为选择它。If your working copy of the AdventureWorks database has a different name than mySampleDatabase, then select it instead.
选择“连接” 。Select Connect . 此时会打开“对象资源管理器”窗口。The Object Explorer window opens.
若要查看数据库的对象,请展开“数据库”,然后展开你的数据库节点。To view the database's objects, expand Databases and then expand your database node.
查询数据Query data
运行此 SELECT Transact-SQL 代码,按类别查询前 20 个产品。Run this SELECT Transact-SQL code to query for the top 20 products by category.
在对象资源管理器中,右键单击“mySampleDatabase”,并选择“新建查询” 。In Object Explorer, right-click mySampleDatabase and select New Query . 此时会打开一个连接到数据库的新查询窗口。A new query window connected to your database opens.
在查询窗口中粘贴以下 SQL 查询:In the query window, paste the following SQL query:
SELECT pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid;
在工具栏上选择“执行”,以便运行查询并从
Product
和ProductCategory
表检索数据。On the toolbar, select Execute to run the query and retrieve data from theProduct
andProductCategory
tables.
插入数据Insert data
运行此 INSERT Transact-SQL 代码,在 SalesLT.Product
表中创建新产品。Run this INSERT Transact-SQL code to create a new product in the SalesLT.Product
table.
将之前的查询替换为此查询。Replace the previous query with this one.
INSERT INTO [SalesLT].[Product] ( [Name] , [ProductNumber] , [Color] , [ProductCategoryID] , [StandardCost] , [ListPrice] , [SellStartDate] ) VALUES ('myNewProduct' ,123456789 ,'NewColor' ,1 ,100 ,100 ,GETDATE() );
选择“执行”在
Product
表中插入新行。Select Execute to insert a new row in theProduct
table. “消息”面板显示“(受影响的 1 行)” 。The Messages pane displays (1 row affected) .
查看结果View the result
将之前的查询替换为此查询。Replace the previous query with this one.
SELECT * FROM [SalesLT].[Product] WHERE Name='myNewProduct'
选择“执行”。Select Execute . 将显示以下结果。The following result appears.
更新数据Update data
运行此 UPDATE Transact-SQL 代码以修改新产品。Run this UPDATE Transact-SQL code to modify your new product.
将上一个查询替换为此查询,以便返回前面创建的新记录:Replace the previous query with this one that returns the new record created previously:
UPDATE [SalesLT].[Product] SET [ListPrice] = 125 WHERE Name = 'myNewProduct';
选择“执行”更新
Product
表中的指定行。Select Execute to update the specified row in theProduct
table. “消息”面板显示“(受影响的 1 行)” 。The Messages pane displays (1 row affected) .
删除数据Delete data
运行此 DELETE Transact-SQL 代码以删除新产品。Run this DELETE Transact-SQL code to remove your new product.
将之前的查询替换为此查询。Replace the previous query with this one.
DELETE FROM [SalesLT].[Product] WHERE Name = 'myNewProduct';
选择“执行”删除
Product
表中的指定行。Select Execute to delete the specified row in theProduct
table. “消息”面板显示“(受影响的 1 行)” 。The Messages pane displays (1 row affected) .
后续步骤Next steps
- 有关 SSMS 的信息,请参阅 SQL Server Management Studio。For information about SSMS, see SQL Server Management Studio.
- 若要使用 Azure 门户进行连接和查询,请参阅使用 Azure 门户 SQL 查询编辑器进行连接和查询。To connect and query using the Azure portal, see Connect and query with the Azure portal SQL Query editor.
- 若要使用 Visual Studio Code 进行连接和查询,请参阅使用 Visual Studio Code 进行连接和查询。To connect and query using Visual Studio Code, see Connect and query with Visual Studio Code.
- 若要使用 .NET 进行连接和查询,请参阅使用 .NET 进行连接和查询。To connect and query using .NET, see Connect and query with .NET.
- 若要使用 PHP 进行连接和查询,请参阅使用 PHP 进行连接和查询。To connect and query using PHP, see Connect and query with PHP.
- 若要使用 Node.js 进行连接和查询,请参阅使用 Node.js 进行连接和查询。To connect and query using Node.js, see Connect and query with Node.js.
- 若要使用 Java 进行连接和查询,请参阅使用 Java 进行连接和查询。To connect and query using Java, see Connect and query with Java.
- 若要使用 Python 进行连接和查询,请参阅使用 Python 进行连接和查询。To connect and query using Python, see Connect and query with Python.
- 若要使用 Ruby 进行连接和查询,请参阅使用 Ruby 进行连接和查询。To connect and query using Ruby, see Connect and query with Ruby.