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);
  • 相关阅读:
    CCF NOI1067 最匹配的矩阵
    POJ NOI0105-29 数字反转
    POJ NOI0105-30 含k个3的数
    POJ NOI0105-32 求分数序列和
    POJ NOI0105-33 计算分数加减表达式的值
    POJ NOI0105-34 求阶乘的和
    POJ NOI0105-35 求出e的值
    POJ NOI0105-36 计算多项式的值
    POJ NOI0105-44 第n小的质数
    POJ NOI0105-43 质因数分解
  • 原文地址:https://www.cnblogs.com/lxyoung/p/6946611.html
Copyright © 2011-2022 走看看