mysql在数据处理时还有一些其他操作。
NULL操作:在mysql中如果要取出一些值为NULL的数据,在python中我们经常使用的是‘=’或者‘!=’,但是在MySQL中,NULL=任何数都为false,所以不能使用,但是我们又必须取出为空的数据,那么我们就用到了mysql中的特殊判断方式:is null 和 is not null
语法:select * from 表名 where 字段名 is [not] null -->加上中括号中的not为取出不为空的数据
例:select * from taoche where tao is null
即:查询taoche表中tao字段不为空的数据
利用正则表达式筛选所需数据:在mysql中我们可以使用‘REGEXP’后面跟正则表达式来查询所需数据。
语法:select * from 表名 where 字段名 regexp ‘正则表达式’
例:select * from taoche where tao regexp ‘^路’
即:查询taoche表中的tao字段以‘路’开始的数据
alter命令:当我们需要修改表名或者字段名的时候,我们会使用到alter命令。
1、alter命令的add添加子句:alter table 表名 add 字段名 字段类型 -->这是给表中添加一个字段
例:alter table taoche add num int
即:给taoche表中添加int类型的num字段
2、alter命令的drop删除子句:alter table 表名 drop 字段名 -->删除表中的字段
例:alter table taoche drop tao
即:删除taoche表中的tao字段
3、alter命令的修改子句:modify和change两个
modity用法:alter table 表名 modify 字段名 字段类型
例:alter table taoche modify tao varchar(100)
即:将taoche表中的套字段类型改为长度为100的varchar
change用法:alter table 表名 change 旧字段名 新字段名 字段类型
例:alter table taoche change tao new_tao varchar(50)
即:将taoche表中的tao字段改为new_tao字段类型为长度为50的varchar
4、alter命令的set修改子句:alter table 表名 alter 字段名 set default 默认值
例:alter table taoche alter tao set default ‘奥迪’
即:修改taoche表中的tao字段的默认值为‘奥迪’
alter子句与drop连用删除字段的默认值
例:alter table taoche alter tao drop default
即:删除了taoche表中tao字段的默认值
这是一些数据库的其他操作,虽不常用但是也需要了解,以备不时之需。