快速入门:使用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 服务器,完成以下 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 DescriptionDescription
    服务器名称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 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

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

  1. 在 Azure SQL 服务器上右键单击服务器侧栏,然后选择新查询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

查询编辑器连接到数据库中,但我们想要创建的表中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.