SMALLINT

SMALLINT is a numeric data type in SQL. It's used to store integers, but with less storage size compared to other integer data types. SMALLINT values are whole numbers, and their range and precision may vary between different SQL databases. The typical range is from -32768 to 32767.

Example

CREATE TABLE Employees (
ID SMALLINT,
NAME TEXT
);
INSERT INTO Employees (ID, NAME) VALUES (1, 'John Doe');

Output

Query OK, 0 rows affected (0.01 sec)

Explanation

The code creates a table Employees with two columns: ID and NAME. The ID field is a SMALLINT type, which can hold a value between -32768 and 32767. A new row is inserted into the table with ID as 1 and name as ‘John Doe’. The output indicates the query was executed successfully without affecting any previous rows.

Example

CREATE TABLE Employees (
id SMALLINT,
name VARCHAR (20)
);
INSERT INTO Employees (id, name)
VALUES (1, 'John Doe');

Output

SELECT * FROM Employees;
id | name
----+---------
1 | John Doe

Explanation

In the example, a table called Employees is created with a column id of the data type SMALLINT, and a column name of the type VARCHAR. Then an entry is inserted into the Employees table with an id of 1 and a name of ‘John Doe’. The output displays the inserted data.

Example

CREATE TABLE SampleTable (
ID SMALLINT
);
INSERT INTO SampleTable (ID) VALUES (5), (10), (15);
SELECT * FROM SampleTable;

Output

ID
---
5
10
15

Explanation

In the given example, a table SampleTable is created with a single column ID of type SMALLINT. Then three values (5, 10, 15) are inserted into the ID column of SampleTable. The SELECT statement fetches all records from SampleTable. The SMALLINT data type stores whole numbers between -32768 and 32767.

Example

CREATE TABLE Employees (
ID SMALLINT,
Name VARCHAR(100)
);
INSERT INTO Employees (ID, Name) VALUES (1, 'John Doe');
INSERT INTO Employees (ID, Name) VALUES (2, 'Jane Doe');
SELECT * FROM Employees;

Output

ID Name
-------
1 John Doe
2 Jane Doe

Explanation

In the example, a table named Employees is created with ID as a SMALLINT and Name as VARCHAR(100). After that, two rows are inserted into the table with ID and Name values. Finally, all rows from the Employees table are selected and displayed.

Example

CREATE TABLE SampleTable (ID SMALLINT);
INSERT INTO SampleTable(ID) VALUES(10);
SELECT * FROM SampleTable;

Output

ID
--
10

Explanation

This example creates a table named ‘SampleTable’ with a single column ‘ID’ of type ‘SMALLINT’. A row is inserted with an ‘ID’ of 10. The SELECT statement retrieves the contents of the ‘SampleTable’ which shows the inserted row.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.