Chinaunix首页 | 论坛 | 博客
  • 博客访问: 651906
  • 博文数量: 109
  • 博客积分: 6081
  • 博客等级: 准将
  • 技术积分: 1318
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:28
文章分类
文章存档

2011年(8)

2010年(39)

2009年(62)

分类: Oracle

2009-12-20 21:49:23

有用的几句SQL 语句

insert into table_name values(id,'name',sex);
update table set name='laigq' where id=100;
delete from table where id=100;
------------------

1、insert into命令,一次插入多行。

insert into table(select * from table_test where name='laigq');

insert into just_name(first,last) (select first_name,last_name from employees);


2、merge 命令可以在同一个命令中执行update 和insert into


实例使用

1、表中原数据
SQL> select * from test.test;

        ID NAME
---------- ------------
         1 laigq

SQL> select * from test.student;

        ID NAME
---------- ------------
         1 lai
         2 wgy

执行一下命令:
merge into test.test a
using(select * from test.student) b
on(a.id=b.id)
when matched then update set a.name=b.name
when not matched then insert (a.id,a.name)
values(b.id,b.name);

结果:
SQL> select * from test.test;

        ID NAME
---------- ------------
         2 wgy
         1 lai

说明:
由于 id=1 已经存在,所以把 test.test 表中 id=1 的 name 更改为 lai(原来为laigq)
由于没有id=2 的行,所以在 test.test 表中插入 id=2,name=wyg 的行。

阅读(1435) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~