=> CREATE DATABASE bookstore;
CREATE DATABASE
=> \c bookstore
You are now connected to database "bookstore" as user "student".
=> CREATE SCHEMA bookstore;
CREATE SCHEMA
=> ALTER DATABASE bookstore SET search_path = bookstore, public;
ALTER DATABASE
=> \c bookstore
You are now connected to database "bookstore" as user "student".
=> SHOW search_path;
search_path ------------------- bookstore, public (1 row)
=> CREATE TABLE authors( author_id serial PRIMARY KEY, last_name text, first_name text, middle_name text );
CREATE TABLE
=> INSERT INTO authors(last_name, first_name, middle_name) VALUES ('Пушкин', 'Александр', 'Сергеевич');
INSERT 0 1
=> INSERT INTO authors(last_name, first_name, middle_name) VALUES ('Тургенев', 'Иван', 'Сергеевич');
INSERT 0 1
=> INSERT INTO authors(last_name, first_name, middle_name) VALUES ('Стругацкий', 'Борис', 'Натанович');
INSERT 0 1
=> INSERT INTO authors(last_name, first_name, middle_name) VALUES ('Стругацкий', 'Аркадий', 'Натанович');
INSERT 0 1
=> INSERT INTO authors(last_name, first_name, middle_name) VALUES ('Толстой', 'Лев', 'Николаевич');
INSERT 0 1
=> INSERT INTO authors(last_name, first_name) VALUES ('Свифт', 'Джонатан');
INSERT 0 1
=> CREATE TABLE books( book_id serial PRIMARY KEY, title text );
CREATE TABLE
=> INSERT INTO books(title) VALUES ('Сказка о царе Салтане');
INSERT 0 1
=> INSERT INTO books(title) VALUES ('Муму');
INSERT 0 1
=> INSERT INTO books(title) VALUES ('Трудно быть богом');
INSERT 0 1
=> INSERT INTO books(title) VALUES ('Война и мир');
INSERT 0 1
=> INSERT INTO books(title) VALUES ('Путешествия в некоторые удаленные страны мира в четырех частях: сочинение Лемюэля Гулливера, сначала хирурга, а затем капитана нескольких кораблей');
INSERT 0 1
=> INSERT INTO books(title) VALUES ('Хрестоматия');
INSERT 0 1
=> CREATE TABLE authorship( book_id integer REFERENCES books, author_id integer REFERENCES authors, seq_num integer, PRIMARY KEY (book_id,author_id) );
CREATE TABLE
=> INSERT INTO authorship(book_id, author_id, seq_num) VALUES(1, 1, 1);
INSERT 0 1
=> INSERT INTO authorship(book_id, author_id, seq_num) VALUES(2, 2, 1);
INSERT 0 1
=> INSERT INTO authorship(book_id, author_id, seq_num) VALUES(3, 3, 2);
INSERT 0 1
=> INSERT INTO authorship(book_id, author_id, seq_num) VALUES(3, 4, 1);
INSERT 0 1
=> INSERT INTO authorship(book_id, author_id, seq_num) VALUES(4, 5, 1);
INSERT 0 1
=> INSERT INTO authorship(book_id, author_id, seq_num) VALUES(5, 6, 1);
INSERT 0 1
=> INSERT INTO authorship(book_id, author_id, seq_num) VALUES(6, 1, 1);
INSERT 0 1
=> INSERT INTO authorship(book_id, author_id, seq_num) VALUES(6, 5, 2);
INSERT 0 1
=> INSERT INTO authorship(book_id, author_id, seq_num) VALUES(6, 2, 3);
INSERT 0 1
=> CREATE TABLE operations( operation_id serial PRIMARY KEY, book_id integer NOT NULL REFERENCES books, qty_change integer, date_created date DEFAULT current_date );
CREATE TABLE
Другой способ вставки данных в таблицу - команда COPY. Она обычно используется, если нужно загрузить большой объем информации. Но в этом случае надо не забыть "передвинуть" значение последовательности:
=> COPY operations (operation_id, book_id, qty_change) FROM stdin; 1 1 10 2 1 10 3 1 -1 \.
COPY 3
=> SELECT pg_catalog.setval('operations_operation_id_seq', 3, true);
setval -------- 3 (1 row)