在 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)
現在,您可以在表的幫助下使用 insert 命令插入記錄。在這裡,我們為 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)
廣告