2.4. Добавление строк в таблицу
Для добавления строк в таблицу используется оператор INSERT
:
INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
Заметьте, что для всех типов данных применяются довольно очевидные форматы. Константы, за исключением простых числовых значений, обычно заключаются в апострофы ('
), как в данном примере. Тип date
на самом деле очень гибок и принимает разные форматы, но в данном введении мы будем придерживаться простого и однозначного.
Тип point
требует ввода пары координат, например таким образом:
INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
Показанный здесь синтаксис требует, чтобы вы запомнили порядок столбцов. Можно также применить альтернативную запись, перечислив столбцы явно:
INSERT INTO weather (city, temp_lo, temp_hi, prcp, date) VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');
Вы можете перечислить столбцы в другом порядке, если желаете опустить некоторые из них, например, если уровень осадков (столбец prcp) неизвестен:
INSERT INTO weather (date, city, temp_hi, temp_lo) VALUES ('1994-11-29', 'Hayward', 54, 37);
Многие разработчики предпочитают явно перечислять столбцы, а не полагаться на их порядок в таблице.
Пожалуйста, введите все показанные выше команды, чтобы у вас были данные, с которыми можно будет работать дальше.
Вы также можете загрузить большой объём данных из обычных текстовых файлов, применив команду COPY
. Обычно это будет быстрее, так как команда COPY
оптимизирована для такого применения, хотя и менее гибка, чем INSERT
. Например, её можно использовать так:
COPY weather FROM '/home/user/weather.txt';
здесь подразумевается, что данный файл доступен на компьютере, где работает серверный процесс, а не на клиенте, так как указанный файл будет прочитан непосредственно на сервере. Подробнее об этом вы можете узнать в описании команды COPY.
2.4. Populating a Table With Rows
The INSERT
statement is used to populate a table with rows:
INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
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.
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.
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';
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.