总结
我是最棒的!基础不牢,地动山摇!
Mysql基本命令
Mysql安装
网上有很多教程,这里就不多做介绍
推荐一篇博文https://www.cnblogs.com/xiaxiaoxu/p/8977418.html
基本命令
打开cmd,开始输入命令
启动服务
net start mysql
停止服务
net stop mysql
进入服务器
Mysql -u用户名 -p密码
常用命令
//显示所有数据库
show databases;
//新建数据库
create database 数据库名;
//删除数据库
drop database 数据库名
//选择要操作的数据库
use 数据库名;
//显示所有表
show tables;
//显示表结构
desc tables;
//删除表
drop table 表名;
数据库存储引擎
MyISAM:不支持事务,也不支持索引
InnoDB:支持事务,也支持事务,效率低,但是安全
mysql中的数据类型
int 短整型
bigint 长整型
char 定长字符串
varchar 可变长字符串
date 日期
time 时间
datetime 日期时间
创建表
CREATE TABLE 表名(
id bigint(11),
name varchar(20),
gender char(1)
);
#需要注意的是,如果表名与关键字冲突,加上反引号``
表中字段的约束
- 非空约束 NOT NULL
- 默认约束 DEFAULT
- 唯一约束 UNIQUE
- 主键约束(非空且唯一)PRIMARY KEY
- 自然主键(不推荐,有业务实际意义的字段)
- 代理主键(推荐)
- 外键约束 FOREIGN KEY 关系多张表
查询命令
数据库中最最重要的部分
普通查询
#查询表中所有信息
select * from 表名
#查询表中具体列
select 列名 from 表名
#查询去重
select distinct 列名 from 表名
#别名
select 列名 as 别名 from 表名
过滤查询
select * from 表名 过滤条件
#利用数学相关来过滤条件 where关键字,多个条件使用逻辑运算符
#AND OR NOT来连接多个条件
范围查询
#bet ... and ...
集合查询
#int(值1,值2,...)
空值查询
#is NULL
模糊查询
#关键字like
#通配符 % 代替多个字符 _代替一个字符
排序
#order by
分页查询
-
假分页
一次先查询出所有数据,放到list集合中,根据条件获取数据,数据一旦多了,效率性能就会很低
-
真分页
一次查询,就只去数据中查询需要展示的数据,把数据放在集合中,直接展示
#关键字limit beginIndex是开始的索引,size是每页显示的条数
select * from 表名 limit BeginIndex,size
#第N页数据 (n-1)*size,size
聚合函数
作用于一组数据,并且对一组数据返回一个值
COUNT:统计结果记录数,如果列为NULL,则不会计算
MAX:计算最大值
MIN:计算最小值
AVG:计算平均值
SUM:计算和
分组查询
#关键字GROUP BY
#HAVING 分组后的过滤条件
#使用聚合函数的时候用Having来过滤条件
下面通过实例来演示
数据库的图
根据需求来写出查询语句
需求: 查询所有货品信息
SELECT * FROM product;
需求: 查询所有货品的id,productName,salePrice
SELECT id,productName,salePrice FROM product;
需求: 查询商品的分类编号。
SELECT dir_id FROM product;
需求: 查询所有货品的id,名称和批发价(批发价=卖价*折扣)
SELECT id,productName,salePrice*cutoff as '批发价' FROM product;
需求: 查询所有货品的id,名称,和各进50个的成本价(成本=costPirce)
SELECT id,productName,costPrice*50 FROM product;
需求: 查询所有货品的id,名称,各进50个,并且每个运费1元的成本
SELECT id,productName,(costPrice+1)*50 FROM product;
需求: 查询所有货品的id,名称,各进50个,并且每个运费1元的成本(使用别名)
SELECT id,productName,(costPrice+1)*50 as '成本价' FROM product;
需求: 查询商品的名字和零售价。格式:xxx商品的零售价为:xxx
;
需求: 查询货品零售价为119的所有货品信息.
select * from product where salePrice = 119;
需求: 查询货品名为罗技G9X的所有货品信息.
select * from product where productName = '罗技G9X'
需求: 查询货品名 不为 罗技G9X的所有货品信息.
select * from product where productName != '罗技G9X'
需求: 查询分类编号不等于2的货品信息
SELECT * from product WHERE dir_id != 2;
需求: 查询货品名称,零售价小于等于200的货品
SELECT productName,salePrice from product where salePrice <= 200;
需求: 查询id,货品名称,批发价大于350的货品
SELECT dir_id,productName,salePrice from product where salePrice*cutoff > 350;
需求: 查询id,货品名称,批发价在300-400之间的货品(使用 and)
SELECT dir_id,productName from product where salePrice*cutoff >= 300 and salePrice*cutoff <= 400;
需求: 查询id,货品名称,分类编号为2,4的所有货品
SELECT dir_id,productName from product WHERE dir_id = 2 or dir_id = 4;
需求: 查询id,货品名词,分类编号不为2的所有商品
SELECT dir_id,productName FROM product WHERE dir_id != 2;
需求: 选择id,货品名称,分类编号的货品零售价大于等于250或者是成本大于等于200
SELECT dir_id,productName from product where salePrice >= 250 or costPrice >=200;
需求: 查询id,货品名称,批发价在300-400之间的货品(使用between)
SELECT dir_id,productName from product where salePrice * cutoff BETWEEN 300 and 400;
需求: 查询id,货品名称,批发价不在300-400之间的货品
SELECT dir_id,productName from product where salePrice * cutoff not BETWEEN 300 and 400;
需求: 查询id,货品名称,分类编号为2,4的所有货品
SELECT dir_id,productName from product where dir_id in (2,4);
需求: 查询id,货品名称,分类编号不为2,4的所有货品
SELECT dir_id,productName from product where dir_id not in (2,4);
需求: 查询商品名为NULL的所有商品信息。
select * from product where productName = NULL
需求: 查询id,货品名称,货品名称匹配'%罗技M9_'
SELECT dir_id,productName from product where productName like '%罗技M9_';
需求: 查询id,货品名称,分类编号,零售价大于等于200并且货品名称匹配'%罗技M1__'
SELECT dir_id,productName from product where salePrice >= 200 and productName like '%罗技M1__';
需求: 查询id,货品名称,分类编号,零售价并且按零售价降序排序
SELECT dir_id,productName,salePrice from product ORDER BY salePrice DESC
需求: 查询id,货品名称,分类编号,零售价先按分类编号排序,再按零售价排序
SELECT dir_id,productName,salePrice from product ORDER BY dir_id,salePrice
需求: 查询M系列并按照批发价排序(加上别名)
SELECT *,salePrice * cutoff as '批发价' from product where productName like '%M%' ORDER BY salePrice * cutoff
需求: 查询分类为2并按照批发价排序(加上别名)
SELECT *,salePrice * cutoff as '成本价' from product where dir_id = 2 ORDER BY salePrice * cutoff
需求:分页查询
每页最多3条记录: pageSize = 3:
SELECT * from product LIMIT 0,3;
------------------------------------------
需求: 查询所有商品平均零售价
SELECT AVG(salePrice) from product
需求: 查询商品总记录数(注意在Java中必须使用long接收)
SELECT count(*) from product
需求: 查询分类为2的商品总数
SELECT count(*) from product where dir_id = 2
需求: 查询商品的最小零售价,最高零售价,以及所有商品零售价总和
SELECT min(salePrice),max(salePrice),sum(salePrice) from product
需求: 查询每个商品分类编号和每个商品分类各自的平均零售价
SELECT dir_id,avg(salePrice) from product
GROUP BY dir_id
SELECT AVG(salePrice) from product where dir_id = 4
需求: 查询每个商品分类编号和每个商品分类各自的商品总数。
select dir_id,count(dir_id) from product
GROUP BY dir_id
需求: 查询每个商品分类编号和每个商品分类中零售价大于100的商品总数:
SELECT dir_id,count(*) from product where salePrice > 100
GROUP BY dir_id
需求: 查询零售价总和大于1500的商品分类编号以及总零售价和:
SELECT dir_id,sum(salePrice) from product
GROUP BY dir_id
HAVING sum(salePrice) > 1500
和每个商品分类各自的商品总数。
select dir_id,count(dir_id) from product
GROUP BY dir_id
需求: 查询每个商品分类编号和每个商品分类中零售价大于100的商品总数:
SELECT dir_id,count(*) from product where salePrice > 100
GROUP BY dir_id
需求: 查询零售价总和大于1500的商品分类编号以及总零售价和:
SELECT dir_id,sum(salePrice) from product
GROUP BY dir_id
HAVING sum(salePrice) > 1500