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 命令在表中插入記錄。integer 使用與 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)

更新於:2019年7月30日

2K+ 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.