CHARACTER

CHARACTER in SQL is a data type used to store alphanumeric characters. The length of the character string can be defined at the time of table creation or modification. The maximum length allowed is specific to each database software, but usually can contain hundreds of characters. It’s typically used when the length of the data entry is fixed.

Example

CREATE TABLE Employees (
ID int,
Name CHARACTER(30),
Age int
);
INSERT INTO Employees (ID, Name, Age)
VALUES (1, 'John Doe', 30);
SELECT * FROM Employees;

Output

+------+------------------+-----+
| ID | Name | Age |
+------+------------------+-----+
| 1 | John Doe | 30 |
+------+------------------+-----+

Explanation

In this example, a table named ‘Employees’ is created with three columns - ‘ID’ as an integer, ‘Name’ as character data type of length 30, and ‘Age’ as an integer. Then, a new row is inserted into the ‘Employees’ table with ‘ID’ as 1, ‘Name’ as ‘John Doe’ and ‘Age’ as 30. Finally, all the records from the ‘Employees’ table are selected. The output shows the records retrieved from the table.

Example

CREATE TABLE Employees (
ID SERIAL PRIMARY KEY,
NAME CHARACTER(15)
);
INSERT INTO Employees(NAME) VALUES ('Doerayme');

Output

INSERT 0 1

Explanation

This example demonstrates the application of the CHARACTER data type in PostgreSQL. The CREATE TABLE statement establishes a new table named ‘Employees’ with columns ‘ID’ and ‘NAME’. The ‘ID’ column is set as the primary key with sequential integers utilizing the SERIAL keyword. The ‘NAME’ column is defined by the CHARACTER(15) data type, which accommodates a string of characters up to 15 in length. With the INSERT INTO command, a row is populated with ‘Doerayme’ inserted into the ‘NAME’ column.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.