Загрузка данных


cat > ~/cars.sql <<'SQL'
CREATE TABLE brands (
    brand_id   serial PRIMARY KEY,
    brand_name text NOT NULL,
    country    text NOT NULL
);
CREATE TABLE owners (
    owner_id   serial PRIMARY KEY,
    last_name  text NOT NULL,
    first_name text NOT NULL,
    city       text NOT NULL
);
CREATE TABLE cars (
    car_id   serial PRIMARY KEY,
    model    text NOT NULL,
    brand_id int REFERENCES brands(brand_id),
    owner_id int REFERENCES owners(owner_id),
    year     int CHECK (year BETWEEN 1990 AND 2026),
    price    numeric(10,2),
    plate    text NOT NULL
);
INSERT INTO brands (brand_name, country) VALUES
('Toyota','Япония'),('BMW','Германия'),('Lada','Россия'),
('Kia','Южная Корея'),('Audi','Германия'),('Hyundai','Южная Корея'),
('Renault','Франция'),('Ford','США'),('Volkswagen','Германия'),('Mazda','Япония');
INSERT INTO owners (last_name, first_name, city) VALUES
('Антонов','Алексей','Санкт-Петербург'),('Абрамова','Анна','Москва'),
('Борисов','Борис','Казань'),('Васильев','Виктор','Самара'),
('Громова','Галина','Пермь'),('Данилов','Дмитрий','Тверь'),
('Егорова','Елена','Уфа'),('Андреев','Артём','Омск'),
('Зайцев','Захар','Тула'),('Ильина','Ирина','Сочи'),('Алексеев','Андрей','Псков');
INSERT INTO cars (model, brand_id, owner_id, year, price, plate) VALUES
('Camry',1,1,2021,2450000,'А123ВС78'),('X5',2,2,2020,5600000,'В234ЕК199'),
('Vesta',3,3,2022,1550000,'С345МН116'),('Rio',4,4,2019,1200000,'Е456ОР163'),
('A4',5,5,2018,2100000,'К567ТУ159'),('Solaris',6,6,2021,1450000,'М678ХА69'),
('Logan',7,7,2017,900000,'Н789РС02'),('Focus',8,8,2016,850000,'О890КМ55'),
('Polo',9,9,2020,1350000,'Р901ВТ71'),('CX-5',10,10,2022,2800000,'Т012НЕ23'),
('Granta',3,11,2023,1100000,'У123АВ60');
SQL

psql -d db_cars -f ~/cars.sql