SQL Server INT Data Type
The INT
data type is one of the commonly used integer data types in SQL Server, which is used to store whole numbers. It supports a value range from -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647).
Syntax
The syntax for INT
data type is as follows:
INT
Usage
The INT
data type is usually used to store integer-type data, such as user IDs, ages, etc. When it is necessary to store integers smaller than -2^31 or larger than 2^31-1, the BIGINT
data type can be considered.
Example
The following is an example of using the INT
data type to store student scores:
CREATE TABLE StudentScore (
ID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Score INT NOT NULL
);
INSERT INTO StudentScore (ID, Name, Score)
VALUES (1, 'Alice', 90),
(2, 'James', 85),
(3, 'Tom', 95);
SELECT * FROM StudentScore;
In the example above, we created a table named StudentScore
, which contains three fields: ID, Name, and Score. The Score
field uses the INT
data type, which is used to store student scores. We inserted three records, each containing a student’s ID, name, and score. Finally, we used the SELECT
statement to query the contents of the StudentScore
table, with the following result:
ID | Name | Score |
---|---|---|
1 | Alice | 90 |
2 | James | 85 |
3 | Tom | 95 |
Conclusion
The INT
data type is one of the commonly used integer data types in SQL Server, which is used to store integer-type data. When it is necessary to store integers smaller than -2^31 or larger than 2^31-1, the BIGINT
data type can be considered. In practical applications, the appropriate data type should be selected based on actual needs.