Character Data Types in SQL: NCHAR, NVARCHAR2, VARCHAR2, and VARCHAR

CHAR (Character):

  • CHAR is a fixed-length character data type.
  • The column will always have a length of 10 characters in length
  • Padding: If the data is shorter than 10 characters, the database will pad it with spaces to make it the specified length.
  • Space efficieny: They are bad for data of varying lengths, howver, theya re good for data with consistent length such as codes and identifiers

Not good for strings that are not of fixed length as they waste database storage.

Example:

CREATE TABLE Countries (
CountryCode CHAR(2),
CountryName CHAR(50)
);

 

 

VARCHAR (Variable Character):

      • VARCHAR is another variable-length character
      • data type similar to VARCHAR2.
      • computer decides the coding system


CREATE TABLE BlogPosts (
PostID NUMBER,
Title VARCHAR(200),
Content VARCHAR(4000)
);

 

VARCHAR2 (Variable Character):

    • VARCHAR2 is a variable-length character data type
    • Suitable for storing text-based data.
    • It efficiently uses storage space, consuming only the exact number of bytes needed for the data.

CREATE TABLE Customers (
CustomerID NUMBER,
FirstName VARCHAR2(50),
LastName VARCHAR2(50),
Email VARCHAR2(100)
);

NVARCHAR2 (National Variable Character):

  • NVARCHAR2 is a variable-length,
  • National character data type.
  • It allows each column to store a variable number of characters, making it beneficial for handling data of varying lengths, such as names, addresses, or comments.

CREATE TABLE Products (
ProductID NUMBER,
ProductName NVARCHAR2(255),
Description NVARCHAR2(4000)
);

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top