MySQL学习(2)----DDL,DML,DQL

1.SQL(结构化查询语言)

可以操作关系型数据库

DDL(Data Definition Language)----操作数据库和表

DML(Data Manipulation Language)----增删改表中数据

DQL(Data Query Language)----查询表中数据

DCL(Data Control Language)----授权

2.SQL通用语法

   1.SQL语句可以单行或多行书写,以分号结尾,可以用空格和缩进增强语句可读性

    2.mysql数据库的SQL语句不区分大小写

      3. 单行注释:-- 注释内容  或 #注释内容  (--和注释内容间有空格,#和注释内容间没有空格)  

           多行注释:/*注释内容*/

3.DDL  操作数据库、表 

3.1操作数据库

1.Create创建:

  创建数据库: create  database  数据库名称;

                        create  database  if  not  exists  数据库名称;(防止重名报错)

                        create  database  数据库名称  character set 字符集;(指定字符集)

2.Retrieve查询:

    查看所有数据库:show databases;

    查询某个数据库的字符集:show  create  database 数据库名称;

3.Update修改:

     修改数据库字符集:alter  database  数据库名称  character  set  字符集名称;

4.Delete删除:

    删除数据库:drop database  数据库名称;

                         drop database  if  exists  数据库;(判断存在数据库)

5.使用:

   使用数据库:use  数据库名称;

    查询当前在用的数据库名称:select  database();

3.2操作表

1.Create创建:

        create  table  表名(

                                     列名1   数据类型1,

                                     列名2   数据类型2,

                                     。。。。。。。

                                     列名n   数据类型n

                           );

            (**最后一个列名 数据类型后没有逗号)

数据类型:

1.整数类型:int                    2.小数类型:double(5,2)               3.日期类型(yyyy-mm-dd):date      

4.日期类型(yyyy-mm-dd  hh:mm:ss):datetime

5. 时间戳类型(yyyy-mm-dd  hh:mm:ss):timestamp    (不给这个字段赋值,默认使用当前系统时间自动赋值)    

6.字符串类型:varchar(20)  (括号里的数字代表允许的最大字符长度)

2.Retrieve查询:

         查询某个数据库中所有表的名称:show  tables;

         查询某个表的结构:desc  表名称;

3.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.Delete删除:

      删除表:drop  table  表名;

4.DML  增删改表中数据

1.添加数据:insert  into  表名(列名1,列名2,.....,列名n)  values(值1,值2,....,值n);

/*  列名和值要一一对应;不写列名则默认给所有列赋值;值除int类型外都用引号(单双均可)引起来*/

2.删除数据:delete  from  表名 【 where  条件】

                   truncate  table  表名;(先删除表,再创建一张一样的表)

/* 不加条件则删除表中所有数据;*/

3.修改数据:update   表名  set   列名1=新值1,列名2=新值2,....【where  条件】

5.DQL  查询表中记录

1.语法:select *  from 表名;(查询表中所有信息)

           select        查询字段列表

              from       表名列表

                where      条件列表

                   group  by   分组字段

                      having       分组之后的条件

                         order  by     排序

                            limit        分页限定

2.基础查询:

             2.1仅查询姓名和年龄:select 

                                                              name,  #姓名

                                                               age     #年龄

                                                   from 

                                                            student;  #学生表

            2.2去除重复:select  distinct  address  from  student;()

            2.3计算math和English分数之和:select  name,math,english,math+english  from  student;

                           (有null参与运算,结果为null----->ifnull(式1,式2) 式1为null 用式2代替 )

            2.4起别名:select  name,math,english,math+english as 总分  from  student;

                                select name 姓名,math 数学,english 英语 from student;(省略as)

3.条件查询

       3.1 where语句后跟条件

       3.2 运算符

             >    ,   >=    ,    <  ,   <=   ,   =  ,    <> 

             between ....and....

             in(.....)

             like  模糊查询

                             占位符:单个任意字符_

                                            多个任意字符%

             is  null

             and  或  &&

             or   或   ||

             not   或   !

       3.3

              年龄小于或等于25:     select *  from  student   where  age<=25;

              年龄不等于25:        select *  from  student   where  age  !=  25;

                                                select *  from  student   where  age  <>  25;

              年龄大于等于20且小于等于30:  select * from  student   where   age>=20  &&  age<=30;

                                                                   select * from  student   where   age>=20  and  age<=30;

                                                                    select * from  student   where   age  between  20  and  30;

             年龄恰为22岁,或18岁,或25岁:

                                                                    select * from  student   where   age=22  or age=18 or age=25;

                                                                    select * from  student   where   age  in (22,18,25); 

             英语成绩为null,不为null: null值不能用=或!=判断

                                                                     select * from  student   where   english  is  null; 

                                                                     select * from  student   where   english  is   not   null; 

            查询姓李的学生:

                                  select * from  student   where  name  like '李%'; 

           查询姓名是三个字的学生:

                                   select * from  student   where  name  like '___'; (三个下划线)

4.排序查询:

                     语法:order   by  子句

                     order   by  排序字段1  排序方式1,排序字段2  排序方式2;

                     ASC:升序 (默认的)          DESC:降序  

           按数学成绩排序:   select * from student order by math;(默认由小到大ASC)

                                           select * from student order by math  desc;(DESC降序)

           数学成绩相同时按英语成绩排序:   select * from student order by math desc,english;

5.聚合函数:将一列数据作为一个整体,进行纵向计算 。(注意:排除null值)

                    count:计算个数(一般选择主键计算)

                    max:计算最大值

                    min:计算最小值

                    sum:求和

                    avg:计算平均值

         依据数学成绩计算学生个数:select  count(math)  from  student;(结果为6)

                                                        select  count(english)  from  student;(有一人英语成绩为null,结果为5)

                                                        select  count(*)  from  student;(一条记录有一列不为空,则计算)

         选择数学成绩最大值,最小值:select  max(math)  from  student;

                                                           select  min(math)   from  student;

         求数学成绩总分:  select   sum(math)   from  student;

         求数学成绩平均分:  select   avg(math)   from  student;

6.分组查询:

                  语法:group   by  分组字段;

                  注意:1.分组之后查询的字段:分组字段、聚合函数

                             2.where 和 having 的区别

                                   where在分组之前进行限定,不满足条件,则不参与分组;

                                   having在分组之后进行限定,不满足结果,则不会被查询出来。

                                  where后不可以跟聚合函数;having可以进行聚合函数的判断。

         按性别分组,分别查询男、女同学的数学平均分:

                       select  sex,count(id)  人数,avg(math)  as  数学平均分    from  student  group  by  sex;

         按性别分组,分别查询男、女同学的数学平均分,要求:只有成绩大于80才统计:

                       select  sex,count(id)  人数,avg(math)  as  数学平均分    from  student   where  math>80  group  by  sex;

7.分页查询:

                   语法:limit  开始的索引,每页查询的条数;(limit只在mysql中用)

                   公式:开始的索引=(当前的页码-1)* 每页显示的条数

         每页3条记录:  select * from student limit  0,3;(第一页,1--3)

                                   select * from student limit  3,3;(第二页,4--6)

发布了19 篇原创文章 · 获赞 0 · 访问量 588

猜你喜欢

转载自blog.csdn.net/qq_44617005/article/details/102825866