zoukankan      html  css  js  c++  java
  • Day.1建表小作业

    Mysql练习题博客地址:http://www.cnblogs.com/wupeiqi/articles/5729934.html

    代码如下:

    班级表class:

    create table class(
        cid int not null auto_increment primary key,
        caption char(10))engine=innodb default charset=utf8;
    
    insert into class (caption) values ('三年二班'),('一年三班'),('三年一班');

    教师表teacher:

    create table teacher(
        tid int not null auto_increment primary key,
        tname char(10))engine=innodb default charset=utf8;
    
    insert into teacher (tname) values ('egon'),('alex'),('wusir');

    学生表student:

    create table student(
        sid int not null auto_increment primary key,
        sname char(10),gender enum('',''),
        class_id int not null,
        constraint fk_sc foreign key (class_id) references     
        class(cid))engine=innodb default charset=utf8;
    
    insert into student (sname,gender,class_id) values('钢蛋','',1),('铁锤','',1),('山炮','',2);

    课程表course:

    create table course(
        cid int not null primary key auto_increment,
        cname char(10),teacher_id int not null,
        constraint fk_ct foreign key (teacher_id) references     
        teacher(tid))engine=innodb default charset=utf8;
    
    insert into course (cname,teacher_id) values('生物',1),('体育',1),('物理',2);

    分数表score:

    create table score(
        sid int not null auto_increment primary key,
        student_id int not null,
        course_id int not null,
        number int not null,
        constraint fk_ss foreign key (student_id) references student(sid),
        constraint fk_scadd foreign key (course_id) references 
        course(cid))engine=innodb default charset=utf8;
    
    insert into score(student_id,course_id,number) values(1,1,60),(1,2,59),(2,2,100);

    为分数表的student_id与course_id绑定为唯一索引值(也可以在建表时在constraint语句上面添加unique语句):

    alter table score add unique uni_score(student_id,course_id);
  • 相关阅读:
    Hadoop Gateway 部署
    java 命令--备忘
    整理下常用硬件性能参数
    python 脚本备份 mysql 数据库到 OSS
    pip 更换国内源
    记录闭包和立即执行函数
    Django 中文乱码问题&富文本显示
    mysql exceeded the 'max_questions' resource 记录
    sql server 数据字典的妙用
    Sublime Text指南
  • 原文地址:https://www.cnblogs.com/lxyoung/p/6946611.html
Copyright © 2011-2022 走看看