80.Oracle数据库SQL开发之 修改表内存——使用INSERT语句

125 阅读2分钟

80.Oracle数据库SQL开发之 修改表内存——使用INSERT语句

欢迎转载,转载请标明出处:blog.csdn.net/notbaron/ar…\

INSERT语句用于向表中添加行。

例如:

store@PDB1> insert into customers (customer_id,first_name,last_name,dob,phone) values (6,'Fred','Brown','01-jan-1970','800-555-1215');

 

1 row created.

验证如下:

store@PDB1> select * from customers;

CUSTOMER_ID FIRST_NAME LAST_NAME  DOB     PHONE

----------- ---------- ---------- ---------------------

           6 Fred      Brown    01-JAN-70 800-555-1215

           1 John      Brown   01-JAN-65 800-555-1211

           2 Cynthia   Green     05-FEB-68 800-555-1212

           3 Steve     White     16-MAR-71 800-555-1213

           4 Gail      Black                  800-555-1214

           5 Doreen    Blue       20-MAY-70

 

6 rows selected.

1.  省略列的列表

例如:

 

store@PDB1> insert into customers values (7,'Jane','Green','01-jan-1970','800-555-1216');

 

1 row created.

2.  为列指定空值

NULL关键字可以用来为一列指定一个空值。

例如:

 

store@PDB1> insert into customers values (8,'Sophie','White',null,null);

 

1 row created.

查看如下:

store@PDB1> select * from customers wherecustomer_id=8;

 

CUSTOMER_ID FIRST_NAME LAST_NAME  DOB     PHONE

----------- ---------- ---------- ---------------------

           8 Sophie    White

3.  在列值中使用单引号和双引号

列值中可以使用单引号和双引号。

例如指定顾客的姓为O’Malley

如下:

store@PDB1> insert into customers values(9,'Kyle','O''Malley',null,null);

 

1 row created.

store@PDB1> select * from customers;

 

CUSTOMER_ID FIRST_NAME LAST_NAME  DOB     PHONE

----------- ---------- ---------- ---------------------

           6 Fred      Brown    01-JAN-70 800-555-1215

           7 Jane      Green    01-JAN-70 800-555-1216

           8 Sophie    White

           9 Kyle      O'Malley

           1 John      Brown   01-JAN-65 800-555-1211

           2 Cynthia   Green     05-FEB-68 800-555-1212

           3 Steve     White     16-MAR-71 800-555-1213

           4 Gail      Black                  800-555-1214

           5 Doreen    Blue       20-MAY-70

 

9 rows selected.

4.  从一个表向另外一个表复制行

在INSERT语句中,可以不使用列值,而是使用查询从一个表向另外一个表复制行。

要求源表和目标表的列数和列的类型必须匹配。

store@PDB1> insert into customers(customer_id,first_name,last_name) select 10,first_name,last_name from customerswhere customer_id=1;

 

1 row created.

新行的CUSTOMER_ID被设置为10.

9i引入了一个新的 MERGE语句,可以将一个表汇总的行合并到另外一个表中。