oracle增删改基本用法

使用SQL语句


a.SELECT语句完成基本的查询功能;


b.INSERT,UPDATE和DELETE语句操作数据库;


c.COMMIT,ROLLBACK和SAVEPOINT语句控制事务;


d.SELECT语句实现各种复杂的查询功能;


1.简单查询语句


select<*,coumn[alias],...> from table;


可以使用desc关键字查看表的结构如:desc table


日期转换格式:select name,to_char(hiredate,'yyyy-mm-dd') from emp;


取消重复的行:select distinct deptno,job from emp;


使用别名: select person_name as name from emp;


处理空值:使用 nvl select nvl(comm,0) as 收入 from emp; 当comm为null,则值取0,当comm不为null则去comm;


使用where子句


select <*,column [alias],..> from table where condition(s)];


使用order by 子句


select <*,column[alias],...> from table where condition(s),order by[asc|desc]


使用多列排序


select <*,column[alias],...> from table where condition(s),order by asl ASC,comm desc;


2.使用DML语句(INSERT,UPDATE,DELETE)用于操纵表和视图。


插入数据:
注意:如果为数字列插入数据,则可以直接提供数字,如果是字符列必须使用单引号,
当插入数据时,数据必须满足约束规则,必须给主键和not null列提供数据,
当插入数据时,数据必须与列的个数和顺序保持一致;


使用default提供数据:当insert语句插入数据时,可以使用default提供数值
当指定default时,如果列存在默认值,则会使用默认值,如果列不存在默认值
则自动使用null。


使用子查询插入数据:可以将一张表的数据复制到另一张表中,语法如下:


insert into emp(empno,ename,deptno)
select empno,ename,sal,deptno from emp
where deptno=10;


使用子查询直接装载:
insert /*+append*/ into emp(empno,ename,deptno)
select empno,ename,sal,deptno from emp
where deptno=10;


注意:尽管以上两种方法的执行结果一样,但是使用/*+append*/表示直接装载,
当要装载大批量数据时,采用第二种方式,效率高。


使用多表插入数据如下所示:
insert all
when depno=10 then into dept10
when depno=20 then into dept20
when job='clerk' then into clerk
else into other


如上所示将depno=10插入到dept10表中,将depno=20插入到dept20表中,
将job='clerk' 插入到clerk表中,将其他的行都插入到other表中。


使用first操作符执行多表插入


当使用first操作符执行夺标操作,如果数据已经满足先前条件,并且插入某表,那么该数据行在后续插入
不会被再次使用,如下所示。


insert first
when deno=10 when into den10
when deno=20 when into den20
when job='clerk' then into clerk
else into other


更新数据:


使用子查询更新数据,更新关联数据,可以降低网络开销,


update emp set (job,sal,comm)=(
select job,sal,comm from emp where ename='smith')
where ename='scott';


 update yy_fj_persons set (person_name,person_level)=
  (select person_name,person_level from yy_fj_persons where person_name='秋实') 
  where person_name='张三';


复制表数据:当使用触发器表数据时,如果表A数据被修改,那么表B数据也随之修改。
使用子查询,可以基于一张表修改另一张表的数据如下所示:


update empp set deptno=2
(select deptno from emp where empno=778)
where job=(select job from emp where empno=778);


删除数据


使用truncate table 截断表
当使用delete语句删除表的所有数据时,不会释放表所占的空间。如果用户确定
删除表的所有数据,那么使用truncate table 语句速度更快如下所示


truncate table emp;


注意使用delete语句操作可以回退,但是使用truncate table语句操作则不会回退。


使用子查询删除数据


delete from emp where depno=(select deptno from dept where dname='soot');

猜你喜欢

转载自blog.csdn.net/silence_xj/article/details/79849060