Sichanov

Untitled

Oct 13th, 2023
1,206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE TABLE addresses
  2. (
  3.     id   serial primary key,
  4.     name varchar(100) NOT NULL
  5. );
  6.  
  7. CREATE table categories
  8. (
  9.     id   serial primary key,
  10.     name varchar(10) NOT NULL
  11. );
  12.  
  13. Create table clients
  14. (
  15.     id           serial primary key,
  16.     full_name    varchar(50) NOT NULL,
  17.     phone_number varchar(20) NOT NULL
  18. );
  19.  
  20. CREATE TABLE drivers
  21. (
  22.     id         serial primary key,
  23.     first_name varchar(30) not null,
  24.     last_name  varchar(30) not null,
  25.     age        INT         not null,
  26.     rating     numeric(2) DEFAULT 5.5
  27. );
  28.  
  29. CREATE TABLE cars
  30. (
  31.     id          serial primary key,
  32.     make        varchar(20)                    NOT NULL,
  33.     model       varchar(20),
  34.     year        INT default 0 CHECK (year > 0) NOT NULL,
  35.     mileage     INT default 0 CHECK (mileage > 0),
  36.     condition   varchar(1)                     NOT NULL,
  37.     category_id INT                            NOT NULL,
  38.     CONSTRAINT fk_cars_categories FOREIGN KEY (category_id) REFERENCES categories (id)
  39. );
  40.  
  41. CREATE TABLE courses
  42. (
  43.     id              serial primary key,
  44.     from_address_id INT       not null,
  45.     start           TIMESTAMP not null,
  46.     bill            NUMERIC(10, 2) DEFAULT 10 CHECK (bill > 10),
  47.     car_id          INT       not null,
  48.     client_id       INT       not null,
  49.     CONSTRAINT fk_courses_addresses FOREIGN KEY (from_address_id) REFERENCES addresses (id) ON DELETE CASCADE,
  50.     CONSTRAINT fk_courses_cars FOREIGN KEY (car_id) REFERENCES cars (id) ON DELETE CASCADE,
  51.     CONSTRAINT fk_courses_clients FOREIGN KEY (client_id) REFERENCES clients (id) ON DELETE CASCADE
  52. );
  53.  
  54. CREATE TABLE cars_drivers
  55. (
  56.     car_id    INT not null,
  57.     driver_id INT not null,
  58.     CONSTRAINT fk_cars_drivers_cars FOREIGN KEY (car_id) REFERENCES cars (id) ON DELETE CASCADE,
  59.     CONSTRAINT fk_cars_drivers_drivers FOREIGN KEY (driver_id) REFERENCES drivers (id) ON DELETE CASCADE
  60. );
Advertisement
Add Comment
Please, Sign In to add comment