数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来。
使得用户可以对非常方便对数据库中的数据进行增加,修改,删除及查询操作。
作用:
方便了对数据的增删改查
数据库管理系统(DataBase Management System,DBMS):
指一种操作和管理维护数据库的大型软件。
用于对数据库服务器 进行统一管理和控制,以保证数据库的安全性和完整性。
大数据hase , 以列族进行存
redis 不用写sql,存的数据量小
int (整形)
double(小数) ==》常规使用的一些基本数据类型
char 字符
varchar 字符(可伸缩)
text (大文本)
时间: date dateTime
1.使用sqlyod进行连接 注意点: 必须保证服务是开启的
2.dos 窗口进行连接
本机连接方式:
mysql -u用户名 -p密码
远程连接:
mysql -hip地址 -u用户名 -p密码
3使用第三种方式直接打开mysql 的dos窗口
DDL:操作数据库库 ,数据表 (比如表名字修改,编码格式的修改)
DML :就是增加 删除 修改(傻瓜式操作)
DQL: 查询(重点)
DCl : 对权限的一些处理
创建库: create database + 库名
创建库的时候设置其编码格式:
create database + 库名 + character set gbk
切换数据库 use +库名
查询当前所有的库:show databases;
查看当前库执行的库: select database()
删除库: drop database + 库名
查看当前表的信息:desc +表名
查看所有的表:show tables
删除表: drop table + 表名
增加一个字段: alter table 表名 add 字段名 数据类型(长度);
修改数据类型 : alter table 表名 modify 字段名 修改的类型(长度)
修改字段名:alter table 表名 change 旧的字段名 新的字段名 数据类型(长度)
删除字段名: alter table + 表名 + drop+字段名
修改表名: rename table 原来的表名 to 新的表名
修改表的编码格式 :alter table 表名 character set gbk
创建表的语法:
create table +表名(
列名 数据类型(大小),
列名1 数据类型(大小)
);
注意点:1.一定要切换到某一个库之后再来创建表
2.表最外层的括号后别忘记加;
3. 每一个列名以逗号进行分割
插入一条数据:
第一种方式: insert into 表名(列名1,列名2)values(“第一条数据”,“第二条数据”);
insert into e(id,age,sal,createdate)values(3,18,2000,‘2019-11-23’);
第二中种插入方式: insert into 表名values(“第一个”,“第二个”) 不指定列名这张表里的所有字段都必须插入
insert into e values(2,“张”,19,2000,‘2019-11-22’);
注意点:
1.第一个列名的数据类型必须与插入值的类型一样
2.插入的值必须小于等于其长度
3.除了数值类型 其它类型都必须加上’’ 或者"" 时间类型给‘’
4.如果需要个一个字段插入null 的值,不用给其插入,默认就是null null与"" 不一样
修改:
update 表名 set 列名=修改的值,;列名=修改的值 where 条件
注意点:
1.修改语句一定要根据条件来进行修改
2. 修改的列名的数据类型也必须与值是一个类型
3.修改 的值长度必须小于列名的长度
删除 :
delete from 表名 where 条件
注意点:1.删除时必须加上条件
增加删除 修改 是改变数据库中的数据
查询是不会改变,相当于一张伪表
#查询product表中所有记录
#查询product表中所有记录,仅显示pid和pname字段
select pid,pname from product
#查询product表中所有的电脑办公记录(加条件 where )
select * from product where category_name=“电脑办公”;
#别名查询
第一种写法 加上了as关键字
select p.pid,p.pname from product as p
第二种写法
select p.pid,p.pname from product p;
给列取别名:
select p.pid “商品的编号”,p.pname “商品的名称” from product p; 注意:列名不给其别名就是其字段名
#c除去价格重复的查询 (distinct 表示去除重复 写在要除去重复的列名的前面)
select distinct price from product
#查询结果是表达式(运算查询):将所有商品的价格+10元进行显示.(可以进行简单的逻辑运算)
select price+10 from product;
select price*10 from product;
//条件查询
#查询商品名称为“花花公子”的商品所有信息:
select * from product where category_name=“花花公子”;
#查询价格为800商品
select * from product where price=800
#查询价格不是800的所有商品
select * from product where price !=800
#查询商品价格大于60元的所有商品信息
select * from product where price>60;
#查询商品价格在200到1000之间所有商品
第一种方式使用and
select * from product where price>=200 and price<=1000;
第二种 between and 在什么范围之间
select * from product where price between 200 and 1000;(包含头也包含尾)
第三种
select * from product where price>=200 && price<=1000;
#查询商品价格是200或800或者2000的所有商品
第一种写法:
select * from product where price=200 or price=800 or price=2000;
第二种写法:
select * from product where price=200 || price=800 || price=2000;
第三种写法:
select * from product where price in(200,800,2000);
模糊查询:
1.%匹配一个字符或者多个字符
2._匹配一个字符 like
#查询含有’霸’字的所有商品
select * from product where pname like ‘%霸%’;
#查询以’香’开头的所有商品
select * from product where pname like ‘香%’;
#查询第二个字为’想’的所有商品
select * from product where pname like ‘_想%’;
#商品没有分类的商品
select * from product where category_name is null;
#查询有分类的商品
select * from product where category_name is not null
排序: order by 降序desc 升序 ASC
#1.使用价格排序(降序)
select * from product order by price Asc;
#2.在价格排序(降序)的基础上,以主键排序(降序) 先按照价格来进行排序(只有价格相同的情况下,才使用主键的排序烦死)
select * from product order by price desc,pid desc;
#即若价格相同,相同价格的数据以pid降序排序
#3.显示商品的价格(去重复),并排序(降序)
select distinct price from product order by price desc;
mysql 中常用的5个函数(聚合函数)
#1 查询商品的总条数 count(可以是* 可以写列的名称 可以写1) 一般建议使用1 效率最高
select count(*) as “总记录数” from product
sum(列名) max(列名) min(列名) avg(列名)
#查看商品总价格、最大价格、最小价格、价格的平均值
select sum(price) as “总价格”, max(price) as “最大价格”, min(price) as “最小价格”, avg(price) as “平均值”
from product
#2 查询价格大于200商品的总条数
select count(1) from product where price>200;
#3 查询分类为’电脑办公’的所有商品的总记录
select count(1) from product where category_name =“电脑办公”;
#4 查询分类为’服装’所有商品的平均价格
select avg(price) from product where category_name =“服装”;
分组的 关键字 group by
#查看商品列表中有哪些分类
select category_name from product group by category_name;
#1 统计各个分类下的商品的个数
select category_name, count(price) from product group by category_name;
#2 统计各个分类商品的个数,且只显示分类名不为空值的数据
使用having 来进行过滤
select category_name,count(price) from product group by category_name having category_name is not null where id=1
where 过滤 having 过滤的区别:
where 是在分组的前面进行过滤 having 是在分组的后面进行过滤
#分页:分页的关键字使用limit关键字 limit ?,? 表示当前数据库的索引是从0 开始的 第二个参数是表示页量(也就是每页显示的数据) 如果只传递一个参数,表示页量
查询出前5条数据:
select * from product limit 0,5
查询出6 -10 条的数据
select * from product limit 5,5 13 5 ==> 1,5 (也就是当前页-1)*页量, 页量
#后面的数据查询出来
select * from product limit 10,5;
查询语句的规范:
select category_name,count(price) from product where 1=1 group by category_name having category_name is not null