一:MySQL简介
MySQL是一种中型的、开放源代码的、关系型数据库管理系统(DBMS)。
进入MySQL环境的方法:mysql -h 主机地址 -u 用户名 -p
查看所有数据库的命令:show databases;
进入数据库命令:use 数据库名;
查看数据库中所有表的命令:show tables;
查询表中所有的数据:select * from 表名称;注意:如果要使查询结果格式化,则在SQL后面加上\G, 即:select * from 表名称\G;
查询当前所在的数据库:select database();
查看数据库创建语句:show create database 数据库名;
查看表创建语句:show create table 表名称;
查看表结构:desc 表名称;
二:基本SQL:
SQL(Structed Query Language):结构化查询语言,专门用来操作关系型数据库的 语言。
1. 创建数据库
create database 数据库名称 [default character set utf8];
1.创建mydb数据库 create database mydb default character set utf8; 2.进入mydb数据库 use mydb; 3.查看创建mydb的语法 show create database mydb;
2.创建表
create table 表名称( 字段名1 数据类型 [约束], 字段名2 数据类型 [约束], ...... 字段名n 数据类型 [约束],);
4.创建学生表 create table students( stuid integer primary key auto_increment, name varchar(20) not null, age int, sex char(5), score double not null );
3.插入记录:
方式一:(指定字段名)insert into 表名称(字段名1,字段名2,...字段名n)values(值1,值2,...值n);
方式二:(未指定字段名)insert into 表名称 values(值1,值2,...值n);
方式三:(指定字段名,一条SQL插入多条记录)insert into 表名称(字段名1,字段名2,...字段名n)values (值1,值2,...值n),(值1,值2,...值n)...(值1,值2,...值n);
方式四:(未指定字段名,一条SQL插入多条记录)insert into 表名称 values(值1,值2,...值n), (值1,值2,...值n)...,(值1,值2,...值n);
5.插入记录: (指定字段名) insert into students(name,age,sex,score)values('风清扬',20,'男',93.5); (未指定字段名) insert into students values(10,'隆美尔',25,'男',83); (指定字段名,一条SQL插入多条记录) insert into students(name,age,sex,score)values('张学良',22,'男',60), ('蒋介石',25,'男',85); (未指定字段名,一条SQL插入多条记录) insert into students values(16,'孙悟空',25,'男',89), (19,'猪八戒',23,'男',69);
4.删除记录
delete from 表名称 [where 条件];
1. 删除成绩小于70的记录 delete from students where score<70;
删除年龄为25岁,且成绩为85的记录 delete from students where age=25 and score=85;
5.修改记录
update 表名称 set 字段名=字段值[,字段名=字段值,...][where 条件]; 3. 将名字为'孙悟空'的记录的成绩修改为60,年龄修改为35 update students set score=60,age=35 where name='孙悟空';
6.查询记录
select 字段名1,字段名2,.. from 表名称 [where 条件];
4. 查询成绩在80到90之间的记录 方式一: select * from students where score>=80 and score<=90; 方式二: select * from students where score between 80 and 90;
7.sql数据类型
MySQL常用数据类型:
varchar(n) 可变长度的字符串类型
char(n) 定长字符串
integer/int 整数类型
double 双精度小数类型
date 日期类型 在数据库表中的格式为'YYYY-MM-DD'
datetime 日期时间类型 在数据库表中的格式为'YYYY-MM-DD HH:MM:SS'
注意:date类型与datetime类型演示
create table temp( id int primary key auto_increment, d date not null, dt datetime not null ); 插入记录: insert into temp(d,dt)values('','1937-07-07 02:32:58'); insert into temp(d,dt)values('20180910','19370707023258'); insert temp(d,dt)values('1937-07-07 02:32:58','-09-10'); insert into temp(d,dt)values(current_date(),now()); insert into temp(d,dt)values(current_date(),sysdate());
三:MySQL中的聚合函数
应用场景:分组查询
1. max(字段名) 查询某个字段的最大值 2. min(字段名) 查询某个字段的最小值 3. avg(字段名) 查询某个字段的平均值 4. sum(字段名) 查询该字段对应值的和 5. count(字段名) 查询某个字段中不为null的记录数 6. count(*) 查询总记录数
四:分组查询
说明:根据分组字段,对不同的组进行数据统计查询 语法: select 字段名1,字段名2...,[聚合函数] from 表名称 group by 分组字段 [having 条件];
一:先创建product产品表 create table product( proid int primary key auto_increment, name varchar(20) not null, price double not null, type varchar(20) not null, note text ); ``` 插入记录 insert into product(name,price,type,note)values ('辣条',3.5,'零食',null), ('洗衣粉',12.5,'日用品',null), ('旺旺雪饼',10,'零食','旺旺雪饼最近销量不好'), ('铅笔',3,'学习用品',null), ('脸盆',15,'日用品',null), ('文具盒',5,'学习用品','文具盒深受小学生喜爱'); 二:编写分组查询语句 按照产品类型进行分组,查询每一组的平均价格 select type as 产品类型, avg(price) as 平均价格 from product group by type; 查询“零食”组的平均价格 select type as 产品类型, avg(price) as 平均价格 from product group by type having type='零食';
注意:查询字段(select后的字段)必须包含在分组字段中; having用来筛选某个分组
五:模糊查询
语法: select 字段名1,字段名2... from 表名称 where 字段名 like 模糊条件;
模糊查询的通配符:
% 匹配任意多个任意字符
_ 匹配一个任意字符
1. 查询所有姓张的学生 select * from students where name like '张%'; 2. 查询名字中含有'空'的学生 select * from students where name like '%空%'; 3. 查询名字中包含三个字,并且最后一个是'空'的学生记录 select * from students where name like '__空';
六:排序查询
语法: select 字段名1,字段名2... from 表名称 [where 条件] order by 字段名[desc,字段名...];
1. 根据成绩从高到低将所有学生排序 select * from students order by score desc; 2. 根据成绩从高到低(降序)将所有学生排序,在成绩相等的情况下 根据年龄从低到高(升序)。 select * from students order by score desc,age;
七:限制查询
1. 限制数量的查询
语法: select 字段名1,字段名2... from 表名称 [where 条件] [其他条件] limit 最多记录数;
查询前三名的学生记录 select * from students order by score desc limit 3;
2.通过偏移量和记录数查询某一部分记录
语法:select 字段名1,字段名2... from 表名称 [where 条件] [其他条件] limit 起始偏移量,最多记录数;注意:第一条记录的起始偏移量是0;
1.查询源表中第4~6条记录 select * from students limit 3,3; 2.查询第二名与第三名的学生记录 select * from students order by score desc limit 1,2;
3.分页查询
已知:页码pagenum与每一页的最大显示记录数pageSize
语法:select 字段名1,字段名2... from 表名称 [where 条件]
[其他条件] limit (pagenum-1)*pageSize,pageSize;
已知每页最多显示3条记录,查询第二页应该显示的记录 select * from students limit 3,3;
八:修改、删除表
1. 添加列
语法: alter table 表名称 add column 字段名 数据类型 [约束]; 1. 给students表添加home列 alter table students add column home varchar(20) not null;
2.删除列
语法:alter table 表名称 drop column 字段名; 1. 将students表中的home列的约束去掉 alter table students modify column home varchar(20);
3.修改列
语法:alter table 表名称 modify column 字段名 数据类型 [约束]; 将students表中的home列删除 alter table students drop column home;
4.删除数据表
语法:drop table 表名称; 删除students表 drop table students;
5.删除数据库
drop database 数据库名;