一、建数据表

学生表Student:学号、姓名、性别、出生年月日、班级

mysql> create database selectTest;
mysql> show databases;
mysql> use selectTest;
Database changed

mysql> create table student(
    -> stu_num varchar(20) primary key,
    -> stu_name varchar(20) not null,
    -> stu_sex varchar(10) not null,
    -> stu_birth datetime,
    -> class varchar(20)
    -> );

教师表Teacher:教师编号、教师名字、教师性别、出生年月日、职称、所在部门

mysql> create table teacher(
    -> tea_num  varchar(20) primary key,
    -> tea_name varchar(20) not null,
    -> tea_sex varchar(10) not null,
    -> tea_birth datetime,
    -> tea_prof varchar(20) not null,
    -> department varchar(20) not null
    -> );

课程表Course:课程号、课程名称、教师编号

mysql> create table course(
    -> cour_num varchar(20) primary key,
    -> cour_name varchar(20) not null,
    -> tea_num varchar(20) not null,
    -> foreign key(tea_num) references teacher(tea_num)
    -> );

成绩表Score:学号、课程号、成绩

mysql> create table score(
    -> stu_num varchar(20) primary key,
    -> cour_num varchar(20) not null,
    -> degree decimal,
    -> foreign key(stu_num) references student(stu_num),
    -> foreign key(cour_num) references course(cour_num)
    -> );
mysql> show tables;
+----------------------+
| Tables_in_selecttest |
+----------------------+
| course               |
| score                |
| student              |
| teacher              |
+----------------------+

 二、向表中插入数据

mysql> INSERT INTO student VALUES('11422','Baker','F','1999-09-22','114');
mysql> INSERT INTO student VALUES('11423','Bob','M','1998-04-25','114');
mysql> INSERT INTO student VALUES('11424','LinJie','M','1994-06-12','114');
mysql> INSERT INTO student VALUES('11425','XieZhou','M','1995-03-11','114');
mysql> INSERT INTO student VALUES('11426','MingHui','F','1998-08-09','114');
mysql> INSERT INTO student VALUES('11427','NanNan','F','1995-10-20','114');
mysql> INSERT INTO student VALUES('11328','DingQi','F','1994-08-15','113');
mysql> INSERT INTO student VALUES('11215','JiaWei','F','1993-07-28','112');
mysql> INSERT INTO teacher VALUES('0435','LiMei','F','1983-02-24','prof','Computer');
mysql> INSERT INTO teacher VALUES('0436','MaDi','F','1984-01-23','assist','Bio');
mysql> INSERT INTO teacher VALUES('0437','LiZhe','F','1974-01-23','prof','Econ');
mysql> INSERT INTO teacher VALUES('0438','ShaoGuoYing','F','1985-06-17','prof','Math');
mysql> INSERT INTO course VALUES('3-105','OS','0435');
mysql> INSERT INTO course VALUES('1-245','Math','0438');
mysql> INSERT INTO course VALUES('2-271','Circuit','0435');
mysql> INSERT INTO course VALUES('4-321','Bio','0436');
mysql> INSERT INTO score VALUES('11422','3-105','92');
mysql> INSERT INTO score VALUES('11423','1-245','84');
mysql> INSERT INTO score VALUES('11424','4-321','75');
mysql> INSERT INTO score VALUES('11425','2-271','89');
mysql> INSERT INTO score VALUES('11426','1-245','61');
mysql> INSERT INTO score VALUES('11427','1-245','78');
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO score VALUES('11426','2-271','82');
ERROR 1062 (23000): Duplicate entry '11426' for key 'PRIMARY'
mysql> INSERT INTO score VALUES('11423','2-271','75');
ERROR 1062 (23000): Duplicate entry '11423' for key 'PRIMARY'

修改表score中的主键为联合主键:

mysql> drop table score;
mysql> create table score( -> stu_num varchar(20) not null, -> cour_num varchar(20) not null, -> degree decimal, -> foreign key(stu_num) references student(stu_num), -> foreign key(cour_num) references course(cour_num), -> primary key(stu_num,cour_num) -> );

插入数据后:

mysql> select * from score;
+---------+----------+--------+
| stu_num | cour_num | degree |
+---------+----------+--------+
| 11422   | 3-105    |     92 |
| 11423   | 1-245    |     84 |
| 11423   | 2-271    |     75 |
| 11424   | 4-321    |     75 |
| 11425   | 2-271    |     89 |
| 11426   | 1-245    |     61 |
| 11426   | 2-271    |     82 |
| 11427   | 1-245    |     78 |
+---------+----------+--------+

04'08''

01-07 13:36