快速入门:使用 SQL Server 连接和查询 Azure Data StudioAzure Data StudioQuickstart: Connect and query SQL Server using Azure Data StudioAzure Data Studio

本快速入门显示如何使用Azure Data StudioAzure Data Studio连接到SQL Server,然后使用Transact-SQL(T-SQL)语句创建Azure Data StudioAzure Data Studio教程中使用的TutorialDBThis quickstart shows how to use Azure Data StudioAzure Data Studio to connect to SQL Server, and then use Transact-SQL (T-SQL) statements to create the TutorialDB used in Azure Data StudioAzure Data Studio tutorials.

必要条件Prerequisites

要完成此快速入门,您需要Azure Data StudioAzure Data Studio并访问SQL Server。To complete this quickstart, you need Azure Data StudioAzure Data Studio, and access to a SQL Server.

如果您无权访问SQL Server,请从以下链接中选择您的平台(确保记住您的SQL登录名和密码!):If you don't have access to a SQL Server, select your platform from the following links (make sure you remember your SQL Login and Password!):

连接到 SQL ServerConnect to a SQL Server

  1. 启动 Azure Data StudioAzure Data StudioStart Azure Data StudioAzure Data Studio.

  2. 首次运行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:

    新的连接图标

  3. 本文使用SQL 登录名,但Windows 身份验证支持。This article uses SQL Login, but Windows Authentication is supported. 按如下所示填写字段:Fill in the fields as follows:

    • 服务器名称: localhostServer Name: localhost
    • 身份验证类型: SQL 登录名Authentication Type: SQL Login
    • 用户名: SQL Server 的用户名称User name: User name for the SQL Server
    • 密码: SQL 服务器的密码Password: Password for the SQL Server
    • 数据库名称: 将此字段留空Database Name: leave this field blank
    • 服务器组: <默认>Server Group: <Default>

    新连接屏幕

创建数据库Create a database

以下步骤创建名为TutorialDB的数据库:The following steps create a database named TutorialDB:

  1. 右键单击您的服务器上localhost,然后选择新查询。Right click on your server, localhost, and select New Query.

  2. 将以下代码片段粘贴到查询窗口:Paste the following snippet into the query window:

    USE master
    GO
    IF NOT EXISTS (
       SELECT name
       FROM sys.databases
       WHERE name = N'TutorialDB'
    )
       CREATE DATABASE [TutorialDB];
    GO
    IF SERVERPROPERTY('ProductVersion') > '12'
        ALTER DATABASE [TutorialDB] SET QUERY_STORE=ON;
    GO
    
  3. 若要执行查询时,请单击运行To execute the query, click Run .

在查询完成后,新的TutorialDB将出现在数据库列表中。After the query completes, the new TutorialDB appears in the list of databases. 如果看不到,请右击数据库节点,然后选择刷新If you don't see it, right-click the Databases node and select Refresh.

创建表Create a table

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

  1. 将连接上下文更改为TutorialDB:Change the connection context to TutorialDB:

    更改上下文

  2. 将以下代码段粘贴到查询窗口中,然后单击运行Paste the following snippet into the query window and click Run:

    备注

    您可以将其追加到编辑器中,或者覆盖编辑器中的前一个查询。You can append this to, or overwrite the previous query in the editor. 注意,单击运行只执行所选的查询。Note that clicking Run executes only the query that is selected. 如果没有选择,单击运行执行编辑器中的所有查询。If nothing is selected, clicking Run executes all queries in the editor.

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

在查询完成后,新客户表将出现在表的列表。After the query completes, the new Customers table appears in the list of tables. 您可能需要右键单击TutorialDB > 表节点,然后选择刷新You might need to right-click the TutorialDB > Tables node and select Refresh.

插入行Insert rows

  • 将以下代码段粘贴到查询窗口中,然后单击运行Paste the following snippet into the query window and click 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 data returned by a query

  1. 将以下代码段粘贴到查询窗口中,然后单击运行Paste the following snippet into the query window and click Run:

    -- Select rows from table 'Customers'
    SELECT * FROM dbo.Customers;
    
  2. 显示查询的结果:The results of the query are displayed:

    选择结果

后续步骤Next steps

现在,你已成功连接到 SQL Server 并运行查询,尝试代码编辑器教程Now that you've successfully connected to SQL Server and run a query, try out the Code editor tutorial.