Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

MySQL: ความแตกต่าง BIGINT และ BIGINT(20) คือ Display Width


คำอธิบาย

BIGINT และ BIGINT(20) เก็บค่าได้เท่ากัน (8 ไบต์, -9,223,372,036,854,775,808 ถึง 9,223,372,036,854,775,807) ความแตกต่างเพียง Display Width (ความกว้างการแสดงผล) เมื่อใช้ร่วมกับ ZEROFILL

ขั้นตอนการสร้างตาราง BIGINT(20) ZEROFILL

mysql> create table DemoTable ( 
    Number bigint(20) zerofill ); 
Query OK, 0 rows affected (0.58 sec)

การเพิ่มข้อมูล

mysql> insert into DemoTable values(1); 
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable values(12); 
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(123); 
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(1234); 
Query OK, 1 row affected (0.11 sec)

แสดงข้อมูล (Padding ด้วย 0 นำหน้า)

mysql> select * from DemoTable;

ผลลัพธ์ (ความกว้าง 20 ตัวอักษร):

+----------------------+
| Number               |
+----------------------+
| 00000000000000000001 |
| 00000000000000000012 |
| 00000000000000000123 |
| 00000000000000001234 |
+----------------------+
4 rows in set (0.00 sec)

คำอธิบาย

  • (20) = Display Width 20 ตัวอักษร
  • ZEROFILL = เติม 0 นำหน้าให้ครบ Display Width
  • UNSIGNED ถูกเพิ่มอัตโนมัติเมื่อใช้ ZEROFILL
  • ไม่มีผลต่อขนาดเก็บข้อมูล (Storage) หรือ Range ของค่า

Display Width ใน Integer Types

TypeDefault Display Width
TINYINT4
SMALLINT6
MEDIUMINT9
INT11
BIGINT20

ข้อควรระวัง

  • Display Width ถูก Deprecated ใน MySQL 8.0.17+ (ยังทำงานแต่เตือน Warning)
  • ZEROFILL ก็ Deprecated เช่นกัน
  • แนะนำจัดรูปแบบใน Application Layer แทน (LPAD, FORMAT, printf)
  • ไม่ส่งผลต่อ JOIN, INDEX, CALCULATION