INT
INT is a datatype in SQL that is used to store integer values. It can store both positive and negative numbers. The storage size of INT is 4 bytes and its range goes from -2,147,483,648 to 2,147,483,647.
Example
CREATE TABLE Employees ( EmployeeID INT, FirstName VARCHAR(255), LastName VARCHAR(255));
INSERT INTO Employees (EmployeeID, FirstName, LastName)VALUES (1, 'John', 'Doe');
SELECT * FROM Employees;
Output
+------------+-----------+----------+| EmployeeID | FirstName | LastName |+------------+-----------+----------+| 1 | John | Doe |+------------+-----------+----------+
Explanation
In the example above, an Employees
table is created with the EmployeeID
column of INT
data type, and the FirstName
and LastName
columns of VARCHAR
data type. One new record is inserted into the Employees
table where the EmployeeID
is 1
, the FirstName
is John
and the LastName
is Doe
. The *
in the SELECT
statement indicates that all rows of the Employees
table are displayed in the output.
Example
CREATE TABLE Employees ( ID INT, NAME TEXT);
INSERT INTO Employees (ID, NAME)VALUES (1, 'John Doe');
SELECT * FROM Employees;
Output
ID | NAME----|---------- 1 | John Doe
Explanation
In the above example, an Employees
table is created with two columns: ID
and NAME
. The ID
column is declared as INT
to hold integer values representing the unique identifiers for each record. A record is inserted into the table with ID
being 1
and NAME
being John Doe
. The final SELECT
statement displays all records in the Employees
table where the ID
is an integer.
Example
DECLARE @TestINT INT;SET @TestINT = 20;SELECT @TestINT AS TestValue;
Output
TestValue20
Explanation
In the example, we declare a variable ‘@TestINT’ of INT type and assign it a value of 20. The SELECT statement is then used to output the value of ‘@TestINT’, which is 20.
Example
CREATE TABLE Person ( ID INT PRIMARY KEY, FirstName VARCHAR(100), LastName VARCHAR(100));
INSERT INTO Person (ID, FirstName, LastName)VALUES (1, 'John', 'Doe');
Output
Statement processed.1 row created.
Explanation
The data type INT
is used for integer values. In the example, ID
is a data column with the integer (INT
) data type. It stores the primary key for each record in the Person
table. After the table is successfully created, we insert one row with the ID value of 1.
Example
CREATE TABLE Employees ( ID INT PRIMARY KEY, Name TEXT NOT NULL, Age INT NOT NULL);
INSERT INTO Employees (ID, Name, Age)VALUES (1, 'John Doe', 32);
SELECT * FROM Employees;
Output
ID | Name | Age---| -------- | ---1 | John Doe | 32
Explanation
In the example code, a table called ‘Employees’ is created with three columns: ID, Name, and Age. The INT data type is used for both the ID, which is declared as the primary key, and the Age. Then, an employee with ID 1, name ‘John Doe’, and age 32 is inserted into the table. The final command selects and outputs all records from the ‘Employees’ table.