快速入门:使用 SQL Server Management Studio 连接和查询 Azure SQL 数据库Quickstart: Use SQL Server Management Studio to connect and query an Azure SQL database

本快速入门介绍如何使用 SQL Server Management Studio (SSMS) 连接到 Azure SQL 数据库并运行一些查询。In this quickstart, you'll learn how to use SQL Server Management Studio (SSMS) to connect to an Azure SQL database and run some queries.

先决条件Prerequisites

完成本快速入门需要以下各项:Completing this quickstart requires the following items:

  • SQL Server Management Studio (SSMS)SQL Server Management Studio (SSMS).
  • AdventureWorksLT 示例数据库。The AdventureWorksLT sample database. 如果需要 AdventureWorksLT 数据库的工作副本,完成创建 Azure SQL 数据库快速入门即可创建一个。If you need a working copy of the AdventureWorksLT database, create one by completing the Create an Azure SQL database quickstart.
    • 本文中脚本的编写目的是使用 AdventureWorksLT 数据库。The scripts in this article are written to use the AdventureWorksLT database. 如果使用托管实例,则必须将 AdventureWorks 数据库导入一个实例数据库,或者修改本文中的脚本,以便使用 Wide World Importers 数据库。If you are using a managed instance, you must either import the AdventureWorks database into an instance database or modify the scripts in this article to use the Wide World Importers database.

如果只是想要在不安装 SSMS 的情况下运行某些即席查询,请参阅快速入门:使用 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 SQL database.

获取 SQL Server 连接信息Get SQL 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. 导航到要查询的“SQL 数据库”或“SQL 托管实例”。 Navigate to the SQL database or SQL managed instance you want to query.

  3. 在“概览” 页上,复制完全限定的服务器名称。On the Overview page, copy the fully qualified server name. 它位于单一数据库的“服务器名称”旁边,或者托管实例的“主机”旁边 。It's next to Server name for a single database, or the fully qualified server name next to Host for a managed instance. 完全限定的名称类似于 servername.database.chinacloudapi.cn,但使用你的实际服务器名称。The fully qualified name looks like: servername.database.chinacloudapi.cn, except it has your actual server name.

连接到数据库Connect to your database

在 SSMS 中,连接到 Azure SQL 数据库服务器。In SSMS, connect to your Azure SQL Database server.

Important

Azure SQL 数据库服务器在端口 1433 上进行侦听。An Azure SQL Database server listens on port 1433. 若要从公司防火墙后连接到 SQL 数据库服务器,该防火墙必须打开此端口。To connect to a SQL Database 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