Create and query data
The following sections walk you through using
sqlcmd to create a new database, add data, and run a simple query.
Create a new database
The following steps create a new database named TestDB.
- From the
sqlcmdcommand prompt, paste the following Transact-SQL command to create a test database:
CREATE DATABASE TestDB
- On the next line, write a query to return the name of all of the databases on your server:
SELECT Name from sys.Databases
- The previous two commands were not executed immediately. You must type GO on a new line to execute the previous commands:
Next create a new table, Inventory, and insert two new rows.
- From the
sqlcmdcommand prompt, switch context to the new TestDB database:
- Create new table named Inventory:
CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
- Insert data into the new table:
INSERT INTO Inventory VALUES (1, ‘banana’, 150); INSERT INTO Inventory VALUES (2, ‘orange’, 154);
- Type GO to execute the previous commands:
Now, run a query to return data from the Inventory table.
- From the
sqlcmdcommand prompt, enter a query that returns rows from the Inventory table where the quantity is greater than 152:
SELECT * FROM Inventory WHERE quantity > 152;
- Execute the command:
Exit the sqlcmd command prompt
To end your
sqlcmd session, type QUIT:
Performance best practices
After installing SQL Server on Linux, review the best practices for configuring Linux and SQL Server to improve performance for production scenarios.
Cross-platform data tools
In addition to
sqlcmd, you can use the following cross-platform tools to manage SQL Server:
|CROSS-PLATFORM DATA TOOLS|
|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.|
Connecting from Windows
SQL Server tools on Windows connect to SQL Server instances on Linux in the same way they would connect to any remote SQL Server 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. Just verify that you use the target Linux machine name or IP address rather than localhost, and make sure that TCP port 1433 is open.
For other tools that run on Windows but connect to SQL Server on Linux, see:
- SQL Server Management Studio (SSMS)
- Windows PowerShell
- SQL Server Data Tools (SSDT)