Build a database for a simple score management system

topic

Build a database for a simple score management system

introduce

Now it is necessary to build a database of a simple grade management system to record the student grades of several courses. There are three tables in the database for recording student information, course information and grade information.

The data in the database table is as follows:

Student table (student): student id, student name and gender

Enter image description here

Course Schedule: Course ID and Course Name

Enter image description here

Grade table: Grade id , student id , course id and grades

Enter image description here

MySQL in the server has not been started yet, please note that the default password of the MySQL root account is empty.

Target

1. The MySQL service is running

2. The name of the new database is gradesystem

3.gradesystem contains three tables: student, course, mark;

  • The student table contains 3 columns: sid (primary key), sname, gender;
  • The course table contains 2 columns: cid (primary key), cname;
  • The mark table contains 4 columns: mid (primary key), sid, cid, score, pay attention to the relationship with the primary keys of the other two tables.

4. Insert the data in the above table into each table respectively

hint

  • When creating a table, pay attention to id auto-increment and key constraints
  • Each table insert statement can be done with one statement

solution

Start mysql:

sudo service mysql start
mysql -u root

Create a gradesystemdatabase called:

create database gradesystem;
use gradesystem;

Create studenttable:

create table student
(
    sid int(10)primary key,
    sname char (10),
    gender char(10)
);

Create coursetable:

create table course
(
    cid int(10)primary key,
    sname char (10)
);

  

Create marktable:

create table mark
(
    mid int(10)primary key,
    sid int(10),foreign key (sid) references student(sid),
    cid int(10),foreign key (cid) references course(cid),
    score int(100)
);

  

studentInsert data:

insert into student values(1,'Tom','male');
insert into student values(2,'Jack','male');
insert into student values(3,'Rose','female');

courseInsert data:

insert into course values(1,'math');
insert into course values(2,'physics');
insert into course values(3,'chemistry');

  

markInsert data:

insert into mark values(7,1,3,95);
insert into mark values(8,2,3,75);
insert into mark values(9,3,3,85);

  

Finally, check out these three tables:

select * from student;
/*
+-----+-------+--------+
| sid | sname | gender |
+-----+-------+--------+
|   1 | Tom   | male   |
|   2 | Jack  | male   |
|   3 | Rose  | female |
+-----+-------+--------+
*/
select * from course;
/*
+-----+-----------+
| cid | sname     |
+-----+-----------+
|   1 | math      |
|   2 | physics   |
|   3 | chemistry |
+-----+-----------+
*/
select * from mark;
/*
+-----+------+------+-------+
| mid | sid  | cid  | score |
+-----+------+------+-------+
|   7 |    1 |    3 |    95 |
|   8 |    2 |    3 |    75 |
|   9 |    3 |    3 |    85 |
+-----+------+------+-------+
*/

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326393365&siteId=291194637