首页 > 数据库 >oracle中的insert语句

oracle中的insert语句

时间:2023-06-09 11:00:52浏览次数:35  
标签:语句 insert INSERT INTO VALUES emp oracle id SELECT


关键字: ORACLE insert in


to table


oracle中的 insert语句
在 oracle中使用DML语言的 insert语句来向表格中插入数据,先介绍每次只能插入一条数据的语法
INSERT INTO 表名(列名列表) VALUES(值列表);
注意:
当对表中所有的列进行赋值,那么列名列表可以省略,小括号也随之省略必须对表中的非空字段进行赋值
具有默认值的字段可以不提供值,此时列名列表中的相应的列名也要省略
举例:有如下表格定义
create table book(bookid char(10) not null , name varchar2(60),price number(5,3))
使用下面的语句来插入数据
INSERT INTO BOOK(bookid,name,price) VALUES('100123',' oracle sql',54.70);
INSERT INTO BOOK VALUES('100123',' oracle sql',54.70);
INSERT INTO BOOK(bookid) VALUES('100123');
由于bookid是非空,所以,对于book来说,至少要对bookid进行赋值,虽然这样的数据不完整
如果想往一个表格中插入多条数据,那么带有values子句的 insert就不行了,这时候必须使用 insert语句和select语句进行配合来实现同时插入多条数据:
例如:现在有一个空表a和一个有数据的表格b,他们的结构是一样, 把b表中的所有数据插入到a表中的语句是:
INSERT INTO A (列1,列2,列3)
SELECT 列1,列2,列3
FROM B ;
--查询语句中可以使用任意复杂的条件或者子查询
如果数据的来源不是现存表的数据,也想多条插入那么使用如下的方法:
INSERT INTO tablename(列1,列2,列3,)
SELECT 值1,值2,值3 FROM DUAL
UNION
SELECT 值1,值2,值3 FROM DUAL
UNION
SELECT 值1,值2,值3 FROM DUAL


如果上面的值有字符和日期型数据,那么使用单引号即可,每一个select语句得到一条数据,然后使用集合操作符union把多条数据合并到一个结果集中,来实现一次插入多条数据的功能。

引用


要点:


一条语句只能插入一张表中1行纪录


一条语句可以插入多张表中,每张表中1行纪录



如果想一条语句插入表中多行纪录,必须用这样的语句从别的表中选择数据才可以,


insert into .....select * from .....



oracle insert 语句语法介绍:


Basic

Inserts


Single Column Table Or View



INSERT INTO <table_name>

(<column_name>)


VALUES


(<value>);


CREATE TABLE state (


state_abbrev VARCHAR2(2));



INSERT INTO state

(state_abbrev)


VALUES


('WA');



COMMIT;



SELECT * FROM state;



Multiple Column Table Or View - All Columns



INSERT INTO <table_name>

VALUES


(<comma_separated_value_list>);



ALTER TABLE state


ADD (state_name VARCHAR2(30));



INSERT INTO state

(state_abbrev, state_name)


VALUES


('OR', 'Oregon');



COMMIT;



SELECT * FROM state;



Multiple Column Table Or View - Not All Columns



INSERT INTO <table_name>

(<comma_separated_column_name_list>)


VALUES


(<comma_separated_value_list>);



RENAME state TO state_city;



ALTER TABLE state_city


ADD (city_name VARCHAR2(30));



INSERT INTO state_city

(state_abbrev, city_name)


VALUES


('CA', 'San Francisco');



COMMIT;



SELECT * FROM state_city;



Problem Not Specifying Column Names Demo



INSERT INTO <table_name>

(<comma_separated_column_name_list>)


VALUES


(<comma_separated_value_list>);



desc state_city



INSERT INTO state_city

VALUES


('NV', 'Nevada', 'Las Vegas');



desc state_city



INSERT SELECT


Insert From SELECT statement


INSERT INTO <table_name> <SELECT Statement>;

CREATE TABLE zip_new (


zip_code VARCHAR2(5) NOT NULL,


state_abbrev VARCHAR2(2) NOT NULL,


city_name VARCHAR2(30));



INSERT INTO zip_new

SELECT zip_code, state_abbrev, city_name


FROM postal_code;



SELECT * FROM zip_new;



RECORD

INSERT


Insert Using A Record


INSERT INTO <table_name>

VALUES <record_name>;


CREATE TABLE t AS


SELECT table_name, tablespace_name


FROM all_tables;



SELECT COUNT(*)


FROM t;



DECLARE


trec t%ROWTYPE;


BEGIN


trec.table_name := 'NEW';


trec.tablespace_name := 'NEW_TBSP';



INSERT INTO t

VALUES trec;



COMMIT;


END;


/



SELECT COUNT(*) FROM t;



INSERT WHEN


Demo Tables


CREATE TABLE emp (


empno NUMBER(4) NOT NULL,


ename VARCHAR2(10),


job VARCHAR2(9),


mgr NUMBER(4),


sal NUMBER(7,2),


deptno NUMBER(2));



CREATE TABLE emp_10 AS SELECT * FROM emp WHERE 1=0;


CREATE TABLE emp_20 AS SELECT * FROM emp WHERE 1=0;


CREATE TABLE emp_30 AS SELECT * FROM emp WHERE 1=0;


CREATE TABLE leftover AS SELECT * FROM emp WHERE 1=0;



Demo Data


INSERT INTO emp VALUES

(7369, 'SMITH', 'CLERK', 7902, 800, 20);


INSERT INTO EMP VALUES

(7499, 'ALLEN', 'SALESMAN', 7698, 1600, 30);


INSERT INTO EMP VALUES

(7521, 'WARD', 'SALESMAN', 7698, 1250, 30);


INSERT INTO EMP VALUES

(7566, 'JONES', 'MANAGER', 7839, 2975, 20);


INSERT INTO EMP VALUES

(7654, 'MARTIN', 'SALESMAN', 7698, 1250, 30);


INSERT INTO EMP VALUES

(7698, 'BLAKE', 'MANAGER', 7839, 2850, 30);


INSERT INTO EMP VALUES

(7782, 'CLARK', 'MANAGER', 7839, 2450, 10);


INSERT INTO EMP VALUES

(7788, 'SCOTT', 'ANALYST', 7566, 3000, 20);


INSERT INTO EMP VALUES

(7839, 'KING', 'PRESIDENT', NULL, 5000, 10);


INSERT INTO EMP VALUES

(7844, 'TURNER', 'SALESMAN', 7698, 1500, 30);


INSERT INTO EMP VALUES

(7876, 'ADAMS', 'CLERK', 7788, 1100, 20);


INSERT INTO EMP VALUES

(7900, 'JAMES', 'CLERK', 7698, 950, 30);


INSERT INTO EMP VALUES

(7902, 'FORD', 'ANALYST', 7566, 3000, 60);


INSERT INTO EMP VALUES

(7934, 'MILLER', 'CLERK', 7782, 1300, 10);


COMMIT;



Demo

Insert Statement


INSERT

WHEN (<condition>) THEN


INTO <table_name> (<column_list>)


VALUES (<values_list>)


WHEN (<condition>) THEN


INTO <table_name> (<column_list>)


VALUES (<values_list>)


ELSE


INTO <table_name> (<column_list>)


VALUES (<values_list>)


SELECT <column_list> FROM <table_name>;



INSERT

WHEN (deptno=10) THEN


INTO emp_10 (empno,ename,job,mgr,sal,deptno)


VALUES (empno,ename,job,mgr,sal,deptno)


WHEN (deptno=20) THEN


INTO emp_20 (empno,ename,job,mgr,sal,deptno)


VALUES (empno,ename,job,mgr,sal,deptno)


WHEN (deptno=30) THEN


INTO emp_30 (empno,ename,job,mgr,sal,deptno)


VALUES (empno,ename,job,mgr,sal,deptno)


ELSE


INTO leftover (empno,ename,job,mgr,sal,deptno)


VALUES (empno,ename,job,mgr,sal,deptno)


SELECT * FROM emp;



SELECT * FROM emp_10;


SELECT * FROM emp_20;


SELECT * FROM emp_30;


SELECT * FROM leftover;



INSERT ALL Without the WHEN clause INSERT ALL performs all inserts

unconditionally


INSERT ALL

INTO <table_name> VALUES <column_name_list)


INTO <table_name> VALUES <column_name_list)


...


<SELECT Statement>;


CREATE TABLE ap_cust (


customer_id VARCHAR2(4),


program_id VARCHAR2(3),


del_date DATE);



CREATE TABLE ap_orders (


order_date DATE,


program_id VARCHAR2(3));



INSERT ALL

INTO ap_cust VALUES (customer_id, program_id, delivered_date)


INTO ap_orders VALUES (order_date, program_id)


SELECT program_id, delivered_date, customer_id, order_date


FROM airplanes;



SELECT * FROM ap_cust


WHERE rownum < 1001;



SELECT * FROM ap_orders


WHERE rownum < 1001;


CREATE TABLE t (


pid NUMBER(5),


fname VARCHAR2(20),


lname VARCHAR2(25));



INSERT ALL

INTO t (pid, fname, lname)


VALUES (1, 'Dan', 'Morgan')


INTO t (pid, fname, lname)


VALUES (2, 'Jack', 'Cline')


INTO t (pid, fname, lname)


VALUES (3, 'Helen', 'Lofstrom')


SELECT * FROM dual;



SELECT * FROM t;



INSERT ALL WHEN


Demo

Insert ALL Variation

INSERT

WHEN (<condition>) THEN


INTO <table_name> (<column_list>)


VALUES (<values_list>)


WHEN (<condition>) THEN


INTO <table_name> (<column_list>)


VALUES (<values_list>)


ELSE


INTO <table_name> (<column_list>)


VALUES (<values_list>)


SELECT <column_list> FROM <table_name>;



TRUNCATE TABLE emp_10;


TRUNCATE TABLE emp_20;


TRUNCATE TABLE emp_30;


TRUNCATE TABLE leftover;



INSERT ALL

WHEN (deptno=10) THEN


INTO emp_10 (empno,ename,job,mgr,sal,deptno)


VALUES (empno,ename,job,mgr,sal,deptno)


WHEN (deptno=20) THEN


INTO emp_20 (empno,ename,job,mgr,sal,deptno)


VALUES (empno,ename,job,mgr,sal,deptno)


WHEN (deptno<=30) THEN


INTO emp_30 (empno,ename,job,mgr,sal,deptno)


VALUES (empno,ename,job,mgr,sal,deptno)


ELSE


INTO leftover (empno,ename,job,mgr,sal,deptno)


VALUES (empno,ename,job,mgr,sal,deptno)


SELECT * FROM emp;



SELECT * FROM emp_10;


SELECT * FROM emp_20;


SELECT * FROM emp_30;


SELECT * FROM leftover;



INSERT FIRST WHEN


The WHEN clause is evaluated in the order in which it appears in the statement. For the first WHEN clause that evaluates to true, the database executes the corresponding INTO clause and skips subsequent WHEN clauses for the given row.


INSERT FIRST

INTO <table_name> VALUES <column_name_list)


INTO <table_name> VALUES <column_name_list)


...


<SELECT Statement>;


CREATE TABLE cust_ah (


customer_id VARCHAR2(4),


program_id VARCHAR2(3),


del_date DATE);



CREATE TABLE cust_ip (


customer_id VARCHAR2(4),


program_id VARCHAR2(3),


del_date DATE);



CREATE TABLE cust_qz (


customer_id VARCHAR2(4),


program_id VARCHAR2(3),


del_date DATE);



INSERT FIRST

WHEN customer_id < 'I' THEN


INTO cust_ah


VALUES (customer_id, program_id, delivered_date)


WHEN customer_id < 'Q' THEN


INTO cust_ip


VALUES (customer_id, program_id, delivered_date)


WHEN customer_id > 'PZZZ' THEN


INTO cust_qz


VALUES (customer_id, program_id, delivered_date)


SELECT program_id, delivered_date, customer_id, order_date


FROM airplanes;



SELECT customer_id, COUNT(*)


FROM cust_ah


GROUP BY customer_id;



SELECT customer_id, COUNT(*)


FROM cust_ip


GROUP BY customer_id;



SELECT customer_id, COUNT(*)


FROM cust_qz


GROUP BY customer_id;



INSERT WITH CHECK OPTION

Note: Use WITH CHECK OPTION to indicate that

Oracle prohibits any changes to the table or view that would produce rows that are not included in the subquery


CHECK OPTION demo

INSERT INTO (

<SQL_statement> WITH CHECK OPTION)


VALUES


(value_list);


CREATE TABLE dept (


deptno NUMBER(2),


dname VARCHAR2(15),


loc VARCHAR2(15));



INSERT INTO dept VALUES (10, 'ACCOUNTING', 'NEW YORK');

INSERT INTO dept VALUES (20, 'RESEARCH', 'DALLAS');

INSERT INTO dept VALUES (30, 'SALES', 'CHICAGO');

INSERT INTO dept VALUES (40, 'OPERATIONS', 'BOSTON');

COMMIT;



SELECT * FROM dept;



INSERT INTO (

SELECT deptno, dname, loc


FROM dept


WHERE deptno < 30)


VALUES (98, 'TRAVEL', 'SEATTLE');



SELECT * FROM dept;



INSERT INTO (

SELECT deptno, dname, loc


FROM dept


WHERE deptno < 30 WITH CHECK OPTION)


VALUES (99, 'TRAVEL', 'SEATTLE');



SELECT * FROM dept;



Anonymous Block

Insert


Demo

Insert Statement BEGIN

<

INSERT Statements>

END;


/


TRUNCATE TABLE zip_new;



-- copy the following 10 lines into SQL*Plus as is:


INSERT INTO zip_new

VALUES ('98101', 'WA', 'Seattle');


INSERT INTO zip_new

VALUES ('98004', 'WA', 'Bellevue');


INSERT INTO zip_new

VALUES ('98040', 'WA', 'Mercer Is.');


INSERT INTO zip_new

VALUES ('98072', 'WA', 'Woodinville');


INSERT INTO zip_new

VALUES ('98065', 'CA', 'Los Angeles');



SELECT * FROM zip_new;



ROLLBACK;



BEGIN


INSERT INTO zip_new

VALUES ('98101', 'WA', 'Seattle');


INSERT INTO zip_new

VALUES ('98004', 'WA', 'Bellevue');


INSERT INTO zip_new

VALUES ('98040', 'WA', 'Mercer Is.');


INSERT INTO zip_new

VALUES ('98072', 'WA', 'Woodinville');


INSERT INTO zip_new

VALUES ('98065', 'CA', 'Los Angeles');


END;


/



SELECT * FROM zip_new;



Inserting Into A SELECT Statement


Demo

Insert Statement INSERT INTO (<SELECT Statement>);

SELECT empno, ename, job, sal, deptno


FROM emp;



INSERT INTO (

SELECT empno, ename, job, sal, deptno


FROM emp)


VALUES


(1, 'MORGAN', 'DBA', '1', 40);



SELECT * FROM emp;



Create Table

INSERT (CTAS)


Demo Table Creation

Insert CREATE TABLE <table_name> AS <SELECT Statement>;

CREATE TABLE servers_bak AS


SELECT *


FROM servers;



desc servers_bak



SELECT COUNT(*)


FROM servers_bak;



INSERT With Returning Clause


Demo

Insert With Returning Clause INSERT INTO <table_name>

(column_list)


VALUES


(values_list)


RETURNING <value_name>


INTO <variable_name>;


-- Use emp table from

INSERT-WHEN demo above


CREATE SEQUENCE seq_emp;



set serveroutput on



DECLARE


x emp.empno%TYPE;


BEGIN


INSERT INTO emp

(empno, ename)


VALUES


(seq_emp.NEXTVAL, 'Morgan')


RETURNING empno


INTO x;



dbms_output.put_line(x);


END;


/



DECLARE


r rowid;


BEGIN


INSERT INTO emp

(empno, ename)


VALUES


(seq_emp.NEXTVAL, 'Morgan')


RETURNING rowid


INTO r;



dbms_output.put_line(r);


END;


/



DECLARE


x emp.empno%TYPE;


r rowid;


BEGIN


INSERT INTO emp

(empno, ename)


VALUES


(seq_emp.NEXTVAL, 'Morgan')


RETURNING rowid, empno


INTO r, x;



dbms_output.put_line(r);


dbms_output.put_line(x);


END;


/



/



/



/



Inserting Dates


Date Format Specification CREATE TABLE t (


scol VARCHAR2(20),


dcol DATE);



INSERT INTO t

(scol, dcol)


VALUES


('Test', TO_DATE('01-12-2007', 'MM-DD-YYYY'));



SELECT * FROM t;



INSERT INTO t

(scol, dcol)


VALUES


('Test', TO_DATE('01-12-2007', 'DD-MM-YYYY'));



SELECT * FROM t;



INSERT INTO t

(scol, dcol)


VALUES


('Test', DATE '2007-07-01');



SELECT * FROM t;


标签:语句,insert,INSERT,INTO,VALUES,emp,oracle,id,SELECT
From: https://blog.51cto.com/u_16065168/6445942

相关文章

  • Oracle重建data pump(expdpd,impdp)How To Reload Datapump Utility EXPDP/IMPDP (Doc ID
    APPLIESTO:OracleDatabaseExadataExpressCloudService-VersionN/AandlaterOracleDatabaseBackupService-VersionN/AandlaterOracleDatabase-EnterpriseEdition-Version10.1.0.2andlaterOracleDatabaseCloudSchemaService-VersionN/Aand......
  • 【Hive】DML及DQL语句
    由于Hive是分布式语言(其本质是操作了HDFS上的文件,因为HSFS是分布式架构,所以就决定了Hive是一门分布式SQL)。丢,我解释的可能不是很专业,将就看看。通常来说存储在HDFS上的数据是不能修改的,但是appendTofile这个linux命令是个特例(hadoopfs-appendToFile/root/2.txt/aa/1.txt)......
  • Hibernate_根据条件动态组装sql/hql语句(仿ibatis动态查询语句功能)
     1.功能需求背景 项目中使用hibernate作为数据持久层框架,主要考虑hibernate在进行一些简单的crud操作时非常便利,不需要和ibatis似的为每个sql操作都写一堆文件,但是同时也带来了一些局限性,如类似ibatis强大的动态查询功能用不了了,但是这个功能在项目中的应用场景又很大,hibernate......
  • ORACLE中Drop table cascade constraints
    当你要drop一个table时,如果删除table的动作会造成trigger或constraint产生矛盾,系统会出现错误警告的讯息而不会允许执行.。一个极简单的例子,例如你有一个员工基本资料表,上面可能有员工编号和员工姓名等字段,另外有一个员工销售表,上面有员工编号和员工销售额两个字段,员工薪资......
  • Oracle聚合函数RANK和dense_rank的使用
    聚合函数RANK和dense_rank主要的功能是计算一组数值中的排序值。在9i版本之前,只有分析功能(analytic),即从一个查询结果中计算每一行的排序值,是基于order_by_clause子句中的value_exprs指定字段的。其语法为:RANK()OVER([query_partition_clause]ord......
  • mysql 查询语句
    一、mysql查询的五种子句    where(条件查询)、having(筛选)、groupby(分组)、orderby(排序)、limit(限制结果数)   1、where常用运算符:      比较运算符        >, < , = , !=(<>), >= , <=         i......
  • sql语句基础
    sql语句基础  ----------------------------------------------------------sqlstructuredquerylanguage--DML--DataManipulationLanguage--数据操作语言queryinformation(SELECT),addnewrows(INSERT),modifyexistingrows(UPDATE),......
  • CAPL 脚本基本语句
    CAPL(CommunicationAccessProgrammingLanguage)是一种用于汽车通信网络分析和仿真的脚本语言。以下是CAPL脚本的基本语句:1.变量声明variables{intvariable1;//整数类型变量声明floatvariable2=3.14;//浮点数类型变量声明并初始化messagevariable3;//CAN......
  • 【python基础】循环语句-break关键字
    1.break关键字break关键字,其作用是在循环中的代码块遇到此关键字,立刻跳出整个循环,执行循环外的下一条语句。其在while和for循环中的作用示意图如下:1.1break在while循环中的使用1.1.1不加else语句比如我们通过键盘输入单词,输出刚才的单词,编写程序如下所示:我们发现当我们输......
  • mysql常见的时间查询语句
    mysql数据库要按当天、昨天、前七日、近三十天、季度、年查询查询今天select*from表名whereto_days(时间字段名)=to_days(now());   查询昨天SELECT*FROM表名WHERETO_DAYS(NOW())-TO_DAYS(`时间字段名`)=1 查询7天 sql语句SELECT*FROM表名whereDATE_SUB(CU......