MySQL的使用


layout: post
title: MySQL的使用
subtitle: MySQL
date: 2018-01-25
author: ZL
header-img: img/20180125.jpg
catalog: true
tags:
- MySQL


一、数据库的分类

  • DDL: Data Definition Language,用来定义数据库对象:数据库database,表table, 列column等。 关键宇: 创建create, 修改alter, 删除drop等 (结构)。
  • DML: Data Manipulation Language,用来对数据库中表的记录进行更新。关键宇:插入insert,删除delete,更新update等(数据)。
  • DQL: Data Query Language,来查询数据库中表的记录。关键宇:select, from, where等。
  • DCL: Data Control Language,用来定义数据库的访问权限和安全级别, 及创建用户: 关键宇: grant等。

二、数据库指令

2.1 数据库的操作:database

2.1.1 创建数据库 & 显示所有的数据库 & 查看数据库编码

create database 库名
create database 库名 character set 编码
show databases :显示所有的数据库
show create database 库名

  • 创建不带编码的

    image

  • 创建带编码的

    image

  • 查看编码


    image

2.1.2 删除一个库

drop database 库名

image

2.1.3 使用库 & 查看当前正在操作的库

use 库名

image

select database()

image

2.2 表操作: table

2.2.1 创建表

create table 表名(
字段名 类型(长度) [约束],
字段名 类型(长度) [约束],
字段名 类型(长度) [约束]
);

image

primary key auto_increment传入null值的话就会自动增长

2.2.2 查看所有的表 & 查看表的结构

show table

image

desc 表名

image

2.2.3 删除一张表

drop table 表名

image

2.2.4 修改表

2.2.4.1 添加一列

alter table 表名 add 字段名(列名) 类型(长度)[约束]

image

2.2.4.2 修改列的类型 (长度、约束)

alter table 表名 modify 列名 类型(长度)[约束]

image

2.2.4.3 修改列的列名

alter table 表名 change 旧列名 新列名 类型(长度)[约束]

image

2.2.4.4 删除表的列

alter table 表名 drop 列名

image

2.2.4.5 修改表名

rename table 旧表名 to 新表名

image

2.2.4.6 修改表的字符集 & 查看当前表的编码

alter table 表名 character set 编码

image

show create table 表名

image

2.3 对记录增删改查

2.3.1 插入记录

insert into table 表名 (列1,列2,列3...) values (值1,值2,值3...)

image

insert into table 表名 values (值1,值2,值3...)需要所有的列都赋值(没有值可以赋null)

image

2.3.2 修改记录

  • 不带条件的

update 表名 set 列名 = 值,列名=值,列名=值...

image
  • 带条件的

update 表名 set 列名=值,列名=值... where 条件

image

2.3.3 删除记录

  • 带条件的

delete from 表名 where 条件

image

删除后,uid不会重置

  • 不带条件的

delete from 表名


image

2.3.4 查询记录

数据准备

#创建商品表
create table product(
    pid int primary key auto_increment,
    pname varchar(20),
    price double,
    pdate timestamp
)

insert into product values (null,'谭妮平',0.01,null);
insert into product values (null,'李士雪',38,null);
insert into product values (null,'左慈',-998,null);
insert into product values (null,'黄迎',99999,null);
insert into product values (null,'南国强',99998,null);
insert into product values (null,'士兵',1,null);
insert into product values (null,'李士兵',698,null);

2.3.4.1 简单查询

  • 查询所有

select * from profuct

image
  • 查询商品名和商品价格

select pname,price from product

image
  • 查询所有商品信息使用表别名

select * from product as p //as可以省略

image
  • 查询商品名使用列别名

select pname as p from product

image
  • 去掉重复值(按照价格值)

select distinct(price) form product

image
  • 将所有的商品价格+10进行显示

select pname,price+10 from profuct

image

2.3.4.2 条件查询

  • 查询商品名称位“左慈”的商品信息

select * from product where pname = '左慈'

image
  • 查询价格>60元的所有商品信息

select * from profuct where price>60

image
  • 查询商品名称含有“士”的商品信息

select * from profuct where pname like '*%士%'

image
  • 查询商品id在(3,6,9)范围内的所有商品信息

select * from profuct where pid in(3,6,9);

image
  • 查询商品名称含有"士"字并且id为6的商品信息

select * from product where pname like '%士%' and pid = 6

image
  • 查询id为2或者6的商品信息

select * from profuct where pid = 2 or pid = 6

image

2.3.4.3 排序

  • 查询所有的商品,按价格进行排序(升序、降序)

select * from product order by pricr desc

image

desc:降序
asc:升序,默认

  • 查询名称有"士"的商品信息并且按照价格降序排序

select * from profuct where pname like '%士%' order by price desc

image

2.3.4.4 聚合函数

  • 获得所有商品的价格的总和

select sum(price) from product

image
  • 获得所有商品的平均价格

select avg(price) from product

image
  • 获得所有商品的个数

select count(*) from profuct

image

2.3.4.5 分组操作

数据准备

1.添加分类id (alter table product add cid varchar(32);)
2.初始化数据
update product set cid='1';
update product set cid='2' where  pid in (5,6,7);
  • 根据cid字段分组,分组后统计商品的个数。

select count(*) from product group by cid

image
  • 根据cid分组,分组统计每组商品的平均价格,并且平均价格大于20000元。

select avg(price) from product group by cid having avg(price) > 20000

image

2.3.4.6 查询总结

关键字出现的顺序:
select
from
where
group by
having 分组后带有条件只能使用having
order by 它必须放到最后面

MySQL字段类型

image

image

插入数据中文乱码问题解决

  • 直接修改数据库安装目录里面的my.ini文件的第57行(不建议)
image
  • set names gbk;
image

delete和truncate

delete:删除的时候是一条一条的删除记录,它配合事务,可以将删除的数据找回。
truncate:它是将整个表摧毁,然后再创建一张一模一样的表。它删除的数据无法找回。

delete演示:

image

Truncate演示:

image

delete删除,uid不会重置!而使用truncate操作,uid会重置

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容