Oracle 单表插入/多表插入(Single Table Insert/Multi-table Insert)

数据库应用中,我们经常需要向表中插入数据,insert语句是最常用的数据插入方式,根据目标表的数量,可以分为单表插入和多表插入。

目录

[一、 单表插入(Single Table Insert)](#一、 单表插入(Single Table Insert))

[二、 多表插入(Multi-table Insert)](#二、 多表插入(Multi-table Insert))

[2.1 单目标表多列插入](#2.1 单目标表多列插入)

[2.2 多目标表条件插入](#2.2 多目标表条件插入)

[2.3 All和First关键字](#2.3 All和First关键字)


一 、 单表 插入( Single Table Insert )

单表插入是最常用插入方式,使用insert into ... 语句向单一表的指定列或者全部列加载数据,也可以利用子查询从已有表中加载数据。

先建一张测试表,bonus列增加了default属性:

sql 复制代码
create table wage(
id number(6),
name varchar2(32),
salary number(6),
bonus number(6) default 100);

insert 语句中在表后提供列名,可以向指定列插入数据

sql 复制代码
insert into wage(id,name,salary) values(1,'Vincent',1000);

Select * from wage;
  • values子句中提供值的数量需要和列的数量相同
  • 这里指定向id,name,salary插入值,bonus列并未提供值
  • bonus列在创建时指定了default 100,所以被赋予了默认值100

如果表名后没有指明列,意味着向所有列插入数据

sql 复制代码
insert into wage values(2,'Victor',2000,200);

insert into wage values(3,'Grace',3000,default);
  • values子句提供的值数量需要和表列数量相同
  • 即使某些列有默认值也不能省略,可以用default关键字来赋默认值

insert into ... select ... 可以使用子查询向表中插入数据,过程中可以是对数据进行加工。要注意子查询的列一定要和加载目标表的列数量相等,如果忽略目标表的列只提供表名,那么子查询必须为每一列都提供数据:

sql 复制代码
create table wage_bak as select * from wage where 1=2;

Insert into wage_bak select id, name, salary+500, bonus from wage where id=1;

select * from wage_bak;
  • Create table 语句复制从wage复制了一张表wage_bak,where 1=2用来控制不复制数据
  • Insert into ... select ... 将ID为1的数据从wage加载到wage_bak中,同时salary加500
  • 其实这里create table一步就可以完成表的创建和数据加载,仅为了演示拆成2步

二、 多表 插入( Multi-table Insert )

多表插入非常适合数据整理分配场景,如果利用单表插入,我们需要对每个条件查询一次源表并插入目标表,而多表插入可以直接定义多个条件,一次查询即可将所有数据分布到不同的表中。

在多表插入中,你必须通过子查询加载数据。但与单表插入不同的是,你可以更灵活的对子查询返回的每条数据进行条件判断,然后再指定插入一张或多张表。

这里再建一张测试表income,用多表插入的方式将数据从wage加载到income,注意income的表结构与wage不同,它新增了type列来区分收入类型。

sql 复制代码
create table income(
id number(6),
name varchar2(64),
type varchar2(64),
amount number(6));

2.1 单目标表多列插入

将数据从wage加载到income,要求当type为"sal"时,加载salary列,当type为"bou"时,加载bonus列:

sql 复制代码
insert all
into income(id, name, type, amount) values(id, name, 'sal', salary)
into income(id, name, type, amount) values(id, name, 'bou', bonus)
select id,name,salary,bonus from wage;

select * from income;
  • 利用多表查询,子查询返回列数量可以大于目标表列数量,你可以定义多个into子句来分配这些列
  • 示例定义了2个into条件,一次查询将salary和bouns插入目标表的amount列,并完成区分
  • 在数据插入较复杂,特别是源表数据量很大的场景,利用多表插入可以避免对源表反复查询

2.2 多目标表条件插入

现需要将wage表中记录根据工资高低,把数据分布到3张不同的表中,先通过wage复制3张表:

sql 复制代码
create table low_wage as select * from wage where 1=2;

create table medium_wage as select * from wage where 1=2;

create table high_wage as select * from wage where 1=2;

通过多表插入的when ... then ... else 语句可以对子查询的每一条数据进行判断,将满足条件的数据分配到指定表中:

sql 复制代码
insert all
when salary <= 1000 then into low_wage
when salary >1000 and salary <=2000 then into medium_wage
else into high_wage
select * from wage;

Select * from low_wage;
Select * from medium_wage;
Select * from high_wage;
  • 第一个when子句将salary小于等于1000的记录插入low_wage表
  • 第二个when子句将salary在1000至2000的记录插入medium_wage表
  • else子句将剩下的记录插入high_wage表,如果省略else语句,那么不满足上面条件的记录不会做任何处理

2.3 All 和 First 关键字

上面示例中,insert后面的关键字是all,代表每条记录都会针对每个when条件做评估。

另一个关键字first,insert first表示发现第一个满足的条件即"熔断",剩余的条件不再评估。all/first关键字在条件范围重叠时会导致不同的结果,在实际应用中要注意。

下面将low_wage表清空,设置两个重叠的条件,分别用all和first关键字插入:

sql 复制代码
truncate table low_wage;

insert all
when salary <= 1000 then into low_wage
when salary <= 1500 then into low_wage
select * from wage where salary=1000;

select * from low_wage;
  • 子查询仅返回1条记录,但由于其同时满足<=1000和<=1500,all关键字对两个条件都进行判断且满足,并插入了2次。
sql 复制代码
truncate table low_wage;

insert first
when salary <= 1000 then into low_wage
when salary <= 1500 then into low_wage
select * from wage where salary=1000;

select * from low_wage;
  • 将关键字all替换为first,当<=1000条件满足时,后续<=1500的条件则不再判断,因此数据只插入了1次。
相关推荐
Seraphina3624 分钟前
SQL注入(二)盲注
网络·数据库·经验分享·笔记·sql
盟接之桥2 小时前
AI助手:你的7×24小时智能管家——让异常预警无处不在
大数据·网络·数据库·人工智能·制造·ai编程
waoooqwe2 小时前
问卷样本真实吗
大数据·数据库·算法·数据分析
可乐ea2 小时前
Agent 评测换判据:用终态数据库状态判定任务是否完成
数据库·ui·oracle·mcp工具·agent评测·终态断言·智能体可靠性
l1t2 小时前
DeepSeek总结的一个不含数据的 DuckDB 数据库
服务器·数据库·duckdb
PaperData3 小时前
2007-2020年税收调查面板数据
数据库
程序员清风4 小时前
CSV、Excel 与数据库数据读取实践
数据库·oracle·excel
GEO实战经验分享4 小时前
GEO技术与工具全景:从监测仪表盘到Schema的实战指南
数据库
一个天蝎座的程序猿4 小时前
IoTDB集群扩容:从慌乱到从容的经验分享
数据库
小小龙学IT5 小时前
Python SQLAlchemy 2.0 深度解析:从 Core 到 ORM 的数据访问双引擎
开发语言·数据库·python