MySQL·身份证校验

本文节选自《Netkiller Architect 手札》

4.17. 数据检查

4.17.1. 身份证校验

该函数能够检查身份证号码是否正确

CREATE DEFINER=`neo`@`%` FUNCTION `check_id_number`(`idnumber` CHAR(18))
	RETURNS enum('true','false')
	LANGUAGE SQL
	NOT DETERMINISTIC
	NO SQL
	SQL SECURITY DEFINER
	COMMENT ''
BEGIN
DECLARE status ENUM('true','false') default 'false';
DECLARE verify CHAR(1);
DECLARE sigma INT;
DECLARE remainder INT;

IF length(idnumber) = 18 THEN
	set sigma = cast(substring(idnumber,1,1) as UNSIGNED) * 7
		+cast(substring(idnumber,2,1) as UNSIGNED) * 9
		+cast(substring(idnumber,3,1) as UNSIGNED) * 10
		+cast(substring(idnumber,4,1) as UNSIGNED) * 5
		+cast(substring(idnumber,5,1) as UNSIGNED) * 8
		+cast(substring(idnumber,6,1) as UNSIGNED) * 4
		+cast(substring(idnumber,7,1) as UNSIGNED) * 2
		+cast(substring(idnumber,8,1) as UNSIGNED) * 1
		+cast(substring(idnumber,9,1) as UNSIGNED) * 6
		+cast(substring(idnumber,10,1) as UNSIGNED) * 3
		+cast(substring(idnumber,11,1) as UNSIGNED) * 7
		+cast(substring(idnumber,12,1) as UNSIGNED) * 9
		+cast(substring(idnumber,13,1) as UNSIGNED) * 10
		+cast(substring(idnumber,14,1) as UNSIGNED) * 5
		+cast(substring(idnumber,15,1) as UNSIGNED) * 8
		+cast(substring(idnumber,16,1) as UNSIGNED) * 4
		+cast(substring(idnumber,17,1) as UNSIGNED) * 2;
	set remainder = MOD(sigma,11);
	set verify = (case remainder
		when 0 then '1' when 1 then '0' when 2 then 'X' when 3 then '9'
		when 4 then '8' when 5 then '7' when 6 then '6' when 7 then '5'
		when 8 then '4' when 9 then '3' when 10 then '2' else '/' end
	);

END IF;

IF right(idnumber,1) = verify THEN
	set status = 'true';
END IF;

RETURN status;

END			

首先我们使用正确身份证号码进行测试,返回true

			mysql> select check_id_number('330702198003090915');
+---------------------------------------+
| check_id_number('330702198003090915') |
+---------------------------------------+
| true                                  |
+---------------------------------------+
1 row in set (0.01 sec)			

长度不符合18位直接返回false.

			mysql> select check_id_number('33070219800309');
+-----------------------------------+
| check_id_number('33070219800309') |
+-----------------------------------+
| false                             |
+-----------------------------------+
1 row in set (0.00 sec)	

mysql> select check_id_number('33070219800309091457889');
+--------------------------------------------+
| check_id_number('33070219800309091457889') |
+--------------------------------------------+
| false                                      |
+--------------------------------------------+
1 row in set, 1 warning (0.00 sec)			

随便改译为数,校验失败返回 false

			mysql> select check_id_number('330702198003090914');
+---------------------------------------+
| check_id_number('330702198003090914') |
+---------------------------------------+
| false                                 |
+---------------------------------------+
1 row in set (0.00 sec)					

原文发布于微信公众号 - Netkiller(netkiller-ebook)

原文发表时间:2016-10-27

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏用户2442861的专栏

好的数据库面试题集合

http://blog.csdn.net/sandyzhs/article/details/4059709

141
来自专栏个人随笔

那些年我们的(具有含金量)MySQL测试题目

 请耐心阅读,下面有惊喜! 1.创建数据库 CREATE DATABASE QQDB; 2.创建各表(表结构;约束) /*******************创...

4398
来自专栏跟着阿笨一起玩NET

SQL Server 2008中的hierarchyid

本人转载:http://www.cnblogs.com/chenxizhang/archive/2009/04/26/1444016.html

351
来自专栏杨建荣的学习笔记

关于操作失误的数据修复(r2笔记48天)

在系统升级的过程中,准备了大量的脚本,分成几个窗口来分别执行。 在碰到问题的时候,一定要很细心和冷静,不经意的错误可以需要几倍,几十倍的努力来挽回。 准生产环境...

3305
来自专栏互联网开发者交流社区

SQL一次查出相关类容避免长时间占用表(上)

1202
来自专栏乐沙弥的世界

PL/SQL --> DBMS_DDL包的使用

为了便于建立性能良好的PL/SQL程序,Oracle提供了大量的系统包供使用。Oracle提供的这些包扩展并增强了数据库的一些功能,以及突

814
来自专栏乐沙弥的世界

dbms_lock.relase 无法释放自定义的锁?

      最近开发人员说使用dbms_lock.allocate_unique自定义的锁在使用dbms_lock.relase无法释放,下面来个演示的例子来看...

462
来自专栏GreenLeaves

sql 查询目标数据库中所有的表以其关键信息

1、查询目标库中的所有表 SELECT obj.name tablename, ---表名 schem.name schemname, ---表所属的方案 i...

17910
来自专栏数据库新发现

如何使用触发器实现数据库级守护,防止DDL操作

--对于重要对象,实施DDL拒绝,防止create,drop,truncate,alter等重要操作

695
来自专栏Grace development

举枪消灭”烂代码”的实战案例

之前我写过一篇如何少写PHP “烂”代码 https://blog.fastrun.cn/2018/06/13/1-9/ 感觉很多新人对此不太理解。今天以打卡...

453

扫描关注云+社区