Skip to content

Latest commit

 

History

History
169 lines (112 loc) · 6.94 KB

quickstart-connect-query.md

File metadata and controls

169 lines (112 loc) · 6.94 KB
author ms.author ms.date ms.service ms.subservice ms.custom
rwestMSFT
randolphwest
11/18/2024
sql
linux
linux-related-content

Connect locally

The following steps use sqlcmd to locally connect to your new [!INCLUDE ssnoversion-md] instance.

Caution

[!INCLUDE password-complexity]

  1. Run sqlcmd with parameters for your [!INCLUDE ssnoversion-md] name (-S), the user name (-U), and the password (-P). In this tutorial, you connect locally, so the server name is localhost. The user name is sa and the password is the one you provided for the sa account during setup.

    sqlcmd -S localhost -U sa -P '<password>'

    [!NOTE]
    Newer versions of sqlcmd are secure by default. For more information about connection encryption, see sqlcmd utility for Windows, and Connecting with sqlcmd for Linux and macOS. If the connection doesn't succeed, you can add the -No option to sqlcmd to specify that encryption is optional, not mandatory.

    You can omit the password on the command line to be prompted to enter it.

    If you later decide to connect remotely, specify the machine name or IP address for the -S parameter, and make sure port 1433 is open on your firewall.

  2. If successful, you should get to a sqlcmd command prompt: 1>.

  3. If you get a connection failure, first attempt to diagnose the problem from the error message. Then review the connection troubleshooting recommendations.

Create and query data

The following sections walk you through using sqlcmd to create a new database, add data, and run a simple query.

For more information about writing Transact-SQL statements and queries, see Tutorial: Write Transact-SQL statements.

Create a new database

The following steps create a new database named TestDB.

  1. From the sqlcmd command prompt, paste the following Transact-SQL command to create a test database:

    CREATE DATABASE TestDB;
  2. On the next line, write a query to return the name of all of the databases on your server:

    SELECT Name
    FROM sys.databases;
  3. The previous two commands aren't executed immediately. You must type GO on a new line to execute the previous commands:

    GO

Insert data

Next create a new table, dbo.Inventory, and insert two new rows.

  1. From the sqlcmd command prompt, switch context to the new TestDB database:

    USE TestDB;
  2. Create new table named dbo.Inventory:

    CREATE TABLE dbo.Inventory
    (
        id INT,
        name NVARCHAR (50),
        quantity INT,
        PRIMARY KEY (id)
    );
  3. Insert data into the new table:

    INSERT INTO dbo.Inventory
    VALUES (1, 'banana', 150);
    
    INSERT INTO dbo.Inventory
    VALUES (2, 'orange', 154);
  4. Type GO to execute the previous commands:

    GO

Select data

Now, run a query to return data from the dbo.Inventory table.

  1. From the sqlcmd command prompt, enter a query that returns rows from the dbo.Inventory table where the quantity is greater than 152:

    SELECT *
    FROM dbo.Inventory
    WHERE quantity > 152;
  2. Execute the command:

    GO

Exit the sqlcmd command prompt

To end your sqlcmd session, type QUIT:

QUIT

Performance best practices

After installing [!INCLUDE ssnoversion-md] on Linux, review the best practices for configuring Linux and [!INCLUDE ssnoversion-md] to improve performance for production scenarios. For more information, see Performance best practices and configuration guidelines for SQL Server on Linux.

Cross-platform data tools

In addition to sqlcmd, you can use the following cross-platform tools to manage [!INCLUDE ssnoversion-md]:

Tool Description
Azure Data Studio A cross-platform GUI database management utility.
Visual Studio Code A cross-platform GUI code editor that run Transact-SQL statements with the mssql extension.
PowerShell Core A cross-platform automation and configuration tool based on cmdlets.
mssql-cli A cross-platform command-line interface for running Transact-SQL commands.

Connect from Windows

[!INCLUDE ssnoversion-md] tools on Windows connect to [!INCLUDE ssnoversion-md] instances on Linux in the same way they would connect to any remote [!INCLUDE ssnoversion-md] instance.

If you have a Windows machine that can connect to your Linux machine, try the same steps in this topic from a Windows command-prompt running sqlcmd. You must use the target Linux machine name or IP address rather than localhost, and make sure that TCP port 1433 is open on the [!INCLUDE ssnoversion-md] machine. If you have any problems connecting from Windows, see connection troubleshooting recommendations.

For other tools that run on Windows but connect to [!INCLUDE ssnoversion-md] on Linux, see:

Other deployment scenarios

For other installation scenarios, see the following resources:

For answers to frequently asked questions, see the SQL Server on Linux FAQ.

Related content

[!INCLUDE contribute-to-content]