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
sqlcmd
command prompt, paste the following Transact-SQL command to create a test database:
SQL
CREATE DATABASE TestDB
- On the next line, write a query to return the name of all of the databases on your server:
SQL
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:
SQL
GO
Insert data
Next create a new table, Inventory, and insert two new rows.
- From the
sqlcmd
command prompt, switch context to the new TestDB database:
SQL
USE TestDB
- Create new table named Inventory:
SQL
CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
- Insert data into the new table:
SQL
INSERT INTO Inventory VALUES (1, ‘banana’, 150); INSERT INTO Inventory VALUES (2, ‘orange’, 154);
- Type GO to execute the previous commands:
SQL
GO
Select data
Now, run a query to return data from the Inventory table.
- From the
sqlcmd
command prompt, enter a query that returns rows from the Inventory table where the quantity is greater than 152:
SQL
SELECT * FROM Inventory WHERE quantity > 152;
- Execute the command:
SQL
GO
Exit the sqlcmd command prompt
To end your sqlcmd
session, type QUIT:
SQL
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 | |
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. |
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)