外观
数据库基础
数据库(DataBase,DB)为数据集合(文件系统)。
数据库管理系统(DataBase Management system,DBMS)为操作和管理数据库的大型软件,用于建立、使用和维护数据库,对数据库进行统一管理和控制,用户通过数据库管理系统访问数据库中的数据。
常用数据库
- Oracle:关系型 DBMS,运行稳定、功能齐全、性能超群、技术领先,主要应用在大型的企业数据库领域。
- SQL Server:Microsoft 产品。软件界面友好、易学易用,在操作性和交互性方面独树一帜。
- MySQL:被广泛用于中小型应用系统,体积小、速度快、总体拥有成本低,开放源码。2008 年被 SUN 收购,2009年 SUN 被 Oracle 收购。
- MongoDB:流行的开源的非关系型数据库。
关系型数据库使用表结构,每行数据必须符合表结构,使用标准化的 SQL 语言。
非关系型数据库使用类似 JSON 的 BSON 格式存储数据,同一集合内的文档结构可以不同。
MySQL
bash
# 启动服务
net start mysql
# 停止服务
net stop mysql
# 登录,-p 后输入密码,也可回车后输入
mysql -u root -p
# 访问他人的 MySQL
mysql -h ip地址 -u root -p数据类型
- int:整型,还有 smallint、bigint。
- float/double:浮点型,如
double(5,2)表示最多5位,其中必须有2位小数,即最大值为999.99。 - char:固定长度字符串类型,如
char(10)。数据库里只有单引号。 - varchar:可变长度字符串类型,如
varchar(10)。 - text:字符串类型,可存大量字符。
- blob:字节类型,存储图片音频视频。
- date:日期类型,格式为
yyyy-MM-dd。 - time:时间类型,格式为
hh:mm:ss。 - timestamp:时间戳类型,
yyyy-MM-dd hh:mm:ss,会自动赋值。 - datetime:日期时间类型,
yyyy-MM-dd hh:mm:ss。
SQL
SQL(Structured Query Language,结构化查询语言)为关系数据库语言,可用来定义和操作数据,维护数据的完整性和安全性,以及进行各种数据库的管理等。
特点
- 不是某个特定数据库供应商专有的语言,几乎所有重要的数据库管理系统都支持 SQL。
- 简单易学,该语言的语句都是由描述性很强的英语单词组成,且这些单词数目不多。
- 高度非过程化,即用 SQL 操作数据库,只需指出“做什么”,无须指明“怎么做”,存取路径的选择和操作的执行由 DBMS 自动完成。
- 不区分大小写。
分类
- DDL:数据定义语言,用来创建数据库对象(库、表、列等)。
- DML:数据操作语言,用来操作数据库表中的记录(数据)。
- DQL:数据查询语言,用来查询记录(数据)。
- DCL:数据控制语言,用来定义访问权限和安全级别。
DDL
关键字:create、alter、drop。
bash
# 创建一个名为 mydb1 的数据库
create database mydb1;
# 创建一个使用 GBK 字符集的 mydb2 数据库
create database mydb2 character set gbk;
# 查看数据库的定义信息
show create database mydb2;
# 创建一个使用 GBK 字符集并带校对规则的 mydb3 数据库
create database mydb3 character set gbk collate gbk_chinese_ci;
# 查询服务器支持的字符集和校对规则
show character set;
# 删除数据库
drop database mydb3;
# 将 mydb2 的字符集修改为 utf8,将 create 改为 alter 即可
alter database mydb2 character set utf8;
# 查看所有数据库
show databases;
# 查看当前使用的数据库
select database();
# 切换数据库
use mydb2;
# 创建数据表
create table emp ( # emp 为表名
id int, # 字段 字段类型,字段间逗号隔开
name varchar(10),
sex varchar(10),
birthday date,
salary double # 最后一个字段不能加逗号
);
# 查看当前数据库中的所有表
show tables;
# 查看表的字段信息
desc emp;
# 在上面员工表的基础上增加一个 image 列
alter table emp add image blob;
# 修改 name 列使其长度为 20
alter table emp modify name varchar(20);
# 删除 image 列,一次只能删一列
alter table emp drop image;
# 表名改为 user
rename table emp to user;
# 显示表格的创建细节
show create table user;
# 格式化后显示表格的创建细节
show create table user\G
# 修改表的字符集为 GBK
alter table user character set gbk;
# 列名 name 修改为 username
alter table user change name username varchar(100);
# 删除表
drop table user;DML
关键字:insert、update、delete。
字符串类型和日期类型都要用单引号括起来:'Sam'、'2017-08-01'。
空值:null。
bash
# 向 emp 表中添加数据
insert into emp (id, name, sex, birthday, salary)
value (1011, 'Sam', '男', '2008-08-08', 8000);
# 如果全都给值可写成
insert into emp value (1011, 'Sam', '男', '2008-08-08', 8000);
# 也可批量插入
insert into emp value
(1012, 'Tom', '男', '1994-05-07', 7500),
(1013, 'May', '女', '1990-07-25', 80000);
# 将所有员工薪水修改为 5000
update emp set salary = 5000;
# 将 Tom 的薪水改为 6000
update emp set salary = 6000 where name = 'Tom';
# 将 May 的薪水改为 85000,生日改为 1988-6-8
update emp set salary = 85000,birthday = '1988-6-8' where name = 'May';
# 将 Sam 的薪水在原有基础上加 1000
update emp set salary = salary + 1000 where name = 'Sam';
# 删除表中所有数据
delete from emp;
# 删除表中名为 Tom 的记录
delete from emp where name = 'Tom';
# 使用 truncate 删除表中记录
truncate table emp;delete 删除表中的数据(一行行删),表结构还在,删除后的数据可找回。
truncate 删除是把表直接 drop 掉,然后再创建一个同样的新表,删除的数据不能找回,执行速度比 delete 快。
DQL
关键字:select。
数据库执行 DQL 语句不会对数据进行改变而是让数据库发送结果集给客户端,查询返回的结果集是一张虚拟表。
bash
# 查询表中的所有数据
select * from emp;
# 查询表中指定列
select name, age, birthday from emp;条件查询
条件查询就是在查询时给出 where 子句,在 where 子句中可使用如下运算符和关键字:
=、!=、<>、<、<=、>、>=,其中<>和!=一样between ... andin (),in里可放多个值,逗号隔开,相当于枚举。is null,判断是否为空值,is not null不为空andornot
操作符前后推荐加空格。
bash
# 查询性别为女,并且年龄大于等于 50 的记录
select * from stu where sex = 'female' and age >= 50;
# 查询学号为 S_1001 或姓名为 lisi 的记录
# name='lisi' 会返回 true 或 false
# 返回 false 则把那条数据剔除出去,再新建一张虚拟表,如此循环
select * from stu where id = 'S_1001' or name = 'lisi';
# 查询学号为S_1001、S_1002、S_1003的记录
select * from stu where id = 'S_1001' or id = 'S_1002' or id = 'S_1003';
# 或
select * from stu where id in ('S_1001', 'S_1002', 'S_1003');
# 查询学号不是S_1001、S_1002的记录
select * from stu where id not in ('S_1001', 'S_1002', 'S_1003');
# 查询年龄为null的记录
select * from stu where age is null;
# 不为空
select * from stu where age is not null;
# 查询年龄在20到40之间的学生记录
select * from stu where age >= 20 and age <= 40;
# 或
select * from stu where between 20 and 40;
# 查询性别非男生的学生记录
select * from stu where sex = 'female';
# 或
select * from stu where sex != 'male';
select * from stu where sex <> 'male';
select * from stu where not sex = 'male';模糊查询
当想查询姓名中包含 a 字母的学生时就需要使用模糊查询,模糊查询需要使用关键字 like。
通配符:_ 表示任意一个字符,% 表示任意个字符,即 0 ~ n。
bash
# 查询姓名由 5 个字母构成的学生记录
select * from stu where name like '_____';
# 查询姓名由 5 个字母构成,并且第五个字母为“i”的学生记录
select * from stu where name like '____i';
# 查询姓名以“z”开头的学生记录
select * from stu where name like 'z%';
# 查询姓名中第二个字母为“i”的学生记录
select * from stu where name like '_i%';
# 查询姓名中包含“a”字母的学生记录
select * from stu where name like '%a%';字段控制查询
去除重复记录(两行或以上记录中的数据都相同),例如 emp 表中 salary 字段就存在相同的记录,当只查询 emp 表的 salary 字段时,会出现重复记录,想去除重复记录,需要用 distinct 关键字。
bash
select distinct salary from emp;
# 查看雇员的月薪和佣金之和
# 任何数值和 null 进行计算都得 null
select salary + comm from emp;
# 考虑到 null 值的存在,可使用滤空函数 ifnull()
# 如果 comm 不为 null 则用原值,为 null 则走右边 0
select salary + ifnull(comm, 0) from emp;
# 给列起别名,则原来的列名 salary + ifnull(comm, 0) 就变为 all
# 起别名时 as 可以省略,空格默认是 as
select salary + ifnull(comm, 0) as all from emp;排序
使用 order by 加 asc/desc。
bash
# 查询所有学生记录,按年龄升序排序
# 因为 asc 默认,所以可省略
select * from stu order by age asc;
# 查询所有雇员,按月薪降序排序,如果月薪相同,按编号升序排序
select * from emp order by salary desc,id;
# 如果都按降序排序
select * from emp order by salary desc, id desc;聚合函数
聚合函数是用来做纵向运算的函数,函数名和左圆括号间推荐不加空格。
count()
统计指定列不为 null 的记录的行数。
bash
# 统计 age 字段不为 null 的记录行数
select count(age) from stu;
# 查询学生表中记录数
select count(*) from stu;
# 查询有佣金的人数,0 也会算入
select count(comm) from emp;
# 查询 emp 表中月薪大于 2500 的人数
select count(comm) from emp where salary > 2500;
# 统计月薪与佣金之和大于 2500 的人数
select count(*) from emp where (salary + ifnull(comm, 0)) > 2500;
# 查询有佣金的人数,和有领导的人数
select count(comm), count(mgr) from emp;
# 查询有佣金且有领导的人数
select count(*) from emp where comm is not null and mgr is not null;max()
计算指定列的最大值,如果指定列是字符串类型,就使用字符串排序运算。
bash
# 计算 age 字段中最大的值
select max(age) from stu;min()
计算指定列的最小值。
sum()
计算指定列的数值和,如果指定列不是数值类型,那计算结果为 0。 sum() 函数会自动滤空。
bash
# 查询所有雇员月薪的和
select sum(salary) from emp;
# 查询所有雇员的月薪和,以及所有雇员佣金和
select sum(salary), sum(comm) from emp;
# 查询所有雇员月薪加佣金和:
select sum(salary + ifnull(comm, 0)) from emp;
# 或
select sum(salary) + sum(comm) from emp;avg()
计算指定列的平均值,若不是数值类型,计算结果为 0。
bash
# 统计所有员工的平均工资
select avg(salary) from emp;分组查询
需要分组查询时要使用 group by 子句,例如查询每个部门的工资和,这说明要使用部门来分组。
bash
# 凡和聚合函数同时出现的列名,则一定要写在 group by 之后,这样才对应得上
select deptno, count(*) from emp group by deptno;
# 查询每个部门的部门编号和每个部门的工资和
# 先按条件分出一组再计算工资和
# 再分出一组计算工资和...最后剩下的作为一组计算工资和
select deptno, sum(salary) from emp group by deptno;
# 查询每个部门的编号和每个部门的人数
select deptno, count(*) from emp group by deptno;
# 查询每个部门编号以及每个部门工资大于1500的人数:
select deptno, count(*) from emp where salary > 1500 group by deptno;如果分组后还要筛选就用 having 子句。
bash
# 查询工资总和大于 9000 的部门编号以及工资和
select deptno, sum(salary) from emp group by deptno having sum(salary) > 9000;having 和 where 的区别:
having是在分组后对数据进行过滤,where是分组前对数据进行过滤。having后可使用聚合函数,where后不可使用where是对分组前记录的条件,如果某行记录没有满足where子句的条件,那么这行记录不会参加分组,而having是对分组后数据的约束。
查询语句书写顺序:
select—from—where—group by—having—order by—limit
查询语句执行顺序:
from—where—group by—having—select—order by—limit
bash
# 查询工资大于 1500,工资总和大于 6000 的部门编号以及工资和
select deptno, sum(salary) from emp where salary > 1500 group by deptno having sum(salary) > 6000 order by sum(salary);limit
limit 用以限定查询结果的起始行以及总行数。
bash
# 第一个参数,从第几行开始查询,第二个参数,一次查几行
select * from emp limit 0, 3;