-
Notifications
You must be signed in to change notification settings - Fork 35
/
create-script.sql
95 lines (77 loc) · 3.88 KB
/
create-script.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
-- Meant for PostgreSQL 9.5 --
-- DO NOT FORGET TO EXECUTE ALSO sql_functions/register_employee.sql
-- (re)create tables first
DROP TABLE ProjectEmployee;
DROP TABLE Project;
DROP TABLE Employee;
DROP TABLE Department;
DROP TABLE Company;
CREATE TABLE Company (
pid SERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
address TEXT
);
CREATE TABLE Department (
pid SERIAL PRIMARY KEY,
company_pid INTEGER NOT NULL REFERENCES Company (pid) ON DELETE NO ACTION,
name TEXT NOT NULL
);
CREATE TABLE Employee (
pid SERIAL PRIMARY KEY,
department_pid INTEGER NOT NULL REFERENCES Department (pid) ON DELETE NO ACTION,
name TEXT NOT NULL,
surname TEXT NOT NULL,
email TEXT,
salary NUMERIC(10, 2) CHECK (salary > 0),
CONSTRAINT emloyee_name_surname_unique UNIQUE (name, surname)
);
CREATE TABLE Project (
pid SERIAL PRIMARY KEY,
name TEXT NOT NULL,
dateStarted date
);
CREATE TABLE ProjectEmployee (
project_pid INTEGER REFERENCES Project ON DELETE CASCADE,
employee_pid INTEGER REFERENCES Employee ON DELETE CASCADE,
PRIMARY KEY (project_pid, employee_pid)
);
-- now insertProject some test data
INSERT INTO Company (name, address) VALUES ('CleverGang', 'Prague, Czech Republic');
INSERT INTO Company (name, address) VALUES ('Supersoft', 'Berlin, Germany');
INSERT INTO Company (name, address) VALUES ('Pear', 'Cupertino, USA');
INSERT INTO Department (company_pid, name) VALUES (1, 'Back office');
INSERT INTO Department (company_pid, name) VALUES (1, 'IT Department');
INSERT INTO Department (company_pid, name) VALUES (1, 'Software Development');
INSERT INTO Department (company_pid, name) VALUES (2, 'Help desk');
INSERT INTO Department (company_pid, name) VALUES (2, 'Sales');
INSERT INTO Department (company_pid, name) VALUES (3, 'Hardware Development');
INSERT INTO Department (company_pid, name) VALUES (1, 'Lazy Department');
-- names generated using listofrandomnames.com ;)
INSERT INTO Employee (department_pid, name, surname, email, salary)
VALUES (1, 'Curt', 'Odegaard', '[email protected]', 10000);
INSERT INTO Employee (department_pid, name, surname, email, salary)
VALUES (2, 'Rupert', 'Spradling', '[email protected]', 11000);
INSERT INTO Employee (department_pid, name, surname, email, salary)
VALUES (3, 'Carita', 'Ladouceur', '[email protected]', 12000);
INSERT INTO Employee (department_pid, name, surname, email, salary)
VALUES (4, 'Abbie', 'Waring', '[email protected]', 13000);
INSERT INTO Employee (department_pid, name, surname, email, salary)
VALUES (5, 'Cecily', 'Devaughn', '[email protected]', 15000);
INSERT INTO Employee (department_pid, name, surname, email, salary)
VALUES (6, 'Yulanda', 'Grado', '[email protected]', 17000);
INSERT INTO Employee (department_pid, name, surname, email, salary)
VALUES (1, 'Chia', 'Kuder', '[email protected]', 20000);
INSERT INTO Employee (department_pid, name, surname, email, salary)
VALUES (2, 'Alica', 'Iannotti', '[email protected]', 30000);
INSERT INTO Employee (department_pid, name, surname, email, salary)
VALUES (3, 'Estrella', 'Heroux', '[email protected]', 51000);
INSERT INTO Employee (department_pid, name, surname, email, salary)
VALUES (4, 'Myrta', 'Lirette', '[email protected]', 54250);
INSERT INTO Project (name, dateStarted) VALUES ('Awesome app', CURRENT_DATE);
INSERT INTO Project (name, dateStarted) VALUES ('Desktop app', CURRENT_DATE);
INSERT INTO ProjectEmployee (project_pid, employee_pid) VALUES (1, 1);
INSERT INTO ProjectEmployee (project_pid, employee_pid) VALUES (1, 2);
INSERT INTO ProjectEmployee (project_pid, employee_pid) VALUES (1, 9);
INSERT INTO ProjectEmployee (project_pid, employee_pid) VALUES (2, 10);
INSERT INTO ProjectEmployee (project_pid, employee_pid) VALUES (2, 5);
INSERT INTO ProjectEmployee (project_pid, employee_pid) VALUES (1, 4);