MySQL 基本学习(1)
June 15, 2019
注:本文命令是在 Mac 终端下执行的
✒️ 连接数据库
// 格式:
mysql -u 用户名 -p
// 示例:
mysql -u root -p
// 此处会提示你输入密码(安装时设置的)
✒️ 断开连接
exit或quit
✒️ 其他命令
// 查看版本(连接数据库之后执行)
select version();
// 显示当前时间(连接数据库之后执行)
select now();
// 命令结尾的( ; )不能省略
✒️ 数据库操作
// 创建数据库
// 格式:
create database 数据库 charset=utf8;
// 示例:
create database person charset=utf8;
// 删除数据库
// 格式:
drop database 数据库名;
// 示例:
drop database person;
// 切换数据库
// 格式:
use 数据库名;
// 示例:
use person;
// 查看当前选择的数据库
select database();
✒️ 数据库表操作
// 查看数据库中的所有表
show tables;
// 创建表
// 格式:
create table 表名(列及类型);
// 示例:
create table student(id int auto_increment primary key, name varchar(20) not null, age int not null, gender bit default 1, address varchar(20));
// id, name, age: 等为字段名
// auto_increment: 表示自增长
// primary key: 表示主键
// int, varchar(20): 等为数据类型, 20为可存储的字节数
// not null: 表示不为空
// default: 为设置默认值
// 删除表
// 格式:
drop table 表名;
// 示例:
drop table student;
// 查看表结构
// 格式:
desc 表名;
// 示例:
desc student;
// 查看建表语句
// 格式:
show create table 表名;
// 示例:
show create table student;
// 重命名表名
// 格式:
rename table 原表名 to 新表名;
// 示例:
rename table car to newcar;
// 修改表
// 格式:
alter table 表名 add|change|drop 列名 类型;
// 示例:
alter table newcar add isDelete bit default 0;
✒️ 数据操作
// 增
// 全列插入
// 格式:insert into 表名 values(...);
// 示例:
insert into student values(0, "tom", 19, 1, "北京", 0);
// 说明:主键列是自动增长,但是在全列插入时需要占位,通常使用0,插入成功以后以实际数据为准
// 缺省插入
// 格式:
insert into 表名(列1,列2,……) values(值1,值2,……);
// 示例:
insert into student(name, age, address) values("titan", 19, "上海");
// 同时插入多条数据
// 格式:
insert into 表名 values(...),(...),……
// 示例:
insert into student values(0, "jun", 18, 0, "北京", 0), (0, "poi", 22, 1, "海南", 0), (0, "coder", 20, 0, "石家庄", 0);
// 删
// 格式:
delete from 表名 where 条件;
// 示例:
delete from student where id=4;
// 注意:没有条件是全部删除,慎用
// 改
// 格式:
update 表名 set 列1=值1,列2=值2,…… where 条件;
// 示例:
update student set age=16 where id=7;
// 注意:没有条件是全部列都修改,慎用
// 查
// 说明:查询表中的全部数据
// 格式:
select * from 表名;
// 示例:
select * from student;
✒️ 查询数据
基本语法
格式:
select * from 表名;
说明:
- from 关键字后面是表名,表示数据来源于这张表
- select 后面写表中的列名,如果是 * 表示在结果集中显示表中的所有列
- 在 select 后面的列名部分,可以使用 as 为列名起别名,这个别名显示在结果集中
- 如果要查询多个列,之间使用逗号分隔
示例:
//查询所有数据
select * from student;
//查询某列数据
select name, age from student;
//以别名显示搜索结果
select name as a, age from student;
消除重复行
在select后面列前面使用distinct可以消除重复的行
示例:
select gender from student;
select distinct gender from student;
条件查询
// 1、语法
select * from 表名 where 条件
// 2、比较运算符
等于 =
大于 >
小于 <
大于等于 >=
小于等于 <=
不等于 !=或<>
需求:查询id值大于8的所有数据
示例:select * from student where id>8;
// 3、逻辑运算符
and 并且
or 或者
not 非
需求:查询id值大于7的女同学
示例:select * from student where id>7 and gender=0;
// 4、模糊查询(like)
%: 表示任意多个任意字符
_: 表示一个任意字符
需求:查询姓习的同学
示例:
select * from student where name like "习%";
select * from student where name like "习_";
// 5、范围查询
in 表示在一个非连续的范围内
between...and... 表示在一个连续的范围内
需求:查询编号为8、10、12的学生
示例:select * from student where id in (8,10,12);
需求:查询编号为6到8的学生
示例:select * from student where id between 6 and 8;
// 6、空判断
注意:null与""是不同的
判断空:is null
判断非空: is not null
需求:查询没有地址的同学
示例:select * from student where address is null;
需求:查询有地址的同学
示例:select * from student where address is not null;
// 7、优先级
小括号,not 比较运算符,逻辑运算符
and比or优先级高,如果同时出现并希望先选or,需要结合()来使用
聚合操作
为了快速等到统计数据,提供了5个聚合函数
- count(): 表示计算总行数,括号中可以写和列名
- max(列): 表示求此列的最大值
- min(列): 表示求此列的最小值
- sum(列): 表示求此列的和
- avg(列): 表示求此列的平均值
//需求:查询学生总数
select count(*) from student;
//需求:查询女生的编号最大值
select max(id) from student where gender=0;
//需求:查询女生的编号最小值
select min(id) from student where gender=0;
//需求:查询所有学生的年龄和
select sum(age) from student;
//需求:查询所有学生的年龄平均值
select avg(age) from student;
分组
- 按照字段分组,表示此字段相同的数据会被放到一个集合中。
- 分组后,只能查询出相同的数据列,对于有差异的数据列无法显示在结果集中
- 可以对分组后的数据进行统计,做聚合运算
- 语法:
- select 列1,列2,聚合…… from 表名 group by 列1,列2,列3,……;
- 需求:查询男女生总数
- 示例:
select gender,count(*) from student group by gender;
select name,gender,count(*) from student group by gender,age;
分组后的数据筛选:
select 列1,列2,聚合…… from 表名 group by 列1,列2,列3,…… having 列1,……聚合……;
示例:select gender,count(*) from student group by gender having gender;
where 与 having 的区别:
- where 是对 from 后面指定的表进行筛选,属于对原始数据的筛选
- having 是对 group by的结果进行筛选
排序
语法:
select * from 表名 order by 列1 asc|desc,列2 asc|desc , ……;
说明:
- 将数据按照列1进行排序,如果某些列1的值相同,则按照列2进行排序
- 默认按照从小到大的顺序排序
- asc: 升序
- desc: 降序
//需求:将没有被删除的数据按年龄排序
select * from student where isDelete=0 order by age desc;
select * from student where isDelete=0 order by age desc, id desc;
分页
- 语法:select * from 表名 limit start,count;
- 说明:start索引从0开始
- 示例:
select * from student limit 0,3;
select * from student limit 3,3;
select * from student where gender=1 limit 0,3;
关联
// 建表语句:
// 1、
create table class(id int auto_increment primary key, name varchar(20) not null, stuNum int not null);
// 2、
create table students(id int auto_increment primary key, name varchar(20) not null, gender bit default 1, classid int not null, foreign key(classid) references class(id));
// 查询所有数据
select * from students;
/* 关联查询:
分类:
1、表A inner join 表B:
表A与表B匹配的行会出现在结果集中
2、表A left join 表B:
表A与表B匹配的行会出现在结果集中,外加表A中独有的数据,未对应的数据使用null填充
3、表A right join 表B:
表A与表B匹配的行会出现在结果集中,外加表B中独有的数据,未对应的数据使用null填充
*/
select students.name,class.name from class inner join students on class.id=students.classid;
select students.name,class.name from class left join students on class.id=students.classid;
select students.name,class.name from class right join students on class.id=students.classid;
以上,是 MySQL 中一些常用的命令行。