数据库----chapter02
mysql 的存储引擎
存储引擎的类型
MyISAM、InnoDB、Memory、CSV等9种
MyISAM和InnoDB的类型区别
名称 | InnoDB | MyISAM |
---|---|---|
事务处理 | 支持 | 不支持 |
数据行锁定 | 支持 | 不支持 |
外键约束 | 支持 | 不支持 |
全文索引 | 不支持 | 支持 |
表空间大小 | 较大,约两倍 | 较小 |
使用场合
使用MyISAM:不需要事务,空间小,以查询访问为主
使用InnoDB:多删除,更新操作,安全性高,事务处理及并发控制
对储存引擎的相关操作
#查看当前的默认存储引擎
show variables like 'storage_engine%';
#修改存储引擎
default-storage-engine=InnoDB;
设置表的存储引擎
create table 表名(
....
)engine = 存储引擎;
DML和DQL语句
插入数据
插入单条数据记录
insert into 表名[(字段名列表)] values (值列表);
示例
insert into `student`(`loginPwd`, `phone`) values('123', '1234567890');
插入多条数据
insert into 表名(字段名列表) values(值列表1),(值列表2)...;
示例
insert into `subject`(`subjectName`, `classHour`, `gradeID`)
values('Logic java', 10, 1),('mysql', 10, 2),('html', 10,4);
将查询结果插入到新表中
create table `phoneList`(
select `studentName`, `phone` from `syudent`
);
数据更新
update 表名 set 字段1 = 值1, 字段2 = 值2... [where 条件];
示例
update `student` set sex = 'man';
update `student` set sex = 'man' where `name` = 'louis' and `age` = 23;
数据删除
delete from 表名 [where 条件];
truncate table 表名;
**Truncate语句删除后将重置自增列,表结构机器字段、约束、索引保持不变,执行速度比DELETE语句快 。 **
示例
delete from `student` where `studentName` = 'khan';
truncate table `student`;
数据查询
查询语法
select 列名|表达式|函数|常量 from `表名` [where 查询条件][order by 排序的列名[ASC或 DESC]]
示例
select `studentNo`, `studentName`, `phone` from `student` where `gradeId` = 1 order by `studentNo`;
查询全部的行和列
select * from `表名`;
查询部分列
select `列1`, `列2`... from `表名` where 条件;
使用AS命名列
示例
select `studentNo` as "学生编号", `studentName` as "学生姓名", `address` as "学生地址"
from `student`
where `address` <> "河南新乡";
用法
select `firstName` + "." + `lastName` as "姓名" from `employee`;
使用以上方法处理数据需要注意:
- + 连接的数据类型必须兼容。
- 如果 + 连接字符型数据,结果为字符串数据的连接。
- 如果 + 连接数值型数据,结果为数值的和。
where 查询条件后不能跟别名,其它后面都可以。
查询空行、常量列
查询空行
SELECT `studentName` from `student` where `email` is null;
原来有数据,但数据被清除的列如何查询?
使用常量列
select `studentName` as "姓名", `address` as "地址", `北京信息中心` as "学校名称" from `student`;
常用函数
聚合函数
函数名 | 作用 |
---|---|
AVG() | 返回某字段的平均值 |
COUNT() | 返回某字段的行数 |
MAX() | 返回某字段的最大值 |
MIN() | 返回某字段的最小值 |
SUM() | 返回某字段的和 |
使用方法
select 聚合函数(数值型字段) from `表名` where 条件;
字符串函数
函数名 | 作用 | 举例 |
---|---|---|
CONCAT(str1, str2,…strn) | 字符串连接 | select concat(‘My’, ‘S’, ‘QL’); 返回:MySQL |
INSERT(str, pos, len, newstr); | 字符串替换 | select insert(“这是SQL Server数据库”, 3, 10, “MySQL”); 返回:“这是MySQL数据库” |
LOWER(str) | 将字符串转为小写 | select lower(“MySQL”); 返回:mysql |
UPPER(str) | 将字符串转为大写 | select upper(“MySQL”); 返回:MYSQL |
SUBSTRING(str, num, len) | 字符串截取 | select substring(“JavaMySQLOracle”, 5, 5) 返回:MySQL |
时间日期函数
函数名 | 作用 | 举例 |
---|---|---|
CURDATE() | 获取当前日期 | select curdate(); 返回:2023-04-04 |
CURTIME() | 获取当前时间 | select curtime(); 返回:16:32:50 |
NOW() | 获取当前时间和日期 | select now(); 返回:2023-04-04 16:32:50 |
WEEK(date) | 返回日期date为一年中的第几周 | select week(now()); 返回:14 |
YEAR(date) | 返回日期date的年份 | select year(now()); 返回:2023 |
HOUR(time) | 返回时间time的小时值 | select hour(now()); 返回:16 |
MINUTE(time) | 返回时间time的分钟值 | select minute(now()); 返回:36 |
DATEDIFF(date1, date2) | 返回日期参数date1和date2之间相隔的天数 | select datediff(now(), “2022-01-01”); 返回:458 |
ADDDATE(date, n) | 计算日期参数date加上n天后的日期 | select adddate(now(), 5); 返回:2023-04-09 16:39:30 |
数学函数
函数名 | 作用 | 举例 |
---|---|---|
CEIL(x) | 返回大于或等于数值x的最小整数 | select ceil(2.3) 返回:3 |
FLOOR(x) | 返回小于或等于数值x的最大整数 | select floor(2.3) 返回:2 |
RAND() | 返回0~1的随机数 | select rand(); 返回:0.2345234145 |
子句
ORDER BY子句
实现按一定顺序显示查询结果。
示例
把成绩都降低10%后加5分,再查询及格成绩,并按照成绩从高到低排序。
select `studentNo` as "学生编号", (score*0.9 + 5) as "综合成绩" from `result`
where(score*0.9 + 5) >= 60
order by `score` desc;
LIMIT 子句
MySQL查询语句中使用LIMIT子句限制结果集。
语法
SELECT <字段名列表>
FROM <表名或视图>
[WHERE <查询条件>]
[GROUP BY <分组的字段名>]
[HAVING <筛选分组的条件>[只能和GROUP BY一起使用]]
[ORDER BY <排序的列名>[ASC 或 DESC]]
[LIMIT[位置偏移量], [行数]];
示例
问题:
查询所有年级编号为1的学员信息,按学号升序排序
1. 显示前4条记录
2. 每页四条记录, 显示2页, 即从第5条记录开始显示4条记录
-- 1.
select * from `student` where `gradeID` = 1
order by `studentNo`
limit 4;
-- 1.
select * from `student` where `gradeID` = 1
order by `studentNo`
limit 4, 4;