MySQL数据库

更新表记录(update)

update 表名 set 字段1=值1,字段2=值2,… where 条件;

注意:update语句后如果不加where条件,所有记录全部更新

e.g.
update class_1 set age=11 where name=‘Abby’;

删除表记录(delete)

delete from 表名 where 条件;

注意:delete语句后如果不加where条件,所有记录全部清空

e.g.
delete from class_1 where name=‘Abby’;

表字段的操作(alter)

语法 :alter table 表名 执行动作;

  • 添加字段(add)
    alter table 表名 add 字段名 数据类型;
    alter table 表名 add 字段名 数据类型 first;
    alter table 表名 add 字段名 数据类型 after 字段名;
  • 删除字段(drop)
    alter table 表名 drop 字段名;
  • 修改数据类型(modify)
    alter table 表名 modify 字段名 新数据类型;
  • 修改字段名(change)
    alter table 表名 change 旧字段名 新字段名 新数据类型;
  • 表重命名(rename)
    alter table 表名 rename 新表名;

e.g.
alter table interest add tel char(11) after name;

时间类型数据

时间和日期类型:

日期DATE,日期时间DATETIME,时间戳TIMESTAMP
时间TIME
年份YEAR
在这里插入图片描述

时间格式

date :“YYYY-MM-DD”
time :“HH:MM:SS”
datetime :“YYYY-MM-DD HH:MM:SS”
timestamp :“YYYY-MM-DD HH:MM:SS”
注意
1、datetime :以系统时间存储
2、timestamp :以标准时间存储但是查看时转换为系统时区,所以表现形式和datetime相同

e.g.
create table marathon (id int primary key auto_increment,athlete varchar(32),birthday date,regi

日期时间函数

now() 返回服务器当前日期时间,格式对应datetime类型
curdate() 返回当前日期,格式对应date类型
curtime() 返回当前时间,格式对应time类型

时间操作

查找操作

select * from marathon where birthday>=‘2000-01-01’;

select * from marathon where birthday>=“2000-07-01” and performance<=“2:30:00”;

日期时间运算
语法格式
select * from 表名 where 字段名 运算符 (时间-interval 时间间隔单位);
时间间隔单位: 2 hour | 1 minute | 2 second | 2 year | 3 month | 1 day

select * from marathon where registration_time > (now()-interval 7 day);

高级查询语句

模糊查询和正则查询

LIKE用于在where子句中进行模糊查询,SQL LIKE 子句中使用百分号 %来表示任意0个或多个字符,下划线_表示任意一个字符。

使用 LIKE 子句从数据表中读取数据的通用语法:

SELECT field1, field2,…fieldN
FROM table_name
WHERE field1 LIKE condition1

e.g.
mysql> select * from class_1 where name like ‘A%’;

mysql中对正则表达式的支持有限,只支持部分正则元字符

SELECT field1, field2,…fieldN
FROM table_name
WHERE field1 REGEXP condition1

e.g.
select * from class_1 where name regexp ‘^B.+’;

as 用法

在sql语句中as用于给字段或者表重命名

select name as 姓名,age as 年龄 from class_1;
select * from class_1 as c where c.age > 17;

排序

ORDER BY 子句来设定你想按哪个字段哪种方式来进行排序,再返回搜索结果。
使用 ORDER BY 子句将查询数据排序后再返回数据:

SELECT field1, field2,…fieldN from table_name1 where field1
ORDER BY field1 [ASC [DESC]]

默认情况ASC表示升序,DESC表示降序

select * from class_1 where sex=‘m’ order by age;

分页(限制)

LIMIT 子句用于限制由 SELECT 语句返回的数据数量 或者 UPDATE,DELETE语句的操作数量带有 LIMIT 子句的 SELECT 语句的基本语法如下:

SELECT column1, column2, columnN
FROM table_name
WHERE field
LIMIT [num]

联合查询

UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。

UNION 操作符语法格式:

SELECT expression1, expression2, … expression_n
FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, … expression_n
FROM tables
[WHERE conditions];

expression1, expression2, … expression_n: 要检索的列。
tables: 要检索的数据表。
WHERE conditions: 可选, 检索条件。
DISTINCT: 可选,删除结果集中重复的数据。默认情况下 UNION 操作符已经删除了重复数据,
所以 DISTINCT 修饰符对结果没啥影响。
ALL: 可选,返回所有结果集,包含重复数据。

select * from class_1 where sex=‘m’ UNION ALL select * from class_1 where age > 9;

子查询

定义 : 当一个select语句中包含另一个select 查询语句,则称之为有子查询的语句
子查询出现的位置:

  1. from 之后 ,此时子查询的内容作为一个新的表内容,再进行外层select查询
    select name from (select * from class_1 where sex=‘m’) as s where s.score > 90;

需要将子查询结果集重命名一下,方便where子句中的引用操作

  1. where字句中,此时select查询到的内容作为外层查询的条件值

select *from class_1 where age = (select age from class_1 where name=‘Tom’);

子句返回的结果需要一个明确值,即需要时某个记录的某个元素,不能是多行或者多列

聚合操作

聚合操作指的是在数据查找基础上对数据的进一步整理筛选行为,在认识聚合之前先看一个更完整的sql语句

select语句执行顺序

(7)SELECT
(8)[DISTINCT] <select_list>
(1)FROM <left_table>
(3)<join_type> JOIN <right_table>
(2)ON <join_condition>
(4)WHERE <where_condition>
(5)GROUP BY <group_by_list>
(6)HAVING <having_condition>
(9)ORDER BY <order_by_condition>
(10)LIMIT <limit_number>

聚合函数

方法功能
avg(字段名)该字段的平均值
max(字段名)该字段的最大值
min(字段名)该字段的最小值
sum(字段名)该字段所有记录的和
count(字段名)统计该字段记录的个数

eg1 : 找出表中的最大攻击力的值?

select max(attack) from sanguo;

eg2 : 表中共有多少个英雄?

select count(name) as number from sanguo;

eg3 : 蜀国英雄中攻击值大于200的英雄的数量

select count(*) from sanguo where attack > 200;

注意: 此时select 后只能写聚合函数,无法查找其他字段。

聚合分组

group by
给查询的结果进行分组
e.g. : 计算每个国家的平均攻击力

select country,avg(attack) from sanguo
group by country;

e.g. : 对多个字段创建索引,此时多个字段都相同时为一组

select age,sex,count(*) from class1 group by age,sex;

e.g. : 所有国家的男英雄中 英雄数量最多的前2名的 国家名称及英雄数量

select country,count(id) as number from sanguo
where gender=‘M’ group by country
order by number DESC
limit 2;

使用分组时select 后的字段为group by分组的字段和聚合函数,不能包含其他内容。group by也可以同时依照多个字段分组,如group by A,B 此时必须A,B两个字段值均相同才算一组。

聚合筛选

having语句

对分组聚合后的结果进行进一步筛选

eg1 : 找出平均攻击力大于105的国家的前2名,显示国家名称和平均攻击力
select country,avg(attack) from sanguo
group by country
having avg(attack)>105
order by avg(attack) DESC
limit 2;

注意
having语句通常与group by联合使用。
having语句存在弥补了where关键字不能与聚合函数联合使用的不足,where只能操作表中实际
存在的字段,having操作的是聚合函数生成的显示列

去重语句

distinct语句

不显示字段重复值

eg1 : 表中都有哪些国家
select distinct name,country from sanguo;
eg2 : 计算一共有多少个国家
select count(distinct country) from sanguo;

注意
distinct和from之间所有字段都相同才会去重

聚合运算

查询表记录时做数学运算

运算符 : + - * / %

eg1: 查询时显示攻击力翻倍
select name,attack2 from sanguo;
eg2: 更新蜀国所有英雄攻击力 * 2
update sanguo set attack=attack
2 where country=‘蜀国’;

# 聚合查询
select avg(attack), max(attack)
from sanguo
where country = '蜀';

select gender, count(*)
from sanguo
group by gender;

# 所有国家中,男英雄比较多的前两个国家,查看这两个
# 国家的国家名称和男英雄数量,展示时请按数量降序排序
# 展示
select country, count(*) as number
from sanguo
where gender = '男'
group by country
order by number desc
limit 2;

索引操作

索引概述

定义
索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息。
优点
加快数据检索速度,提高查找效率
缺点
占用数据库物理存储空间
当对表中数据更新时,索引需要动态维护,降低数据写入效率

索引分类

普通(MUL)
普通索引 :字段值无约束,KEY标志为 MUL

唯一索引(UNI)
唯一索引(unique) :字段值不允许重复,但可为 NULL,KEY标志为 UNI

主键索引(PRI)
一个表中只能有一个主键字段, 主键字段不允许重复,且不能为NULL,KEY标志为PRI。通常设置记录编号字段id,能唯一锁定一条记录

索引创建

主键索引的创建方法已经在数据表中介绍过了,下面是普通索引和唯一索引的创建方法:
创建表时直接创建索引

create table 表名(
字段名 数据类型,
字段名 数据类型,
index(字段名),
index(字段名),
unique(字段名)
);

在已有表中创建索引:

create [unique] index 索引名 on 表名(字段名);

查看索引

1、desc 表名;
–> KEY标志为:MUL 、UNI。
2、show index from 表名;

扩展: 借助性能查看选项去查看索引性能

show variables like ‘profiling’;
set profiling = 1; 打开功能 (项目上线一般不打开)
show profiles
查看语句执行信息

删除索引

drop index 索引名 on 表名;

# 索引
create index name_index on cls (name);
create table index_test
(
    id   int auto_increment,
    name varchar(20),
    primary key (id),
    index (name)
);

# 删除索引
drop index name on index_test;

已有表添加主键索引或自增长属性

alter table 表名 add primary key(id);
alter table 表名 modify id int auto_increment;

创建复合主键

primary key(uid,pid)
此时两个字段只要不都相同即可。

删除主键索引或自增长属性

alter table 表名 modify id int; 要先删除自增长,因为它只作用于主键字段
alter table 表名 drop primary key;

扩展:索引结构

mysql最为常用的就是使用B-Tree结构构建索引表。那么为什么树形结构能够加快查找速度呢?
二叉查找树
在这里插入图片描述
B 树是对查找结构的进一步优化,加快了区间查找效率:
B树
在这里插入图片描述
B+树
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值