快速入门:使用 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 托管实例 APPLIES TO: yesAzure SQL Database yesAzure SQL Managed Instance

本快速入门介绍如何使用 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:

如果只是想要在不安装 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.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 导航到要查询的“数据库”或“托管实例”。 Navigate to the database or managed instance you want to query.

  3. 在“概述”页上,在“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

在 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.

  1. 打开 SSMS。Open SSMS.

  2. 此时会显示“连接到服务器”对话框。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.cnSomething 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.

    连接到服务器

  3. 选择“连接到服务器”对话框中的“选项” 。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.

    连接到服务器上的 DB

  4. 选择“连接” 。Select Connect. 此时会打开“对象资源管理器”窗口。The Object Explorer window opens.

  5. 若要查看数据库的对象,请展开“数据库”,然后展开你的数据库节点。To view the database's objects, expand Databases and then expand your database node.

    mySampleDatabase 对象

查询数据Query data

运行此 SELECT Transact-SQL 代码,按类别查询前 20 个产品。Run this SELECT Transact-SQL code to query for the top 20 products by category.

  1. 在对象资源管理器中,右键单击“mySampleDatabase”,并选择“新建查询” 。In Object Explorer, right-click mySampleDatabase and select New Query. 此时会打开一个连接到数据库的新查询窗口。A new query window connected to your database opens.

  2. 在查询窗口中粘贴以下 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;
    
  3. 在工具栏上选择“执行”,以便运行查询并从 ProductProductCategory 表检索数据。On the toolbar, select Execute to run the query and retrieve data from the Product and ProductCategory tables.

    从表 Product 和 ProductCategory 中检索数据的查询

插入数据Insert data

运行此 INSERT Transact-SQL 代码,在 SalesLT.Product 表中创建新产品。Run this INSERT Transact-SQL code to create a new product in the SalesLT.Product table.

  1. 将之前的查询替换为此查询。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() );
    
  2. 选择“执行”在 Product 表中插入新行。Select Execute to insert a new row in the Product table. “消息”面板显示“(受影响的 1 行)” 。The Messages pane displays (1 row affected).

查看结果View the result

  1. 将之前的查询替换为此查询。Replace the previous query with this one.

    SELECT * FROM [SalesLT].[Product]
    WHERE Name='myNewProduct'
    
  2. 选择“执行”。Select Execute. 将显示以下结果。The following result appears.

    Product 表查询的结果

更新数据Update data

运行此 UPDATE Transact-SQL 代码以修改新产品。Run this UPDATE Transact-SQL code to modify your new product.

  1. 将上一个查询替换为此查询,以便返回前面创建的新记录:Replace the previous query with this one that returns the new record created previously:

    UPDATE [SalesLT].[Product]
    SET [ListPrice] = 125
    WHERE Name = 'myNewProduct';
    
  2. 选择“执行”更新 Product 表中的指定行。Select Execute to update the specified row in the Product 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.

  1. 将之前的查询替换为此查询。Replace the previous query with this one.

    DELETE FROM [SalesLT].[Product]
    WHERE Name = 'myNewProduct';
    
  2. 选择“执行”删除 Product 表中的指定行。Select Execute to delete the specified row in the Product table. “消息”面板显示“(受影响的 1 行)” 。The Messages pane displays (1 row affected).

后续步骤Next steps