<每日一题>题目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('三年一班');

第二个表创建:
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);

第三个表创建:
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('饭岛');

第四个表创建:
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);

第五个表创建:
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创建表及相关约束的更多相关文章
- oracle与mysql创建表时的区别
oracle创建表时,不支持在建表时同时增加字段注释.故采用以下方式: #创建表CREATE TABLE predict_data as ( id integer ), mid ), time dat ...
- 【转载】Mysql创建表时报错error150
从mysql数据库中导出正常数据库的脚本语句,而后使用脚本语句创建数据库的过程中,执行语句提示Can't Create Table 'XXX' erro150的错误,语句执行中断,创建table失败, ...
- mysql 创建表时注意事项
mysql 创建表时注意事项 mysql 想必大家都不会陌生吧 是我学习中第一个接触的的数据库 已学习就很快上手的 这是一个关系型数据库 不懂什么是关系型数据库 啊哈哈哈 现在知道啦 因 ...
- MySQL 创建表时,设置时间字段自己主动插入当前时间
MySQL 创建表时,设置时间字段自己主动插入当前时间 DROP TABLE IF EXISTS `CONTENT`; CREATE TABLE `CONTENT` ( `ID` char(20) N ...
- Python MySQL 创建表
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- mysql创建表分区
MySQL创建表分区 create table erp_bill_index( id int primary key auto_increment, addtime datetime ); inser ...
- mysql创建表时,设置timestamp DEFAULT NULL报错1067 - Invalid default value for 'updated_at'
问题背景: 线上的linux服务器上的mysql服务器中导出数据库的结构.想要在本地创建一个测试版本 导出后再本地mysql上运行却报错 1067 - Invalid default value ...
- mysql创建表的注意事项
1 库名,表名,字段名必须使用小写字母,"_"分割. 2 库名,表名,字段名必须不超过12个字符. 3 库名,表名,字段名见名识意,建议使用名词而不是动词. 4 建议使用InnoD ...
- MYSQL创建表的约束条件(可选)
一.常用的一些约束条件 一.创建表的完整语法1.创建表的万能模板:create table 库名.表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型 ...
随机推荐
- CSV导入到hive中,处理分号问题
1.导入的原数据 103744;545479945;2017.05.17 06:41:08;sell;eurusd_;0.10;1.11080;1.11280;1.10880;1.11081;0.00 ...
- 2018-8-10-VisualStduio-打断点调试和不打断点调试有什么区别
title author date CreateTime categories VisualStduio 打断点调试和不打断点调试有什么区别 lindexi 2018-08-10 19:16:52 + ...
- 2019-9-11-在-P2P-文件分享应用以文件或文件段为单位的优缺
title author date CreateTime categories 在 P2P 文件分享应用以文件或文件段为单位的优缺 lindexi 2019-09-11 10:23:27 +0800 ...
- WIN10安装CUDA10 cuDNN
文章目录 CPU和GPU 什么是CUDA 什么是cuDNN WIN10安装CUDA10 WIN10安装cuDNN CPU和GPU CPU和GPU是不一样的计算机设备,CPU作为计算机心脏一直被人们所认 ...
- 【hihocoder】Demo Day
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You work as an intern at a robotics startup. Today is your co ...
- Linux 添加时间
添加在指令后面 `date +%Y%m%d%H%M`注意date和+之间一定要有空格 ps: %% 一个文字的 % %a 当前locale 的星期名缩写(例如: 日,代表星期日) %A ...
- MySQL语句基本操作增删改查
select * from 表名; --------->效率低
- Perl 基础语法
Perl 基础语法 Perl借用了C.sed.awk.shell脚本以及很多其他编程语言的特性,语法与这些语言有些类似,也有自己的特点. Perl 程序有声明与语句组成,程序自上而下执行,包含了循环, ...
- hdu6607 min25筛+杜教筛+伯努利数求k次方前缀和
推导过程类似https://www.cnblogs.com/acjiumeng/p/9742073.html 前面部分min25筛,后面部分杜教筛,预处理min25筛需要伯努利数 //#pragma ...
- grunt是什么
Grunt是什么? 博客分类: Grunt GruntJavascript Grunt是一个自动化的项目构建工具.如果你需要重复的执行像压缩,编译,单元测试,代码检查以及打包发布的任务.那么你可以使 ...