1 Installation
1.1 Add the Microsoft SQL Server 2019 repository
The Microsoft SQL database server packages are available on Red Hat repository which needs to be added manually.
sudo curl https://packages.microsoft.com/config/rhel/8/mssql-server-2019.repo -o /etc/yum.repos.d/mssql-server-2019.repo
sudo curl https://packages.microsoft.com/config/rhel/8/prod.repo -o /etc/yum.repos.d/msprod.repo
1.2 Installation SQL Server on Centos 8
Run the following commands to install SQL Server:
sudo yum install -y mssql-server
In order to use sqlcmd we need to link the MS SQL tools into a location accessible to all users:
sudo ln -s /opt/mssql-tools/bin/* /usr/local/bin/
1.3 Configuration
After the package installation finishes, run mssql-conf setup and follow the prompts to set the SA password and choose your edition.
sudo /opt/mssql/bin/mssql-conf setup
After that, we need to verify that the mssql service is running:
systemctl enable mssql-server systemctl status mssql-server
1.4 Install the SQL Server command-line tools
To create a database, you need to connect with a tool that can run Transact-SQL statements on the SQL Server.
Run the following command to install the mssql-tools on Centos:
yum install -y mssql-tools unixODBC-devel
2 Basic examples
List of the common and basic examples of how to use SQL Server using terminal.
2.1 Connection to SQL Server
sqlcmd -S localhost -U SA
2.2 Create database
From the sqlcmd command prompt, paste the following Transact-SQL command to create a test database:
CREATE DATABASE SampleDB
The previous command does not run immediately. You must type GO on a new line to execute the previous command:
GO
From the sqlcmd command prompt, switch context to the new SampleDB database:
USE SampleDB GO
2.3 Create table
Example of how to create a table with 3 columns.
CREATE TABLE Users (id INT, name NVARCHAR(50), last_name NVARCHAR(50)) GO
2.4 Select, Insert, Update, Delete
Insert of two rows in the table Users.
INSERT INTO Users VALUES (1, 'John', 'Smith'); INSERT INTO Users VALUES (2, 'Robert', 'Miller'); GO
Select of all the columns in the table Users.
SELECT * FROM Users GO
Update column last_name where user id is 1.
UPDATE Users SET last_name = 'Williams' WHERE id = 1 GO
Delete row where id is equal to 2.
DELETE FROM Users WHERE id = 2 GO