当前位置:Gxlcms > 数据库问题 > Oracle系列:(19)增删改数据

Oracle系列:(19)增删改数据

时间:2021-07-01 10:21:17 帮助过:20人阅读

使用&占位符,动态输入值,&可以运用在任何一个DML语句中,在values子句中使用,例如:‘&ename‘和&sal

insert into emp values(&empno,‘&ename‘,‘&job‘,&mgr,&hiredate,&sal,&comm,&xxxxxxxx);

注意:&是sqlplus工具提供的占位符,如果是字符串或日期型要加‘‘符,数值型无需加‘‘符


&占位符应用于select的表名】使用&占位符,动态输入值,&可以运用在任何一个DML语句中,在from子句中使用

select * from &table;

技术分享


&占位符应用于select的列名】使用&占位符,动态输入值,&可以运用在任何一个DML语句中,在select子句中使用

select empno,ename,&colname from emp;

技术分享


&占位符应用于where】使用&占位符,动态输入值,&可以运用在任何一个DML语句中,在where子句中使用

select * from emp where sal > &money;


&占位符应用于group by和having】使用&占位符,动态输入值,&可以运用在任何一个DML语句中,在group by 和 having子句中使用

select deptno,avg(sal)
from emp
group by &deptno
having avg(sal) > &money;


删除emp表中的所有记录

delete from emp;


将xxx_emp表中所有20号部门的员工,复制到emp表中,批量插入,insert into 表名 select ...语法

insert into emp
select * 
from xxx_emp
where deptno=20;

技术分享


将‘SMITH‘的工资增加20%

update emp set sal=sal*1.2 where ename = upper(‘smith‘);

技术分享


将‘SMITH‘的工资设置为20号部门的平均工资,这是一个条件未知的事物,优先考虑子查询

第一:20号部门的平均工资

     

select avg(sal) from emp where deptno=20;

第二:将‘SMITH‘的工资设置为2207

     

update emp set sal=2207 where ename = ‘SMITH‘;

 

子查询:

update emp 
set sal = (
    select avg(sal) 
    from emp 
    where deptno=20
    ) 
where ename = ‘SMITH‘;

 


删除工资比所有部门平均工资都低的员工,这是一个条件未知的事物,优先考虑子查询

第一:查询所有部门的平均工资

select avg(sal) from emp group by deptno;

第二:删除工资比(*,*,*)都低的员工

delete from emp where sal<all(*,*,*);

子查询:

delete 
from emp 
where sal < all(
    select avg(sal) 
    from emp 
    group by deptno
);


删除无佣金的员工

delete from emp where comm is null;


将emp表丢入回收站,drop table 表名

drop table emp;


从回收站将emp表闪回,flashback table 表名 to before drop

flashback table emp to before drop;


查询回收站,show recyclebin

show recyclebin;


清空回收站,purge recyclebin

purge recyclebin;


使用关键字purge,彻底删除emp表,即不会将emp表丢入回收站,永久删除emp表,drop table 表名 purge

drop table emp purge;


依据xxx_emp表结构,创建emp表的结构,但不会插入数据

create table emp
as
select * from xxx_emp where 1<>1;
create table emp(empno,ename)
as
select empno,ename from xxx_emp where 1=2;


技术分享


创建emp表,复制xxx_emp表中的结构,同时复制xxx_emp表的所有数据

create table emp
as
select * from xxx_emp where 1=1;

注意:where不写的话,默认为true


将emp截断,再自动创建emp表,truncate table 表名

truncate table emp;


向emp表,批量插入来自xxx_emp表中部门号为20的员工信息,只包括empno,ename,job,sal字段

insert into emp(empno,ename,job,sal)
select empno,ename,job,sal 
from xxx_emp 
where deptno=20;

技术分享



向emp表(只含有empno和ename字段),批量插入xxx_emp表中部门号为20的员工信息

insert into emp(empno,ename)
select empno,ename from xxx_emp where deptno=20;



drop table 和 truncate table 和 delete from 区别

drop table

1)属于DDL

2)不可回滚

3)不可带where

4)表内容和结构删除

5)删除速度快

truncate table

1)属于DDL

2)不可回滚

3)不可带where

4)表内容删除

5)删除速度快

delete from

1)属于DML

2)可回滚

3)可带where

4)表结构在,表内容要看where执行的情况

5)删除速度慢,需要逐行删除


Oracle系列:(19)增删改数据

标签:oracle

人气教程排行