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);

 

转载于:https://www.cnblogs.com/lxyoung/p/6946611.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值