数据库——增删改查

增删改查
    增加:
        类型匹配;(字符串、日期都需要使用单引号)
        类型范围;
        插入值的顺序;
        隐含列,列名一致;
        批量插入,用多组value值
    主键冲突:
         更新操作:on duplicate key updete goods_name='葡萄',price=10;
         替换操作:replace into goods(id,name,price) value (1,'葡萄',12);
    更新:update goods set price=300;(全部)
          update goods set price=1000 where id=100;(指定项)
          update goods set price=price+200 where id =101;(价格改变)
          update goods set price=200 limit 3;(修改前N个)
    删除:
        复制表:create table goods2 like goods(创建)
                insert into goods2 select * from goods(数据导入)
         delete from goods  (删除全部)                        
         delete from goods where id=101;(指定删除)配合where 灵活删除某一列;
         truncate goods(删除表的全部内容,无返回值):速度高,清空时使用;
         drop table goods2;(删除表)
    查询:select id,name,math from student;(只查询指定列)
          select * from student;(显示所有)效率低
          select distinct math from student(去重,减少重复行)
          select id,name,math+2 from student (不改变原表,查出数据作运算)
          select id,name,(总分)*1.6 from student where name like '唐%'(姓唐的所有增加)          
    where 语句:
          select id,name,(chinese+eglish+math)as total from student where (chinese+english+math)>200
          select id,name,math from student order by math asc(升序,默认)/desc(降序)
          
    limit 分页:(提升查询效率;)
        select id,name from student limit 0,3;
        select id,name from student limit 3,3;
    聚合函数:
      Count (列名)返回某一列,行的总数;
        select count(*)from student;
        select count(*)from student where math >=90;
        * 统计一共的记录数  count(1)不会扫描全表
        列名 统计时省略NULL
      sum (一列总和)——对数值有效
        select sum(math) from student;
        select sum(Chinese),sum (eglish),sum(math) from student;
        select sum(Chinese+English+math) from student;
        select sum(math)/count(name) from student ;
      avg:求平均
        select avg(chinese+english+math) from student;
      max/min    
        select max(math),min(math) from student;
      group by :分组查询 (不能使用where,需要使用having)
        select deptno,avg(sal),max(sal) from EMP group by deptno ;
        select deptno,avg(sal) as avgsal from emp group by deptno having avgsal<2000;

猜你喜欢

转载自blog.csdn.net/fayfayfaydyt/article/details/82080734