快速入门:使用 Azure Data StudioAzure Data Studio 连接并查询 Azure SQL 数据库Quickstart: Use Azure Data StudioAzure Data Studio to connect and query Azure SQL database

在本快速入门中,你将使用 Azure Data StudioAzure Data Studio 连接到 Azure SQL 数据库服务器。In this quickstart, you'll use Azure Data StudioAzure Data Studio to connect to an Azure SQL Database server. 随后,你将运行 Transact-SQL (T-SQL) 语句来创建和查询 TutorialDB 数据库,该数据库亦用于其他 Azure Data StudioAzure Data Studio 教程。You'll then run Transact-SQL (T-SQL) statements to create and query the TutorialDB database, which is used in other Azure Data StudioAzure Data Studio tutorials.

必备条件Prerequisites

若要完成本快速入门,你需要 Azure Data StudioAzure Data Studio 和 Azure SQL 数据库服务器。To complete this quickstart, you need Azure Data StudioAzure Data Studio, and an Azure SQL Database server.

如果你没有 Azure SQL Server,请完成以下 Azure SQL 数据库快速入门之一。If you don't have an Azure SQL server, complete one of the following Azure SQL Database quickstarts. 请记住完全限定服务器名称和登录凭据以用于后续步骤:Remember the fully qualified server name and sign in credentials for later steps:

连接到 Azure SQL 数据库服务器Connect to your Azure SQL Database server

使用 Azure Data StudioAzure Data Studio 建立与 Azure SQL 数据库服务器的连接。Use Azure Data StudioAzure Data Studio to establish a connection to your Azure SQL Database server.

  1. 首次运行 Azure Data StudioAzure Data Studio 时,应该会打开“欢迎”页 。The first time you run Azure Data StudioAzure Data Studio the Welcome page should open. 如果没有看到“欢迎”页,请选择“帮助” > “欢迎” 。If you don't see the Welcome page, select Help > Welcome. 选择“新建连接”以打开“连接”窗格 :Select New Connection to open the Connection pane:

    新建连接图标

  2. 本文使用 SQL 登录名,但也支持 Windows 身份验证。This article uses SQL sign-in, but also supports Windows authentication. 使用 Azure SQL Server 的服务器名称、用户名和密码填写以下字段:Fill in the following fields using the server name, user name, and password for your Azure SQL server:

    设置Setting 建议的值Suggested value 描述Description
    服务器名称Server name 完全限定的服务器名称The fully qualified server name 类似于 servername.database.windows.netSomething like: servername.database.windows.net.
    身份验证Authentication SQL 登录名SQL Login 本教程使用 SQL 身份验证。This tutorial uses SQL Authentication.
    User nameUser name 服务器管理员帐户用户名The server admin account user name 用于创建服务器的帐户的用户名。The user name from the account used to create the server.
    密码(SQL 登录名)Password (SQL Login) 服务器管理员帐户密码The server admin account password 用于创建服务器的帐户的密码。The password from the account used to create the server.
    是否保存密码?Save Password? 是或否Yes or No 如果不想每次都输入密码,请选择“是” 。Select Yes if you don't want to enter the password each time.
    数据库名称Database name 留空leave blank 仅连接到此处的服务器。You're only connecting to the server here.
    服务器组Server Group 选择 Select 可将此字段设置为创建的特定服务器组。You can set this field to a specific server group you created.

    新建连接图标

  3. 选择“连接” 。Select Connect.

  4. 如果服务器没有允许 Azure Data Studio 连接的防火墙规则,则会打开“新建防火墙规则”表单 。If your server doesn't have a firewall rule allowing Azure Data Studio to connect, the Create new firewall rule form opens. 填写表单以新建防火墙规则。Complete the form to create a new firewall rule. 有关详细信息,请参阅防火墙规则For details, see Firewall rules.

    新建防火墙规则

成功连接后,你的服务器将在“服务器”侧栏中打开 。After successfully connecting, your server opens in the SERVERS sidebar.

创建教程数据库Create the tutorial database

接下来的部分会创建在其他 Azure Data StudioAzure Data Studio 教程中使用的 TutorialDB 数据库。The next sections create the TutorialDB database that's used in other Azure Data StudioAzure Data Studio tutorials.

  1. 右键单击“服务器”侧栏中的 Azure SQL Server,然后选择“新建查询” 。Right-click on your Azure SQL server in the SERVERS sidebar and select New Query.

  2. 将此 SQL 粘贴到查询编辑器中。Paste this SQL into the query editor.

    IF NOT EXISTS (
       SELECT name
       FROM sys.databases
       WHERE name = N'TutorialDB'
    )
    CREATE DATABASE [TutorialDB]
    GO
    
    ALTER DATABASE [TutorialDB] SET QUERY_STORE=ON
    GO
    
  3. 在工具栏中选择“运行” 。From the toolbar, select Run. 通知在显示查询进度的“消息”窗格中显示 。Notifications appear in the MESSAGES pane showing query progress.

创建表Create a table

查询编辑器连接到 master 数据库,但我们想要在 TutorialDB 数据库中创建一个表。The query editor is connected to the master database, but we want to create a table in the TutorialDB database.

  1. 连接到 TutorialDB 数据库。Connect to the TutorialDB database.

    更改上下文

  2. 创建 Customers 表。Create a Customers table.

    将查询编辑器中的上一个查询替换为此查询,然后选择“运行” 。Replace the previous query in the query editor with this one and select Run.

    -- Create a new table called 'Customers' in schema 'dbo'
    -- Drop the table if it already exists
    IF OBJECT_ID('dbo.Customers', 'U') IS NOT NULL
    DROP TABLE dbo.Customers
    GO
    -- Create the table in the specified schema
    CREATE TABLE dbo.Customers
    (
       CustomerId        INT    NOT NULL   PRIMARY KEY, -- primary key column
       Name      [NVARCHAR](50)  NOT NULL,
       Location  [NVARCHAR](50)  NOT NULL,
       Email     [NVARCHAR](50)  NOT NULL
    );
    GO
    

在表中插入行Insert rows into the table

将上一个查询替换为此查询,然后选择“运行” 。Replace the previous query with this one and select Run.

-- Insert rows into table 'Customers'
INSERT INTO dbo.Customers
   ([CustomerId],[Name],[Location],[Email])
VALUES
   ( 1, N'Orlando', N'Australia', N''),
   ( 2, N'Keith', N'India', N'keith0@adventure-works.com'),
   ( 3, N'Donna', N'Germany', N'donna0@adventure-works.com'),
   ( 4, N'Janet', N'United States', N'janet1@adventure-works.com')
GO

查看结果View the result

将上一个查询替换为此查询,然后选择“运行” 。Replace the previous query with this one and select Run.

-- Select rows from table 'Customers'
SELECT * FROM dbo.Customers;

查询结果显示:The query results display:

选择结果

清理资源Clean up resources

后续的快速入门文章以此处创建的资源为基础。Later quickstart articles build upon the resources created here. 如果计划学习这些文章,请确保不要删除这些资源。If you plan to work through these articles, be sure not to delete these resources. 否则,在 Azure 门户中删除不再需要的资源。Otherwise, in the Azure portal, delete the resources you no longer need. 有关详细信息,请参阅清理资源For details, see Clean up resources.

后续步骤Next steps

你现在已成功连接到 Azure SQL 数据库并运行查询,接下来请尝试代码编辑器教程Now that you've successfully connected to an Azure SQL database and run a query, try the Code editor tutorial.