Example
Let's create a table to store names in VARCHAR. This is useful when you are only storing English or other ASCII-based characters.
CREATE TABLE Employees_Varchar (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50)
);
-- Inserting data with English names
INSERT INTO Employees_Varchar (EmployeeID, FirstName, LastName)
VALUES (1, 'John', 'Doe'),
(2, 'Alice', 'Smith');
NVARCHAR
stores data using
Unicode, which uses a multi-byte character encoding. Each character
typically requires 2 bytes (or more in some encodings, like UTF-8), allowing
it to store a much wider range of characters.NVARCHAR
supports Unicode
characters, which means it can store text from virtually any language
(including languages with complex characters, like Chinese, Arabic, etc.).Example
Now, let's create a similar table but use NVARCHAR. This is beneficial when you need to store multilingual data.
CREATE TABLE Employees_Nvarchar (
EmployeeID INT PRIMARY KEY,
FirstName NVARCHAR(50),
LastName NVARCHAR(50)
);
-- Inserting data with names in English and Arabic
INSERT INTO Employees_Nvarchar (EmployeeID, FirstName, LastName)
VALUES (1, 'John', 'Doe'),
(2, 'أحمد', 'العلي'); -- Arabic text in the names