ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Oracle insert into table

Oracle insert into table

原创 Linux操作系统 作者:lyytc 时间:2011-03-17 15:44:27 0 删除 编辑

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 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 () THEN

INTO ()

VALUES ()

WHEN () THEN

INTO ()

VALUES ()

ELSE

INTO ()

VALUES ()

SELECT FROM ;

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 VALUES

INTO VALUES

...

);

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 AS

请登录后发表评论 登录
全部评论

注册时间:2008-06-28

  • 博文量
    15
  • 访问量
    14515