insert ,update 以及merge 的使用
本次将用到的几个表,以及字段
> emp表
empno, ename,job,mgr,hiredate,sal,comm,deptno
> dept表
deptno,dname,loc
> emp_bonus表
empno, received,type
>new_sal表
demptno,sal
/*----------------- INSERT 的使用 --------------------------------*/
--1. 插入到一个表
insert into dept
(deptno,dname,loc)
vaules(1,"A","C"),
(2,"V","T") insert into dept
values(1,"lily","NEW YORK") -- 2. 从一个表向另外一个表复制行
insert into dept_east
(deptno,dname,loc)
select deptno,
dname,
loc
from dept
where loc in ("NEW YORK") -- 3. 复制表定义(只复制表的结构,没有数据)
-- In Mysql
create table dept_2 like dept --In MSSQL or oracle
select *
into dept_2
from dept
where 1=0 /*没有这个限制条件将连数据一起复制*/ create table dept_2
as
select *
from dept
where 1=0 --4. 一次向多个表中插入记录
--In oracle
insert all
when loc in ("NEW YORK") then
into dept_east(dept_no,dname,loc)
vaules(dept_no,dname,loc)
when loc in ("CHICAGO") then
into dept_mid(dept_no,dname,loc)
vaules(dept_no,dname,loc)
else
into dept_west(dept_no,dname,loc)
vaules(dept_no,dname,loc)
select dept_no,dname,loc
from dept
/*----------------------update 的使用------------------------------------*/ --5. 在表中更新记录
update emp
set sal=sal*1.1
where deptno=20 update emp
set sal=sal*1.2
where empno in
(select empno from emp_bonus) update emp
set sal=sal*1.2
where exists(
select null
from emp_bonus as eb
where emp.empno=eb.empno
) -- 6. 用其他表更新
update emp
set sal=ns.sal
comm=ns.sal/2
from emp e,
new_sal ns
where ns.deptno=e.deptno
/*-----------------------------merge 函数的使用------------------------------------ */
/*
-- 简单来说就是 : 如果记录存在则更新,不存在则插入
>emp_commission 表
empno,sal,comm,deptno
>emp表
empno, ename,job,mgr,hiredate,sal,comm,deptno
1. 如果emp_commission 表员工也存在于emp表中,那么将emp_commission 的提成(comm)更新为1000
对于更新到1000的员工,SAL少于2000则删除
2. 否则,从emp表取出empno,sal,comm,deptno,插入到emp_commission
*/
-- in oracle, MSSQL merge into emp_commission ec
using (select * from emp) emp
on(ec.empno=emp.empno)
when matched then
update set ec.comm=1000
delete where(sal<2000)
when not matched then
insert (ec.empno,ec.name,ec.comm)
values(emp.empno,emp.name,emp.comm)
insert ,update 以及merge 的使用的更多相关文章
- 《oracle每天一练》Merge Into 语句代替Insert/Update在Oracle中的应用实战
转载自窃破天道 动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也 ...
- 使用Merge Into 语句实现 Insert/Update
网址: http://www.eygle.com/digest/2009/01/merge_into_insertupdate.html 动机: 想在Oracle中用一条SQL语句直接进行Insert ...
- Merge Into 语句代替Insert/Update在Oracle中的应用实战
动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也就是说当存在记录 ...
- Oracle Merge Into Insert/Update
出自:http://blog.csdn.net/yuzhic/article/details/1896878 动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明 ...
- Hibernate三种状态的区分,以及save,update,saveOrUpdate,merge等的使用 引自http://www.blogjava.net/TiGERTiAN/archive/2008/10/25/236519.html
Hibernate的对象有3种状态,分别为:瞬时态(Transient). 持久态(Persistent).脱管态(Detached).处于持久态的对象也称为PO(Persistence Object ...
- hibernate的各种保存方式的区别 (save,persist,update,saveOrUpdte,merge,flush,lock)等
hibernate的保存hibernate对于对象的保存提供了太多的方法,他们之间有很多不同,这里细说一下,以便区别:一.预备知识:在所有之前,说明一下,对于hibernate,它的对象有三种状态,t ...
- Hibernate各保存方法之间的差 (save,persist,update,saveOrUpdte,merge,flush,lock)等一下
hibernate保存 hibernate要保存的目的是提供一个方法,多.它们之间有许多不同之处,点击此处详细说明.使得差: 一.预赛: 在所有.阐释.供hibernate,,transient.p ...
- Hibernate update 和 merge 、saveOrUpdate的区别
this.getSession().update(obj); this.getSession().merge(obj); this.getSession().saveOrUpdate(obj); 1. ...
- Oracle 数据库实现数据更新:update、merge
工作中遇到的数据更新,学习记录. 1.使用update进行数据更新 1)最简单的更新 update tablea a set a.price=1.00 2)带条件的数据更新 update tablea ...
随机推荐
- JAVA 调用 R 语言之升华篇
由于项目的需要,最近做了一个javaWeb调用R的组件,在这里,我把自己走的一些弯路给大家总结一下: 一.选取什么插件作为java和R之间的通信? 1. 在传统的方式中,大致可以分为两类:一类是JRI ...
- c# 实现mysql事务
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 安装linux下面用来配置网络,防火墙,系统服务等设置的图形小工具Setup
Setup命令是linux下面用来配置网络,防火墙,系统服务等设置的图形小工具.使用起来非常方便简单,可是centos\redhat最小化安装之后发现setup命令工具用不了. 接下来介绍如何安装se ...
- Spring的事务管理(理论篇,下篇提供代码实现)
事务:逻辑上的一组操作,这组操作要么全部成功,要么全部失败(事务十大特性:原子性,一致性,隔离性,持久性) 原子性:事务是不可分割的工作单位,事务中的操作要么都发生了,要么都不发生.(也就是说不能单独 ...
- netstream播放rtmp直播流卡顿
标准的flash播放器ActionScript3语句,播放flash publish的rtmp流,NetConnection--->NetStream--->play--->atta ...
- 【三思笔记】 全面学习Oracle分区表及分区索引
[三思笔记]全面学习Oracle分区表及分区索引 2008-04-15 关于分区表和分区索引(About PartitionedTables and Indexes) 对于 10gR2 而言,基本上可 ...
- Swift百万线程攻破单例(Singleton)模式
一.不安全的单例实现 在上一篇文章我们给出了单例的设计模式,直接给出了线程安全的实现方法.单例的实现有多种方法,如下面: class SwiftSingleton { class var shared ...
- iOS实现微信外部H5支付完成后返回原APP
看到微信最近放开了微信H5支付,公司决定把H5集成到多款APP上.下面记录下了开发过程. 由于是微信新推出的支付方式,在网上搜索到的相关资料并不多,其中有一篇文件(点此跳转)对我的整个开发过程起到了很 ...
- Activiti中的各个service的作用
各个Service的作用: RepositoryService 管理流程定义 RuntimeService 执行管理,包括启动.推进.删除流程实例等操作 TaskService 任务管理 Histor ...
- 函数重载overload
与void show(int a, char b, double c){}构成重载的有: a) void show(int x, char y, double z){} //no b) int sho ...