Introduction to Oracle CHAR Data Type
The Oracle CHAR
data type is used to store fixed-length strings. Unlike the VARCHAR2
data type, data of CHAR
type is automatically padded to a fixed length with space characters.
Syntax
When defining a CHAR
type, you need to specify a fixed length, for example: CHAR(10)
.
Use Cases
The CHAR
data type is used when you need to store strings of fixed length. For example, storing information such as bank account numbers, social security numbers, etc., where the length of the information is fixed and does not change with data variations.
Examples
Here are two examples of using the CHAR
data type:
Example 1
Create a table person
with a CHAR
type column name
:
CREATE TABLE person (
id NUMBER,
name CHAR(20)
);
Insert a record with the name “Alice”:
INSERT INTO person VALUES (1, 'Alice');
Query the records:
SELECT * FROM person;
The query result is:
ID NAME
-- --------------------
1 Alice
Example 2
Create a table student
with a CHAR
type column student_id
:
CREATE TABLE student (
name VARCHAR2(20),
student_id CHAR(10)
);
Insert a record with the name “Bob” and student ID “1234567890”:
INSERT INTO student VALUES ('Bob', '1234567890');
Query the records:
SELECT * FROM student;
The query result is:
NAME STUDENT_ID
------ ----------
Bob 1234567890
Conclusion
The Oracle CHAR
data type is used to store fixed-length strings, where the remaining space is padded with space characters. It is used when you need to store strings of fixed length.