八、mysql学习笔记——mysql格式化(五)

mysql学习笔记(五)

上一节学习了mysql的alter语句、索引/主键等添加/更改操作、以及表单内重复数据的处理,本节将学习mysql的null值处理、正则化、序列(有序数列)等。

 

1、NULL值处理

在mysql中,没有“=Null、!=Null”等判断语句,只有“is null"、“is not null”、“<=>”等判断语句,当符合条件时返回True,否则返回为空“empty”。

 

2、正则表达式

mysql的正则表达式与PHP/Perl类似,使用REGEXP后跟固定结构来匹配:

select column_name from table_name where column_name REGEXP '...';

以下是固定结构:

模式 描述
^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的 Multiline 属性,^ 也匹配 '\n' 或 '\r' 之后的位置。
$ 匹配输入字符串的结束位置。如果设置了RegExp 对象的 Multiline 属性,$ 也匹配 '\n' 或 '\r' 之前的位置。
. 匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用象 '[.\n]' 的模式。
[...] 字符集合。匹配所包含的任意一个字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'。
[^...] 负值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'。
p1|p2|p3 匹配 p1 或 p2 或 p3。例如,'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 则匹配 "zood" 或 "food"。
* 匹配前面的子表达式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。* 等价于{0,}。
+ 匹配前面的子表达式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等价于 {1,}。
{n} n 是一个非负整数。匹配确定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o。
{n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。

例如:

select name from tcount_tbl where name REGEXP '^mi'; 匹配以mi开头的字符串
select name from tcount_tbl where name REGEXP 'ok$'; 匹配以ok结尾的字符串

select name from tcount_tbl where name REGEXP '[an]'; 匹配含‘an’中任一字符的字符串

select name from tcount_tbl where name REGEXP '[^an]'; 排除只含‘an’中任一字符的字符串(即只有a或n组成),匹配剩下的字符串
select name from tcount_tbl where name REGEXP 'o{2}'; 匹配以含连续两个o的字符串
select name from tcount_tbl where name REGEXP 'o{1,2}'; 匹配含o最少一次最多两次的字符串
select name from tcount_tbl where name REGEXP '^[mi]|ok$'; 匹配以’mi‘中任一字符开头或以’ok‘结尾的字符串

 


3、mysql序列

通常的序列用来对id(整数)操作,常用的几个操作如下:

使用auto_increment来实现id自增:

  • 在创建表时:create table table_name(id bigint not null auto_increment,primary key(id),...);
  • 当误删部分数据需要对id重新排序时,重置序列(需要三步):alter table table_name drop id;

                                                                              alter table table_name add id bigint not null auto_increment first;

                                                                              alter table table_name add primary key(id);

  • 或者直接设置序列额定开始值:engine=innodb auto_increment=100 charset=utf8;

 

 

 

你可能感兴趣的:(mysql学习笔记,mysql)