標籤:mysql;mysql資料類型
跟王老師學MySQL:MySQL資料類型之整數類型
主講教師:王少華 QQ群號:483773664
MySQL的資料類型包括整數類型、浮點數類型、定點數類型、日期和時間類型、字串類型和位元據類型。
學習目標
整型包括哪些類型以及它們的取值範圍
以tinyint為例,講解了定義整型的三個屬性
一、簡介
整數類型是資料庫中最基本的資料類型。
標準SQL中支援INTEGER和SMALLINT這兩類整數類型。
MySQL資料庫除了支援這兩種類型以外,還擴充支援了TINYINT、MEDIUMINT和BIGINT。
整數類型最主要掌握其取值範圍
二、取值範圍
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M02/83/A5/wKiom1d5vIHQGLqEAABb6J5Eogk661.png" alt="wKiom1d5vIHQGLqEAABb6J5Eogk661.png" />
三、以tinyint為例(一)列的設計原則
夠存放:存放的範圍
不浪費:佔據空間
(二) 建立一張表
1 2 3 4 5 |
create table student(
id int primary key auto_increment,
name varchar(10),
age tinyint
) charset utf8;
|
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M01/83/A4/wKioL1d5vIHBWCO-AAAL3a3QVJ4377.png" alt="wKioL1d5vIHBWCO-AAAL3a3QVJ4377.png" />
(三) 插入資料1 插入正常的值
1 |
insert into student(name,age)values(‘zhangsan‘,25);
|
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M01/83/A5/wKiom1d5vIHQIzGDAAAZ9GbRMAE343.png" alt="wKiom1d5vIHQIzGDAAAZ9GbRMAE343.png" />
2 插入大於範圍的值
1 |
insert into student(name,age)values(‘lisi‘,200);
|
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M00/83/A4/wKioL1d5vIGRezmcAAAJ_QGflHY504.png" alt="wKioL1d5vIGRezmcAAAJ_QGflHY504.png" />
3 插入邊界值
1 |
insert into student(name,age)values(‘lisi‘,-128);
|
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M02/83/A4/wKioL1d5vIGhfkZcAAAI0YMfAoY733.png" alt="wKioL1d5vIGhfkZcAAAI0YMfAoY733.png" />
(四)整數型的可選屬性
1 |
資料類型 (顯示寬度) unsigned zerofill
|
顯示寬度:在0填充時才有意義
unsigned:無符號類型(非負)
zerofill:0填充
1 unsigned
為student添加一個無符號類型的列
1 |
alter table student add age2 tinyint unsigned;
|
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M00/83/A5/wKiom1d5vIGhmOgpAAAm0EcIz3k678.png" alt="wKiom1d5vIGhmOgpAAAm0EcIz3k678.png" />
為age2添加-1
1 |
insert into student(name,age,age2)values(‘lisi‘,24,-1);
|
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M01/83/A4/wKioL1d5vIKzoelSAAAJubGVJrA956.png" alt="wKioL1d5vIKzoelSAAAJubGVJrA956.png" />
添加0
1 |
insert into student(name,age,age2)values(‘lisi‘,24,0);
|
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M02/83/A5/wKiom1d5vIKBT04CAAAIwZR_pTw384.png" alt="wKiom1d5vIKBT04CAAAIwZR_pTw384.png" />
所以unsigned是大於等於0
2、顯示寬度
這隻是顯示寬度,跟儲存範圍一點關係都沒有
1 |
alter table student add age3 tinyint(1);
|
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M00/83/A4/wKioL1d5vILhXLdRAAAook4nelM892.png" alt="wKioL1d5vILhXLdRAAAook4nelM892.png" />
插入age3=100
1 |
insert into student(name,age3)values(‘lisi‘,100);
|
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M01/83/A5/wKiom1d5vILS8b7EAAAIXncnhog125.png" alt="wKiom1d5vILS8b7EAAAIXncnhog125.png" />
3 zerofill
零填充 00001 00005
如果某列是zerofill,預設是unsigned
添加一列
1 |
alter table student add age4 tinyint(5) zerofill;
|
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M02/83/A4/wKioL1d5vILCuvx_AAA4vxnuBJ8685.png" alt="wKioL1d5vILCuvx_AAA4vxnuBJ8685.png" />
插入資料age4=9
1 |
insert into student(name,age4)values(‘lisi‘,9);
|
650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M00/83/A5/wKiom1d5vIPhNwnzAAAj-hdzq5s943.png" alt="wKiom1d5vIPhNwnzAAAj-hdzq5s943.png" />
四、教學視頻
http://edu.51cto.com/course/course_id-6420.html
本文出自 “跟王老師學編程” 部落格,請務必保留此出處http://teacherwang.blog.51cto.com/10946447/1795473
跟王老師學MySQL:MySQL資料類型之整數類型