In SQL, the INTEGER data type is used to represent a whole number value that does not have any fractional part. It is a commonly used data type for storing numerical data in databases, particularly when dealing with data that involves counting or measuring.
INTEGER | INT
The INTEGER data type can be specified with a variety of sizes, depending on the range of values that need to be stored. The most common sizes are:
INT This is the standard integer size, which typically uses 4 bytes of storage and can store values between -2147483648 and 2147483647.
SMALLINT This uses 2 bytes of storage and can store values between -32768 and 32767.
BIGINT This uses 8 bytes of storage and can store values between -9223372036854775808 and 9223372036854775807.
When creating a table in SQL, the INTEGER data type can be used to define a column that will store integer values. For example, the following SQL statement creates a table called “employees” with columns for “employee_id”, “first_name”, “last_name”, and “age”, where the “age” column is defined as an INTEGER:
CREATE TABLE employees ( employee_id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), age INT );
Once a column has been defined as an INTEGER, data can be inserted into the table using an INSERT statement. For example:
INSERT INTO employees (employee_id, first_name, last_name, age) VALUES (1, 'John', 'Doe', 30);
Queries can also be performed on INTEGER columns using various SQL statements, such as SELECT, WHERE, and ORDER BY. For example, the following SQL statement would select all employees with an age greater than 25:
SELECT * FROM employees WHERE age > 25;
In summary, the INTEGER data type is a commonly used data type for storing whole number values in SQL databases. It can be specified with a variety of sizes depending on the range of values that need to be stored, and can be used to define columns in tables for storing integer data.