MySQL数据表拆分

首先我们需要测试如何实现拆分表

  1. 创建测试数据库

    mysql> CREATE DATABASE test;

  2. 创建examinees数据样表并插入数据

     mysql> CREATE TABLE examinees( id INT(64) UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), exam_info_id INT(64) NOT NULL UNIQUE, company_id INT(64) NOT NULL, user_ids VARCHAR(1024), department_ids MEDIUMTEXT, is_all enum('0','1') DEFAULT '0' );
    
     mysql> INSERT INTO examinees (exam_info_id,company_id,department_ids) VALUES (1,1,'1,2,3'),(2,1,'2,3,4');
    
     mysql> INSERT INTO examinees (exam_info_id,company_id,department_ids) VALUES (33,2,'4,5,6'),(37,2,'4,5,6,64,65,66');
    
  3. 创建结果表

    mysql> CREATE TABLE exam_dep( id INT(64) UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), exam_info_id INT(64) NOT NULL, company_id INT(64) NOT NULL, department_id INT(64));
    
  4. 确定全员字段is_all没用

  5. 创建函数
    注意定界符,先修改定界符为$:delimiter $

    • 创建函数,获取拆分字符串后得到的字符串的个数

      sql> CREATE FUNCTION `func_get_split_string_total`( f_string mediumtext,f_delimiter varchar(5) )RETURNS int(11) BEGIN return 1+(length(f_string)-length(replace(f_string,f_delimiter,''))); END$
      
    • 创建函数,获取字符串

      sql> CREATE FUNCTION `func_get_split_string`( f_string mediumtext,f_delimiter varchar(5),f_order int) RETURNS int(64) BEGIN  declare result int(64); set result=reverse(substring_index(reverse(substring_index(f_string,f_delimiter,f_order)),f_delimiter,1)); return result; end$
      
    • 创建函数,循环获得字符串,插入新表格

      sql> CREATE FUNCTION `func_split_string`(e_id int(64),c_id int(64),d_ids mediumtext) RETURNS varchar(100) BEGIN DECLARE i int(64); set i=1; if(d_ids is null or length(d_ids)=0) then  return 'error'; else while i<=func_get_split_string_total(d_ids,',')  do insert into exam_dep(exam_info_id,company_id,department_id) values (e_id,c_id,func_get_split_string(d_ids,',',i)); set i=i+1; end while; return 'success'; end if; end$
      
  6. 执行函数测试

    mysql> select func_split_string(exam_info_id,company_id,department_ids) result, t.* from examinees t;
    

测试成功,好了,我们可以正式开始工作了

  1. 创建model 参见admin/model/examinees_new.py

  2. 进入服务器创建表格

  3. 依照上面的测试创建函数并执行

    可能会遇到此问题:

     > ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)    解决办法:在returns和begin之间添加*DETERMINISTIC READS SQL DATA*,即:    ```
    

    mysql> CREATE FUNCTION func_get_split_string_total( f_string mediumtext,f_delimiter varchar(5) )RETURNS int(11) DETERMINISTIC READS SQL DATA BEGIN return 1+(length(f_string)-length(replace(f_string,f_delimiter,’’))); END$`

    ```

    具体文档请参见:https://dev.mysql.com/doc/refman/5.7/en/stored-programs-logging.html

  4. 后端逻辑

    • mapping.py

      get_examinees_info_new 新建根据exam_info_id获取一个考试可以参加考试的人员信息,格式为字典

      get_examinees_by_company_new 根据company_id获取此公司所有状态为启用的考试以及参考人员的字典

      get_exam_info_by_userid_new 根据user_id获取此考生能参加的考试,过滤掉已过期的无效的考试,格式为list

      get_exam_info_all_new 根据exam_info_id获取一个考试的所有信息,格式为字典

      get_deps_info 根据exam_info获取能参加此考试的部门id用逗号拼接的字符

    • 页面逻辑

      考生登录界面显示能参加的考试

      成绩查询批改显示考试下的参考人员:不直接通过新建表查询

      成绩查询批改显示考生能参加的考试:不直接通过新建表查询

  5. 增加功能

    • 前端增加指定考生

      url:admin/exam_add

      html:admin_exam_add_new.html

    • 修改指定部门逻辑

      blueprint:exam.py 135行

      选择部门树如何传递数据,传递字符串

      修改写入数据库

    • 修改指定人员逻辑

      弹出选择考生复选框,参照成绩查询批改选择考生

    • 修改更改考试信息逻辑

    • 搜索其他与examinees相关需要更改的地方