创建表
create table toys (
toy_id integer,
toy_name varchar2(100),
colour varchar2(10)
);
create table bricks (
brick_id integer,
colour varchar2(10),
shape varchar2(10)
);
select table_name, column_name, data_type
from user_tab_columns
where table_name in ( 'TOYS', 'BRICKS' )
order by table_name, column_id
单行插入
insert into toys values ( 1, 'Miss Snuggles', 'pink' );
如果值的数量与目标列的数量和数据类型不匹配,您将收到错误消息
insert into toys values ( 2, 'Baby Turtle' );
没有列名的插入变得脆弱。如果更改表中的列,插入很可能会失败。最好列出您为其提供值的列
insert into toys ( toy_id, toy_name ) values ( 2, 'Baby Turtle' );
select * from toys;
多行插入
插入查询的输出。
insert into bricks ( brick_id )
select toy_id
from toys;
select * from bricks;
与单行插入一样,查询和目标表中的列数必须匹配。每个位置的列的数据类型也必须兼容。
insert into bricks ( brick_id )
select *
from toys;
性能:单行与多行(没看懂,也没弄明白)
比较了单行插入与多行插入的性能。两者都添加了 50,000 行。
declare
start_time pls_integer;
insert_count pls_integer := 50000;
begin
start_time := dbms_utility.get_time ();
for i in 1 .. insert_count loop
insert into bricks
values ( i, 'red', 'cube' );
end loop;
dbms_output.put_line ( 'Single-row duration = ' || ( dbms_utility.get_time () - start_time) );
rollback;
start_time := dbms_utility.get_time ();
insert into bricks
select level, 'red', 'cube' from dual
connect by level <= insert_count;
dbms_output.put_line ( 'Multi-row duration = ' || ( dbms_utility.get_time () - start_time) );
rollback;
end;
/
insert into toys ( toy_id, colour ) values ( 4, 'blue' );
insert into toys ( toy_id, colour ) values ( 5, 'green' );
保存 DML 更改
insert into toys ( toy_id, toy_name, colour )
values ( 6, 'Green Rabbit', 'green' );
commit;
您提交后对其他用户可见。在此之前,只有您的会话(数据库连接)可以看到新数据。
select *
from toys
where toy_id = 6;
撤消 DML
insert into toys ( toy_id, toy_name, colour )
values ( 7, 'Pink Rabbit', 'pink' );
select * from toys
where toy_id = 7;
select * from toys
where toy_id = 7;
提交后在回滚
insert into toys ( toy_id, toy_name, colour )
values ( 7, 'Pink Rabbit', 'pink' );
select * from toys
where toy_id = 7;
commit;
rollback;
select * from toys
where toy_id = 7;
在回滚之前提交更改 - 因此 Pink Rabbit 的行保留在表中:
保存点
exec savepoint save_this;
首先使用 savepoint 命令创建检查点。给它起个名字供以后参考
exec 是 LiveSQL 的要求。在其他环境中不需要此前缀。
insert into toys ( toy_id, toy_name, colour )
values ( 8, 'Pink Rabbit', 'pink' );
savepoint after_six;
insert into toys ( toy_id, toy_name, colour )
values ( 9, 'Purple Ninja', 'purple' );
select * from toys
where toy_id in ( 8, 9 );
rollback to savepoint after_six;
多表插入
插入到砖块两次,玩具一次
insert all
into bricks ( brick_id ) values ( id )
into bricks ( brick_id ) values ( id )
into toys ( toy_id ) values ( id )
select 0 id from dual;
条件多表插入
insert into toys ( toy_id, toy_name, colour )
values ( 11, 'Cuteasaurus', 'blue' );
insert into toys ( toy_id, toy_name, colour )
values ( 12, 'Sir Stripypants', 'blue' );
insert into toys ( toy_id, toy_name, colour )
values ( 13, 'White Rabbit', 'white' );
savepoint post_toys;
insert all
when colour = 'blue' then
into bricks ( brick_id, colour )
values ( toy_id, colour )
when toy_name = 'Cuteasaurus' then
into bricks ( brick_id, colour )
values ( toy_id, colour )
else
into bricks ( brick_id, colour )
values ( toy_id, colour )
select * from toys
where toy_id >= 11;
select * from bricks
where brick_id >= 11;
insert first
when colour = 'blue' then
into bricks ( brick_id, colour )
values ( toy_id, colour )
when toy_name = 'Cuteasaurus' then
into bricks ( brick_id, colour )
values ( toy_id, colour )
else
into bricks ( brick_id, colour )
values ( toy_id, colour )
select * from toys
where toy_id >= 11;