postgresql
  1. postgresql-smalllint

SMALLINT - (PostgreSQL Data Types)

PostgreSQL offers a variety of data types to store data in a table. One of these data types is SMALLINT, which allows you to store small integer values in a table. In this tutorial, we'll discuss the syntax, example, output, explanation, use, important points, and summary of the SMALLINT data type in PostgreSQL.

Syntax

The syntax for creating a column of type SMALLINT in a table is as follows:

column_name SMALLINT

Example

Let's create a students table with a student_id column of type SMALLINT.

CREATE TABLE students (
  student_id SMALLINT,
  first_name VARCHAR(50),
  last_name VARCHAR(50),
  age SMALLINT,
  PRIMARY KEY (student_id)
);

Output

After executing the above CREATE TABLE statement, a new table named students will be created with four columns: student_id, first_name, last_name, and age.

Explanation

In the example above, we created a students table with a student_id column of type SMALLINT. This column will store small integer values that range from -32768 to +32767.

The SMALLINT data type uses 2 bytes of storage and is optimized for small integer values. It is a signed integer data type, which means that it can store both positive and negative values.

Use

The SMALLINT data type is useful for storing small integer values in a table. It is ideal for columns that store values that are too large for the TINYINT data type but too small for the INT or BIGINT data types.

Important Points

  • SMALLINT can store values that range from -32768 to +32767.
  • SMALLINT uses 2 bytes of storage.
  • SMALLINT is a signed integer data type.
  • SMALLINT can be used in arithmetic operations, just like any other integer data type.
  • SMALLINT columns can be used as primary and foreign keys in a table.

Summary

In this tutorial, we discussed the SMALLINT data type in PostgreSQL. We covered the syntax, example, output, explanation, use, and important points of the SMALLINT data type. This data type is useful for storing small integer values in a table and is ideal for columns that store values that are too large for the TINYINT data type but too small for the INT or BIGINT data types.

Published on: