PostgreSQL – INTEGER Data Type
PostgreSQL allows a type of integer type namely INTEGER
. It requires 4 bytes of storage size and can store integers in the range of -2, 147, 483, 648 to 2, 147, 483, 647. It comes in handy for storing data like the population of a country, the number of active users on a social media app, etc.
Syntax: variable_name INTEGER
Now let’s look into some examples of use cases of INTEGER integer type.
Example 1:
In this example we will create a table that stores the population of various countries by using the below commands:
CREATE TABLE countries_population(
country_id SERIAL PRIMARY KEY,
name VARCHAR (255) NOT NULL,
population INTEGER NOT NULL CHECK (population> 0)
);
Now let’s add some data to the table using the below command:
INSERT INTO countries_population(name, population)
VALUES
('India', 1352600000),
('Russia', 14450000),
('Canada', 37600000),
('Japan', 126500000);
Now let’s check our inserted data using the below commands:
SELECT * FROM countries_population;
Output:
Example 2:
In this example we will create a table that stores the number of active users on various social media apps by using the below commands:
CREATE TABLE social_media(
id SERIAL PRIMARY KEY,
name VARCHAR (255) NOT NULL,
active_users INTEGER NOT NULL CHECK (active_users> 0)
);
Now let’s add some data to the table using the below command:
INSERT INTO social_media(name, active_users)
VALUES
('Facebook', 249279585),
('Twitter', 330000000),
('Instagram', 1070000000),
('Linkedin', 210000000);
Now let’s check our inserted data using the below commands:
SELECT * FROM social_media;
Output:
Last Updated :
28 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...