-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqldb.sql
211 lines (160 loc) · 5.29 KB
/
mysqldb.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
CREATE TABLE KontraktStatus (
kontraktStatusId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
kontraktStatusNavn VARCHAR(50) NOT NULL
);
CREATE TABLE Opgavetype (
opgavetypeId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
opgavetypeNavn VARCHAR(50) NOT NULL
);
CREATE TABLE OpgaveStatus (
opgaveStatusId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
opgaveStatusNavn VARCHAR(50) NOT NULL
);
CREATE TABLE Lokation (
lokationId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
lokationNavn VARCHAR(50) NOT NULL
);
CREATE TABLE KonsulentProfil (
konsulentProfilId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
konsulentProfilNavn VARCHAR(50) NOT NULL
);
CREATE TABLE Kunde (
kundeId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
fornavn VARCHAR(30) NOT NULL,
efternavn VARCHAR(30) NOT NULL,
firma VARCHAR(50)
);
CREATE TABLE Opgaveloser (
opgaveloserId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
fornavn VARCHAR(30) NOT NULL,
efternavn VARCHAR(30) NOT NULL,
arbejdstidPrUge DECIMAL(4,2) NOT NULL,
lokationId INT UNSIGNED NOT NULL,
FOREIGN KEY (lokationId)
REFERENCES Lokation(lokationId)
ON DELETE RESTRICT
);
#Tilføj trigger når nogen updater arbejdstidPrUge så grafer ikke bliver ændret
CREATE TABLE Opgavestiller (
opgavestillerId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
fornavn VARCHAR(30) NOT NULL,
efternavn VARCHAR(30) NOT NULL,
lokationId INT UNSIGNED NOT NULL,
FOREIGN KEY (lokationId)
REFERENCES Lokation(lokationId)
ON DELETE RESTRICT
);
CREATE TABLE Kundeansvarlig (
kundeansvarligId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
fornavn VARCHAR(30) NOT NULL,
efternavn VARCHAR(30) NOT NULL,
lokationId INT UNSIGNED NOT NULL,
FOREIGN KEY (lokationId)
REFERENCES Lokation(lokationId)
ON DELETE RESTRICT
);
CREATE TABLE OpgaveloserArbejdsTider (
arbejdstiderId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
dag INT(1) UNSIGNED NOT NULL,
dagStart TIME NOT NULL,
dagSlut TIME NOT NULL,
opgaveloserId INT UNSIGNED NOT NULL,
UNIQUE KEY arbejdstiderId (dag,opgaveloserId),
FOREIGN KEY (opgaveloserId)
REFERENCES Opgaveloser(opgaveloserId)
ON DELETE CASCADE
);
CREATE TABLE OpgaveloserKonsulentprofil (
opgaveloserKonsulentProfilId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
opgaveloserId INT UNSIGNED NOT NULL,
konsulentProfilId INT UNSIGNED NOT NULL,
konsulentProfilWeight INT(1) UNSIGNED NOT NULL,
UNIQUE KEY opgaveloserKonsulentProfilId (opgaveloserId,konsulentProfilId),
FOREIGN KEY (konsulentProfilId)
REFERENCES KonsulentProfil(konsulentProfilId)
ON DELETE CASCADE,
FOREIGN KEY (opgaveloserId)
REFERENCES Opgaveloser(opgaveloserId)
ON DELETE CASCADE
);
CREATE TABLE Opgave (
opgaveId INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
opgaveNavn VARCHAR(100) NOT NULL,
kundeId INT UNSIGNED default NULL,
kundeansvarligId INT UNSIGNED default NULL,
opgavestillerId INT UNSIGNED default NULL,
opgaveStatusId INT UNSIGNED default NULL,
opgavetypeId INT UNSIGNED default NULL,
lokationId INT UNSIGNED default NULL,
kontraktStatusId INT UNSIGNED default NULL,
startDato DATE default NULL,
fixedStartDato BOOLEAN default false,
slutDato DATE default NULL,
fixedSlutDato BOOLEAN default false,
kommentar VARCHAR(254) DEFAULT '',
estimeretTimetal INT(2) UNSIGNED default 0,
aktiv BOOLEAN default false,
FOREIGN KEY (kundeId)
REFERENCES Kunde(kundeId)
ON DELETE SET NULL,
FOREIGN KEY (kundeansvarligId)
REFERENCES Kundeansvarlig(kundeansvarligId)
ON DELETE SET NULL,
FOREIGN KEY (opgavestillerId)
REFERENCES Opgavestiller(opgavestillerId)
ON DELETE SET NULL,
FOREIGN KEY (opgaveStatusId)
REFERENCES OpgaveStatus(opgaveStatusId)
ON DELETE SET NULL,
FOREIGN KEY (opgavetypeId)
REFERENCES Opgavetype(opgavetypeId)
ON DELETE SET NULL,
FOREIGN KEY (lokationId)
REFERENCES Lokation(lokationId),
FOREIGN KEY (kontraktStatusId)
REFERENCES KontraktStatus(kontraktStatusId)
ON DELETE SET NULL
);
CREATE TABLE OpgaveloserOpgave (
opgaveloserOpgaveId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
opgaveId INT UNSIGNED NOT NULL,
opgaveloserKonsulentProfilId INT UNSIGNED NOT NULL,
UNIQUE KEY opgaveloserOpgaveId (opgaveId,opgaveloserKonsulentProfilId),
FOREIGN KEY (opgaveId)
REFERENCES Opgave(opgaveId)
ON DELETE CASCADE,
FOREIGN KEY (opgaveloserKonsulentProfilId)
REFERENCES OpgaveloserKonsulentprofil(opgaveloserKonsulentProfilId)
ON DELETE CASCADE
);
CREATE TABLE UgeTimeOpgave (
ugeTimeOpgaveId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
opgaveloserOpgaveId INT UNSIGNED NOT NULL,
year INT(2) UNSIGNED NOT NULL,
month INT(1) UNSIGNED NOT NULL,
week INT(1) UNSIGNED NOT NULL,
timeAntal DECIMAL(4,1) UNSIGNED NOT NULL,
dato DATE default NULL,
UNIQUE KEY ugeTimeOpgaveId (opgaveloserOpgaveId,year,month,week),
FOREIGN KEY (opgaveloserOpgaveId)
REFERENCES OpgaveloserOpgave(opgaveloserOpgaveId)
ON DELETE CASCADE
);
DELIMITER ;;
CREATE TRIGGER createDate
BEFORE INSERT ON UgeTimeOpgave
FOR EACH ROW
BEGIN
#https://stackoverflow.com/questions/3960049/create-date-from-day-month-year-fields-in-mysql#comment60757399_3960097
SET NEW.dato = STR_TO_DATE(CONCAT(NEW.year,'-',LPAD(NEW.month,2,'00'),'-',LPAD('01',2,'00')), '%Y-%m-%d');
END;;
DELIMITER ;
CREATE TABLE Deadline (
deadlineId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
opgaveId INT UNSIGNED NOT NULL,
deadlineDato DATE NOT NULL,
deadlineKommentar VARCHAR(254) DEFAULT '',
FOREIGN KEY (opgaveId)
REFERENCES Opgave(opgaveId)
ON DELETE CASCADE
);