2、MySQL数据库的DDL&DML&DQL

SQL

1.什么是SQL?
    Structured Query Language:结构化查询语言
    其实就是定义了操作所有关系型数据库的规则。每一种数据库操作的方式存在不一样的地方,称为“方言”。
    
2.SQL通用语法
    1. SQL 语句可以单行或多行书写,以分号结尾。
    2. 可使用空格和缩进来增强语句的可读性。
    3. MySQL 数据库的 SQL 语句不区分大小写,关键字建议使用大写。
    4. 3 种注释
        * 单行注释: -- 注释内容 或 # 注释内容(mysql 特有)
        * 多行注释: /* 注释 */
    
3. SQL分类
    1) DDL(Data Definition Language)数据定义语言
        用来定义数据库对象:数据库,表,列等。关键字:create, drop,alter 等
    2) DML(Data Manipulation Language)数据操作语言
        用来对数据库中表的数据进行增删改。关键字:insert, delete, update 等
    3) DQL(Data Query Language)数据查询语言
        用来查询数据库中表的记录(数据)。关键字:select, where 等
    4) DCL(Data Control Language)数据控制语言(了解)
        用来定义数据库的访问权限和安全级别,及创建用户。关键字:GRANT, REVOKE 等

DDL:操作数据库、表

1. 操作数据库:CRUD
    1. C(Create):创建
        * 创建数据库:
            * create database 数据库名称;
        * 创建数据库,判断不存在,再创建:
            * create database if not exists 数据库名称;
        * 创建数据库,并指定字符集
            * create database 数据库名称 character set 字符集名;

        * 练习: 创建db4数据库,判断是否存在,并制定字符集为gbk
            * create database if not exists db4 character set gbk;
    2. R(Retrieve):查询
        * 查询所有数据库的名称:
            * show databases;
        * 查询某个数据库的字符集:查询某个数据库的创建语句
            * show create database 数据库名称;
    3. U(Update):修改
        * 修改数据库的字符集
            * alter database 数据库名称 character set 字符集名称;
    4. D(Delete):删除
        * 删除数据库
            * drop database 数据库名称;
        * 判断数据库存在,存在再删除
            * drop database if exists 数据库名称;
    5. 使用数据库
        * 查询当前正在使用的数据库名称
            * select database();
        * 使用数据库
            * use 数据库名称;

##DDL
drop database if exists db;
create database if not exists db character set utf8;
alter database db character set utf8;
show create database db;
use db;
select database();

2. 操作表
    1. C(Create):创建
        1. 语法:
            create table 表名(
                列名1 数据类型1,
                列名2 数据类型2,
                ....
                列名n 数据类型n
            );
            * 注意:最后一列,不需要加逗号(,)
            * 数据库类型:
                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    
                    * 如果将来不给这个字段赋值,或赋值为null,则默认使用当前的系统时间,来自动赋值

                6. varchar:字符串
                    * name varchar(20):姓名最大20个字符
                    * zhangsan 8个字符  张三 2个字符

* 创建表
            create table student(
                id int,
                name varchar(32),
                age int ,
                score double(4,1),
                birthday date,
                insert_time timestamp
            );
        * 复制表:
            * create table 表名 like 被复制的表名;          
    2. R(Retrieve):查询
        * 查询某个数据库中所有的表名称
            * show tables;
        * 查询表结构
            * desc 表名;
    3. U(Update):修改
        1. 修改表名
            alter table 表名 rename to 新的表名;
        2. 修改表的字符集
            alter table 表名 character set 字符集名称;
        3. 添加一列
            alter table 表名 add 列名 数据类型;
        4. 修改列名称 类型
            alter table 表名 change 列名 新列别 新数据类型;
            alter table 表名 modify 列名 新数据类型;
        5. 删除列
            alter table 表名 drop 列名;
    4. D(Delete):删除
        * drop table 表名;
        * drop table  if exists 表名 ;

drop table if exists student;
create table if not exists student (
	id int PRIMARY KEY auto_increment,
	name varchar(32),
	age int ,
	score double(4,1),	#分数
	birthday date,			#出生日期
	insert_time timestamp NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP() 
	#添加时间
);
drop table if exists stu; 
create table if not exists stu like student;
drop table if exists stu; 
alter table student add gender varchar(10);#性别
alter table student change gender sex varchar(20);
alter table student modify sex varchar(10);
alter table student drop sex;
show  create table student;
alter table student rename to stu;
alter table stu character set utf8;
show  create table stu;
show tables;
desc stu;

客户端图形化工具:SQLYog 、Navicat

DML:增删改表中数据

1. 添加数据:
    * 语法:
        * insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n);
    * 注意:
        1. 列名和值要一一对应。
        2. 如果表名后,不定义列名,则默认给所有列添加值
            insert into 表名 values(值1,值2,...值n);
        3. 除了数字类型,其他类型需要使用引号(单双都可以)引起来
2. 删除数据:
    * 语法:
        * delete from 表名 [where 条件]
    * 注意:
        1. 如果不加条件,则删除表中所有记录。
        2. 如果要删除所有记录
            1. delete from 表名; -- 不推荐使用。有多少条记录就会执行多少次删除操作
            2. TRUNCATE TABLE 表名; -- 推荐使用,效率更高 先删除表,然后再创建一张一样的表。
3. 修改数据:
    * 语法:
        * update 表名 set 列名1 = 值1, 列名2 = 值2,... [where 条件];

    * 注意:
        1. 如果不加任何条件,则会将表中所有记录全部修改。

##DML
insert into stu(id,name,age) values(1,"王祖贤",18);
insert into stu values(2,"赵敏",17,99.9,"1893-11-11",CURRENT_TIMESTAMP());
insert into stu values(3,"黄明皓",19,96.9,"1997-3-17",null);
#timestamp赋值在声明参数时且为空无法使用默认值
update stu set age=33  where id = 2;
update stu set age=26 , score=80 where id = 3;
-- delete from stu;
-- truncate table stu;
delete from stu where id=1;
-- select * from stu;

DQL:查询表中的记录

* select * from 表名;

1. 语法:
    select
        字段列表
    from
        表名列表
    where
        条件列表
    group by
        分组字段
    having
        分组之后的条件
    order by
        排序
    limit
        分页限定

2. 基础查询
    1. 多个字段的查询
        select 字段名1,字段名2... from 表名;
        * 注意:
            * 如果查询所有字段,则可以使用*来替代字段列表。
    2. 去除重复:
        * distinct
    3. 计算列
        * 一般可以使用四则运算计算一些列的值。(一般只会进行数值型的计算)
        * ifnull(表达式1,表达式2):null参与的运算,计算结果都为null
            * 表达式1:哪个字段需要判断是否为null
            * 如果该字段为null后的替换值。
    4. 起别名:
        * as:as也可以省略

##DQL
create table if not exists student (
	id int PRIMARY KEY auto_increment,
	name varchar(32),
	age int ,
	sex varchar(5),
	address varchar(100),
	math int,
	english int
);
insert into student(id,name,age,sex,address,math,english) values(null,"马云",55,"男","杭州",66,78),(null,"马化腾",45,"男","深圳",91,98),(null,"马景涛",55,"女","香港",76,88),(null,"柳岩 ",20,"女","湖南",null,78),(null,"柳青",18,"女","湖南",56,null),(null,"刘德华",18,"男","香港",76,88),(null,"黄明皓",26,"女","珠海",89,91);

select * from student;
select distinct address from student;
select distinct sex,address from student;
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student;

3. 条件查询
    1. where子句后跟条件
    2. 运算符
        * > 、< 、<= 、>= 、= 、<>
        * BETWEEN...AND  
        * IN( 集合)
        * LIKE:模糊查询
            * 占位符:
                * _:单个任意字符
                * %:多个任意字符
        * IS NULL  
        * and  或 &&
        * or  或 ||
        * not  或 !

#查询年龄大于等于20
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where age>=20;

#查询年龄等于20
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where age=20;

#查询年龄不等于20
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where age!=20;
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where age<>20;

#查询年龄大于等于20 小于等于30 
#不推荐使用
-- select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where age=>20 && age<=30;
-- select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where age=>20 and age<=30;
#推荐写法
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where age between 20 and 30;

#查询年龄22岁,19岁,25岁,18岁的信息
-- select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where age = 22 or age = 19 or age = 25 or age = 18;
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where age in(22,19,25,18)

#查询英语成绩为null
-- select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where english =null; -- 不对的.null值不能使用=(!=)判断
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where english is null;
 
#查询英语成绩不为null
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where english is not null; 

#查询姓马的有哪些? like
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where name like "马%";

#查询姓名第二个字是化的人
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where name like "_化%";

#查询姓名是三个字的人
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where name like "___";
#查询姓名中包含马的人
select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student where name like "%马%";

DQL:查询语句

1. 排序查询
    * 语法:order by 子句
        * order by 排序字段1 排序方式1 ,  排序字段2 排序方式2...

    * 排序方式:
        * ASC:升序,默认的。
        * DESC:降序。

    * 注意:
        * 如果有多个排序条件,则当前边的条件值一样时,才会判断第二条件。

2. 聚合函数:将一列数据作为一个整体,进行纵向的计算。
    1. count:计算个数
        1. 一般选择非空的列:主键
        2. count(*)
    2. max:计算最大值
    3. min:计算最小值
    4. sum:计算和
    5. avg:计算平均值

* 注意:聚合函数的计算,排除null值。
        解决方案:
            1. 选择不包含非空的列进行计算
            2. IFNULL函数

3. 分组查询:
    1. 语法:group by 分组字段;
    2. 注意:
        1. 分组之后查询的字段:分组字段、聚合函数
        2. where 和 having 的区别?
            1. where 在分组之前进行限定,如果不满足条件,则不参与分组。having在分组之后进行限定,如果不满足结果,则不会被查询出来
            2. where 后不可以跟聚合函数,having可以进行聚合函数的判断。

4. 分页查询
    1. 语法:limit 开始的索引,每页查询的条数;
    2. 公式:开始的索引 = (当前的页码 - 1) * 每页显示的条数
        -- 每页显示3条记录
    3. limit 是一个MySQL"方言"

 select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student order  by math; -- 排序 默认升序
 select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student order  by math asc; -- 排序 升序

 select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student order  by math desc; -- 排序 降序

#按照数学成绩排名,如果数学成绩一样,泽按照英语成绩排名
 select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student order  by math asc ,english desc;

 select distinct count(english) from student;
 select distinct count(ifnull(english,0)) from student;
# *不推荐使用
-- select distinct count(*) from student;

 select distinct max(english) from student;
 select distinct min(english) from student;
 select distinct sum(english) from student;
 select distinct avg(english) from student;


#按照性别分组,分别查询男、女同学的平均分,人数
 select sex,avg(math),count(id) from student group by sex;

#按照性别分组,分别查询男、女同学的平均分,人数 要求:分数低于70分的人,不参与分组
 select sex,avg(math),count(id) from student where math>70 group by sex;

#按照性别分组,分别查询男、女同学的平均分,人数 要求:分数低于65分的人,不参与分组.分组之后,人数大于2
 select sex,avg(math),count(id) 人数 from student where math>65 group by sex having count(id)>2;

#每页显示3条记录 limit只能在mysql用
 select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student limit 0,3;
 select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student limit 3,3;

select distinct name,age,sex as "性别",address,math "数学",english 英语,IFNULL(math,0) + IFNULL(english,0) as 总分 from student limit 6,3;
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值