首页 技术 正文
技术 2022年11月23日
0 收藏 961 点赞 4,919 浏览 1916 个字

 题目:

<每日一题>题目15:mysql创建表及相关约束

解答:

    第一个表创建:
create table class(
cid int not null auto_increment primary key,
caption char(20) not null
)engine=innodb default charset=utf8;
插入数据:
insert into class(caption) values('三年二班');
insert into class(caption) values('一年三班');
insert into class(caption) values('三年一班');

<每日一题>题目15:mysql创建表及相关约束

    第二个表创建:
create table student(
sid int not null auto_increment primary key,
sname char(20) not null,
gender char(20) not null,
class_id int
)engine=innodb default charset=utf8;
增加约束(外键):
alter table student add constraint foreign key student(class_id) references class(cid);
插入数据:
insert student(sname,gender,class_id) values('钢弹','女',1);
insert student(sname,gender,class_id) values('铁锤','女',1);
insert student(sname,gender,class_id) values('山炮','男',2);

<每日一题>题目15:mysql创建表及相关约束

   第三个表创建:
create table teacher(
tid int not null auto_increment primary key,
tname char(20) not null
)engine=innodb default charset=utf8;
插入数据:
insert teacher(tname) values('波多');
insert teacher(tname) values('苍空');
insert teacher(tname) values('饭岛');

<每日一题>题目15:mysql创建表及相关约束

   第四个表创建:
create table course(
cid int not null auto_increment primary key,
cname char(20) not null,
tearch_id int
)engine=innodb default charset=utf8;
增加约束:
alter table course add constraint foreign key course(tearch_id) references teacher(tid);
插入数据:
insert course(cname,tearch_id) values('生物',1);
insert course(cname,tearch_id) values('体育',1);
insert course(cname,tearch_id) values('物理',2);

<每日一题>题目15:mysql创建表及相关约束

    第五个表创建:
create table score(
sid int not null auto_increment primary key,
student_id int not null,
corse_id int not null,
number int not null
)engine=innodb default charset=utf8;
增加约束:
alter table score add constraint foreign key score(student_id) references student(sid);
alter table score add constraint foreign key (corse_id) references course(cid);
可能存在的问题:
假设第二句写成:alter table score add constraint foreign key score(corse_id) references course(cid);
会报错:ERROR 1061 (42000): Duplicate key name 'score'
原因是:外键名称重复,在key后面增加表名会默认作为外键名,因此如果写2条,就会出现外键名称重复
解决办法:删除key后面的表名,mysql会默认增加索引
插入数据:
insert score(student_id,corse_id,number) values(1,1,60);
insert score(student_id,corse_id,number) values(1,2,59);
insert score(student_id,corse_id,number) values(2,2,100);

<每日一题>题目15:mysql创建表及相关约束

 

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838