题目:

解答:

    第一个表创建:
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创建表及相关约束的更多相关文章

  1. oracle与mysql创建表时的区别

    oracle创建表时,不支持在建表时同时增加字段注释.故采用以下方式: #创建表CREATE TABLE predict_data as ( id integer ), mid ), time dat ...

  2. 【转载】Mysql创建表时报错error150

    从mysql数据库中导出正常数据库的脚本语句,而后使用脚本语句创建数据库的过程中,执行语句提示Can't Create Table 'XXX' erro150的错误,语句执行中断,创建table失败, ...

  3. mysql 创建表时注意事项

    mysql  创建表时注意事项 mysql 想必大家都不会陌生吧  是我学习中第一个接触的的数据库 已学习就很快上手的   这是一个关系型数据库  不懂什么是关系型数据库 啊哈哈哈  现在知道啦  因 ...

  4. MySQL 创建表时,设置时间字段自己主动插入当前时间

    MySQL 创建表时,设置时间字段自己主动插入当前时间 DROP TABLE IF EXISTS `CONTENT`; CREATE TABLE `CONTENT` ( `ID` char(20) N ...

  5. Python MySQL 创建表

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  6. mysql创建表分区

    MySQL创建表分区 create table erp_bill_index( id int primary key auto_increment, addtime datetime ); inser ...

  7. mysql创建表时,设置timestamp DEFAULT NULL报错1067 - Invalid default value for 'updated_at'

    问题背景: 线上的linux服务器上的mysql服务器中导出数据库的结构.想要在本地创建一个测试版本 导出后再本地mysql上运行却报错   1067 - Invalid default value ...

  8. mysql创建表的注意事项

    1 库名,表名,字段名必须使用小写字母,"_"分割. 2 库名,表名,字段名必须不超过12个字符. 3 库名,表名,字段名见名识意,建议使用名词而不是动词. 4 建议使用InnoD ...

  9. MYSQL创建表的约束条件(可选)

    一.常用的一些约束条件 一.创建表的完整语法1.创建表的万能模板:create table 库名.表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型 ...

随机推荐

  1. JS函数 函数调用 函数定义好后,是不能自动执行的,需要调用它,直接在需要的位置写函数名。

    函数调用 函数定义好后,是不能自动执行的,需要调用它,直接在需要的位置写函数名. 第一种情况:在<script>标签内调用. <script type="text/java ...

  2. 【CSP-S/J 2019】初赛注意事项

    UPD:10-25-13:33 正式成绩出了,省里500多名应该进了吧... UPD:10-20-10:07 现在又很慌啊,怎么感觉82又一点都不稳啊... 然后现在又不太想写文化课作业...我是不是 ...

  3. Redis问题整理

    Redis问题总结 1.单点登录的两个项目cookie不一致 由于在配置自定义Cookie的时候 @Bean("shiroCookie") public SimpleCookie ...

  4. Java中怎样实现字符串截取

    使用substring()对字符串进行截取: /** * str.indexOf()查找下标 * substring();//字符串截取 * length();//字符串长度 * */ @Test p ...

  5. leetcood学习笔记-79-单词搜索

    题目描述: 方法一;回溯 class Solution: def exist(self, board: List[List[str]], word: str) -> bool: max_x,ma ...

  6. 两个对象值相同 (x.equals(y) == true),但却可有不同的 hash code,这句话对不对?

    不对,如果两个对象x和y满足x.equals(y) == true,它们的哈希码(hash code)应当相同.Java对于eqauls方法和hashCode方法是这样规定的: (1)如果两个对象相同 ...

  7. eclipse查看源码的配置

    1.打开eclipse软件,点击window-preference 2.在弹出框中选择java-Installed JRES,选中的默认就行,然后点一下选中的,点击edit 3.弹出框中选择第二个,展 ...

  8. flink提交文件出现java.io.IOException:unable to close file because the last block does not have enough number of replicas异常

    当提交已经打包好的jar包时候,控制台出现以下的错误.

  9. PSCC2019常用基础操作

    一.常用设置 1.界面设置(快捷键Ctrl+K):可以对PS界面的颜色.导出格式.性能等等进行设置(这里暂存盘建议设置D盘或F盘,默认C盘). 2.常用面板整理(菜单栏->窗口) 二.常用快捷键 ...

  10. WinDBG常用断点命令

    WinDBG提供了多种设断点的命令: bp 命令是在某个地址 下断点, 可以 bp 0x7783FEB 也可以 bp MyApp!SomeFunction . 对于后者,WinDBG 会自动找到MyA ...