mysql简单总结-爱代码爱编程
1. 数据库
存储数据的仓库,常见的数据库有两类,
包括
关系型数据库: mysql, oracle,SQLserver等
非关系型数据库:hbase, MongoDB,redis等
关系型数据库表与表之间有关系:一对一,一对多,多对多
非关系型数据库之间以键值对的形式进行存储
2. MySQL数据库
mysql数据库是一个关系型数据库,开源,使用标准SQL语言
mysql数据库的登录包含三种方式
- 输入: mysql -u 用户名 -p ,按enter之后输入密码即可登录
- 输入: mysql -u用户名 -p密码 ,按enter之后可以直接登录
- 输入: mysql --host=主机名 --user=用户名 --password=密码 ,可以远程访问非本机上的mysql
mysql的登出可以使用quit,exit或者直接点击右上角的叉号退出
3. SQL
SQL 结构化查询语句,分为
- DDL 数据定义语言, 用来定义数据库 表 列等. 常使用create , alter , drop等关键字.
- DML 数据操作语言, 用来操作数据库中表中的数据, 常使用insert, delete, updata等关键字
- DQL 数据查询语言, 用来查询数据表中的数据信息,常使用select, from, where等关键字
- DCL 数据控制语言, 用来定义数据库访问权限和安全级别以及常见用户,常是运维在使用
4. DDL数据定义语言
#----------------------数据库DDL操作--------------------------------
-- 创建数据库
create database summary;
-- 使用刚创建的数据库
use summary;
-- 创建数据表
create table ddl(
id int ,
name varchar(20)
);
-- 若数据表已存在,使用if not exists防止报错
create table if not exists ddl(
id int ,
name varchar(20)
);
-- 添加字段
alter table ddl add sex varchar(10);
-- 关键字作为添加的字段
alter table ddl add `desc` varchar(10);
# 关于desc desc是一个关键字,可以查看数据表的表结构信息,同时在排序时使用,表示降序排序
-- 查看表信息,
desc ddl;
-- 修改表字段
alter table ddl change `desc` dp varchar(20);
-- 删除字段
alter table ddl drop dp;
5. DML数据操作语言
#----------------------数据库DML操作--------------------------------
-- 向表中插入数据,一次插入一行
insert into ddl value (1,'张三','男');
insert into ddl value (2,'李四','女');
-- 向表中插入数据,一次插入多行
insert into ddl values (3,'王二','男'),(4,'麻子','女');
-- 指定字段插入
insert into ddl (id ,name) value (5,'小明');
-- 修改即更新数据,注意,一定要加where条件,否则会将表中所有数据全部更改
update ddl set name='小红' where id=5;
-- 查看表数据
select * from ddl;
-- 删除表数据
delete from ddl where id=5;
-- 删除表中所有数据,方式一
delete from ddl;
-- 删除表中所有数据,方式二
truncate ddl;
# 两种方式都是直接删除数据库中的所有数据,
# 但delete是删除表中数据,但不会删除标中索引,
# truncate会删除索引,相当于直接删除一张表同时新建一个和原来的表结构完全相同的新的表
6. 约束
#----------------------数据库约束操作--------------------------------
-- 约束可以保证数据的正确有效,数据约束是在数据类型的基础上对数据进行的额外的限制
create table const(
id int primary key auto_increment, # primary key 约束 auto_increment自增约束
name varchar(20) not null , # 非空约束
ip varchar(20) unique , # 唯一约束
sex varchar(10) default '男' # 默认约束
);
create table fokey(
id int,
name varchar(20)
);
-- 删除主键约束
-- 修改数据类型
alter table fokey modify id int primary key ;
-- 删除主键
alter table fokey drop primary key ;
-- 查看表结构
desc fokey;
desc const;
-- 添加主键
alter table const add primary key (id);
-- 设置自增
alter table const change id id int auto_increment;
7. DQM数据查询语句
-- 查询数据表中全部信息
select * from product;
-- 获取部分信息
select pname,price from product;
-- 获取pid为1的商品信息
select * from product where pid=1;
-- 获取价格大于800的商品信息
select * from product where price>800;
-- 获取商品价格在200到1000之间的商品信息
select * from product where price>200 and price<1000;
select * from product where price between 200 and 1000;
-- 获取含有"香"的商品的商品信息
select * from product where pname like '%香%';
-- 获取以"斯"结尾并包含三个字的商品信息
select * from product where pname like '__斯';
-- 查找商品价格为200,400,600的商品
select * from product where price in (200,400,600);
-- 查看商品中商品类别为空的商品信息
select * from product where category_id is null;
select * from product where category_id is not null; #商品类别不为空的商品
-- 以价格降序排序,并显示商品信息
select * from product order by price desc ;
-- 求当前商品一共有多少件
select count(*) from product;
-- 求商品价格最大值,最小值
select max(price) from product;
select min(price) from product;
-- 求商品价值总和
select max(price) from product;
-- 求商品平均价格
select round(avg(price),2) from product;
-- 以商品id进行分组 分组查询是,前面查询的信息必须是后面分组的信息或者聚合函数
select category_id from product group by category_id;
-- 查询每类商品的最大值
select category_id,max(price) from product group by category_id;
-- 将商品按组分类,并查询分组和平均价值在600以上的商品
select category_id,avg(price) from product group by category_id having avg(price)>600;
# having 组后筛选,能和聚合函数同时使用, where组前筛选,不能和聚合函数一起使用
-- 分页查询 索引从0开始,每页显示三条数据
select * from product limit 0,3;
8. 多表查询
-- 删除外键约束
alter table products drop foreign key products_ibfk_1;
-- 添加外键约束
alter table products
add constraint fk01 foreign key (category_id) references category(cid);
# 外表的外键列不能出现主表的主键列没有的内容
# 通过外键约束可以保证数据的安全性和完整性
-- 隐式内连接
select * from hero ,kongfu where hero.kongfu_id=kongfu.kid;
-- 显示内连接
select * from hero join kongfu on kongfu_id=kongfu.kid;
-- 左连接查询
select * from kongfu left join hero on hero.kongfu_id=kongfu.kid;
-- 右连接查询
select * from kongfu right join hero on hero.kongfu_id=kongfu.kid;
-- 自连接查询所有周口市信息
select county.id,county.title
from areas city join areas county
on city.id=county.pid
where city.title='周口市';
-- 子查询
select *
from areas
where pid = (select id from areas where title = '河南省');
-- 子查询所有周口市的信息
select * from areas where pid=(select id from areas where title='周口市');