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.
Use the Configuration Manager to start and stop SQL Server services.
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.
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.
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.
T-SQL has some notable type differences compared to MySQL and PostgreSQL:
| MySQL / PostgreSQL | SQL Server | Notes |
|---|---|---|
INT AUTO_INCREMENT / SERIAL | INT IDENTITY(1,1) | Start value and increment can be set: IDENTITY(100,10) |
BOOLEAN / TINYINT(1) | BIT | Stores 0 or 1; no true boolean type |
VARCHAR(n) | VARCHAR(n) / NVARCHAR(n) | Use NVARCHAR for Unicode (multilingual) data |
TEXT | VARCHAR(MAX) | Up to 2 GB; TEXT type is deprecated in SQL Server |
DATETIME / TIMESTAMP | DATETIME / DATETIME2 | DATETIME2 has higher precision and wider range; preferred |
DOUBLE | FLOAT | SQL Server also has REAL (4-byte) and DECIMAL(p,s) for exact values |
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;
SQL Server has its own date and time functions that differ from MySQL and PostgreSQL:
| MySQL | PostgreSQL | SQL Server |
|---|---|---|
NOW() | NOW() | GETDATE() / SYSDATETIME() |
CURDATE() | CURRENT_DATE | CAST(GETDATE() AS DATE) |
DATE_ADD(d, INTERVAL n DAY) | d + INTERVAL '1 day' | DATEADD(day, n, d) |
DATEDIFF(d1, d2) | d1 - d2 | DATEDIFF(day, d2, d1) |
YEAR(d), MONTH(d) | EXTRACT(year FROM d) | YEAR(d), MONTH(d), DAY(d) |
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 ( ... );
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.
You can add the database to SQL Server from SQL-file or from bak-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
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 can create the file like this:
Here is an example of how to create a database named netshop, a user named netuser, and grant that user access to the database.
Log in as netuser and create tables in the netshop database.
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);