简体   繁体   中英

How do you improve the design for my database struct?

I have a little knowledge for databases designing and SQL only. I wrote a simple students scores manager for learning programming and database, but I had terrible designing.

Database Struct

My database has three tables:

  1. students_info
  2. logs
  3. week

Let's talk about student_info first. This table struct just like that:

id  student_name    group    the_score_for_1_week    the_score_for_2_week
1   Adam            2        24                      2

The number of the fields are unknown.

logs is preey messy:

id  date    student_name    score   change  week
1   01-01   Adam            2       2       1
2   01-02   Adam            1       -1      1
2   01-02   Adam            24      23      1

Table week has 1 field only.

week
2

Program

After I showed the struct of the database. I'm going to talk how my program works.

How is it look like?

The program has two grid, one chart, three buttons. The first grid is for student names and scores. The second grid is for logs, we can see how the scores changed. The chart is for showing the scores changing too. Here is my first blueprint.

蓝图

When user's mouse clicked a student's name on the first chart, the second chart will show the logs of the scores of this student, the chart will show a line chart of this student's scores.

The program can save different scores for different weeks. So, it has three button: Last Week , Next Week , New Week . We can edit the latest week only.

How is it works

When the program started, it will select the latest week from week .

SELECT week FROM week

Then select student_name , group , latest week from student_info . We can add/minus the scores of the students.

"SELECT student_name,the_score_for_%s_week,group FROM student_info" % (week)

After the scores changed, a record will inserted to the logs table.

"INSERT INTO log(change,score,date,student_name,week) VALUES ("
                 "'%s', '%s', '%s', '%s', '%s')" % (
                 newScore - oldScore, newScore,
                 time.strftime("%m-%d", time.localtime()),
                 student, week))

When a new week started, a new field the_score_for_%d_week will inserted to student_info , we also need to update week table.

"ALTER TABLE student_info ADD the_score_for_%s_week,group INTEGER DEFAULT 0" % week
"UPDATE student_info SET the_score_for_%s_week,group=0" % week)
"UPDATE week SET week =%s" % week

When user's mouse clicked on a student name, the second grid will show the historical scores.

 "SELECT date,change FROM log WHERE the_score_for_%s_week=%s and student_name='%s'" % (week, student)

The chart will show how the scores changing. Unlike the second grid, we need the full scores, not +1, or -1 to draw the chart.

"SELECT score FROM log WHERE the_score_for_%s_week=%s and student_name='%s'" % (week, student))

As you see, the database designing is terrible. I know StackOverFlow is not a code review website. But I just want to tell you more details. I tried improved the database struct for many time, but I don't have any idea for that.

Finally, thanks for reading this very long question.

I would suggest taking the scores out of the student info table and then putting them into a separate table. This might negate the need for the LOG table as you can check these details on the fly.

Student_Info would then contain:

ID
Name
Group

A new table Result_Details would contain:

ID
Date
Student_Info_ID
Score

When a new week starts you can then enter the details as a new row in Result_Details rather than have to add a new column to Student_Info for every week.

Then to retrieve the list of scores for an student you could use something like this:

select SI.Name, RD.Date, RD.Score 
from Student_Info as SI 
Join Result_Details RD on RD.Student_Info_ID = SI.ID
Where SI.Name = 'Tom'

Well I assume that your question is aimed at improving your knowledge with a view to learning about aspects of programming, rather than some school homework project which your trying to get a short cut answer. The following link is in my view reasonably clear on undersanding what SQL is about. http://www3.ntu.edu.sg/home/ehchua/programming/sql/Relational_Database_Design.html The only cavetat is that these days there is NO SQL and SQL. NOSQL databases are essentially databases where the rows and columns format originated by CODD is disgarded in favour of blocks of data where there is no formal row structure. The two should not be confused. Each will outperform the other in the appropriate circumstances.

Googling "relational database design tutorial" will help if the above article does not. To put things in a nutshell: Step 1 of this problem is to understand what SQL databases are about. All else follows....

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM