lxyoung

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

 

分类:

技术点:

相关文章: