Relational databases
SQL Server

SQL Server is Microsoft's relational database system. The Express edition is free and suitable for learning and small applications. When you install SQL Server, you get the SQL Server Configuration Manager for starting and stopping services, and optionally SQL Server Management Studio (SSMS) : a graphical client for managing databases and running queries.

For installation instructions to SQL Server Express and Management Studio, see the SQL Server Installation Guide.

SQL Server Configuration Manager

Use the Configuration Manager to start and stop SQL Server services.
Configuration Manager

The SQL Server service must be running whenever you want to use the database. If you want to connect from an application, the SQL Server Browser service must also be running.

SQL Server Management Studio

Use Management Studio to manage databases and run queries. On startup you will see the connection dialog below. Yyou can authenticate with your Windows account or with a SQL Server account such as sa.
Management Studio login

Differences from MySQL / PostgreSQL

SQL Server uses T-SQL (Transact-SQL), which extends standard SQL with its own syntax. If you are coming from MySQL or PostgreSQL, the differences below are the most important to know.

Data Types

T-SQL has some notable type differences compared to MySQL and PostgreSQL:

MySQL / PostgreSQLSQL ServerNotes
INT AUTO_INCREMENT / SERIALINT IDENTITY(1,1)Start value and increment can be set: IDENTITY(100,10)
BOOLEAN / TINYINT(1)BITStores 0 or 1; no true boolean type
VARCHAR(n)VARCHAR(n) / NVARCHAR(n)Use NVARCHAR for Unicode (multilingual) data
TEXTVARCHAR(MAX)Up to 2 GB; TEXT type is deprecated in SQL Server
DATETIME / TIMESTAMPDATETIME / DATETIME2DATETIME2 has higher precision and wider range; preferred
DOUBLEFLOATSQL Server also has REAL (4-byte) and DECIMAL(p,s) for exact values
TOP instead of LIMIT

SQL Server uses TOP to limit the number of returned rows, while MySQL and PostgreSQL use LIMIT:

-- MySQL / PostgreSQL
SELECT * FROM Customers LIMIT 10;

-- SQL Server
SELECT TOP 10 * FROM Customers;

-- SQL Server with percentage
SELECT TOP 10 PERCENT * FROM Customers;

For pagination (equivalent to LIMIT x OFFSET y), SQL Server uses OFFSET ... FETCH NEXT:

SELECT * FROM Customers
ORDER BY id_Customers
OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;
Date and Time Functions

SQL Server has its own date and time functions that differ from MySQL and PostgreSQL:

MySQLPostgreSQLSQL Server
NOW()NOW()GETDATE() / SYSDATETIME()
CURDATE()CURRENT_DATECAST(GETDATE() AS DATE)
DATE_ADD(d, INTERVAL n DAY)d + INTERVAL '1 day'DATEADD(day, n, d)
DATEDIFF(d1, d2)d1 - d2DATEDIFF(day, d2, d1)
YEAR(d), MONTH(d)EXTRACT(year FROM d)YEAR(d), MONTH(d), DAY(d)
Default Schema: dbo

SQL Server organizes objects into schemas, similar to PostgreSQL. The default schema is dbo (database owner). You can reference objects with or without the schema prefix:

SELECT * FROM dbo.Customers;  -- explicit schema
SELECT * FROM Customers;       -- dbo assumed by default

Create your own schema with:

CREATE SCHEMA sales;
CREATE TABLE sales.Orders ( ... );
Node.js Connection

The most common SQL Server client for Node.js is mssql. Install it with:

npm install mssql

Connect using a connection string and a connection pool:

const sql = require('mssql');

const pool = new sql.ConnectionPool({
  connectionString: 'Server=localhost\\SQLEXPRESS;Database=netshop;User Id=netuser;Password=netpass;TrustServerCertificate=true;',
});

async function getCustomers() {
  await pool.connect();
  const res = await pool.request()
    .input('city', sql.VarChar, '90100')
    .query('SELECT * FROM Customers WHERE postnumber = @city');
  return res.recordset;
}

SQL Server uses named parameters prefixed with @. The .input() call binds the value safely, preventing SQL injection. Use TrustServerCertificate=true in local development; in production, configure a proper certificate instead.

Adding the database from backup file

You can add the database to SQL Server from SQL-file or from bak-file.

Executing the SQL-file

Here is assumed that you have an sql-file(northwind.sql), which includes the code that is needed for creating the database.

      Create database Northwind

      Give the command in cmd:
      sqlcmd -S .\SQLEXPRESS  -d Northwind  -i northwind.sql
    

Executing the Bak-file

You can create and execute the Bak-file from Management Studio. The file should be located on directory, which something like:C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Backup

You can add the database like this

  • You don't have to create the database
  • Right click Databases
  • Choose Restore database
  • Choose Device and click the button with ellipses
  • Click Add and choose the right bak-file
  • Click several times OK

Creating the Bak-file

You can create the file like this:

  • Right click the desired database (in Management Studio)
  • Choose Tasks->Backup
Now in folder ...MSSQL\Backup should exists the new bak-file.

Example

Here is an example of how to create a database named netshop, a user named netuser, and grant that user access to the database.

  1. Start your server and connect with Management Studio (using user sa)
  2. Click the plus sign next to "Security"
  3. Right-click Logins and choose "New Login"
    • Fill in name: netuser
    • Choose SQL Server authentication
    • Fill in the password
    • Remove "Enforce password policy" (in production you can leave it active)
    • Click OK
  4. Right-click "Databases" and choose "New Database", fill in the name "netshop" and click OK
  5. Right-click netshop and choose Properties, then Files
  6. Click the ellipsis beside Owner and choose Browse
  7. Choose netuser as the owner and click OK

Log in as netuser and create tables in the netshop database.

  1. Login
  2. Click the plus sign left from Databases
  3. Right Click netshop and Choose New Query
  4. Copy-Paste below codes and execute them
    CREATE TABLE  Customers  (
       id_Customers  INT NOT NULL IDENTITY,
       first_name  varchar(50) DEFAULT NULL,
       last_name  varchar(50) DEFAULT NULL,
       street_address  varchar(255) DEFAULT NULL,
       postnumber  char(5) DEFAULT NULL,
      PRIMARY KEY ( id_Customers )
    );
    CREATE TABLE  Products  (
       id_Products  INT NOT NULL IDENTITY,
       product_name  varchar(50) DEFAULT NULL,
       manufacturer  varchar(255) DEFAULT NULL,
       model  varchar(255) DEFAULT NULL,
      PRIMARY KEY ( id_Products )
    ) ;
    CREATE TABLE  Orders  (
       id_Orders  INT NOT NULL IDENTITY,
       id_Products  INT NOT NULL DEFAULT '0',
       id_Customers  INT NOT NULL DEFAULT '0',
       order_day  date DEFAULT NULL,
       amount  INT DEFAULT NULL,
      PRIMARY KEY ( id_Orders ),
      FOREIGN KEY (id_Customers) REFERENCES Customers,
      FOREIGN KEY (id_Products) REFERENCES Products
    ) ;
    
    INSERT INTO Customers (first_name,last_name,street_address,postnumber) VALUES ('Jim','Smith','Kotkantie 1','90650');
    INSERT INTO Customers (first_name,last_name,street_address,postnumber) VALUES ('Lisa','Simpson','Uusikatu 1','90100');
    INSERT INTO Customers (first_name,last_name,street_address,postnumber) VALUES ('Ann','Jones','Uusikatu 4','90100');
    INSERT INTO Customers (first_name,last_name,street_address,postnumber) VALUES ('Bruce','Wayne','Aleksanterinkatu 4','01250');
    
    INSERT INTO Products (product_name,manufacturer,model) VALUES ('Laptop','Lenovo','Ideapad');
    INSERT INTO Products (product_name,manufacturer,model) VALUES ('Laptop','Asus','U31XU');
    INSERT INTO Products (product_name,manufacturer,model) VALUES ('Camera','Canon','G11');
    INSERT INTO Products (product_name,manufacturer,model) VALUES ('Navigator','TomTom','GoLive');
    
    INSERT INTO Orders (id_Products,id_Customers,order_day,amount) VALUES (4,1,'2013-05-20',1);
    INSERT INTO Orders (id_Products,id_Customers,order_day,amount) VALUES (2,2,'2013-05-18',1);
    INSERT INTO Orders (id_Products,id_Customers,order_day,amount) VALUES (3,3,'2013-05-17',2);
    INSERT INTO Orders (id_Products,id_Customers,order_day,amount) VALUES (1,4,'2013-05-16',1);
    INSERT INTO Orders (id_Products,id_Customers,order_day,amount) VALUES (3,1,'2013-05-15',1);
    INSERT INTO Orders (id_Products,id_Customers,order_day,amount) VALUES (4,2,'2013-05-14',3);
    INSERT INTO Orders (id_Products,id_Customers,order_day,amount) VALUES (2,3,'2013-05-13',1);
    INSERT INTO Orders (id_Products,id_Customers,order_day,amount) VALUES (2,4,'2013-05-12',1);
    



Toggle Menu