本篇文章为mysql 操作比较全面的整理, mysql 基本的增删改查及一些聚合分组和关联查询。
目录
一、基本命令
1、连接数据
格式:mysql -u 用户名 -p
示例:mysql -u root -p
输入密码(安装时设置的)
2、退出登录
断开连接quit或exit
3、查看版本
(连接后可以执行)
示例:select version();
4、显示当前时间
(连接后可以执行)
示例:select now();
5、远程连接
格式:mysql -h ip地址 -u 用户名 -p
输入对方mysql密码
二、数据库操作
1、创建数据库
格式:create database数据库名 charset=utf8;
示例:create database test charset=utf8;
2、切换数据库
格式:use 数据库名;
示例:use test;
3、查看当前选择的数据库
示例:select database();
4、删除数据库
格式:drop database数据库名;
示例:drop database test;
三、表操作
1、查看当前数据库中所有表
show tables;
2、创建表
格式:create table 表名(列及类型);
auto_increment表示自增长
primary key表示主键
not null表示不为空
示例:
说明:
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),
isDelete bit default 0);
3、查看表结构
格式:desc 表名;
示例:desc student;
如下图:
4、查看建表语句
格式:show create table 表名;
示例:show create table student;
如下图:
5、重命名表名
格式:rename table 原表名 to 新表名;
示例:
rename table student to newStudent;
5、修改表
格式:alter table 表名 add | change | drop 列名类型;
示例:
alter table `newStudent`
modify column `gender` bit(1) null default 0 after `age`;
7、删除表
格式:drop table 表名;
示例:
drop table newStudent;
四、数据操作
1、增
全列插入
格式:insert into 表名values(...);
说明:主键列是自动增长,但是在全列插入时需要占位,
通常使用0,插入成功以后以实际数据为准
示例:
insert into student values(0,"tom",19,1,"北京",0);
缺省插入
格式:insert into 表名(列1,列2,…) values(值1,值2,......);
示例:
insert into student(name,age,address) values("lilei",19,"上海");
同时插入多条数据
格式:insert into 表名 values(...),(...),...
示例:
insert into student values(0,"韩梅",19,0,"北京",0),(0,"约翰",22,1,"美国", 0),(0,"李忠",20,0,"石家庄", 0);
2、删除
格式:delete from 表名 where 条件;
示例:delete from student where id = 3;
注意:没有条件是全部删除,慎用
3、修改
格式:update 表名 set 列1=值1,列2=值2,...... where 条件;
示例:update student set age=20 where id = 2;
注意:没有条件是全部列都修改
4、查询
说明:查询表中全部数据
格式:select * from 表名;
示例:select * from student;
如下图:
五、查询
1、基本语法
格式:select * from 表名;
说明:
a、from关键字后面是表名,表示数据来源于这张表
b、select后面写表中的列名,如果是*表示在结果集中显示表中的所有列
c、在select后面的列名部分,可以使用as为列起别名,这个别名显示在结果集中
d、如果要查询多个列,之间使用逗号分隔
示例:
select * from student;
select name, age from student;
select name as a, age from student;
2、消除重复行
在select后而列前面使用distinct可以消除重复的行
示例:
select age from student;
select distinct age from student;
如下图:
3、条件查询
a、语法
select * from 表名where 条件
b、比较运算符
等于 =
大于 >
小于 <
大于等于 >=
小于等于 <=
不等于 != 或 <>
需求:查询id值大于3的所有数据
示例:select * from student where id>3;
c、逻辑运算符
并且或者or非not
需求:查询id值大于4的女同学
示例:select * from student where id>4 and gender=0;
d、模糊查询
insert into student values(0,"刘一民",30,1,"北京",0);
insert into student values(0,"刘罗锅",66,1,"北京",0);
like
%表示任意多个任意字符
_表示一个任意字符
需求:查询姓刘的同学示例:
select * from student where name like "刘%”;
select * from student where name like “刘__”
e、范围查询
In 表示在一个非连续的范围内
between...and...表示在一个连续的范围内
需求:查询编号为4、6、8的学生
示例:select * from student where id in(4,6,8);
如下图:
需求:查询编号为6到8的学生
示例:select *from student where id between 6 and 8;
如下图:
f、空判断
insert into student(name,age) values("特朗普",70);
注意:nu11与""是不同
判断空:is null
判断非空:is not null
需求:查询没有地址的同学
示例:select * from student where address is null;
如下图:
需求:查询有地址的同学
示例:select * from student where address is not null;
如下图:
g、优先级
小括号,not比较运算符,逻辑运算符and比or优先级高;
如果同时出现并希望先选or,需要结合()来使用。
4、聚合
为了快速等到统计数据,提供了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;
5、分组
按照字段分组,表示此字段相同的数据会被放到一个集合中。
分组后,只能查询出相同的数据列,对于有差异的数据列无法显示在结果集中
可以对分组后的数据进行统计,做聚合运算
语法:select 列1,列2,聚合...from 表名 group by 列1列2,列3,
需求:查询男女生总数
示例:select gender,count(*)from student group by gender;
分组后的数据筛选:select 列1,列2,聚合.... from 表名group by 列1,列2,列3,…
分组后的数据筛选:
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的结果进行筛选
6、排序
语法:select * from 表名 order by 列1 asc | desc,列2 asc | desc说明:
a、.将数据按照列1进行排序,如果某些列1的值相同,则按照列2进行排序
b、默认按照从小到大的顺序排序
c、asc升序
d、desc降序
需求:将没有被删除的数据按年龄排序
示例:
select * from student where isDelete=0 order by age desc;
select * from student where isDelete=0 order by age desc, id desc;
7、分页
语法:select * from 表名limit start,count;
说明:start索引从8开始
示例:
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, strNum int not null);
2、create table student (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))
插入一些数据:
insert into class values(0, “一班”, 30), (0, “二班”, 30),(0, “三班”, 40),(0, “四班”, 50);
insert into student value(0, “张三”, 1, 1);
insert into student value(0, “李四”, 1, 2);
insert into student value(0, “王五”, 1, 3);
关联查询
select student.name,class.name from class inner join student on class.id=student.classid;
结果如下:
select student.name,class.name from class left join student on class.id=student.classid;
结果如下:
select student.name,class.name from class right join student on class.id=student.classid;
结果如下:
分类:
1、表A inner join 表B:
表A与表B匹配的行会出现在结果集中
2、表A left join 表B:
表A与表B匹配的行会出现在结果集中,外加表A中独有的数据,未对应的数据使用null填充
3、表A right join 表B:
表A与表B匹配的行会出现在结果集中,外加表A中独有的数据,未对应的数据使用null填充
总结
mysql 基本的增删改查及一些聚合分组和关联查询。