该【(完整)Mysql燕十八学习笔记 】是由【花开花落】上传分享,文档一共【93】页,该文档可以免费在线阅读,需要了解更多关于【(完整)Mysql燕十八学习笔记 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。Mysql三大列类型
数值型
整型
Tinyint、Smallint、Mediumint、Int、Bigint
小数型
Float(D,M),Decimal(D,M)
字符串型
Char(M)
Varchar(M)
Text文本类型
日期时间类型
Date日期
Time时间
Datetime时间类型
Year年类型
整型列的字节与存储范围
从数学上讨论tinyint,建表时既能足够存放,又不浪费
空间。
即:
.占据空间
.存储范围
Tinyint微小的列类型,1字节。
类型
字节
最小值
最大值
(带符号/无符号)
(带符号/无符号)
Tinyint
1
-128
127
0
255
Smallint
2
-32768
32767
0
65535
Mediumint
3
-2八23
2八23-1
0
2八24-1
Int
4
-2八31
2八31-1
0
2八32-1
Bigint
8
-2八63
2八63-1
0
2八64-1
通过占用字节和所存范围分关系合理的选择类型。
例:Tinyint
mysql>createtabletest(
->idintprimarykeyauto_increment,
->nameuarcharCIO),
->agetinyint)chars©t=utfS;QueryOK,6rowsaffected()
ysql>insertintotest(name,ag$)ualuesC'张三LNQ。,);******@ryOK,1rowaffected,1warning()
ysql>select我fromtest; + + +
idInameIageI
I张三 I25I
I束三 I127I
+ + +
rowsinset()
my$ql>insertintotest(name,age)values('-129');Quer^jOK,1rowaffected,1warning(”)
my$ql>select笑fromtest;
+ + + +
Iid|name|ageI
三三三
长长长
nyJI^J司J
578
222
1—1—
3rowsinset()
经测试,Tinyint的默认为有符号-128~127之间,当越界时自动取相应的边界值。若要存0~255无符号时:列的可选属性:
Tinyint(M)unsignedzerofill.
M:宽度(在0填充的时候才有意义)
Unsigned:无符号类型(非负),影响存储范围
Zerofill:0填充(默认无符号)
lmyGql>******@3tinyint(5)zerofill;QueryOK,Srowsaffected()Records:SDuplicates:0Warnings:0
|mysql>desctest:
+ + + + + +
IField I Type I NullI KeyI DefaultI Extra
I id I inti;11j I NO IPRII NULL Iautt>_incr&nentI
I name I uarchar(10j I YES I I MULL I I
I age I tinyint(4) I VES I I NULL I I
I 眄但1 I tinyint(3] umigE I YES I I NULL I I
I mg" I tinyintf1) I YES I I NULL I I
I agc3 | tinyint(5) ursignedzerofill| VES I I MULL I I
6row$)
insertintotest(name,age1)ualues(nameI
张三OK,1rowaffected()
insertintotest(nam@,age1)ualue9(1李四','-100');
OK,1rowaffected,1warning()
t
s
e
t
m
^1
e
9
a
L
LuN
L
L
u
N
7
2
1—
L
L
u
N
8
2
1—
L
LuN
7
2
-1—
L
LU
N
三三三三四三四张张张张李张李
1234567
7rowsinset()
1>insertintotest
->(name,age3)
->ualu&s
->('起六’,’5,);
gOK,1rowaffected()
xFromtest;
IidInameI
三三三三四三四五六-张张张张李张李王赵-
123456739-
I25 I NULL I NULL I NULLI
I127 I NULL I NULL I NULLI
I-128 I NULL I NULL I NULLI
I127 | NULL | NULL | NULL|
I-10G I NULL I NULL I NULLI
INULL | 200 | NULL | NULL |
INULL I 0 I NULL I NULLI
INULL I NULL I 127 I NULLI
INULL I NULL I NULL I 00005I
rows
()
列可以声明默认值,而且推荐声明默认值notnulldefault0;
ysql>altertabletestaddag©4tinyint(3)notnulldefault0:ueryOK,9rowsaffected()ecords:9Duplicates:0learnings:0
InameI
123456789
INULL
INULL
INULL
INULL
INULL
INULL
INULL
INULL
INULL
INULL
I127
INULL
INULL
INULL
INULL
INULL
INULL
INULL
INULL
INULL
IOOQOS
0I
0I
0I
0I
0I
0I
0I
0I0I
INULL
INULL
INULL
INULL
INULL
小数型/浮点型、定点型
Float(M,D):M代表总位数,D代表小数位
(6,2)为例:-->
my$ql>createtablete&t2(
->nameuarchar(IG)notnulldefault'',
->pricefloat(6,2)
">);
QueryOK,6rowsaffected()
mysql>******@******@st2;
IField | Type I Null IKeyI DefaultIExtraI
+ + 十 + + 十 +
\\name I uarcharf10) I NO I I I I
price I float(6?2) I NO I I I I
+ + + + + + +
rowsinset()
mysql>insertintotest2(name,price)ualues('ueaglefly';'');()
mysql>select甚FROMtest2;
+ + +
Iname I price I
+ + +
Iueaglefly I I
+- + +
1rowinset()
Decimal(D,M):
mysql>altertabletest2
->add
->bigpricedeci帕1(9,2);QueryOK,0rowsaffected()Records:0Duplicates:0Darnings:0
mgsql>altertabletest?
->add
->decipricedecimal(3,2);QueryOK,0rowsaffected()Records:6Duplicates:0Warnings:6
mysql>desctest2;+ + + + + + 4
IField I Type I Null IKeyI Default IExtraI
+ + + + + + 4
Iname I uarchar(10) I NO I I I I
Iprice I float(6,2) I NO I I I I
Ibigprice I decimal(9,2) I NO I I I I
Ideciprice I decimal(9,2) I NO I I I I
+ + + + + + 4
4rowsinset()
mysql>insertintotest2
->(name,bigprice,deciprice)values
->(.自行车1/);
Query0K,1rowaffected()
mysql>select«FROMtest2;
InameIpric©IbigpriceIdecipriceI+ + + + +
1rowinset(&ec)
Char:定长,char(M),M代表宽度,即:可容纳的字符数0<=M<=255
Varchar:变长,0<=M<=65535(约2W—6W个字符受字符集影响)
Text文本串,(约2W—6W个字符受字符集影响)
区别在哪呢?
char定长:M个字符,如果存的小于M个字符,实占M个字符varchar:M个字符,存的小于M个字符,设为N,N<M,实占N个
字符
加蛔MlM代表贯度,)
类型
夷型
宽度
可莓字得
卖存字冲俱
实占芝闾
利用率
Char
IUE
L
i/M<-iotm
Varchar
i事符序节
或1+1-2)4IQQ%
cmOu
祯0UM如0(血T型加果不拶回个宇苻,内盛用空格补齐)
因此,char定长若存空格,读取时会丢失。而变长不会。
my9ql>insertintostu
->(name,waihao)->values
->('ueagleflij1,'veaglef1g');QueryOK,1rowaffected,ZwarningsC©.00sec)
mysql>selectconcat(namQ,'*')^oncatCwaihao,'!'Jfromstu;+ + +
Iconcat(name,'?')Iconcat(waihao,'?')I+ h +
I张三? L小三? I
Izhang^an! I小三! I
Izhangsan! I小三! I
Iueaglef1! Iueaglefly• I
+ + +
4rowsinset()
Char利用率小于等于100%,而varchar永远小于100%,1-2个字节用于标志实存字符长度。
Char(M)如何占据M个字符宽度?
答:如果实际存储内容不足M个,则后面加空格补齐,取出来的时候再把后面的空格去掉,(所以如果内容后面有空格,将会被清除)选择原则:
空间利用效率(M固定选Char)
速度
速度上:定长速度快些,
(完整)Mysql燕十八学习笔记 来自淘豆网m.daumloan.com转载请标明出处.