ORACLE insert into table oracle学习
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
(
VALUES
(
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
VALUES
(
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
(
VALUES
(
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
(
VALUES
(
desc state_city
INSERT INTO state_city
VALUES
('NV', 'Nevada', 'Las Vegas');
desc state_city
INSERT SELECT
Insert From SELECT statement
INSERT INTO
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
VALUES
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 (
INTO
VALUES (
WHEN (
INTO
VALUES (
ELSE
INTO
VALUES (
SELECT
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 INTO ... 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 ( INTO VALUES ( WHEN ( INTO VALUES ( ELSE INTO VALUES ( SELECT 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 INTO ... 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 ( 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 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 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 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 (column_list) VALUES (values_list) RETURNING INTO -- 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;