PostgreSQL SMALLINT

In this section, we are going to understand the working of the PostgreSQL Smallint data type. And we also see examples of the Smallint data type.

What is PostgreSQL Smallint Data Type?

In PostgreSQL, the next integer data type that we are going to understand is SMALLINT.

The SMALLINT data type small range integer involves 2 bytes of storage size and store integers in the signed range and unsigned range.

And the Signed range starts from -32768 to 32767. And the unsigned range began at 0 to 65535. The size parameter is used to define the maximum display width that is 255.

Note: PostgreSQL does not allow us to use the unsigned integer types.

Typically, the SMALLINT data type is used to store only a partial range of records, such as the number of pages in a book, age of a person, and so on, as compared to other built-in numeric data types.

Suppose if a number is beyond the range of the Maximum and Minimum SMALLINT values. The database server does not store the data value, but as an alternative occurring an error message.

Syntax of PostgreSQL SMALLINT data type

The syntax of the PostgreSQL SMALLINT data type is as follows:

Examples of PostgreSQL Smallint data type

Let us see different examples to understand how the PostgreSQL Smallint data type works.

We are creating one new table with the CREATE command's help and inserting some values using the INSERT command.

In the following example, we are going to create Employee_age into a javatpoint database by using the CREATE command to store employee's age.

Output

After executing the above command, we will get the below message: The Employee_age table has been successfully created, as shown in the below screenshot:

PostgreSQL SMALLINT

Once the Employee_age table has been generated, we can insert some values into it using the INSERT command.

Output

After executing the above command, we will get the below message: the particular value has been inserted successfully in the Employee_age table.

PostgreSQL SMALLINT

As we can see in the above screenshot, the multiple values have been inserted successfully into the Employee_age table.

After creating and inserting the Employee_age table's values, we will use the SELECT command to return all rows of the Employee_age table:

Output

After successfully implementing the above command, we will get the below output:

PostgreSQL SMALLINT

Example2

Let us see one more example to learn the Smallint data type in detail.

So, we are going to create another new table as Book_pages table with the help of CREATE command into a similar database that is javatpoint to store the numbers of pages in a Book_pages table:

Output

The Book_pages table has been successfully created after executing the above commands, as shown in the below screenshot:

PostgreSQL SMALLINT

Note: In the above command, we added a CHECK constraint to implement the number of pages of a book that must be positive, as the pages_in_book column is a SMALLINT column.

Once the Book_pages table has been generated, we will insert some values into it using the INSERT command as shown in the below command:

Output

We will get the following message on implementing the above command: the value has been inserted successfully into the Book_pages table.

PostgreSQL SMALLINT

After creating and inserting the Book_pages table's values, we will use the SELECT command to return all rows of the Book_pages table:

Output

After implementing the above command, we will get the following output as shown in the below screenshot:

PostgreSQL SMALLINT

Overview

In the PostgreSQL Smallint data type section, we have learned that it stores only a limited range of records such as the number of pages in a book, person's age.






Latest Courses