数据类型分类
数值类型
tinyint类型
create database test_db;
use test_db;
建表时一定要跟着写上属性
mysql> create table if not exists t1(
-> num tinyint
-> );
//查看
desc t1;
show tables;
show create table t1\G;
//无符号整数
mysql> create table if not exists t2(
-> num tinyint unsigned
-> );
bit类型
bit[(M)] : 位字段类型。M表示每个值的位数,**范围从1到64**。如果M被忽略,默认为1
mysql> create table if not exists t3(
-> id int,
//一个bit位,只能插0或插1
-> online bit(1)
-> );
浮点数类型
float
mysql> create table if not exists t4(
-> id int,
-> salary float(4,2)
-> );
create table if not exists t5(id bigint, salary float(4,2) unsigned);
alter table t5 modify salary float;
insert into t5 values (1,23456789.234526614);//ok
insert into t5 values (1,2349.234526);
insert into t5 values (1,23429.234526);
select * from t5;
decimal
decimal(m, d) [unsigned] : 定点数m指定长度,d表示小数点的位数
mysql> create table if not exists t6(
-> f1 float(10,8),
-> f2 decimal(10,8)
-> );
总结:
字符串类型
char
char(L): 固定长度字符串,L是可以存储的长度,单位为字符,最大长度值可以为255
varchar
varchar(L): 可变长度字符串,L表示字符长度,最大长度65535个字节
mysql> create table t8(
-> id int,
-> name varchar(6)
-> );