zoukankan      html  css  js  c++  java
  • 数据库的DML操作

    DML:数据操纵语言 表中的数据进行操作的语言

    增加数据 insert
    删除数据 delete
    修改数据 update
    查找数据 select 最高的使用频率

    一、DML数据操纵语言
    数据操纵语言是对数据库对象中数据的操作,比如对数据进行增加、删除、修改和查询等操作
    关键字:insert / delete / update / select

    1:插入数据
    语法:insert into 表名[(字段名….)] values(值….)
    #创建数据表
    create table classroom(
    cid int,
    cname varchar(20),
    des varchar(50)
    )

    #添加数据到classroom
    #1.1、给所有字段添加值,和表中字段顺序一致
    insert into classroom values(1,'java','java班');

    #1.2给cname字段添加值,和指定字段的顺序必须一致
    insert into classroom(cname) values('python');
    insert into classroom(cid,cname) values(3,'test');

    #1.3添加3条记录(批量插入)
    insert into classroom values(4,'oracle','oracle班'),
    (5,'html','html班'),
    (6,'Mysql','Mysql班');

    #1.4将classroom的值整体复制到classroom1
    create table classroom1(
    cid int,
    cname varchar(20),
    des varchar(50)
    )

    insert into classroom1 select * from classroom;
    select * from classroom1;
    -- 前提:classroom1和classroom表的结构必须要一样

    #1.5查看表的所有记录
    select * from classroom;

    2:删除数据
    语法:delete from 表名 [where 条件]

    #2.1、删除classroom中的一条数据
    select * from classroom;
    delete from classroom where cname='python';

    #2.2、删除classroom中的所有数据
    delete from classroom;
    select * from classroom;

    #2.3、truncate清空表的数据
    语法:truncate table 表名;
    truncate table classroom;

    注意:delete与truncate的区别是什么
    1:delete是逐行删除,truncate是文件级别的清空
    2:delete删除后,自增性会继续执行,不会重置
    truncate删除后,自增性重置

    3:更改数据
    语法:update 表名 set 字段=新值… [where 条件]
    #3.1修改classroom中的一条数据
    update classroom set cname='test' where cid=1; #cid主键列
    update classroom set cname='test1' where des='oracle班';

    #3.2、修改classroom中的所有数据
    update classroom set des='班级信息';

    4:查询数据
    查询的完整语法:
    select 字段|表达式 from 表名|视图|结果集
    [where 条件]
    [group by 分组]
    [having 分组之后进行检索]
    [order by 排序]
    [limit 限制结果]

    #4.1查询所有信息
    select * from classroom

    #4.1.1、查看部分信息
    select cid,cname from classroom;

    #4.1.2、查看所有员工的姓名和工资 员工表emp 部门表dept表
    链接:https://pan.baidu.com/s/1UeJJBuzlgOrTKCAjjQ3rwA
    提取码:1cxi
    select * from emp;
    select * from dept;
    select ename,sal from emp;

    #4.1.3、员工工资提升5%之后的员工姓名和工资 --算术运算
    select ename,sal+sal*0.05 from emp;

    #4.2、范围查询
    #4.2.1、查询工资大于2000的员工信息 --比较运算
    select * from emp where sal>2000;

    #4.2.2、查询工资在1000-2000之间的员工信息(范围查询) --比较+逻辑运算
    select * from emp where sal between 1000 and 2000; #包括边界
    select * from emp where sal>=1000 and sal<=2000;

    #4.3、集合查询
    #4.3.1、查询员工编号为7521,7369,7788的员工信息(集合查询)
    select * from emp where empno=7521 or empno=7369 or empno=7788;
    select * from emp where empno in(7521,7369,7788);
    select * from emp where empno not in(7521,7369,7788);

    #4.4、取别名 (小名,别号)
    #4.4.1、字段,表达式,结果集,表都可以起别名
    select ename,sal+sal*0.05 as 提升后的薪资 from emp;
    select ename 姓名,sal+sal*0.05 提升后的薪资 from emp;

    #4.5、去重查询distinct
    #4.5.1、查询所有的职位信息 去重
    select distinct job from emp;

    #4.6、模糊查询
    #4.6.1、查询名字以s开头的员工信息
    模糊查询 like %:匹配字符,匹配0个或多个长度的任意字符
    _:匹配一个长度的任意字符
    select * from emp where ename like 's%';

    #4.6.2、查询名字中包含s的员工信息
    select * from emp where ename like '%s%';

    #4.6.3、查询第二字符为L的所有员工信息
    select * from emp where ename like '_L%';

    #4.7、排序
    #4.7.1、对所有员工的工资进行排序 升序asc和降序desc
    select * from emp order by sal;#升序 asc 可以省略,默认是升序的方式排列
    select * from emp order by sal desc;


    #4.7.2、根据员工的工资降序排,如果工资一致,则按照员工编号降序排列
    select * from emp order by sal desc,empno desc;


    #4.8、限制结果集显示
    #4.8.1、查询在10号部门工资最高的员工信息
    -- 1:查询在10号部门员工信息
    select * from emp where deptno=10;

    -- 2:工资最高 排序 desc
    select * from emp where deptno=10 order by sal desc;

    -- 3:只要第一个人的人信息
    select * from emp where deptno=10 order by sal desc limit 0,1
    -- limit m,n m:开始的位置,索引值从0开始 n:取值的长度(个数)

    #4.9、为空/非空数据查询与操作
    #4.9.1、查询所有有奖金的员工信息
    -- #查询所有有奖金的员工信息
    select *from emp where comm>0;

    -- #查询所有奖金列为空的员工信息
    select * from emp where comm is null;
    select * from emp where comm is not null;


    #4.9.2、将奖金小于500的员工奖金加100元
    -- #将奖金小于500的员工奖金加100元
    select * from emp where comm<500 or comm is null;
    update emp set comm=comm+100 where comm<500 or comm is null;

    update emp set comm=0 where comm is null;
    update emp set comm=comm+100 where comm<500 or comm is null;
    -- ifnull(comm,0) 判断comm列是否是null,如果是null,则赋值为0
    select * from emp;
    update emp set comm=ifnull(comm,0)+100 where comm<500 or comm is null;


    练习;

    1. 查询工资提升100元后超过2000的所有员工信息
    select sal+100 from emp where sal>2000

    2. 查询工资在1000-2000之间的10号部门的最高工资的员工信息
    select * from emp where deptno=10 and sal between 1000 and 2000 order by sal desc limit 0,1

    3. 将所有工资低于2000的员工工资提升5%
    update emp set sal=sal+sal*0.05 where sal<2000

    4. 查询名字包含s的并且在20号部门的员工信息
    select * from emp where DEPTNO=20 and ename like '%s%'

    5. 查询工资大于1000的10号部门的前5条记录
    select * from emp where DEPTNO=10 and sal>1000 limit 0,5

    6. 将奖金小于500的员工奖金加100元
    update emp set comm=ifnull(comm,0)+100 where comm<500 or comm is null

    二、SQL运算符
    算数运算符、比较运算符、逻辑运算符
    1:算数运算符
    +、-、*、/、div、%、 mod
    select 10/3; #3.3333 / 会保留小数
    select 10 div 3; #3 div 整除(只会取整个结果的整数部分)
    select 10 % 3; #1 % 取模 取余(取结果的余数)
    select 10 mod 3; #1 mod 取模 取余

    2:比较运算符
    >、<、>=、<=、=、!=、is null / is not null / between..and / in / not in
    返回结果永远都是布尔类型的值(true/false)

    3:逻辑运算符
    and 、or、!

    select 1<0 and 2>1; #0 0==>False
    select 1<0 or 2>1; #1 1==>True
    select !(1<0);#1 1==>True
    select 1>0; #1 1==>True
    select 1<0; #0 0==>False
    select 1!=0;
    select 10/3; #3.3333 / 会保留小数
    select 10 div 3; #3 div 整除(只会取整个结果的整数部分)
    select 10 % 3; #1 % 取模 取余(取结果的余数)
    select 10 mod 3; #1 mod 取模 取余

  • 相关阅读:
    iptables学习笔记_____摘自朱双印个人日志 ____http://www.zsythink.net/
    使用xmanager图形化远程连接rhel6
    powerdesigner
    CentOS和RedHat Linux的区别
    win7防火墙端口开放
    微服务 环境问题处理
    lombok
    pl/sql Devloper 如何查看表结构
    pl/sql Devloper使用
    luogu P4231 三步必杀
  • 原文地址:https://www.cnblogs.com/gskk/p/13213349.html
Copyright © 2011-2022 走看看