• 生成十万考生的语数英理化高考成绩流水表


    现在有一个需求,需要生成十万考生的五门高考成绩表,这个表里应该有以下字段

    # 字段名 说明
    1 id 标识记录唯一值
    2 学生id 标识学生唯一值
    3 科目id 标识是哪一门科目
    4 得分 该科目得分

    如果大连市有十万考生参加今年的高考,那么这张表里应该有五十万条数据。

    如何生成这五十万条数据呢?以前使用过一种随机生成然后去重的方法,现在看来这种方法要生成十万考生的数据太繁琐,难以在短时间内达成目的,下面要介绍是利用中间表来生成成绩表的方法。

    首先构建中间表:

    create table gk_fivescore(
       id number(8,0) primary key,
       chinese number(3,0) not null,
       math number(3,0) not null,
       english number(3,0) not null,
       physics number(3,0) not null,
       chemistry number(3,0) not null
    )

    然后这样充值:

    insert into gk_fivescore 
    select rownum,dbms_random.value(0,150),dbms_random.value(0,150),dbms_random.value(0,150),dbms_random.value(0,150),dbms_random.value(0,150)  from dual
    connect by level<=100000
    order by dbms_random.random

    commit之后,让我们先看看gk_fivescore表中的数据:

    SQL> select * from gk_fivescore where rownum<10;
    
            ID    CHINESE       MATH    ENGLISH    PHYSICS  CHEMISTRY
    ---------- ---------- ---------- ---------- ---------- ----------
         26090         14         41        110         89         97
         16146         75        140         66         72         44
         62074        114        131         23          8        150
         45425         59        136         10        122         48
         86232         26         42          0         96          2
         33635         17         61         74         89        139
         83313         66         20          6         53        120
         33493         81        134        128         35         92
         93475         24         25         86         45         54
    
    已选择9行。
    
    已用时间:  00: 00: 00.00

    有同学会说,这不就完事了吗?学生id和成绩都在里面了。

    在某些场景中,这是可以的。但是,需求还没有满足,我们暂时只得到了一张中间表,这是一张状态表,而需求的最终表是一张流水表。

    然后让我们创建最终表的表结构:

    create table gk_score(
       id number(8,0) primary key,
       subjectid number(1,0) not null,
       stuid number(8,0) not null,
       score number(3,0) not null
    )

    然后这样给它充值:

    insert into gk_score(id,stuid,score,subjectid) select rownum,id,chinese,1 from gk_fivescore;
    insert into gk_score(id,stuid,score,subjectid) select 100000+rownum,id,math,2 from gk_fivescore;
    insert into gk_score(id,stuid,score,subjectid) select 200000+rownum,id,english,3 from gk_fivescore;
    insert into gk_score(id,stuid,score,subjectid) select 300000+rownum,id,physics,4 from gk_fivescore;
    insert into gk_score(id,stuid,score,subjectid) select 400000+rownum,id,chemistry,5 from gk_fivescore;

    commit后,任务就圆满完成了。

    让我们来看看表里情况:

    SQL> select * from gk_score where rownum<10;
    
            ID  SUBJECTID      STUID      SCORE
    ---------- ---------- ---------- ----------
          1083          1      46110         26
          1084          1      17309        118
          1085          1      82287        144
          1086          1      25989        108
          1087          1      40912         61
          1088          1      38971        137
          1089          1      88624        118
          1090          1       8646        112
          1091          1      23097        130
    
    已选择9行。

    再看看单个考生的情况:

    SQL> select * from gk_score where stuid=46110;
    
            ID  SUBJECTID      STUID      SCORE
    ---------- ---------- ---------- ----------
          1083          1      46110         26
        101083          2      46110        116
        201083          3      46110        133
        301083          4      46110         50
        401083          5      46110         31
    
    已用时间:  00: 00: 00.01
    SQL> select * from gk_score where stuid=17309;
    
            ID  SUBJECTID      STUID      SCORE
    ---------- ---------- ---------- ----------
          1084          1      17309        118
        101084          2      17309         45
        201084          3      17309         56
        301084          4      17309         19
        401084          5      17309        147
    
    已用时间:  00: 00: 00.03

    最后查查总数据量:

    SQL> select count(*) from gk_score;
    
      COUNT(*)
    ----------
        500000
    
    已用时间:  00: 00: 00.01

    正好五十万,到此确认任务完成。

    以上SQL,看过程并不复杂,时间也短,但简洁快速的背后是经验的沉淀和积极的思索,这也正是中年程序员的价值所在。

    --2020年2月15日--

  • 相关阅读:
    hdu 4707 Pet
    hdu 3584 Cube (三维树状数组)
    poj 2155 Matrix (树状数组)
    poj 1195 Mobile phones (树状数组)
    工厂方法模式
    简单工厂模式
    关于设计模式
    UML类图
    UML
    【转载】UML用例图
  • 原文地址:https://www.cnblogs.com/heyang78/p/12310596.html
Copyright © 2020-2023  润新知