快速入門:使用 Azure Data StudioAzure Data Studio 連線及查詢 SQL ServerQuickstart: 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.

PrerequisitesPrerequisites

若要完成本快速入門,您需要 Azure Data StudioAzure Data Studio 和 SQL Server 的存取權。To complete this quickstart, you need Azure Data StudioAzure Data Studio, and access to 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:

  • 伺服器名稱: 在這裡輸入伺服器名稱。Server Name: Enter server name here. 例如 localhost。For example, localhost.

  • 驗證類型: SQL 登入Authentication Type: SQL Login

  • 使用者名稱: SQL Server 的使用者名稱User name: User name for the SQL Server

  • 密碼: SQL Server 的密碼Password: Password for the SQL Server

  • 資料庫名稱: <Default>Database Name: <Default>

  • 伺服器群組: <Default>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: and then select Run.

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

查詢完成後,新的 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. 將連線內容變更為 TutorialDBChange the connection context to TutorialDB:

    變更內容

  2. 將下列程式碼片段貼到查詢視窗,然後按一下 [執行] :Paste the following snippet into the query window and click Run:

    注意

    您可以在編輯器中將此程式碼片段附加至查詢,也可以覆寫先前的查詢。You can append this too, 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;

選取結果

後續步驟Next steps

現在您已成功連線到 SQL Server 並執行查詢,請嘗試程式碼編輯器教學課程Now that you've successfully connected to SQL Server and run a query try out the Code editor tutorial.