在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、数值类型1.1、数值类型分类
MySQL中数值类型:
1.1.1、浮点数浮点数如果不写精度和标度,则会按照实际精度值显示,如果有精度和标度,则会自动将四舍五入后的结果插入,系统不会报错;定点数如果不写精度和标度,则按照默认值 1.1.2、位类型BIT(位)类型:范围从1-64,如果不写,默认为1位,对于此字段,直接使用select命令将不会看到结果,可以用bin()(显示为二进制格式)或者hex()(显示为16进制格式)函数进行读取 例: mysql> desc t2; +-------+--------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------+------+-----+---------+-------+ | id | bit(1) | YES | | NULL | | +-------+--------+------+-----+---------+-------+ 1 row in set (0.00 sec) mysql> insert into t2 values(1); Query OK, 1 row affected (0.05 sec) mysql> select * from t2; +------+ | id | +------+ | | +------+ 1 row in set (0.00 sec) mysql> select bin(id),hex(id) from t2; +---------+---------+ | bin(id) | hex(id) | +---------+---------+ | 1 | 1 | +---------+---------+ 1 row in set (0.03 sec)
例:如果在刚刚的那张表中插入2,实际转换为二进制为10,超出了 mysql> insert into t2 values(2); ERROR 1406 (22001): Data too long for column 'id' at row 1 mysql> alter table t2 modify id bit(2); Query OK, 1 row affected (0.67 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into t2 values(2); Query OK, 1 row affected (0.03 sec) mysql> select bin(id),hex(id) from t2; +---------+---------+ | bin(id) | hex(id) | +---------+---------+ | 1 | 1 | | 10 | 2 | +---------+---------+ 2 rows in set (0.00 sec) 1.1.3、时间日期类型1.1.3、时间日期类型
例1:创建时间表(dt),字段分别为 date、time、datetime 三种日期类型,并插入数值,查看显示结果 mysql> create table dt(d date,t time,dt datetime); Query OK, 0 rows affected (0.23 sec) mysql> insert into dt values(now(),now(),now()); Query OK, 1 row affected, 1 warning (0.05 sec) mysql> select * from dt; +------------+----------+---------------------+ | d | t | dt | +------------+----------+---------------------+ | 2021-05-13 | 10:14:07 | 2021-05-13 10:14:07 | +------------+----------+---------------------+ 1 row in set (0.00 sec) 例2:创建测试表t,字段id1为TIMESTAMP类型,插入空值,并显示 mysql> create table t(id1 timestamp); Query OK, 0 rows affected (0.22 sec) mysql> insert into t values(null); Query OK, 1 row affected (0.05 sec) mysql> select * from t; +---------------------+ | id1 | +---------------------+ | 2021-05-13 10:18:05 | +---------------------+ 1 row in set (0.00 sec) 可以发现,系统给 id1 自动创建了默认值 例3:解释如上说明 mysql> alter table t add column id2 timestamp; Query OK, 0 rows affected (0.48 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table t \G; *************************** 1. row *************************** Table: t Create Table: CREATE TABLE `t` ( `id1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `id2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) ERROR: No query specified 1.1.4、字符串类型
例: mysql> create table varc(v varchar(4),c char(4)); Query OK, 0 rows affected (0.20 sec) mysql> insert into varc values('abc ','abc '); Query OK, 1 row affected (0.03 sec) mysql> select length(v),length(c) from varc; +-----------+-----------+ | length(v) | length(c) | +-----------+-----------+ | 4 | 3 | +-----------+-----------+ 1 row in set (0.01 sec) mysql> select concat(v,'+'),concat(c,'+') from varc; +---------------+---------------+ | concat(v,'+') | concat(c,'+') | +---------------+---------------+ | abc + | abc+ | +---------------+---------------+ 1 row in set (0.00 sec)
例: mysql> create table bina(c binary(3)); Query OK, 0 rows affected (0.22 sec) mysql> insert into t set c='a'; ERROR 1054 (42S22): Unknown column 'c' in 'field list' mysql> insert into bina set c='a'; Query OK, 1 row affected (0.05 sec) mysql> select *,hex(c),c='a',c='a\0',c='a\0\0' from bina; +------+--------+-------+---------+-----------+ | c | hex(c) | c='a' | c='a\0' | c='a\0\0' | +------+--------+-------+---------+-----------+ | a | 610000 | 0 | 0 | 1 | +------+--------+-------+---------+-----------+ 1 row in set (0.00 sec) 可以发现,当保存 1.1.5、ENUM 类型
例: mysql> create table gend(gender enum('M','F')); Query OK, 0 rows affected (0.20 sec) mysql> insert into gend values('M'),('F'),('1'),(null); Query OK, 4 rows affected (0.03 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from gend; +--------+ | gender | +--------+ | M | | F | | M | | NULL | +--------+ 4 rows in set (0.00 sec) 由此可以看出,枚举类型的插入值得时候,是忽略大小写的,初始下标从1开始;查询的时候会转换为大写,也可以插入空(null) 1.1.6、SET类型
1~8 成员的集合,占 1 个字节。 例: mysql> create table st(col set('a','b','c','d')); Query OK, 0 rows affected (0.20 sec) mysql> insert into st values('a,b'),('a,d,a'),('a,c'),('a'); Query OK, 4 rows affected (0.03 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> insert into st values('f'); ERROR 1265 (01000): Data truncated for column 'col' at row 1 mysql> insert into st values(null); Query OK, 1 row affected (0.03 sec) mysql> select * from st; +------+ | col | +------+ | a,b | | a,d | | a,c | | a | | NULL | +------+ 5 rows in set (0.00 sec) 由此可以看出,set是可以插入多个成员的,也可以插入null,插入不存在的定义列时,会报异常插入失败 |
请发表评论