Postgresql 简明教程

PostgreSQL - INSERT Query

PostgreSQL INSERT INTO 语句允许将新行插入到表中。可以一次插入一行或作为查询结果插入多行。

The PostgreSQL INSERT INTO statement 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.

Syntax

INSERT INTO 语句的基本语法如下:

Basic syntax of INSERT INTO statement is as follows −

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
  1. Here, column1, column2,…​columnN are the names of the columns in the table into which you want to insert data.

  2. The target column names can be listed in any order. The values supplied by the VALUES clause or query are associated with the explicit or implicit column list left-to-right.

如果您要为表的所有列添加值,则不必在 SQL 查询中指定列名称。但是,确保值的顺序与表中的列顺序相同。SQL INSERT INTO 语法如下:

You may not need to specify the column(s) name in the SQL query if you are adding values for all the columns of the table. However, make sure the order of the values is in the same order as the columns in the table. The SQL INSERT INTO syntax would be as follows −

INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);

Output

下表总结了输出消息及其含义:

The following table summarizes the output messages and their meaning −

S. No.

Output Message & Description

1

INSERT oid 1 Message returned if only one row was inserted. oid is the numeric OID of the inserted row.

2

INSERT 0 # Message returned if more than one rows were inserted. # is the number of rows inserted.

Examples

让我们在 testdb 中创建 COMPANY 表,如下所示:

Let us create COMPANY table in testdb as follows −

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL,
   JOIN_DATE	  DATE
);

以下示例将一行插入到 COMPANY 表中:

The following example inserts a row into the COMPANY table −

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (1, 'Paul', 32, 'California', 20000.00,'2001-07-13');

以下示例是插入一行;此处省略了薪酬列,因此它将具有默认值 −

The following example is to insert a row; here salary column is omitted and therefore it will have the default value −

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,JOIN_DATE) VALUES (2, 'Allen', 25, 'Texas', '2007-12-13');

以下示例对 JOIN_DATE 列使用 DEFAULT 子句,而不是指定一个值 −

The following example uses the DEFAULT clause for the JOIN_DATE column rather than specifying a value −

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (3, 'Teddy', 23, 'Norway', 20000.00, DEFAULT );

以下示例使用多行 VALUES 语法插入多行 −

The following example inserts multiple rows using the multirow VALUES syntax −

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00, '2007-12-13' ), (5, 'David', 27, 'Texas', 85000.00, '2007-12-13');

以上所有语句都会在 COMPANY 表中创建以下记录。下一章将会教你如何从表中显示所有这些记录。

All the above statements would create the following records in COMPANY table. The next chapter will teach you how to display all these records from a table.

ID        NAME        AGE        ADDRESS     SALARY	  JOIN_DATE
----      ----------  -----      ----------  -------      --------
1         Paul        32         California  20000.0      2001-07-13
2         Allen       25         Texas                    2007-12-13
3         Teddy       23         Norway      20000.0
4         Mark        25         Rich-Mond   65000.0      2007-12-13
5         David       27         Texas       85000.0      2007-12-13