Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications.

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.

Copy
sudo curl https://packages.microsoft.com/config/rhel/8/mssql-server-2019.repo -o /etc/yum.repos.d/mssql-server-2019.repo
Copy
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:

Copy
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:

Copy
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.

Copy
sudo /opt/mssql/bin/mssql-conf setup
Choose an edition of SQL Server:
  1) Evaluation (free, no production use rights, 180-day limit)
  2) Developer (free, no production use rights)
  3) Express (free)
  4) Web (PAID)
  5) Standard (PAID)
  6) Enterprise (PAID) - CPU Core utilization restricted to 20 physical/40 hyperthreaded
  7) Enterprise Core (PAID) - CPU Core utilization up to Operating System Maximum
  8) I bought a license through a retail sales channel and have a product key to enter.

Details about editions can be found at
https://go.microsoft.com/fwlink/?LinkId=2109348&clcid=0x409

Use of PAID editions of this software requires separate licensing through a
Microsoft Volume Licensing program.
By choosing a PAID edition, you are verifying that you have the appropriate
number of licenses in place to install and run this software.

Enter your edition(1-8): 3
The license terms for this product can be found in
/usr/share/doc/mssql-server or downloaded from:
https://go.microsoft.com/fwlink/?LinkId=2104294&clcid=0x409

The privacy statement can be viewed at:
https://go.microsoft.com/fwlink/?LinkId=853010&clcid=0x409

Enter the SQL Server system administrator password: 
Confirm the SQL Server system administrator password: 
Configuring SQL Server...

ForceFlush is enabled for this instance. 
ForceFlush feature is enabled for log durability.
Setup has completed successfully. SQL Server is now starting.

After that, we need to verify that the mssql service is running:

Copy
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:

Copy
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

Copy
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:

Copy
CREATE DATABASE SampleDB

The previous command does not run immediately. You must type GO on a new line to execute the previous command:

Copy
GO

From the sqlcmd command prompt, switch context to the new SampleDB database:

Copy
USE SampleDB
GO

2.3 Create table

Example of how to create a table with 3 columns.

Copy
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.

Copy
INSERT INTO Users VALUES (1, 'John', 'Smith'); 
INSERT INTO Users VALUES (2, 'Robert', 'Miller');
GO

Select of all the columns in the table Users.

Copy
SELECT * FROM Users
GO
id          name                                               last_name                                         
----------- -------------------------------------------------- --------------------------------------------------
          1 John                                               Smith                                             
          2 Robert                                             Miller

Update column last_name where user id is 1.

Copy
UPDATE Users SET last_name = 'Williams' WHERE id = 1
GO

Delete row where id is equal to 2.

Copy
DELETE FROM Users WHERE id = 2
GO