搜尋
首頁資料庫mysql教程Mysql tinyint(1)與tinyint(4)的差別是什麼

什麼是tinyint(M)?

先來了解下mysql中字串型別varchar(M) 與數值型別tinyint(M) 的差異?
字串列類型: varchar(M) 而言,M 是可以儲存在欄位中的最大字元長度,也就是說是欄位長度。根據設置,當你插入超出字段長度的數據時,你很可能會收到錯誤提示,即使沒有收到錯誤提示,你插入的數據也會被自動截斷以適應該字段的預定義長度。所以,varchar(20) 和 varchar(40) 是不同的,其真實反映了該欄位可以儲存的資料長度
數值列類型:其長度修飾符表示最大顯示寬度,與該欄位實體儲存沒有任何關係。也就是說,tinyint(1) 和tinyint(4) 能夠儲存的數值範圍都是-128…127 (or for unsigned values 0…255),他們是相同的資料型,當然他們還是有一點差異,以下會有說明。
對於 tinyint 資料類型,只佔 1 個位元組

- 無符號的(unsigned),範圍為0 到255,預設長度是3。
- 有符號的(signed),範圍是 -128 到 127,預設長度是 4。

範圍演算法:tinyint佔1個位元組,一個位元組8 位,也就是1*8=8,可以表示的數字個數是2的8 次方(2^8 = 256個數字)。

區別:若使用了 zerofill,當實際長度無法達到指定的顯示長度時,就會用 0 在前面補齊。 (簡記zerofill作用就是補零)

測試

先建立一張測試表,對 tinyint 類型都使用 zerofill。

CREATE TABLE `pre_demo` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  `unsigned_t` tinyint(3) unsigned zerofill NOT NULL DEFAULT '000',
  `signed_t` tinyint(4) unsigned zerofill NOT NULL DEFAULT '0000',
  `t1` tinyint(1) unsigned zerofill NOT NULL DEFAULT '0',
  `t2` tinyint(2) unsigned zerofill NOT NULL DEFAULT '00',
  `t3` tinyint(3) unsigned zerofill NOT NULL DEFAULT '000',
  `t4` tinyint(4) unsigned zerofill NOT NULL DEFAULT '0000',
  `t5` tinyint(5) unsigned zerofill NOT NULL DEFAULT '00000',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

然後,插入測試資料。

NSERT INTO pre_demo VALUES(NULL,8,8,8,8,8,8,8);
INSERT INTO pre_demo VALUES(NULL,123,123,123,123,123,123,123);

最後,查詢資料表中的資料。

mysql> SELECT * FROM pre_demo;
+----+------------+----------+-----+-----+-----+------+-------+
| id | unsigned_t | signed_t | t1  | t2  | t3  | t4   | t5    |
+----+------------+----------+-----+-----+-----+------+-------+
|  1 |        008 |     0008 |   8 |  08 | 008 | 0008 | 00008 |
|  2 |        123 |     0123 | 123 | 123 | 123 | 0123 | 00123 |
+----+------------+----------+-----+-----+-----+------+-------+
2 rows in set (0.00 sec)
mysql> SELECT *,LENGTH(id),LENGTH(unsigned_t),LENGTH(t1) FROM pre_demo;
+----+------------+----------+-----+-----+-----+------+-------+------------+--------------------+------------+
| id | unsigned_t | signed_t | t1  | t2  | t3  | t4   | t5    | LENGTH(id) | LENGTH(unsigned_t) | LENGTH(t1) |
+----+------------+----------+-----+-----+-----+------+-------+------------+--------------------+------------+
|  1 |        008 |     0008 |   8 |  08 | 008 | 0008 | 00008 |          1 |                  3 |          1 |
|  2 |        123 |     0123 | 123 | 123 | 123 | 0123 | 00123 |          1 |                  3 |          3 |
+----+------------+----------+-----+-----+-----+------+-------+------------+--------------------+------------+
2 rows in set (0.00 sec)

以上是Mysql tinyint(1)與tinyint(4)的差別是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
如何在MySQL中刪除或修改現有視圖?如何在MySQL中刪除或修改現有視圖?May 16, 2025 am 12:11 AM

todropaviewInmySQL,使用“ dropviewifexistsview_name;” andTomodifyAview,使用“ createOrreplaceViewViewViewview_nameAsSelect ...”。 whendroppingaview,asew dectivectenciesanduse和showcreateateviewViewview_name;“ tounderStanditSsstructure.whenModifying

MySQL視圖:我可以使用哪些設計模式?MySQL視圖:我可以使用哪些設計模式?May 16, 2025 am 12:10 AM

mySqlViewScaneFectectialized unizedesignpatternslikeadapter,Decorator,Factory,andObserver.1)adapterPatternadaptSdataForomDifferentTablesIntoAunifiendView.2)decoratorPatternenhancateDataWithCalcalcualdCalcalculenfields.3)fieldfields.3)

在MySQL中使用視圖的優點是什麼?在MySQL中使用視圖的優點是什麼?May 16, 2025 am 12:09 AM

查看InMysqlareBeneForsImplifyingComplexqueries,增強安全性,確保dataConsistency,andOptimizingPerformance.1)他們simimplifycomplexqueriesbleiesbyEncapsbyEnculatingThemintoreusableviews.2)viewsEnenenhancesecuritybyControllityByControllingDataAcces.3)

如何在MySQL中創建一個簡單的視圖?如何在MySQL中創建一個簡單的視圖?May 16, 2025 am 12:08 AM

toCreateAsimpleViewInmySQL,USEthecReateaTeviewStatement.1)defitEtheetEtheTeViewWithCreatEaTeviewView_nameas.2)指定usethectstatementTorivedesireddata.3)usethectStatementTorivedesireddata.3)usetheviewlikeatlikeatlikeatlikeatlikeatlikeatable.views.viewssimplplifefifydataaccessandenenanceberity but consisterfort,butconserfort,consoncontorfinft

MySQL創建用戶語句:示例和常見錯誤MySQL創建用戶語句:示例和常見錯誤May 16, 2025 am 12:04 AM

1)foralocaluser:createUser'localuser'@'@'localhost'Indidendify'securepassword'; 2)foraremoteuser:creationuser's creationuser'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Rocaluser'@'localhost'Indidendify'seceledify'Securepassword'; 2)

在MySQL中使用視圖的局限性是什麼?在MySQL中使用視圖的局限性是什麼?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他們不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinsOrsubqueries.2)他們canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

確保您的MySQL數據庫:添加用戶並授予特權確保您的MySQL數據庫:添加用戶並授予特權May 14, 2025 am 12:09 AM

porthusermanagementinmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素會影響我可以在MySQL中使用的觸發器數量?哪些因素會影響我可以在MySQL中使用的觸發器數量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)複雜的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境