历史版本。 PostgreSQL 6.4 已结束支持。 请参阅 当前版本手册.
INSERT
INSERT — 向一个表中插入新行
大纲
INSERT INTOtable[ (column[, ...] ) ] { VALUES (expression[, ...] ) | SELECTquery}
输入
-
table 一个已存在表的名称。
-
column table中一个列的名称。-
expression 要赋予
column的一个有效表达式或值。-
query 一个有效的查询。有效参数的进一步描述请参考 SELECT 语句。
输出
- →
INSERT1oid 只插入了一行时返回的消息。→
是被插入行的行标识符。oid- →
INSERT 0# 插入了多行时返回的消息。→
是插入的行数。#
描述
INSERT 允许我们向一个表中插入新行。可以一次插入一行,也可以插入一个查询结果的多个行。目标列表中的列可以按任意顺序列出。对于目标列表中没有出现的每个列,将插入默认值;如果列没有声明的默认值,则假定为 NULL。如果每个列的表达式数据类型不正确,将尝试自动类型强制转换。
要向一个表追加数据,你必须拥有该表上的 insert 权限,并且对 WHERE 子句中指定的任何表拥有 select 权限。
用法
--Insert a single row into table films;
--(in the second example the column date_prod is omitted
--therefore will be stored in it a default value of NULL):
--
INSERT INTO films VALUES
('UA502','Bananas',105,'1971-07-13','Comedy',INTERVAL '82 minute');
INSERT INTO films (code, title, did, date_prod, kind)
VALUES ('T_601', 'Yojimbo', 106, DATE '1961-06-16', 'Drama');
--Insert a single row into table distributors, note that
--only column "name" is specified, to the non specified
--column "did" will be assigned its default value:
--
INSERT INTO distributors (name) VALUES ('British Lion');
--Insert several rows into table films from table tmp:
--
INSERT INTO films
SELECT * FROM tmp;
--Insert into arrays:
--Create an empty 3x3 gameboard for noughts-and-crosses
--(all of these queries create the same board attribute)
--(Refer to the PostgreSQL User's Guide for further
--information about arrays).
INSERT INTO tictactoe (game, board[1:3][1:3])
VALUES (1,'{{"","",""},{},{"",""}}');
INSERT INTO tictactoe (game, board[3][3])
VALUES (2,'{}');
INSERT INTO tictactoe (game, board)
VALUES (3,'{{,,},{,,},{,,}}');
兼容性
SQL92
INSERT 语句与 SQL92 完全兼容。query子句特性方面可能存在的限制记录在 SELECT 语句中。