MySQL修炼之路二
1. 表字段的操作
1. 语法: alter table 表名 执行动作;
2. 添加字段(add)
alter table 表名 add 字段名 数据类型;
alter table 表名 add 字段名 数据类型 first;
alter table 表名 add 字段名 数据类型 after 字段名;
3. 删除字段(drop)
alter table 表名 drop 字段名;
4. 修改数据类型(modify)
alter table 表名 modify 字段名 新数据类型;
5. 表重命名
alter table 表名 rename 新表名;
6. 练习
1. 创建库 studb2
create database studb2;
2. 在库中创建表 t1, 字段有3个:name, age, phnumber
use studb2;
create table t1(
name char(15),
age tinyint unsigned,
phnumber int);
3. 查看表结构
desc t1;
4. 在表中第一列添加一个id 字段
alter table t1 add id int first;
5. 把 phnumber 的数据类型改为 bigint
alter table t1 modify phnumber bigint;
6. 在表中最后一列添加一个字段 address
alter table t1 add address char(100);
7. 删除表中的 age 字段
alter table t1 drop age;
8. 查看表结构
desc t1;
2. 表记录管理
1. 删除表记录
1. delete from 表名 where 条件;
2. 注意
delete 语句后如果不加where条件,所有记录全部清空
2. 更新表记录
1. update 表名 set 字段1=值1,字段2=值2,... where 条件;
2.注意
必须加where条件
3. 练习
1. 查找所有蜀国人的信息
select * from hero where country='蜀国';
2. 查找所有女英雄的姓名、性别和国家
select name,sex,country from hero where sex='女';
3. 把id位2的记录改为典韦,性别男,国家魏国
update hero set name='典韦', sex='男', country='魏国' where id=2;
4. 删除所有蜀国英雄
delete from hero where country = '蜀国';
5. 把貂蝉的国籍改为魏国
update hero set country='魏国' where name='貂蝉';
6. 删除所有表记录
delete from hero;
3. 运算符操作
1. 数值比较/字符比较
1. 数值比较 : = != > >= < <=
2. 字符比较 : = !=
3 练习
1. 查找攻击力高于150的英雄的名字和攻击值
select name,gongji from sanguo where gongji > 150;
2. 将赵云的攻击力设置位360,防御力设置位68
update sanguo set gongji=360, fangyu=68 where name='赵云';
2. 逻辑比较
1. and(两个或多个条件同时成立)
2. or(任意一个条件成立即可)
3. 练习
1. 找出攻击值高于200的蜀国英雄的名字、攻击力
select name,gongji from sanguo where gongji > 200 and country='蜀国';
select name as n, gongji as g from sanguo where gongji > 200 and country='蜀国';
2. 将吴国英雄中攻击值位110的英雄的攻击值改为100,防御力改为60
update sanguo set gongji=100, fangyu=60 where country='吴国' and gongji=110;
3. 查找蜀国和魏国的英雄信息
select * from sanguo where country='蜀国' or country='魏国';
3. 范围内比较
1. between 值1 and 值2
2. where 字段名 in(值1, 值2, ...)
3. where 字段名 not in(值1, 值2, ...)
4. 练习
1. 查找攻击值100-200的蜀国英雄信息
select * from sanguo where gongji between 100 and 200 and country='蜀国';
2. 找到蜀国和吴国以外的国家的女英雄信息
select * from sanguo where country not in('蜀国', '吴国') and sex='女';
3. 找到id 位1、3或5的蜀国英雄和貂蝉的信息
select * from sanguo where (id in(1,3,5) and country='蜀国') or name='貂蝉';
4. 匹配空、非空
1. 空:where name is null
2. 非空:where name is not null
3. 示例:
1. 姓名位NULL值的蜀国女英雄
select * from sanguo
where name is null and country='蜀国' and sex = '女';
2. 姓名位“”的英雄信息
select * from sanguo where name="";
4. 注意
1. NULL:空值,只能用is 或者 is not 去匹配
2. "" :空字符串,用 = 或者 != 去匹配
5. 模糊比较
1. where 字段名 like 表达式
2. 表达式
1. _: 匹配单个字符
2. %:匹配0到多个字符
3. 示例
select name from sanguo where name like "_%_";
select name from sanguo where name like "%";
# NULL 不会被统计,只能用is、is not 去匹配
select name from sanguo where name like "___";
select name from sanguo where name like "赵%";
4. SQL查询
1. 总结
3.select ... 聚合函数 from 表名
1. where ...
2. group by ...
4. having ...
5. order by ...
6. limit ...;
2. order by
1. 给查询结果进行排序
2. ... order by 字段名 升序/降序
3. 升序:ASC
降序:DESC
4. 示例
1. 将英雄按防御值从高到低排序
select * from sanguo order by fangyu desc;
2. 将蜀国英雄按攻击值从高到低排序
select * from sanguo order by gongji desc;
3. 将魏蜀两国英雄中名字为三个字的按防御值升序排列
select * from sanguo where country in('魏国', '蜀国') and name like '___' order by fangyu;
3. limit(永远放在SQL语句的最后写)
1. 作用:限制显示查询记录的个数
2. 用法
1. limit n -> 显示n条记录
2. limit m, n
m 表示 从第m+1条记录开始显示,显示 n 条
limit 2, 3 : 第 3、4、5 三条记录
3. 示例
1. 在蜀国英雄中,查找防御值倒数第二名至倒数第四名的英雄的记录
select * from sanguo
where country='蜀国'
order by fangyu asc
limit 1, 3;
2. 在蜀国英雄中,查找攻击值前3名且名字不为 NULL的英雄的姓名、攻击值和国家
select name,gongji,country from sanguo
where country="蜀国" and name is not null
order by gongji desc
limit 3;
4. 分页
每页显示5条记录,显示第4页的内容
第1页: limit 0,5 # 1 2 3 4 5
第2页: limit (2-1)*5,5 # 6 7 8 9 10
第3页: limit (3-1)*5,5 # 11 12 13 14 15
第4页; limit (4-1)*5,5 # 16 17 18 19 20
.....
每页显示n条记录,显示第m页:limit (m-1)*n, n
4. 聚合函数
1. 分类
avg(字段名) : 求该字段平均值
sum(字段名): 求和
max(字段名):求最大值
min(字段名): 求最小值
count(字段名): 统计该字段记录的个数
2. 示例
1. 攻击最强值是多少
select avg(gongji) from MOSHOU.sanguo;
select avg(gongji) as best from MOSHOU.sanguo;
select max(gongji) from MOSHOU.sanguo;
2. 统计id、name两个字段分别有几条记录
select count(id), count(name) from sanguo;
## 空值 NULL 不会被统计
3. 计算蜀国英雄的总攻击力
select sum(gongji) from MOSHOU.sanguo where country='蜀国';
4. 统计蜀国英雄中攻击值大于200的英雄的数量
select count(*) from MOSHOU.sanguo where country='蜀国' and gongji > 200;
MySQL修炼之路二的更多相关文章
- MySQL学习之路(二)——数据类型和操作数据表
数据类型和操作数据表 2.1 MySQL类型之整型 2.2 MySQL数据类型之浮点型 2.3 日期时间型 DATE 1支持时间:1000年1月1日~9999年12月31日 DATETIME 3 支持 ...
- MySQL修炼之路四
1. 外键(foreign key) 1. 定义:让当前表字段的值在另一个表的范围内选择 2. 语法 foreign key(参考字段名) references 主表(被参考字段名) on delet ...
- MySQL修炼之路一
1. MySQL概述 1. 什么是数据库 存储数据的仓库 2. 都有哪些公司在用数据库 金融机构.游戏网站.购物网站.论坛网站 ... ... 3. 提供数据库服务的软件 1. 软件分类 MySQL. ...
- MySQL修炼之路五
1. 存储引擎和锁 1. 存储引擎(处理表的处理器) 1. 基本操作 1. 查看所有存储引擎 mysql>show engines; 2. 查看已有表的存储引擎 mysql>show cr ...
- MySQL修炼之路三
1. SQL查询 1. 执行顺序 3. select ... 聚合函数 from 表名 1. where ... 2. group by ... 4. having ... 5. order by . ...
- 微博MySQL优化之路--dockone微信群分享
微博MySQL优化之路 数据库是所有架构中不可缺少的一环,一旦数据库出现性能问题,那对整个系统都回来带灾难性的后果.并且数据库一旦出现问题,由于数据库天生有状态(分主从)带数据(一般还不小),所以出问 ...
- Java工程师修炼之路(校招总结)
Java工程师修炼之路(校招总结) 前言 在下本是跨专业渣考研的985渣硕一枚,经历研究生两年的学习积累,有幸于2019秋季招聘中拿到几个公司的研发岗offer,包括百度,阿里,腾讯,今日头条,网易, ...
- MySQL学习之路(一)——初涉MySQL。
MySQL学习之路(一) 1.1MySQL的概述 MySQL由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一个开源的关系型数据库管理系统. MySQL分为社区版和企业版. 1 ...
- iOS攻城狮修炼之路
自己总结的学习iOS的笔记,打造一个全面的知识体系,iOS攻城狮修炼之路[持续更新中] iOS学习笔记01-APP相关 iOS学习笔记02-UIScrollView iOS学习笔记03-UITable ...
随机推荐
- python27期day07:基础数据类型补充、循环删除的坑、二次编码、作业题。
1.求最大位数bit_length: a = 10 #8421 1010print(a.bit_length())结果:42.capitalize首字母变大写: s = "alex" ...
- LG4782 「模板」2-SAT问题 2-SAT
问题描述 LG4782 题解 对于一个限制条件,建边如下: 如果\(x,-x\)在同一个强联通分量里,则不行,否则可以 构造方案:输出\(bel_i<bel_{i+n}\) \(\mathrm{ ...
- [LeetCode] 875. Koko Eating Bananas 科科吃香蕉
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The g ...
- pytorch的state_dict()拷贝问题
先说结论,model.state_dict()是浅拷贝,返回的参数仍然会随着网络的训练而变化.应该使用deepcopy(model.state_dict()),或将参数及时序列化到硬盘. 再讲故事,前 ...
- Debian/Ubuntu下安装Apache的Mod_Rewrite模块的步骤
启用 Mod_rewrite 模块:sudo a2enmod rewrite 另外,也可以通过将 /etc/apache2/mods-available/rewrite.load 连接到 /etc/a ...
- CodeChef MAXDTREE(DP套DP)
题意 链接:https://www.codechef.com/problems/MAXDTREE 给定一个 \(n\) 个节点的树,其中 \(1\) 为根节点,每个点有点权,我们定义" ...
- STM32Cube生成的HID项目,找不到hUsbDeviceFS
症状 在main中尝试发消息给上位机: 解决方法 在STM32生成的HID项目里,默认是没有把hUsbDeviceFS导出的,需要修改usb_device.h文件,在USER CODE BEGIN V ...
- rocketmq常用命令整理
1. 启动namesrv和borker sh /opt/alibaba-rocketmq/bin/runserver.sh com.alibaba.rocketmq.namesrv.NamesrvSt ...
- 用 ubuntu 自带的 gome-screenshot 来实现类似QQ截图那样的功能,同时设置键盘快捷键
在window下习惯了使用ctrl+Alt+A截图,在linux还真有点不习惯,所以下面介绍一下替代的用法. 打开 ubuntu 的系统设置-->键盘-->快捷键:界面如下: 01 添加一 ...
- Key ssd_300_vgg/block3_box/L2Normalization/gamma not found in checkpoint的解决方案
在Tensorflow下使用SSD模型训练自己的数据集时,经过查找很多博客资料,已经成功训练出来了自己的模型,但就是在测试自己模型效果的时候,出现了如下错误. 2019-10-27 14:47:12. ...