2 2.4. Populating a Table With Rows #
4 The INSERT statement is used to populate a table with rows:
5 INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
7 Note that all data types use rather obvious input formats. Constants
8 that are not simple numeric values usually must be surrounded by single
9 quotes ('), as in the example. The date type is actually quite flexible
10 in what it accepts, but for this tutorial we will stick to the
11 unambiguous format shown here.
13 The point type requires a coordinate pair as input, as shown here:
14 INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
16 The syntax used so far requires you to remember the order of the
17 columns. An alternative syntax allows you to list the columns
19 INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
20 VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');
22 You can list the columns in a different order if you wish or even omit
23 some columns, e.g., if the precipitation is unknown:
24 INSERT INTO weather (date, city, temp_hi, temp_lo)
25 VALUES ('1994-11-29', 'Hayward', 54, 37);
27 Many developers consider explicitly listing the columns better style
28 than relying on the order implicitly.
30 Please enter all the commands shown above so you have some data to work
31 with in the following sections.
33 You could also have used COPY to load large amounts of data from
34 flat-text files. This is usually faster because the COPY command is
35 optimized for this application while allowing less flexibility than
36 INSERT. An example would be:
37 COPY weather FROM '/home/user/weather.txt';
39 where the file name for the source file must be available on the
40 machine running the backend process, not the client, since the backend
41 process reads the file directly. The data inserted above into the
42 weather table could also be inserted from a file containing (values are
43 separated by a tab character):
44 San Francisco 46 50 0.25 1994-11-27
45 San Francisco 43 57 0.0 1994-11-29
46 Hayward 37 54 \N 1994-11-29
48 You can read more about the COPY command in COPY.