--Створення бази даних
CREATE DATABASE OrdersDB;

use OrdersDB;

--Створення таблиці Salespeople
CREATE TABLE Salespeople (
    SNUM int NOT NULL,
    SNAME char(20) NOT NULL,
    CITY char(20),
    COMM decimal(8, 2),
    PRIMARY KEY (SNUM)
);

--Створення таблиці Customers
CREATE TABLE Customers (
    CNUM int NOT NULL,
    CNAME char(20) NOT NULL,
    CITY char(20),
    RATING int,
    SNUM int,
    PRIMARY KEY (CNUM),
    FOREIGN KEY (SNUM) REFERENCES Salespeople (SNUM)
);

--Створення таблиці Orders
CREATE TABLE Orders (
    ONUM int NOT NULL,
    AMT DECIMAL(8, 2) NOT NULL,
    ODATE DATE,
    SNUM int,
    CNUM int,
    PRIMARY KEY (ONUM),
    FOREIGN KEY (SNUM) REFERENCES Salespeople (SNUM),
    FOREIGN KEY (CNUM) REFERENCES Customers (CNUM)
);

--Масова ставка даних у таблиці
INSERT INTO
    Salespeople (snum, sname, city, comm)
VALUES (1001, 'Peel', 'London', 0.12),
    (
        1002,
        'Serres',
        'San Jose',
        0.13
    ),
    (
        1003,
        'Axelrod',
        'New York',
        0.1
    ),
    (
        1004,
        'Motika',
        'London',
        0.11
    ),
    (
        1007,
        'Rifkin',
        'Barcelona',
        0.15
    );

INSERT INTO
    Customers (
        cnum,
        cname,
        city,
        rating,
        snum
    )
VALUES (
        2001,
        'Hoffman',
        'London',
        100,
        1001
    ),
    (
        2002,
        'Giovanni',
        'Rome',
        200,
        1003
    ),
    (
        2003,
        'Liu',
        'San Jose',
        200,
        1002
    ),
    (
        2004,
        'Grass',
        'Berlin',
        300,
        1002
    ),
    (
        2006,
        'Clemens',
        'London',
        100,
        1001
    ),
    (
        2007,
        'Pereira',
        'Rome',
        100,
        1004
    ),
    (
        2008,
        'Cisneros',
        'San Jose',
        300,
        1007
    );

INSERT INTO
    Orders (onum, amt, odate, cnum, snum)
VALUES (
        3001,
        18.69,
        '1990-10-03',
        2008,
        1007
    ),
    (
        3002,
        1900.1,
        '1990-10-03',
        2007,
        1004
    ),
    (
        3003,
        767.19,
        '1990-10-03',
        2001,
        1001
    ),
    (
        3005,
        5160.45,
        '1990-10-03',
        2003,
        1002
    ),
    (
        3006,
        1098.16,
        '1990-10-03',
        2008,
        1007
    ),
    (
        3007,
        75.75,
        '1990-10-04',
        2004,
        1002
    ),
    (
        3008,
        4723.0,
        '1990-10-05',
        2006,
        1001
    ),
    (
        3009,
        1713.23,
        '1990-10-04',
        2002,
        1003
    ),
    (
        3010,
        1309.95,
        '1990-10-06',
        2004,
        1002
    ),
    (
        3011,
        9891.88,
        '1990-10-06',
        2006,
        1001
    );