使用 INSERT
语句添加一个或多个记录到表中。
示例如下:
假设有如下数据的表 t1
。
obclient> CREATE TABLE t1(c1 int primary key, c2 int);
Query OK, 0 rows affected (0.16 sec)
obclient> SELECT * FROM t1;
Empty set (0.02 sec)
单表插入:向表
t1
中插入一行数据obclient> INSERT INTO t1 VALUES(1,1);
Query OK, 1 row affected (0.01 sec)
obclient> SELECT * FROM t1;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
+----+------+
1 row in set (0.04 sec)
单表插入:直接向子查询中插入数据
obclient> INSERT INTO (SELECT * FROM t1) VALUES(1,1);
Query OK, 1 row affected (0.01 sec)
obclient> SELECT * FROM t1;
+----+------+
| C1 | C2 |
+----+------+
| 1 | 1 |
+----+------+
1 row in set (0.01 sec)
单表插入:包含
RETURNING
子句obclient> INSERT INTO t1 VALUES(1,1) RETURNING c1;
+----+
| C1 |
+----+
| 1 |
+----+
1 row in set (0.02 sec)
obclient> SELECT * FROM t1;
+----+------+
| C1 | C2 |
+----+------+
| 1 | 1 |
+----+------+
1 row in set (0.01 sec)
更多 INSERT
语句相关的语法请参见《SQL 参考(Oracle 模式)》中 INSERT 章节。