CHAR(n) or CHARACTER(n):
This data type is used to store the fixed length of string data. The length value will be provided at the time of table creation. The field of the table that contains char data will fill up the unused space with space characters. So, this data type is better to use for storing data of fixed length such as id of fixed length, phone number of a particular format, etc. The error will appear if the user tries to store string data larger than the length of the field of char data.
VARCHAR(n) or CHARACTER VARYING(n):
This data type is used to store the variable length of string data. The length will be provided at the time of table creation like the char data. The unused space of the field that contains this data type will remain unused and takes the space occupied by the string data only. The error will appear if the user tries to store string data larger than the length of the field of varchar data.
TEXT or VARCHAR:
This data type is used to store string data of unlimited length. So, the length value is not defined at the time of table creation. This is not SQL standard data type and it is removed from some relational database management systems (RDBMS) such as Oracle, MS SQL Server, etc.
You have to install the latest version of PostgreSQL packages on the Linux operating system before executing the SQL statements shown in this tutorial. Run the following commands to install and start the PostgreSQL:
$ sudo systemctl start postgresql.service
Run the following command to login to PostgreSQL with root permission:
Use of character data types in PostgreSQL:
Before creating any table with the Boolean data type you have to create a PostgreSQL database. So, run the following command to create a database named ‘testdb’:
The following output will appear after creating the database:
Create a table:
Run the following SQL statement to create a table named clients in a PostgreSQL database by using different types of character data types. Here, the first field name is id which is the primary key of the table. The value of this field will be incremented automatically when a new record will insert. The name of the second field is name and the data type of this field is varchar which will be able to store a maximum of 15 characters. The name of the third field is address and the data type of this field is text. The name of the fourth field is email and the data type of this field is character varying that will be able to store maximum of 30 characters. The fifth field is phone and the data type is char which will store the string data of 14 characters.
id serial PRIMARY KEY,
name varchar (15) NOT NULL,
address text NOT NULL,
email character varying (30),
phone char (14) NOT NULL );
The following output will appear if the table is created successfully:
Insert data into the table:
Run the following insert query with the valid data to insert a record into the clients table:
VALUES ('AR Company', '12/A, Dhanmondi, Dhaka.', 'email@example.com', '+8801826783423');
The following output will appear if the insert query is executed successfully:
Run the following insert query to insert a record into the clients table. Here the value of the name field exceeds the size of the field. So, an error message will appear.
VALUES ('RFL Company Ltd.', '23, Motijeel, Dhaka.', 'firstname.lastname@example.org', '+8801927891278');
The following output will appear after executing the above insert query. Here, the name field can store a maximum of 15 characters but 16 characters are given in the query.
Run the following insert query to insert a record into the clients table. Here the value of the phone field exceeds the size of the field. So, an error message will appear.
VALUES ('Vision', '190/1, Mirpur, Dhaka.', 'email@example.com', '+880152678342312');
The following output will appear after executing the above insert query. Here, the phone field can store 14 characters but 16 characters are given in the query.
Read data from the table:
Only one insert query has been executed successfully after executing the above three insert queries. Now, run the following select query to retrieve all records from the clients table:
The following output will appear after executing the above query:
The character data type is a very useful data type of the PostgreSQL table. Some fields of the table require to store fixed-length of string data and some fields of the table require the variable length of data. The uses of three different data types of PostgreSQL tables have been explained in this tutorial by creating a table. The new PostgreSQL users will get proper knowledge about the character data types and will be able to use them properly for creating tables after reading this tutorial.