<每日一题>题目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 类型 ...
随机推荐
- 2019-5-21-win10-uwp-xaml-兼容多个版本条件编译
title author date CreateTime categories win10 uwp xaml 兼容多个版本条件编译 lindexi 2019-5-21 11:19:3 +0800 20 ...
- 2018-12-22-WPF-在绑定表达式添加计算
title author date CreateTime categories WPF 在绑定表达式添加计算 lindexi 2018-12-22 16:12:56 +0800 2018-12-22 ...
- 笔记:简单的面向对象-web服务器
import socket import re import multiprocessing import time import mini_frame class WSGIServer(object ...
- 随笔记录 grub引导故障修复 2019.8.7
系统备份: [root@localhost ~]# mkdir /abc [root@localhost ~]# mount /dev/sdb1 /abc [root@localhost ~]# dd ...
- LeetCode第二题—— Add Two Numbers(模拟两数相加)
Description: You are given two non-empty linked lists representing two non-negative integers. The di ...
- js结巴程序
var str="我.....我是一个个......帅帅帅帅哥!"; var reg=/\./gi; str=str.replace(reg,""); reg= ...
- jvm虚拟内存分布 与 GC算法
jvm虚拟内存分布 程序计数器(PC寄存器)(线程私有): 每个线程启动的时候,都会创建一个PC(Program Counter,程序计数器)寄存器. PC寄存器的内容总是指向下一条将被执行指令的地址 ...
- 针对发送网络附件的java方法(使用Apache的jar包调用)
1.先要在pom.xml文件中引入对应的jar包 <!--添加邮件的网络附件 start--> <dependency> <groupId>org.apache.c ...
- CF 1063B Labyrinth
传送门 解题思路 看上去很简单,\(bfs\)写了一发被\(fst\)...后来才知道好像一群人都被\(fst\)了,这道题好像那些每个点只经过一次的传统\(bfs\)都能被叉,只需要构造出一个一块一 ...
- JQ实现全选、全不选
代码: <table class="table myTable table-hover"> <thead> <tr> <th> &l ...