相关推荐recommended
mysql 创建学生表、课程表、学生选课表
作者:mmseoamin日期:2023-12-11

数据库系统概论(第五版)79页提供的三个表,为了我们上机操作容易,下面创建这三个表

学生-课程数据库中包含以下三个表

  • 学生表:Student(Sno,Sname,Ssex,Sage,Sdept)
  • 课程表:Course(Cno,Cname,Cpno,Ccredit)
  • 学生选课表:SC(Sno,Cno,Grade)

    关系的主码加粗表示,各个表中的数据实例:

    Student

    学号(Sno)姓名(Sname)性别(Ssex)年龄(Sage)所在系 (Sdept)
    201215121李勇20CS
    201215122刘晨19CS
    201215123王敏18MA
    201215125张立19IS

    Course

    课程号(Cno)课程名(Cname)先行课(Cpno)学分(Ccredit)
    1数据库54
    2数学2
    3信息系统14
    4操作系统63
    5数据结构74
    6数据处理2
    7PASCAL语言64

    SC

    学号(Sno)课程号(Cno)成绩(Grade)
    201215121192
    201215121285
    201215121388
    201215122290
    201215122380
    1、建表语句

    Sno为Student表的主键,Cno为Course表的主键,SC表中外键Sno,Cno分别是Student表和Course表的主键

    create table Student (Sno char(9) primary key,Sname char(20),Ssex char(2),Sage int,Sdept char(10))charset=utf8;
    create table Course (Cno char(2) primary key,Cname char(20),Cpno char(2),Ccredit integer)charset=utf8;
    create table SC (Sno char(9),Cno char(2),Grade float,foreign key(Sno) references Student(Sno),foreign key(Cno) references Course(Cno))charset=utf8;
    
    2、插入数据
    insert into Student values('201215121','李勇','男',20,'CS');
    insert into Student values('201215122','刘晨','女',19,'CS');
    insert into Student values('201215123','王敏','女',18,'MA');
    insert into Student values('201215125','张立','男',19,'IS');
    insert into Course values('1','数据库','5',4);
    insert into Course values('2','数学','',2);
    insert into Course values('3','信息系统','1',4);
    insert into Course values('4','操作系统','6',3);
    insert into Course values('5','数据结构','7',4);
    insert into Course values('6','数据处理','',2);
    insert into Course values('7','PASCAL语言','6',4);
    insert into SC values('201215121','1',92);
    insert into SC values('201215121','2',85);
    insert into SC values('201215121','3',88);
    insert into SC values('201215122','2',90);
    insert into SC values('201215122','3',80);
    

    3、查询表

    select * from Student;
    select * from Course;
    select * from SC;