5、DML语句(数据操纵语言-增删改查)

2023-11-19

一、DML语句介绍

  1. DML语句是表层级的语句,只要用于增删改查表中的内容;
  2. oracle对数据的安全性要求很高,所有数据的增删改必须commit提交,并且每行代码只能插入一行数据;
  3. mysql中支持同时插入多行数据,且不需要commit,因此mysql的数据安全性要求没Oracle高。

二、insert 增加,插入数据

1. 基本语法

insert into 表名(1,2) values(1,2);

2. 示例

(1)创建一个表t_a,表中有两个列id,name.其中id的数据类型为number,name的数据类型为varchar2,长度20.插入一列time,数据类型为时间戳.并插入两行数据,id,name,time分别为1111,aaaa,null;2222,bbbb,null.

create table t_a (id number,name varchar2(20));
alter table t_a add time timestamp;
insert into t_a(id,name,time) values ('1111','aaaa',null);
insert into t_a(id,name,time) values ('2222','bbbb',null);
commit;

三、delete 删除表中的某行或者某些行数据

1. 基本语法

delete from 表名 where 限制条件;
commit;

注意:加了where指定删除内容,只删除满足条件的数据,若不加where,则删除全表

2. 示例

(1)删除t_a中id为111的行数据

delete from t_a where id=1111;
commit;

(2)删除t_a中time为null的值

delete from t_a where time is null;
commit;

注意:null用is来限定,is null / is not null

(3)删除t_a中的所有数据

delete from t_a;
commit;

四、update 更新、修改

只修改表中的具体数据,无法修改表结构

1. 基本语法

update 表名 set 列名=(修改后的数据) where 限制条件;
commit;

2. 示例

(1)将表t_b中99999客户的pay_id里的111改为222

update t_b
set pay_id =222
where cust_id = 99999;
commit;

五、select 查询

1. 基本语法

select 列名 from 表名 where 限制条件;

注意,select可以查得到更改了但是未提交的数据

2. 基本查询

(1)查询emp表中的指定列,比如empno,ename和job

select empno, ename, job from EMP;

(2)以别名的形式查询emp表中的指定列,比如empno,ename和job

select empno as 工号, ename 姓名, job 岗位 from EMP;

(3)以表别名的形式查询emp表中的指定列,比如empno,ename和job

select t1.EMPNO,t1.ENAME,t1.JOB from EMP t1;

3. 模糊查询 like、_

%:表示0个或者多个字符
_:表示一位字符,一般称_为占位符

(1) 查询emp中姓名为S开头的员工信息。

select empno, ename, job, mgr, hiredate, sal, comm, deptno from EMP  where ENAME like 'S%';

(2)查询emp中姓名结尾为S的员工信息。

select empno, ename, job, mgr, hiredate, sal, comm, deptno from EMP  where ENAME like '%S';

(3) 查询emp中姓名的包含A的员工信息。

select empno, ename, job, mgr, hiredate, sal, comm, deptno from EMP  where ENAME like '%A%';

(4) 查询emp中姓名的中间有A的员工信息。

select empno, ename, job, mgr, hiredate, sal, comm, deptno from EMP  where ENAME like '_%A%_';

(5) 查询emp中名字的第二个字符为L的员工信息

select *from EMP where ENAME like '_L%';

escape函数 表示a后面的一位数没有特殊含义

(6)查询姓名中含%的学生信息

select * from stu where sname like '%a%%' escape('a');

(7) 查询姓名中含_的学生信息

select * from stu where sname like '%a_%' escape('a');

(8)查询姓名中含a和_的学生信息

select * from stu where sname like '%aaa_%' escape('a');

(9) 查询姓名中含_和%的学生信息

select * from stu where sname like '%a_%a%%' escape('a');

注意:查询所有列时,用列名代替 select * 会提高查询效率

select * from emp;
select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp;  

4. 条件查询

(1) 查询工号为7788的员工信息

select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp where EMPNO=7788;

(2) 查询部门20的员工信息

select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp where DEPTNO=20;

5. 不等查询

(1) 查询不在部门20的员工信息

select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp where DEPTNO<>20;
select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp where DEPTNO!=20;

(2) 查询工资在1500以下的员工信息

select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp where sal<1500;

(3) 查询工资在2500以上的员工信息

select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp where sal > 2500;

6. 逻辑查询

and / or / in / not in / between and / all / any

(1) 查询在20部门并且工资在1500以上的员工信息

select * from emp where DEPTNO=20 and SAL>1500;

(2) 查询工资在1500以下或者工资在2500以上的员工信息

select * from emp where sal<1500 or sal>2500;

(3) 查询在20部门并且工资在1500以下或者在10部门工资2500以上的员工信息

select empno, ename, job, mgr, hiredate, sal, comm, deptno
from emp
where (DEPTNO = 20 and sal < 1500)
   or (DEPTNO = 10 and sal > 2500);

(4) 查询在部门10,20的员工信息

select empno, ename, job, mgr, hiredate, sal, comm, deptno
from emp
where DEPTNO = 10
   or DEPTNO = 20;
select empno, ename, job, mgr, hiredate, sal, comm, deptno
from emp
where DEPTNO in (10, 20);

(5)查询岗位不是clerk、salesman、manager的员工信息

select EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
from emp
where job not in ('CLERK', 'SALESMAN', 'MANAGER');

(6)查询工资在1500到2500的员工信息

select * from emp where sal between 1500 and 2500;

between and 包含两边,是闭区间,必须小的数值在前面

(7) 查询工资比20部门的所有人的工资都高的员工信息 >all(子查询) 相当于大于子查询中的最大值

select empno, ename, job, mgr, hiredate, sal, comm, deptno
from emp
where sal > all (select sal from emp where DEPTNO = 20);

(8) 查询工资比20部门的有些人的工资都高的员工信息 -->any 相当于大于子查询中的最小值

select * from emp
where sal > any(select sal from EMP where DEPTNO=20);

(9) 查询出comm列为空值的员工信息

select * from emp where COMM is null;

(10) 查询出comm列不为空值的员工信息

select * from emp where COMM is not null;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

5、DML语句(数据操纵语言-增删改查) 的相关文章

随机推荐