Postgresql 中文操作指南
2.4. Populating a Table With Rows #
INSERT 语句用于使用行填充表:
The INSERT statement is used to populate a table with rows:
INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
请注意,所有数据类型都使用非常明显的输入格式。通常,不是简单数值值的常量都必须用单引号 (') 围起来,如示例中所示。实际上,date 类型对它接受的内容相当灵活,但对于本教程,我们将坚持此处所示的明确格式。
Note that all data types use rather obvious input formats. Constants that are not simple numeric values usually must be surrounded by single quotes ('), as in the example. The date type is actually quite flexible in what it accepts, but for this tutorial we will stick to the unambiguous format shown here.
point 类型需要一对坐标作为输入,如下所示:
The point type requires a coordinate pair as input, as shown here:
INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
到目前为止使用的语法要求你记住列的顺序。一种替代语法允许你明确列出列:
The syntax used so far requires you to remember the order of the columns. An alternative syntax allows you to list the columns explicitly:
INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');
如果你希望可以按不同的顺序列出列,甚至省略某些列,例如,如果未知降水量:
You can list the columns in a different order if you wish or even omit some columns, e.g., if the precipitation is unknown:
INSERT INTO weather (date, city, temp_hi, temp_lo)
VALUES ('1994-11-29', 'Hayward', 54, 37);
许多开发人员认为,显式列出列比依赖隐式顺序是一种更好的风格。
Many developers consider explicitly listing the columns better style than relying on the order implicitly.
请输入上述所有命令,以便在以下各节中处理一些数据。
Please enter all the commands shown above so you have some data to work with in the following sections.
你还可以使用 COPY 从平面文本文件中加载大量数据。这通常会更快,因为 COPY 命令针对此应用程序进行了优化,同时比 INSERT 允许更少的灵活性。示例如下:
You could also have used COPY to load large amounts of data from flat-text files. This is usually faster because the COPY command is optimized for this application while allowing less flexibility than INSERT. An example would be:
COPY weather FROM '/home/user/weather.txt';
其中,源文件的名称必须在运行后端进程的机器而非客户端上可用,因为后端进程会直接读取文件。你可以在 COPY 中阅读有关 COPY 命令的更多信息。
where the file name for the source file must be available on the machine running the backend process, not the client, since the backend process reads the file directly. You can read more about the COPY command in COPY.