当前位置:网站首页>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语句中使用相同的列名,但是不需要列名匹配.
边栏推荐
- Target detection learning process
- 不会就坚持71天吧 链表排序
- 论pyscript使用感想(实现office预览)
- 你真的会写Restful API吗?
- Coding questions encountered in the interview
- Won't you insist on 71 days? List sorting
- Svg -- loading animation
- Methods of using multiple deformations on an element
- kotlin的List,Map,Set等集合类不指定类型
- 12. Priority queue and inert queue
猜你喜欢
Locally call tensorboard and Jupiter notebook on the server (using mobaxterm)
12. Priority queue and inert queue
Definition and implementation of stack and queue (detailed)
Not 67 days, square root
What is the difference between field, variable and property
不会就坚持65天吧 只出现一次的数字
Not for 63 days. The biggest XOR
14. Haproxy+kept load balancing and high availability
不会就坚持67天吧 平方根
Code or script to speed up the video playback of video websites
随机推荐
11.备份交换机
不会就坚持66天吧 权重生成随机数
Pytorch fixed random seed & recurrence model
Interview notes of a company
Fuzzy query of SQL
Cad2020 introductory learning (2021.4.13)
顺序表和链表
对一个元素使用多种变形的方法
Object detection: object_ Detection API +ssd target detection model
Not for 60 days, magical dictionary
异常解决:cococaption包出现找不到edu.stanford.nlp.semgraph.semgrex.SemgrexPattern错误
不会就坚持67天吧 平方根
Don't insist on 66 days. Weight generates random numbers
Won't you just stick to 62 days? Sum of words
14.haproxy+keepalived负载均衡和高可用
不会就坚持61天吧 最短的单词编码
Class starts! See how smardaten decomposes complex business scenarios
恒星科通邀您“湘”约第24届中国高速公路信息化大会暨技术产品展示会
Won't you just stick to 69 days? Merge range
索引的最左前缀原理