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
Course Schedule: Course ID and Course Name
Grade table: Grade id , student id , course id and grades
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 gradesystem
database called:
create database gradesystem; use gradesystem;
Create student
table:
create table student ( sid int(10)primary key, sname char (10), gender char(10) );
Create course
table:
create table course ( cid int(10)primary key, sname char (10) );
Create mark
table:
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) );
student
Insert data:
insert into student values(1,'Tom','male'); insert into student values(2,'Jack','male'); insert into student values(3,'Rose','female');
course
Insert data:
insert into course values(1,'math'); insert into course values(2,'physics'); insert into course values(3,'chemistry');
mark
Insert 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 | +-----+------+------+-------+ */