首頁  >  文章  >  資料庫  >  在 MySQL 中使用 INT(1) 與 TINYINT(1) 有差別嗎?

在 MySQL 中使用 INT(1) 與 TINYINT(1) 有差別嗎?

PHPz
PHPz轉載
2023-08-30 08:13:08795瀏覽

在 MySQL 中使用 INT(1) 与 TINYINT(1) 有区别吗?

括號中使用的數字1僅用於寬度顯示。 INT(1)和TINYINT(1)不影響儲存。

TINYINT佔用1個位元組,這意味著它的範圍是-128到127,而int佔用4個位元組;它的範圍是-2147483648 到2147483647

要了解寬度顯示,讓我們建立一個表格-

mysql> create table intAndTinyint
   −> (
   −> FirstNumber int(1) zerofill,
   −> SecondNumber tinyint(1) zerofill
   −> );
Query OK, 0 rows affected (0.52 sec)

現在您可以在表格中插入記錄。查詢如下 -

mysql> insert into intAndTinyint values(1,1);
Query OK, 1 row affected (0.32 sec)

mysql> insert into intAndTinyint values(12,12);
Query OK, 1 row affected (0.26 sec)

mysql> insert into intAndTinyint values(123,123);
Query OK, 1 row affected (0.14 sec)

使用 select 語句顯示表中的所有記錄。查詢如下 -

mysql> select *from intAndTinyint;

以下是輸出 -

+-------------+--------------+
| FirstNumber | SecondNumber |
+-------------+--------------+
|           1 |            1 |
|          12 |           12 |
|         123 |          123 |
+-------------+--------------+
3 rows in set (0.00 sec)

當括號的數字 1 透過填零增加到大於 1 時,您就會明白這一點。讓我們來看一個僅用於 INT 的範例來理解寬度填零的概念。

建立一個表格。以下是建立表格的查詢 -

mysql> create table intVsIntAnyThingDemo
   −> (
   −> Number1 int(11) unsigned zerofill,
   −> Number int(13) unsigned zerofill
   −> );
Query OK, 0 rows affected (1.17 sec)

現在您可以藉助插入命令在表格中插入記錄。這裡,我們為INT設定了不同的寬度。查詢如下 -

mysql> insert into intVsIntAnyThingDemo values(12345,6789);
Query OK, 1 row affected (0.44 sec)

mysql> insert into intVsIntAnyThingDemo values(3,2);
Query OK, 1 row affected (0.20 sec)

mysql> insert into intVsIntAnyThingDemo values(12,89);
Query OK, 1 row affected (0.15 sec)

mysql> insert into intVsIntAnyThingDemo values(123,6789);
Query OK, 1 row affected (0.17 sec)

mysql> insert into intVsIntAnyThingDemo values(1234,6789);
Query OK, 1 row affected (0.14 sec)

借助select語句顯示所有記錄。查詢如下 -

mysql> select *from intVsIntAnyThingDemo;

以下是顯示不同寬度和零填充的輸出

+-------------+---------------+
| Number1     | Number        |
+-------------+---------------+
| 00000012345 | 0000000006789 |
| 00000000003 | 0000000000002 |
| 00000000012 | 0000000000089 |
| 00000000123 | 0000000006789 |
| 00000001234 | 0000000006789 |
+-------------+---------------+
5 rows in set (0.00 sec)

以上是在 MySQL 中使用 INT(1) 與 TINYINT(1) 有差別嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除