Mysql数据类型和运算符(二)

xiaoxiao2021-02-28  142

一、定点数与浮点数类型

无论是定点数还是浮点数 如果用户指定的类型精度超过指定精度范围吗,会四舍五入进行处理

//创建表并想表中插入数据 mysql> create table tb_emp5 (x float(5,1), y double(5,1),z decimal(5,1)); Query OK, 0 rows affected

mysql> insert into tb_emp5 values(5.12,5.15,5.123); Query OK, 1 row affected

mysql> desc tb_emp5; +——-+————–+——+—–+———+——-+ | Field | Type | Null | Key | Default | Extra | +——-+————–+——+—–+———+——-+ | x | float(5,1) | YES | | NULL | | | y | double(5,1) | YES | | NULL | | | z | decimal(5,1) | YES | | NULL | | +——-+————–+——+—–+———+——-+ 3 rows in set

mysql> select *from tb_emp5; +—–+—–+—–+ | x | y | z | +—–+—–+—–+ | 5.1 | 5.2 | 5.1 | +—–+—–+—–+ 1 row in set

二、日期与时间类型

Mysql中日期的数据类型:datetime date timestamp time year

1、yea类型r //删除表中的数据,并重新添加 mysql> delete from tb_emp6; Query OK, 3 rows affected

mysql> select *from tb_emp6; Empty set

mysql> insert into tb_emp6 values(‘0’),(‘99’),(‘12’),(‘34’),(‘00’),(‘11’),(08); Query OK, 7 rows affected Records: 7 Duplicates: 0 Warnings: 0

mysql> select *from tb_emp6; +——+ | y | +——+ | 2000 | | 1999 | | 2012 | | 2034 | | 2000 | | 2011 | | 2008 | +——+ 7 rows in set

2、time类型

time类型用于只需要时间的值 格式‘HH:MM:SS’

指定格式 D HH:MM:SS HH:MM D HH:MM D HH或SS

Mysql中‘1122’和1122表示00:11:22,Time中‘11:22’ 表示11:22:00

//创建time表并插入数据 mysql> create table tb_emp7 (t time); Query OK, 0 rows affected

mysql> insert into tb_emp7 values (‘11:05:05’),(‘23:23’),(‘2 10:10’),(‘302’),(‘10’); Query OK, 5 rows affected Records: 5 Duplicates: 0 Warnings: 0

mysql> select *from tb_emp7; +———-+ | t | +———-+ | 11:05:05 | | 23:23:00 | | 58:10:00 | | 00:03:02 | | 00:00:10 | +———-+ 5 rows in set

//插入当前时间

mysql> insert into tb_emp7 values (current_time),(now()); Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0

mysql> select *from tb_emp7; +———-+ | t | +———-+ | 22:51:17 | | 22:51:17 | | 22:51:43 | | 22:51:43 | +———-+ 4 rows in set

3、data类型

//插入date类型数据 mysql> insert into tb_emp8 values (‘1996-07-28’),(‘2010-10-10’); Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0

mysql> select *from tb_emp8; +————+ | d | +————+ | 1996-07-28 | | 2010-10-10 | +————+ 2 rows in set

mysql> insert into tb_emp8 values (‘000101’),(‘991010’); Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0

mysql> select *from tb_emp8; +————+ | d | +————+ | 1996-07-28 | | 2010-10-10 | | 2000-01-01 | | 1999-10-10 | +————+ 4 rows in set //获取当前日期 mysql> insert into tb_emp8 values (current_date()),(now()); Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 1

mysql> select *from tb_emp8; +————+ | d | +————+ | 1996-07-28 | | 2010-10-10 | | 2000-01-01 | | 1999-10-10 | | 2017-08-06 | | 2017-08-06 | +————+ 6 rows in set

4.datetime类型

同时包含日期和时间 8字节 格式‘YY-MM-Dd HH-MM-SS’

//创建datatime并插入数据 mysql> create table tb_emp9 (dt datetime); Query OK, 0 rows affected

mysql> insert into tb_emp9 values (‘1995-10-10 08-08-08’),(‘19920303121212’); Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0

mysql> select *from tb_emp9; +———————+ | dt | +———————+ | 1995-10-10 08:08:08 | | 1992-03-03 12:12:12 | +———————+

//获取当前时间值 mysql> insert into tb_emp9 values (now()); Query OK, 1 row affected

mysql> select *from tb_emp9; +———————+ | dt | +———————+ | 1995-10-10 08:08:08 | | 1992-03-03 12:12:12 | | 2017-08-06 23:07:34 | +———————+ 3 rows in set

5.timestamp类型

与datetime的显示格式相同 存储占4字节 区别:范围小、存储以UTC(世界标准格式)保存 存储时对当前时区进行转换,检索时再转换回当前时区 即根据当前时区的不同,显示的时间不一样的

//timestamp创建与添加 mysql> create table tb_emp10 (ts timestamp); Query OK, 0 rows affected

mysql> insert into tb_emp10 values(‘98 03 03 03$03#03’); Query OK, 1 row affected

//获取当前时间 mysql> insert into tb_emp10 values(now()); Query OK, 1 row affected

mysql> select tb_emp10; 1054 - Unknown column ‘tb_emp10’ in ‘field list’ mysql> select *from tb_emp10; +———————+ | ts | +———————+ | 1998-03-03 03:03:03 | | 2017-08-06 23:17:15 | +———————+ 2 rows in set

//修改当前时区为东十区 mysql> set time_zone=’+10:00’; Query OK, 0 rows affected

mysql> select *from tb_emp10; +———————+ | ts | +———————+ | 1998-03-03 05:03:03 | | 2017-08-07 01:17:15 | +———————+ 2 rows in set

三、文本字符串类型

char和varchar text类型 enum类型

1.text类型

//创建并输入enm类型 mysql> insert into tb_emp3 values (‘first’),(‘second’),(‘third’),(null); Query OK, 4 rows affected Records: 4 Duplicates: 0 Warnings: 0

mysql> select *from tb_emp3; +——–+ | enm | +——–+ | first | | second | | third | | NULL | +——–+ 4 rows in set

//查看列表的索引 mysql> select enm,enm+0 from tb_emp3; +——–+——-+ | enm | enm+0 | +——–+——-+ | first | 1 | | second | 2 | | third | 3 | | NULL | NULL | +——–+——-+ 4 rows in set

2.ENU类型

//定义int型的soc ENUM类型的字段为level

ENUM列表在Mysql中以编号排列的,因此插入列表中的值与编号对应

mysql> create table tb_emp4 (soc int,level enum(‘excellent’,’good’,’bad’)); Query OK, 0 rows affected

mysql> insert into tb_emp4 values(70,’good’),(90,1),(75,2),(50,3); Query OK, 4 rows affected Records: 4 Duplicates: 0 Warnings: 0

//best不在ENUM列表中

mysql> insert into tb_emp4 values(100,’best’); 1265 - Data truncated for column ‘level’ at row 1

select *from tb_emp4’ at line 1 mysql> select *from tb_emp4; +—–+———–+ | soc | level | +—–+———–+ | 70 | good | | 90 | excellent | | 75 | good | | 50 | bad | +—–+———–+ 4 rows in set

3.set类型

字符串对象 其值为创建时规定的值 包含多个值set(‘值1’,’值2’,’值3’)

插入 set字段Mysql会自动删除重复的值

4.二进制字符串类型

BIN类型把数字转化为二进制

BINARY类型长度固定 VARBINARY类型长度可变

BLOB类型存储二进制大数据

四、运算符类型

算数运算符

BETWEEN ADD运算符expr BETWEEN min AND max 假如expr大于或等于min且小于等于max 返回1 否则0

位运算符。。。。太多了 省略一百行

转载请注明原文地址: https://www.6miu.com/read-68019.html

最新回复(0)