SQL - columns for different categories

后端 未结 6 661
予麋鹿
予麋鹿 2020-12-09 11:39

I am new to SQL. I have a database with data for different exams, for example:

Student Test Grade
--------------------
St1    T1   A
St2    T1   B
St3    T1          


        
6条回答
  •  温柔的废话
    2020-12-09 12:08

    I believe that if you are going to expand this system to include more information then, you could benefit from reworking your database, I would build it like so:

    Table name = Bold

    Column name = Italicized

    Students:

    • SID (Primary Key)
    • Other information about the student

    Tests:

    • TID (Primary Key)
    • Other information about the test

    Test Grades

    • GID (Primary Key)
    • TID (Foreign key)
    • SID (Foreign Key)
    • Grade

    This structure is based on an idea called database normalizing (you'll get lots of information if you google it). I will give you a partial summary below, but if you're going to do lots of SQL you should read up on it yourself:

    The first thing to know is that a primary key is just a unique identifier, it is not usually part of the information (however, since it is unique every datum must have different value for its primary key), and a foreign key is a way to reference a row in one table from a row in another table, using the referencee's primary key: for example here the foreign key SID in each grade references a single student, based on their primary key SID.

    e.g. student one has SID 1 and all his tests have 1 in the SID column. same for student 2, 3, 4, and so on.

    The basic idea of normalizing, is that all unique data is stored only once and then referenced in the other places that use it (If you take a look at how the keys are structured in the example, all student information is stored in a table and then referenced in their test grades, instead of being duplicated in each grade).

    To retrieve what you want from these tables I would use this (its written in PHP):

    $sql = 'SELECT * FROM Tests ORDER BY TID';
    $tempresult = mysql_query($sql);
    while($temprow = mysql_fetch_array($tempresult)){
        echo $temprow['TID'];
    }
    
    $sql = 'SELECT * FROM Students';
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)){
       echo '\n'.$row['SID'];
       $sql = 'SELECT * FROM Grades WHERE SID='.$row['SID'].' ORDER BY TID';
       $result2 = mysql_query($sql);
       while($row2 = mysql_fetch_array($result2)){
          echo ' '.$rows['Grade'];
       }
    }
    

    You can add formatting to this in the echo statements and also print any of the extra information you choose to add. If you ave any questions please ask them.

    EDIT: I have read the others, and agree that their method is in all likelihood superior, the only thing I'm not sure about is whether pivot tables can expand to handle a varying number of tests, if it can (or you won't need to), then I suggest their method, otherwise I feel this might have a place in your application.

提交回复
热议问题