Oracle增刪改查

1.向emp表中插入一條記錄(方式一:按表默認結構順序)insert into 表名 values …語法

  • insert into emp values(1111,’JACK’,’IT’,7788,sysdate,1000,100,40);

2.向emp表中插入一條記錄(方式二:按自定義順序)insert into 表名(列名) values …語法

  • insert into emp(ENAME,EMPNO,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
    values(‘MARRY’,2222,’IT’,7788,sysdate,1000,100,40);

3.向emp表中插入NULL值(方式一:採用顯示插入NULL值)

  • insert into emp values(3333,’SISI’,’IT’,7788,sysdate,1000,NULL,40);

4.向emp表中插入NULL值 (方式二:採用隱式插入NULL值),前提是所插入的字段允許插入NULL值

  • insert into emp(ENAME,EMPNO,JOB,MGR,HIREDATE,SAL,DEPTNO)
    values(‘SOSO’,4444,’IT’,7788,sysdate,1000,40);

5.使用&佔位符,動態輸入值,&可以運用在任何一個DML語句中,在values子句中使用,例如:’&ename’和&sal

  • insert into emp values(&empno,’&ename’,’&job’,&mgr,&hiredate,&sal,&comm,&xxxxxxxx);
    注意:&是sqlplus工具提供的佔位符,如果是字符串或日期型要加”符,數值型無需加”符

6.使用&佔位符,動態輸入值,&可以運用在任何一個DML語句中,在from子句中使用

  • select * from &table;

7.使用&佔位符,動態輸入值,&可以運用在任何一個DML語句中,在select子句中使用

  • select empno,ename,&colname from emp;

8.使用&佔位符,動態輸入值,&可以運用在任何一個DML語句中,在where子句中使用

  • select * from emp where sal > &money;

9.使用&佔位符,動態輸入值,&可以運用在任何一個DML語句中,在group by 和 having子句中使用

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

10.刪除emp表中的所有記錄

  • delete from emp;

11.將xxx_emp表中所有20號部門的員工,複製到emp表中,批量插入,

  • insert into 表名 select …語法
    insert into emp
    select *
    from xxx_emp
    where deptno=20;

12.將’SMITH’的工資增加20%

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

13.將’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’;

14.刪除工資比所有部門平均工資都低的員工,這是一個條件未知的事物,優先考慮子查詢
第一:查詢所有部門的平均工資

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
);

15.刪除無佣金的員工

  • delete from emp where comm is null;

16.將emp表丟入回收站,drop table 表名

  • drop table emp;

17.從回收站將emp表閃回,flashback table 表名 to before drop

  • flashback table emp to before drop;

18.查詢回收站,show recyclebin

  • show recyclebin;

19.清空回收站,purge recyclebin

  • purge recyclebin;

20.使用關鍵字purge,徹底刪除emp表,即不會將emp表丟入回收站,永久刪除emp表,drop table 表名 purge

  • drop table emp purge;

21.依據xxx_emp表結構,創建emp表的結構,但不會插入數據

  • create table emp
    as
    select * from xxx_emp where 1<>1;

22.創建emp表,複製xxx_emp表中的結構,同時複製xxx_emp表的所有數據

  • create table emp
    as
    select * from xxx_emp where 1=1;
    注意:where不寫的話,默認爲true

23.將emp截斷,再自動創建emp表,truncate table 表名

  • truncate table emp;

24.向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;

25.使用關鍵字purge,徹底刪除emp表,即不會將emp表丟入回收站

-drop table emp purge;

26.依據xxx_emp表,只創建emp表,但不復制數據,且emp表只包括empno,ename字段

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

27.向emp表(只含有empno和ename字段),批量插入xxx_emp表中部門號爲20的員工信息

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

28.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)刪除速度慢,需要逐行刪除

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章