postgresql
  1. postgresql-character

Character - (PostgreSQL Data Types)

In PostgreSQL, the character data type is used to store character strings of varying lengths. In this tutorial, we'll discuss the syntax, example, output, explanation, use, important points, and summary of the character data type in PostgreSQL.

Syntax

CHARACTER VARYING(n) or VARCHAR(n)

The n represents the maximum number of characters that can be stored in this field.

Example

Let's create a table with a VARCHAR column:

CREATE TABLE employees (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100)
);

INSERT INTO employees (name)
VALUES ('John Doe'), ('Jane Smith'), ('Bob Johnson');

Output

Executing the above SQL statements will create a table called employees with two columns, id and name. The output will show the table creation confirmation message.

Explanation

In the above example, we used the VARCHAR data type to define the name column in the employees table. We set the maximum length of the string to 100 characters.

A VARCHAR data type is used to store a variable-length string of characters. The n in the syntax represents the maximum length of the string that can be stored in that column.

When inserting data into the table, we provided a string value for the name column. The value of the string was within the maximum limit of the VARCHAR data type specified in the table definition.

Use

The character data type in PostgreSQL is used to store character strings of varying lengths.

It is commonly used for storing names, addresses, titles, email addresses, and other textual data.

Important Points

  • The VARCHAR data type is used to store a variable-length string of characters.
  • The maximum length of a VARCHAR field must be specified when creating a table.
  • If the length of the string exceeds the maximum length specified, the string will be truncated.
  • PostgreSQL also provides the CHAR data type for storing fixed-length strings of characters.

Summary

In this tutorial, we discussed the CHARACTER data type, or VARCHAR, in PostgreSQL. We covered the syntax, example, output, explanation, use, and important points of the CHARACTER data type. With this knowledge, you can now create tables and store character string data in PostgreSQL.

Published on: