Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL: Character Data Types

PostgreSQL supports character types such as CHARACTER VARYING (n) or VARCHAR(n), CHARACTER(n) or CHAR(n), and TEXT, where n is a positive integer.

CHAR(n) and VARCHAR(n) are both SQL-supported data types. TEXT is the one supported by PostgreSQL.

Character Data TypeDescription
CHARACTER VARYING (n) or VARCHAR(n)Variable length with limit
CHARACTER(n) or CHAR(n)Fixed length with blank padded
TEXT, VARCHARVariable unlimited length

CHAR(n) and VARCHAR(n)

  • Both CHAR(n) and VARCHAR(n) can store up to n characters.
  • CHAR(n) is a fixed-length data type so if you store characters less than n in the column, it will pad the string with a blank before storing it.
  • VARCHAR(n) is a variable length data type so if you store characters less than n in the column, it will store the string as it is.
  • If you try to store more than n characters in CHAR(n) or VARCHAR(n), it will raise an error. However, if excessive characters are all spaces, then PostgreSQL truncates spaces up to length n and stores the characters.
  • If you explicitly cast a string to CHAR(n) or VARCHAR(n), then PostgreSQL will truncate a string to n characters before storing them.
  • If you specify CHAR without length (n), by default it will take as CHAR(1).
  • If you specify VARCHAR without length (n), it behaves just like the TEXT data type.

Let's create Person table with CHAR, VARCHAR and TEXT fields.

Example: Character Type
CREATE TABLE Person (
	id serial PRIMARY KEY,
	name VARCHAR(15),
	gender CHAR,
	address TEXT
);

Then, insert a row in Product table like below

Example: Character Type
INSERT INTO person(name, gender, address)
VALUES('Anie Smith', 'Female',' Keaton Underwood Ap #636-8081 Arc Avenue Thiensville Maryland 19689');

PostgreSQL clearly raises an error as the value is too large for the CHARACTER(1). This is because we specified CHAR for gender column that set gender column as CHARACTER(1) by default and we are trying to insert 5 character value 'Female' to it.

Now let's try to insert name as 'Anie Smith ' with six trailing spaces after name and gender as 'F' with four trailing spaces like 'F ' and try to insert again.

Example: Character Type
INSERT INTO person(name, gender, address)
VALUES('Anie Smith      ', 'F    ',' Keaton Underwood Ap #636-8081 Arc Avenue Thiensville Maryland 19689');

This time value got inserted as PostgreSQL truncated trailing spaces for name and gender columns and stored only character values 'Anie Smith' as name and 'F' in the gender columns.

The above query result shows the data type of each column along with the data. You can see the address column is a text data type and we could store large text in it.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.