INSERT 中文man页面

系统
INSERT 允许我们向表中插入新行。 我们可以一次插入一行或多行作为查询结果。

NAME

INSERT - 在表中创建新行

SYNOPSIS

INSERT INTO table [ ( column [, ...] ) ]
    { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) | query }

DESCRIPTION 描述

INSERT 允许我们向表中插入新行。 我们可以一次插入一行或多行作为查询结果。


 目标列表中的列/字段可以按任何顺序排列。 在目标列中没有出现的列/字段将插入缺省值, 可能是定义了的缺省值或者 NULL。


 如果每行的表达式不是正确的数据类型,系统将试图进行自动的类型转换。


 要想向表中插入数据,你必须有 INSERT 权限, 如果你使用了 query 子句插入来自查询里的数据行, 你还需要拥有在查询里使用的表的 SELECT 权限。  

PARAMETERS 参数

table

 现存表的名称(可以有模式修饰)。
column

 表 table 中的字段名。
DEFAULT VALUES

 所有字段都会用它们的缺省值填充。
expression

 赋予 column 的一个有效表达式或值。
DEFAULT

 这个字段将被字段它的填充。
query

 一个查询(SELECT 语句),它提供插入的数据行。 请参考 SELECT 语句获取语法描述。

OUTPUTS 输出


 成功完成后,一条 INSERT 命令返回一个下面形式的命令标签

INSERT oid count

count 是插入的行数。 如果 count 正好是一,并且目标表有 OID, 那么 oid 是赋予插入行的 OID。 否则 oid 是零。  

EXAMPLES 例子


 向表 films 里插入一行:

INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');


 在第二个例子里面省略了字段 len  因此在它里面将只存储缺省的 NULL 值:

INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');


 在第三个例子里,我们用 DEFAULT 值作为数据字段,而不是声明一个数值:

INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes');
INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama');


 从表 tmp 中插入几行到表 films 中:

INSERT INTO films SELECT * FROM tmp;


 插入数组:

-- 创建一个空的 3x3 游戏板来玩圈-和-叉游戏
-- (所有这些查询创建相同的游戏)
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,'{{,,},{,,},{,,}}');

COMPATIBILITY 兼容性

INSERT 完全遵循 SQL 标准。可能碰到的关于 query 子句特性的限制在 SELECT [select(7)] 语句中有相关文档。  

#p#

NAME

INSERT - create new rows in a table

SYNOPSIS

INSERT INTO table [ ( column [, ...] ) ]
    { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) | query }

DESCRIPTION

INSERT allows one to insert new rows into a table. One can insert a single row at a time or several rows as a result of a query.

The columns in the target list may be listed in any order. Each column not present in the target list will be inserted using a default value, either its declared default value or null.

If the expression for each column is not of the correct data type, automatic type conversion will be attempted.

You must have INSERT privilege to a table in order to insert into it. If you use the query clause to insert rows from a query, you also need to have SELECT privilege on any table used in the query.  

PARAMETERS

table
The name (optionally schema-qualified) of an existing table.
column
The name of a column in table.
DEFAULT VALUES
All columns will be filled with their default values.
expression
An expression or value to assign to column.
DEFAULT
This column will be filled with its default value.
query
A query (SELECT statement) that supplies the rows to be inserted. Refer to the SELECT statement for a description of the syntax.

OUTPUTS

On successful completion, an INSERT command returns a command tag of the form

INSERT oid count

The count is the number of rows inserted. If count is exactly one, and the target table has OIDs, then oid is the OID assigned to the inserted row. Otherwise oid is zero.  

EXAMPLES

Insert a single row into table films:

INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');

In this second example, the last column len is omitted and therefore it will have the default value of null:

INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');

The third example uses the DEFAULT clause for the date columns rather than specifying a value:

INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes');
INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama');

This examples inserts several rows into table films from table tmp:

INSERT INTO films SELECT * FROM tmp;

This example inserts into array columns:

-- Create an empty 3x3 gameboard for noughts-and-crosses
-- (all of these commands create the same board)
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,'{{,,},{,,},{,,}}');

COMPATIBILITY

INSERT conforms fully to the SQL standard. Possible limitations of the query clause are documented under SELECT [select(7)].

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-25 15:39:42

fcloseall中文man

2011-08-25 15:00:15

cfgetispeed中文man

2011-08-19 18:35:50

issue中文man

2011-08-25 17:03:51

pclose中文man

2011-08-25 17:40:25

setvbuf中文man

2011-08-23 14:21:16

poweroff中文man

2011-08-24 15:52:59

intro中文man

2011-08-23 13:40:31

2011-08-25 15:54:08

ferror中文man

2011-08-25 17:24:54

puts中文man

2011-08-25 18:34:55

ungetc中文man

2011-08-23 10:03:40

useradd中文man

2011-08-23 10:29:02

chpasswd中文man

2011-08-23 10:34:22

convertquot中文man

2011-08-23 15:39:34

rpmbuild中文man

2011-08-25 09:40:49

UPDATE中文man
点赞
收藏

51CTO技术栈公众号