Introduction to Oracle NCHAR Data Type
In Oracle Database, NCHAR
is a character data type used to store fixed-length strings in the Unicode character set. Unlike the CHAR
type, which uses the character set of the current database, NCHAR
type uses the Unicode character set.
Syntax
The following syntax can be used to define a column of NCHAR
type when creating a table:
column_name NCHAR(size) [CHARACTER SET Unicode_name]
Where column_name
is the name of the column, size
is the maximum number of characters for that column, and Unicode_name
is the name of the Unicode character set, which defaults to AL16UTF16
.
Use Cases
Since NCHAR
type supports the Unicode character set, it is suitable for storing data in different languages or special character sets, such as international applications, multilingual websites, etc.
Additionally, since NCHAR
type has a fixed length, it is suitable for scenarios where string comparison or sorting is required.
Examples
Here are two examples of using NCHAR
type:
Example 1
Create an employees
table with a last_name
column and a first_name
column, both of which are of NCHAR
type:
CREATE TABLE employees (
last_name NCHAR(20),
first_name NCHAR(10)
);
Insert a record into this table:
INSERT INTO employees VALUES ('Adam', 'Alice');
Query the table:
SELECT * FROM employees;
The result is:
LAST_NAME FIRST_NAME
-------------------- ----------
Adam Alice
Example 2
Create a books
table with a title
column and an author
column, both of which are of NCHAR
type:
CREATE TABLE books (
title NCHAR(50),
author NCHAR(20)
);
Insert a record into this table:
INSERT INTO books VALUES ('Harry Potter', 'J.K. Rowling');
Query the table:
SELECT * FROM books;
The result is:
TITLE AUTHOR
-------------- --------------------
Harry Potter J.K. Rowling
Conclusion
NCHAR
type is suitable for storing data in different languages or special character sets, as well as scenarios where string comparison or sorting is required. When creating a table, the maximum number of characters for the column and the name of the Unicode character set should be specified.