当前位置:网站首页>Oracle 插入数据
Oracle 插入数据
2022-07-29 04:23:00 【zhiyou-rookie】
Oracle 插入数据
顾名思义,INSERT用于向数据库插入(添加行)。
可以以多种方式使用插入操作:
- 插入单个完整的行;
- 插入单个部分行;
- 插入多行;
- 插入查询的结果。
提示:insert 和系统安全性
可以使用Oracle安全性基于每个表或每个用户禁用INSERT语句。
1.1 插入完整的行
语法示例:
INSERT INTO Customers
VALUES(10006,
'Pep E. LaPew',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'USA',
NULL,
NULL);
注意:无输出
INSERT语句通常不会产生任何输出,但是如果在Oracle SQL Developer中执行上面的语句,应该会看到“1 row inserted.”消息。
在VALUES子句中指定要在每个表列中存储的数据,并且必须为每一列提供一个值。如果列没有值,就应该使用NULL值(假定表允许为该列不指定值)。必须按表定义中的顺序填充列。编写依赖于特定顺序的SQL语句是非常不安全的。
更安全(不幸的是也是更麻烦)的方式是编写如下所示的INSERT语句:
INSERT INTO customers(cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country
)
VALUES(10006,
'Pep E. LaPew',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'USA'
);
提示:自动主键
每次添加行时,无需手动分配唯一的值(也无需记录上次使用的是什么值),大多数DBMS都提供了一种自动分配的方式:每次向表中添加行时,都会自动分配下一个可用的数字,这种功能称为 自动递增。
由于提供了列名,VALUES必须以指定的顺序来匹配指定的列名,这个顺序不一定是列出现在实际表中的顺序。
提示:总是使用列所在的列表
通常,如果没有显式指定所在列的列表,那么就永远也不要使用INSERT。
警告:省略列
如果表定义允许,可以从INSERT操作中省略列。此时,必须存在以下条件:
- 将列定义为允许NULL值
- 在表定义中指定默认值。这意味者如果没有指定值,则将使用默认值。
提示:插入多个行
与大多数其他的DBMS不同,Oracle不支持可以同时插入多个行的insert版本。
解决方法:
--这种语法为一种错误示范,但是在其他的DBMS中是可以使用的
insert into table_name(colum1, colun2)
values(1,2),(3,4);
--正确用法:
1、采用union all拼接查询方式:
insert into CB_PRACTICE(id_, type_,remark)
select 5,'物理','浮力' from dual
union all select 6,'物理','阻力' from dual;
2、采用insert all的方式:
INSERT ALL
INTO CB_PRACTICE(id_, type_,remark) VALUES (7,'语文','唐诗')
INTO CB_PRACTICE(id_, type_,remark) VALUES (8,'语文','宋词')
SELECT * FROM DUAL;
2. 插入检索的数据
INSERT还有另外一种形式,可用于把INSERT语句的结果插入表中.这称为INSERT SELECT.
INSERT INTO customers(cust_id,
cust_contact,
cust_email,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country)
SELECT cust_id,
cust_contact,
cust_email,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country
FROM custnew;
提示:INSERT SELECT中的列名
为了简单起见,这个示例在INSERT和SELECT语句中使用相同的列名,但是不需要列名匹配.
边栏推荐
猜你喜欢

11. Backup switch

Code or script to speed up the video playback of video websites

C语言力扣第61题之旋转链表。双端队列与构造循环链表

不会就坚持59天吧 替换单词

Not for 61 days. The shortest word code

Class starts! See how smardaten decomposes complex business scenarios

论pyscript使用感想(实现office预览)

顺序表和链表

Realize the effect of univariate quadratic equation through JS. Enter the coefficients of a, B and C to calculate the values of X1 and x2

Installation and use of stm32cubemx (5.3.0)
随机推荐
6.pytest生成allure报告
No, just stick to it for 64 days. Find the insertion location
Beginner: array & String
Labelme cannot open the picture
[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation
Deploy Jenkins using containers
What the hell is this error? It doesn't affect the execution result, but it always reports errors when executing SQL... Connecting maxcomputer uses
Machine vision Series 2: vs DLL debugging
不会就坚持61天吧 最短的单词编码
索引的最左前缀原理
C language: talking about various complex statements
Openfeign asynchronous call problem
Niuke IOI weekly 27 popularity group
[material delivery UAV] record (ROS + Px4 + yolov5 + esp8266 + steering gear)
Not for 61 days. The shortest word code
Model tuning, training model trick
Why is it necessary to scale the attention before softmax (why divide by the square root of d_k)
MySQL - 深入解析MySQL索引数据结构
12. Priority queue and inert queue
不会就坚持71天吧 链表排序