创建,读取,修改,删除表 简单查询 12种

  1. 注释语法- -, #
  2. .sql后缀的文件是数据库查询文件,用数据库查询打开
  3. 保存查询
  4. 在数据库中 列有个名字叫字段,行有个名字叫记录。

CRUD操作:create 创建 read  读取update 修改delete  删除

一、添加数据

Insert  into  info  values

(

‘p009’,                                        # 主键不能重复

’张三’,

1,                                             # 布尔型 不是字符串不用引号

’n001’,

’2016-8-30 12:9:8’

 )  ;                                                #往info表中添加数据

给特定的列添加

Insert  into  info  values(‘p009’, 张三’,’’,’’,’’);             #这样是空字符串   红底的是表名字   红字是列名字  蓝字是数据

或者

Insert  into  info (code,name )  values(‘p009’, ’张三’);          #这样式全空    红底是表名   红字是列名字  蓝字是数据

自增长的列的处理

Inset  into family  values(‘’, ‘p001’,’名字’ );                      #自增长设置空字符串

二.删除数据 

         删除所有数据 Delete  from  family                                           红字是列名字

         删除部分数据Delete  from  info  family  where  code=’p001’                      红字是列名字  蓝字是数据

         Delete  from  表名 where 条件

三,修改数据

         全部修改 Update  info  set  name=’姓名’                                           把name列下面的所有数据全部改成了名字

         修改特定数据 Update  info  set  name=’姓名’ where code =’p002’          修改code  列下面第p002个的name列的名字为姓名

         修改多列 Update  info  set  name=’姓名’ , sex=1  where code =’p002

    Update  表名  set  要修改的内容  where 条件

四,读取数据

  1. 简单读取 查询所有列(*) 所有行(没有加条件)

      Select*from info                                     从info表中查询所有列所有行

  2.读取特定列

      Select  code,name from info                               从info表中查询code和name列

  3.条件查询

      Select*from info whrer code=’p003

  4. 多条件查询

      Select*from info wherer code=’p003’ or  nation=’n002’                  或的关系   两个条件满足一个都可以

      Select*from info wherer code=’p003’  and  nation=’n002’                 与的关系  同时满足两个条件才可以

       5.关键字查询(模糊查询)

                   查所有包含奥迪的汽车

                   Select*from info wherer name like ‘%奥迪%’;          百分号%代表任意多个字符

                   查以’皇冠’开头的的所有汽车

                   Select*from info wherer name like ‘皇冠%’; 

                  查询汽车名称中第二个字符是’马’的

                   Select*from info wherer name like ‘_% ’            下划线_代表任意一个字符

         6.排序查询

                   Select*from  car  order  by  powers      根据powers排序  默认升序排列

                   Select*from  car  order  by  powers  desc        降序排列

                   先按brand升序排,再按price降序排

                   Select*from  car  order  by  brand,price  desc    第一个主要的,先按第一个排

    

    7.范围查询

      Select  *  from  car  where   price > 40 and price<60        查询  40到60之间的

      Select  *  from  car  where   price  between  40  and  60

      #红色标注是列名, 紫色是表名字

    8.离散查询

      Select  *  from  car  where  price=30 or  price=40  or  price=50 or  price=60           查询 等于30   等于40  等于50  等于60

      Select  *  from  car  where  price  in(30,40,50,60)

      Select  *  from  car  where  price  not  in(30,40,50,60)                                           查询 不等于30.40.50.60 的

    9.聚合函数(统计查询)    #后面可以加条件就是加where

      Select  count(*)  from car                               count(*)所有数据条数

      Select  count(主键)  from car                         取所有数据条数           count(主键)效率较高                                                                                             

      Select  sum(price)  from  car                   求price列的总和

      Select  avg(price)  from  car                  求price列的平均值

      Select  max(price)  from  car                    求price列的最大值

      Select  min(price)  from  car                 求price 列里面最小值

    10.分页查询

      Select  *  from  car   limit  0,10                 跳过0条取10条   limit  跳过

      首先规定每页显示条数:m

      当前页数:n

      Select* from  car  limit  (n-1)*m,m                     做分页

     11.去重查询

      Select  brand  from  car                                   查汽车表中出现的brand列中所有数

      Select  distinct  brand  from  car                      distinct后面跟列名 去重

    12.分组查询

      查询car列表中,每个系列下汽车的数量

      Select  brand,count(*)  from  car  group  by  brand           groud分组的意思       就是把后面的分成一组然后在查询

      分组后只能查询该列和聚合函数

      取该系列平均值大于40的系列序号

      Select  *  from  car  group  by brand  having  avg(price)>40                #分组后加条件用having   

      取该系列油耗最大值大于8的系列代号

      Select  *  from  car  group  by brand  having  max(oli)>8    

原文地址:https://www.cnblogs.com/sq45711478/p/5966726.html