MySQL中的int和integer有什么区别?

MySQL中的int和integer有什么区别?

MySQL 5.0中,int是integer的同义词。这里演示int和integer在内部都表示int(11)。

创建一个具有int数据类型的表格

mysql> create table IntDemo
   -> (
   -> Id int
   -> );
Query OK, 0 rows affected (1.04 sec)

这是表格的描述。查询如下:

mysql> desc IntDemo;

以下是输出结果

+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| Id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.06 sec)

看一下列类型,即int(11),现在它存储了与integer定义的相同范围。插入记录的查询如下

mysql> insert into IntDemo values(2147483647);
Query OK, 1 row affected (0.20 sec)

mysql> insert into IntDemo values(-2147483648);
Query OK, 1 row affected (0.42 sec)

使用select语句显示表中的所有记录。查询如下:

mysql> select *from IntDemo;

以下是输出结果

+-------------+
| Id          |
+-------------+
| 2147483647  |
| -2147483648 |
+-------------+
2 rows in set (0.00 sec)

使用数据类型integer创建表格。

创建表格的查询如下:

mysql> create table IntegerDemo
   -> (
   -> Id integer
   -> );
Query OK, 0 rows affected (0.93 sec)

使用desc命令检查表格的描述。

mysql> desc IntegerDemo;

以下是输出结果

+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| Id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

使用insert命令向表格中插入记录。整数的范围与int相同。查询如下:

mysql> insert into IntegerDemo values(2147483647);
Query OK, 1 row affected (0.11 sec)

mysql> insert into IntegerDemo values(-2147483648);
Query OK, 1 row affected (0.27 sec)

使用select语句显示表中的所有记录。查询如下:

mysql> select *from IntegerDemo;

以下是输出结果

+-------------+
| Id          |
+-------------+
| 2147483647  |
| -2147483648 |
+-------------+
2 rows in set (0.00 sec)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程