- DDL(Data Definition Langua ge):操作数据库,表(create,drop,alter等)
- 操作数据库:CRUD
- C(Create):创建
- 创建数据库:create database 数据库;
- 判断数据库是否存在并创建数据库(如果存在就不创建):create database if not exists 数据库名称;
- 按照指定的字符集创建数据库:create database db1 character set 字符集;
- 例:创建一个db1的数据库,判断是否存在,并指定字符集为gbk:create database if not exists db1 character set gbk;
- R(Retrieve):查询
- 查询所有数据库的名称:show databases;
- 查询某个数据库的字符集(查看创建 某个数据库的语法):show create database 数据库名称: ;
- U(Update):修改
- 修改数据库的字符集:alter database 数据库名称 character set 字符集;
- D(Delete):删除
- 删除数据库:drop database 数据库名称;
- 判断数据库是否存在并删除(如果存在就删除):drop database if exists 数据库名称;
- 使用数据库
- 查询当前正在使用的数据库名称:select database();
- 使用数据库:use 数据库名称;
- 操作表
- C(Create):创建
- 创建表: create table 表名称;
create table 表名(
列名1 数据类型1,
列名2 数据类型2,
列名3 数据类型3,
列名4 数据类型4
);
注意:最后一列,不需要加(,)否则会报错
数据库类型:
1.int 整数类型
age int,
2.double 小数类型
score double(5,2)
3.date 日期,只包含年月日,yyyy-MM-dd
4.datetime 日期,包含年月日时分秒 yyyy-MM-dd HH:mm:ss
5.timestamp 时间戳 包含年月日时分秒 yyyy-MM-dd HH:mm:ss
如果是timestamp 不给这个字段赋值,或赋值为null,默认使用当前的系统时间,来自动赋值
6.varchar 字符串类型
name varchar (20):姓名大于20个字符
创建表
create table student(
id int,
name varchar(32),
age int,
score double(4,1),
birthday date,
insert timestamp
);
3.复制表:
- create table 表名 like 被复制的表名;
- R(Retrieve):查询
- 查询某个数据库所有的表名称:show tables;
- 查询表结构:desc 表名;
- U(Update):修改
- 修改表的表名
- alter table 表名 rename to 新的表名;
- 修改表的字符集
- alter table 表名 character set 字符集的名称;
- 修改列名称 类型
- 添加一列
- alter table 表名 add 列名 数据类型;
- 即修改列名又修改数据类型
- alter table 表名 change 旧列名 新列名 新数据类型;
- 只修改数据类型
- alter table 表名 modify 列名 新数据类型;
- 删除列
- alter table 表名 drop 列名;
- D(Delete):删除
- drop table 表名;
- drop table if exists 表名;
- DML(Data Manipulation Language):增删改表中的数据(insert,delete,update)
- 添加数据
- insert into 表名 (列名1,列名2,列名3,.....列名4)values(值1,指2,值3...值4);
- 注意:
- 列名和值要一一对应,(名字和数据类型)
- 如果表名之后,不定义列名,默认给所有的添加;
- insert into 表名 values(值1,指2,值3...值4);
- 修改数据
- update 表名 set 列名1=值1,列名2=值2 where 条件;
- 注意:
- 如果不加任何条件,就会对表中的所有数据都进行更改;
- 删除数据
- delete from 表名 [where 条件]; (不推荐使用,表中有多少条数据就会删除多少次记录)
- 注意
- 如果不加条件,就会删除表中的所有的记录;
- truncate table 表名; (先删除表,在创建一条一样的表)
- DQL(Data Query Language):查询表中的数据(select ,where)
- select * from 表名;
- 语法:
select
字段列表
from
表名列表
where
条件列表
group by
分组字段
order by
排序
limit
分页限定
- 基础查询
- 多个字段查询
- select 字段名1, 字段名2, 字段名3, 字段名4 from 表名;
- 注意:
- 如果使用所用的字段可以使用*代替;
- 去除重复
- select distinct 字段名 表名;
- select distinct name student;
- 如何计算列
- 一般使用四则运算计算一些列的值(一般只会进行计算数值型的数据)
- 计算成绩总分
- select name ,math,English,math+English from 表名;
- 如果存在null 就要将null 转换,null参与计算的结果都为null;
- select name ,math,English,math+if null(English ,0) from 表名;
- 起别名
- 在计算之后的列名是非常难看的,所以就可以给他起一个别名
- select name ,math 数学,English 英语,math+ if null(English ,0)as(as可写可不写) 总分 from 表名;
- 条件查询
- where 字句后面跟条件
- 运算符
- 两种不等于
- !=
- <>
- 中间查询
- select * from 表名 where age >=20 &&
- select * from 表名 where age >=20 and
- select * from 表名 where age between 20 and
- 查询或条件
- select * from student where age=10 or age=3 or age=12;
- select * from student where age in (10 ,3, 12);
- 如果查询条件为null,要使用is而不是=;
- select * from 表名 字段 is null;
- 模糊查询 like
- 占位符
- _ :单个任意字符
- 例如:要查询名字的第二个字为中
- select * from 表名 where name like ' _中%';
- %:多个任意字符
- 例如:要查询姓张的人
- select * from 表名 where name like '张%';
- 例如:要查询姓名中包含张字的人
- select * from 表名 where name like '%张%';
- DCL(Data Control Language):数据库授权(grant,revoke)
- 排序查询
- order by 字句
- order by 排序字段1 排序方式1,排序字段2 排序方式2....;
- 排序方式:升序(asc) 降序(desc)
- select * from student order by math asc;
- select * from student order by math desc;
- select * from student order by math desc,English asc;
- 注意:
- 如果有多个排序条件,则当前面的条件值一样时,才会判断第二条件;
- 聚合函数
- 将一列的数据看作为一个整体,进行纵向的计算;
- count:计算个数
- 一般会选择非空 的列,主键;
- count(*)
- max:计算最大值
- select max(math) from student;
- min:计算最小值
- select min(math) from student;
- sum:计算和
- select sum(math) from student;
- avg:计算平均值
- 计算表中人数
- select count(name) from 表名;
- 注意:
- 聚合函数的计算会排出null值;
- 解决方案:
- 选择不包含非空的列进行计算;
- if null 函数
- 分组查询
- group by 分组字段;
- select
- 注意:
- 分组之后查询的字段, 分组字段,聚合函数
- 例:
- 查询男女同学的平均分
- select sex,avg(math)from student group by sex;
- 查询男女同学的平均分及人数
- select sex,avg(math),count (id)from student group by sex;
- 查询男女同学的平均分及人数,成绩小于70的不参与分组,分组之后的人数要大于2人
- select sex,avg(math),count (id)from student where math>70 group by sex having count(id)>2;
- where和having的区别?
- where之后不可以跟聚合函数;having可以进行聚合函数的判断
- where在分组之前进行限定,如果不满足条件,则不参与分组,having在分组之后进行限定,如果不满足结果,则不会被查询出来
- 分页查询
- 例:每页显示三条记录
- select * from limit 0,3; 第一页
- select * from limit 3,3; 第二页
- select * from limit 6,3; 第三页
- 公式: 开始的索引等于《(当前的页码-1)*每页显示的条数》;
- limit 是一个mysql的"方营"(只在mysql中使用)