2、创建一个stu表,字段有:自增主键id,不为空姓名,默认值性别(枚举类型),无限制身高
create table stu(
id int primary key auto_increment,
name char not null,
gender enum('男','女') default '男',
height float
);
3、为stu表依次插入以下三条数据

​ i)插入一条包含id,name,gender,height四个信息的数据

insert into stu values('0','nick','男','176')

​ ii)插入一条name,gender,height三个信息的数据

insert into stu(name,gender,height) values('nick','男','176')

​ iii)插入一条只有name信息的数据

insert into stu(name) values('nick')
4、创建一张有姓名、年龄的teacher表,在最后添加工资字段,在姓名后添加id主键字段
create table teacher(
name char not null,
id int primary key auto_increment,
age int,
salary float
);
5、思考:将4中id字段移到到表的最前方,形成最终字段顺序为id、姓名、年龄、工资
alter table 表名
change name name char not null default 0 alter id;
6、完成 学生表 与 国家表 的 多对一 表关系的创建,并完成数据测试
create table country(
id int primary key auto_increment,
countryname varchar(64) not null,
);
create table stu(
id int primary key auto_increment,
name char not null,
gender enum('男','女') default '男',
height float
stu_id int, # 一对多的外键不能设置唯一
foreign key(stu_id) references country(id)
on update cascade
on delete cascade,
);
# 增:先增加被关联表(country)的数据,再增加关联表(stu)的数据
mysql>: insert into country(name) values('中国'),('美国'),('德国');
mysql>: insert into stu(name, height, stu_id) values
('西游哥', 172.0, 1),
('东游哥', 186.1, 1),
('python小弟', 194.9, 2),
('轮回大哥', 166.4, 3);
# 更新:直接更新被关联表的(country) 主键,关联表(stu) 外键 会级联更新
mysql>: update country set id=10 where id=1;
# 更新:直接更新关联表的(stu) 外键,修改的值对应被关联表(country) 主键 如果存在,可以更新成功,反之失败
mysql>: update stu set stu_id=2 where id=3; # 成功
mysql>: update stu set stu_id=1 where id=4; # 失败
# 删被关联表,关联表会被级联删除
mysql>: delete from country where id = 2;
# 删关联表,被关联表不会发生变化
mysql>: delete from stu where stu_id = 3;
7、完成 学生表 与 课程表 的 多对多 表关系的创建,并完成数据测试
create table course(
id int primary key auto_increment,
name char not null,
course_time int unsigned default 0 # 课时
);
create table stu(
id int primary key auto_increment,
name char not null,
age int unsigned default 0
);
# 作者与出版社关系表:id, author_id, publish_id
create table stu_course(
id int primary key auto_increment,
# 关系表一定有多个外键,关联着多张表
stu_id int,
foreign key(stu_id) references stu(id)
on update cascade
on delete cascade,
course_id int,
foreign key(course_id) references course(id)
on update cascade
on delete cascade,
# 建立两个字段的联合唯一
unique(stu_id, course_id)
);
# 注:关系表 关联着 stu 和 course 两张表,在表结构上 stu 与 course 两表键没有任何关系
# 增:两张被关联表,没有前后关系,但关系表必须在两个表都提供数据后才能进行 关系匹配
mysql>: insert into course(name, course_time) values('python', 67),('java', 76),('go',3);
mysql>: insert into stu(name, age) values('nick', 20),('tank', 19);
# 操作关系表:
mysql>: insert into stu_course(stu_id, course_id) values(1,1),(1,2),(2,1),(2,2),(3,1);
# 关系表操作:增、删、改,只要两张被关系表有提供对应的操作数据,都可以操作成功,且对两张被关系表没有影响
# 操作两张被关系表:
# 增:不会影响关系表
mysql>: insert into course(name, course_time) values('PHP', 60);
# 改:关系表都会级联更新
mysql>: update course set id=10 where id=1;
# 删:关系表都会级联删除
mysql>: delete from stu where name='tank';
8、完成 学生表 与 学生简介表 的 一对一 表关系的创建,并完成数据测试
create table stu_detail(
id int primary key auto_increment,
info varchar(256),
info_address varchar(256)
);
create table stu(
id int primary key auto_increment,
name varchar(64) not null,
mobile char(11) unique not null,
sex enum('男', '女') default '男',
age int default 0,
detail_id int unique not null,
foreign key(detail_id) references stu_detail(id)
on update cascade
on delete cascade,
);
# 必须先创建被关联表数据,有关联表外键关联的记录后,关联表才可以创建数据
mysql>: insert into stu_detail(info,info_address) values('Tom_info','Tom_address');
mysql>: insert into author(name,mobile,detail_id) values('Tom','13344556677', 1);
mysql>: insert into author_detail(info,info_address) values('Bob_info','Bob_address');
mysql>: insert into author(name,mobile,detail_id) values('Bob','15666882233', 2);
# 修改关联表 author
mysql>: update stu set detail_id=3 where detail_id=2; # 失败,3详情不存在
mysql>: update stu set detail_id=1 where detail_id=2; # 失败,1详情已被关联
mysql>: insert into stu_detail(info,info_address) values('Tom_info_sup','Tom_address_sup');
mysql>: update stu set detail_id=3 where detail_id=2; # 有未被其他数据关联的数据,就可以修改
# 删除关联表 author
mysql>: delete from stu where detail_id=3; # 直接删除
# 修改被关联表 author_detail
mysql>: update stu_detail set id=10 where id=1; # 级联修改,同步关系关联表外键
# 删除被关联表 stu_detail
mysql>: delete from stu where detail_id=10; # 可以删除对被关联表无影响
mysql>: insert into stu(name,mobile,detail_id) values('Tom','13344556677', 10);
mysql>: delete from stu_detail where id=10; # 可以删除,将关联表的记录级联删除掉
9、将 学生表、国家表、课程表、学生简介表 四个表放在一起考虑表关系,并完成数据的增删测试
create table country(
id int primary key auto_increment,
countryname varchar(64) not null,
);
create table stu_detail(
id int primary key auto_increment,
info varchar(256),
info_address varchar(256)
);
create table course(
id int primary key auto_increment,
name char not null,
course_time int unsigned default 0
);
create table stu(
id int primary key auto_increment,
name char not null,
stu_id int,
foreign key(stu_id) references country(id)
on update cascade
on delete cascade,
detail_id int unique not null,
foreign key(detail_id) references stu_detail(id)
on update cascade
on delete cascade,
);
create table stu_course(
id int primary key auto_increment,
stu_id int,
foreign key(stu_id) references stu(id)
on update cascade
on delete cascade,
course_id int,
foreign key(course_id) references course(id)
on update cascade
on delete cascade,
unique(stu_id, course_id)
);

日考

第9题

9、将 学生表、国家表、课程表、学生简介表 四个表放在一起考虑表关系,并完成数据的增删测试

MySQL小测试(2)的更多相关文章

  1. MySQL小测试

    <> 2.创建一个字符集为utf8的数据库,将数据库字符集修改为gbk create database db1 charset='utf8'; alter database db1 cha ...

  2. mysql基础测试

    mysql基础测试 测试原因   为什么需要做性能测试 模拟比当前系统更高的负载,找出性能瓶颈 重现线上异常 测试不同硬件软件配置 规划未来的业务增长   测试分类   性能测试的分类 设备层的测试 ...

  3. PHP中使用PDO操作事务的一些小测试

    关于事务的问题,我们就不多解释了,以后在学习 MySQL 的相关内容时再深入的了解.今天我们主要是对 PDO 中操作事务的一些小测试,或许能发现一些比较好玩的内容. 在 MyISAM 上使用事务会怎么 ...

  4. Cad 二次开发关于SelectCrossingPolygon和SelectFence返回结果Status为error的小测试

    CAD2008的二次开发,有个很奇怪的现象,只要你选择的点集不在当前视图上SelectCrossingPolygon和SelectFence返回结果Status就会为error,所以要获取正确的结果, ...

  5. paip.提升性能---mysql 性能 测试以及 参数调整.txt

    paip.提升性能---mysql 性能 测试以及 参数调整.txt 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://b ...

  6. MySQL Router 测试使用 转

    MySQL Router 测试使用 . 特性 MySQL Router 并没有包括一些特别新的特性, 总体上看中规中矩, 不过 first-available 和插件两个特性挺有意思, 后续会进行讲解 ...

  7. 使用mysqlslap进行MySQL压力测试

    使用mysqlslap进行MySQL压力测试发表于236 天前 ? MySQL ? 暂无评论 MySQL从5.1.4版开始带有一个压力测试工具mysqlslap,通过模拟多个并发客户端访问mysql来 ...

  8. mysql 通过测试'for update',深入了解行锁、表锁、索引

    mysql 通过测试'for update',深入了解行锁.表锁.索引 条件 FOR UPDATE 仅适用于InnoDB存储引擎,且必须在事务区块(BEGIN/COMMIT)中才能生效. mysql默 ...

  9. LINUX系统下MySQL 压力测试工具super smack

    摘要:1.源文件下载地址:http://vegan.net/tony/supersmack/2.安装:注意在编译时,可以先把对应的libmysqlclient.so.*拷贝到/usr/lib3.测试: ...

随机推荐

  1. NOI 2010 海拔(最小割转最短路)

    题意 https://www.lydsy.com/JudgeOnline/problem.php?id=2007 思路 首先可以发现一个结论,每个位置的海拔只有能是 \(0\) 和 \(1\) ,然后 ...

  2. 强烈推荐一个和朋友远程桌面的软件teamviewer

    强烈推荐一个和朋友远程桌面的软件teamviewer个人认为:贼溜

  3. kubeadm部署K8S集群v1.16.3

    本次先更新kubeadm快速安装K8S,二进制安装上次没写文档,后续更新,此次最新的版本是V1.16.3 1.关闭防火墙.关闭selinux.关闭swapoff -a systemctl stop f ...

  4. 联合CRF和字典学习的自顶向下的视觉显著性-全文解读

    top-down visual saliency via joint CRF anddictionary learning 自顶向下的视觉显著性是使用目标对象的可判别表示和一个降低搜索空间的概率图来进 ...

  5. 一步一步的理解javascript的预编译

    首先,我们要知道javascript是单线程.解释性语言.所谓解释性语言,就是翻译一句执行一句.而不是通篇编译成一个文件再去执行. 其实这么说还没有这么直观,读一句执行一句那是到最后的事了.到JS执行 ...

  6. Prometheus 监控目标运行状态并邮件通知

    Prometheus 监控目标运行状态并邮件通知 邮件服务安装:https://www.cnblogs.com/xiangsikai/p/9809654.html 告警规则示例:https://pro ...

  7. 错误:error: failed to push some refs to 'https://github.com/pzq7025/KG.git'的解决办法

    一.问题在进行[git push orgin master]的时候出现如下错误 ! [rejected] master -> master (non-fast-forward) error: f ...

  8. Go语言-1-标识符与变量

    目录 1. Go标识符 1.1 Go关键字 1.2 常量标识符(4个) 1.3 空白标识符(1个) 1.4 内置数据类型标识符 1.5 内置函数(15个) 2. Go语言操作符 3. Go语言变量 3 ...

  9. Redis-1-简介与安装

    目录 1.Redis 简介 2.安装Redis 1.安装gcc redis是c语言编写的 2.下载redis安装包,在root目录下执行 3.解压redis安装包 4.进入redis目录 5.编译安装 ...

  10. pom文件语法无误却报红叉(Unknown error)的可能原因

    本文链接:https://blog.csdn.net/Little_Stars/article/details/94553090可能原因及解决办法: >> 缓存问题,只要 “Maven - ...