一:MySQL简介与安装
MySQL是一种中型、易用的关系型数据库管理系统(DBMS)
安装步骤:
第一步: sudo apt-get install mysql-server
第二步:
sudo apt-get install mysql-client
第三步:sudo apt install libmysqlclient-dev
若安装时忘记设置密码:
use mysql
update user set authentication_string=PASSWORD('123456') where user='root';
flush privileges;
进入MySQL环境:
mysql -u root -p
二:MySQL常用操作命令
1. show databases; #显示所有数据库.
2.user 数据库名称; #进入(使用)某个数据库
3.show tables; # 显示当前数据库中所有的表
4. desc 表名称; # c查看表的结构
三:SQL语句
SQL(structured query language) 结构化查询语言,专门用来操作关系型数据库的语言。
1.创建数据库
create database 数据库名 [default character set 'utf8']
#1.创建一个名为mydb的数据库
create database mydb default character set 'utf8';
2.进入表
#2.进入mydb
use mydb;
3.创建表
create table 表名称(
字段名 字段数据类型 约束,
字段名 字段数据类型 约束,
....
字段名 字段数据类型 约束
);
#3.创建student表
create table student(
stuid int auto_increment primary key,
name varchar(20) not null,
age int,
score float not null,
note text
);
4.插入记录
方式一:
insert into 表名称(字段名1,字段名2,...)values(值1,值2,...) ;
方式二:
insert into 表名称values(值1,值2,...) ;
方式三:
insert into 表名称(字段名1,字段名2,...)values(值1,值2,...) ,
(值1,值2,...) ,...(值1,值2,...) ;
#4.箱student表中插入一条记录
insert into student(name,age,score,note)values('风清扬',20,93.5,'风清扬独孤九剑');
#插入成功之后查询表的所有记录
select * from student;
insert into student values(23,'name',18,65.5,'家住华山');
insert into student(name,age,score)values('郭靖',25,62),('黄蓉',24,73.5),('马云',40,79.5);
5.修改记录
update 表名称 set 字段名=修改值 [,字段名=修改值,....] [where 条件]
#5.把所有的学生的成绩改为60分
update student set score=60;
#将姓名为令狐冲的记录的成绩改为75分
update student set score=75 where name='令狐冲';
6.删除记录
delete from 表名称 [where 条件];
#6.删除学号为 6的学生记录
delete from student where stuid=6;
7 .查询记录
select 字段列表 from 表名称 [where 条件];
#7.查询所有学生的成绩和姓名,并以别名的形式成
select name as 姓名, score as 成绩 from student;
#.查询出所有成绩在85到95之间的学生记录
select * from student where score>=85 and score=95;
或者
select * from student where score between 85 and 95;
#.查询学生表中的前三条记录
select * from student limit 3;
#.查询成绩大于等于70分的学生中,前三条记录
select * from student where score>=70 limit 3;
#,查询学生表中第三条与第四条记录
select * from student limit 2,2;
#.假如每页最多显示两条记录,查询第三页应该显示的记录
select * from student limit 4,2;
#.查询所有备注信息不为null的学生记录
select * from student where note is not null;
#.按照成绩从高到低,将学生排序
select * from student order by score;
#.按照成绩从高到低,将学生排序,如果成绩相等则按照年龄降序排序
select * from student order by score desc,age desc;
四:限制查询、分页查询、分组查询
1.限制查询
第一种情况:查询限定条数
select 字段列表 from 表名称 [where 条件] limit 最多查询数;
第二种情况:从指定偏移量查询限定条数
第二种情况:从指定偏移量查询限定条数
select 字段列表 from 表名称 [where 条件] limit 偏移量,最多记录数;
2.分页查询
已知当前页为currentPage,每页最多显示的记录数为pageSize,则currentPage页显示的表中的记录为:
select 字段列表 from 表名称 【where 条件] limit (currentPage-1)*pageSize,pageSize
3.排序查询
select 字段列表 from 表名称 [where 条件] order by 字段名[desc] [,字段名]
;
(desc是降序)
4.MySQL中的聚合函数
A : max(字段名) 获取某字段中的最大值
B : min(字段名) 获取某字段中的最小值
C : sum(字段名) 机选某字段值的和
D : avg(字段名) 计算某字段的平均值
E :conut (字段名) 计算某字段值中不为Null的记录数
count(*) 计算表中的总记录数
5.分组查询
select 字段列表 from 表名称 [where 条件] group by 分组字段
列表 [having 对分组后的筛选条件]
注意:select后的字段列表应该包含在group by 后的分组字段列表中!
#17.创建商品表product
create table product(
proid int auto_increment primary key,
proname varchar(20) not null,
price float not null,
type varchar(20) not null,
note text
);
insert into product(proname,price,type,note)values('锅巴',5,'零食',null),('洗衣粉',8,'日用品',null),('可口可乐',12,'饮料','新上市的cola'),('辣条',2,'零食',null),('脸盆',10,'日用品',null),('薯条',12,'零食',null);
#18.按照商品类型type对product表中的记录进行分组,求出每一组的平平均价格
select type,avg(price) from product group by type;
#18.按照商品类型type对product表中的记录进行分组,求出零食组的平均价格
select type,avg(price) from product group by type having type='零食';
配置远程连接MySQL的权限
1.sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
对该文件进行编辑:注释掉 bind-address = 127.0.0.1
2.进入MySQL环境,授予root用户所有权限
grant all on *.* to ‘root’@‘%’ indentified by ‘root用户的密码’ with grant option
3.刷新权限
flush privileges;
3.退出MySQL环境
systemctl restart mysql;
eg1:一:按照步骤操作如下:
1.创建doctor(医生)表,id 是整型自增主键,name 是字符串,salary是工资
2.向表中插入多条记录
3.更新工资为3000的记录的工资为5000
4.查询所有工资大于等于3000的记录
5.删除名字为"李时珍"的记录.
6.删除表 (提示:可使用 drop table 表名称)
create table doctor(
docid int auto_increment primary key,
name varchar(20) not null,
salary int
);
2.
insert into doctor(name,salary)values('李彦组',40000),('李于晏',30000),('李金武',20000),('李时珍',99999);
3.
update doctor set salary=66666;
4.
select * from doctor where salary>=3000;
5.
delete from doctor where name='李时珍';
6.
drop table doctor;
eg2:二:删除原来的学生表(student表),并创建一个新的学生表,
表中包括stuid,name,sex,score四个字段,
查询学生表中成绩为前3名的学生记录。
create table student(
stuid int auto_increment primary key,
name varchar(20) not null,
sex varchar(20) not null,
score float not null
);
insert into student(name,sex,score)values('李彦组','男',99.9),('李城武','男',99.8),('李于晏','男',99.7),
('李志双','男',100);
insert into student(name,sex,score)values('李发发','女',88.88),('李呱呱','女',88.99),('李哈哈','女',100);
select * from student order by score desc limit 3
eg3:三:查询学生表中男生和女生的平均成绩。
select sex,avg(score) from student group by sex having sex='男';
select sex,avg(score) from student group by sex having sex='女';
eg4:四:查询成绩大于80且年龄小于20岁的学生记录
select * from student where score>80,age<20;