sql greensauna

Sql code posted by filip skokan
created at 10 May 00:32

Edit | Back
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
USE greensauna
;


# Drop Tables, Stored Procedures and Views 
DROP TABLE IF EXISTS designs
;
DROP TABLE IF EXISTS control_units
;
DROP TABLE IF EXISTS facings
;
DROP TABLE IF EXISTS heaters
;
DROP TABLE IF EXISTS customers
;
DROP TABLE IF EXISTS cabins
;
DROP TABLE IF EXISTS employees
;
DROP TABLE IF EXISTS assembly_teams
;
DROP TABLE IF EXISTS assemblies
;
DROP TABLE IF EXISTS cars
;

# Create Tables 
CREATE TABLE designs
(
  id INTEGER NOT NULL AUTO_INCREMENT,
  name VARCHAR(30) NOT NULL,
  active BOOL NOT NULL DEFAULT 1,
  PRIMARY KEY (id),
  UNIQUE UQ_designs_id(id),
  UNIQUE UQ_designs_name(name)
) 
;


CREATE TABLE control_units
(
  id INTEGER NOT NULL AUTO_INCREMENT,
  name VARCHAR(30) NOT NULL,
  active BOOL NOT NULL DEFAULT 1,
  PRIMARY KEY (id),
  UNIQUE UQ_control_units_id(id),
  UNIQUE UQ_control_units_name(name)
) 
;


CREATE TABLE facings
(
  id INTEGER NOT NULL AUTO_INCREMENT,
  name VARCHAR(30) NOT NULL,
  active BOOL NOT NULL DEFAULT 1,
  PRIMARY KEY (id),
  UNIQUE UQ_facings_id(id),
  UNIQUE UQ_facings_name(name)
) 
;


CREATE TABLE heaters
(
  id INTEGER NOT NULL AUTO_INCREMENT,
  name VARCHAR(30) NOT NULL,
  active BOOL NOT NULL DEFAULT 1,
  PRIMARY KEY (id),
  UNIQUE UQ_heaters_id(id),
  UNIQUE UQ_heaters_name(name)
) 
;


CREATE TABLE customers
(
  id INTEGER NOT NULL AUTO_INCREMENT,
  name VARCHAR(60) NOT NULL,
  contact VARCHAR(255) NULL,
  cabins_count INTEGER NOT NULL DEFAULT 0,
  PRIMARY KEY (id),
  UNIQUE UQ_customers_id(id),
  UNIQUE UQ_customers_name(name)
) 
;


CREATE TABLE cabins
(
  id INTEGER NOT NULL AUTO_INCREMENT,
  address VARCHAR(255) NULL,
  note TEXT NULL,
  control_unit_id INTEGER NULL,
  design_id INTEGER NULL,
  facing_in_id INTEGER NULL,
  facing_out_id INTEGER NULL,
  customer_id INTEGER NULL,
  heater_id INTEGER NULL,
  assembly_id INTEGER NULL,
  PRIMARY KEY (id),
  UNIQUE UQ_cabins_id(id),
  KEY (assembly_id),
  KEY (facing_in_id),
  KEY (facing_out_id),
  KEY (control_unit_id),
  KEY (design_id),
  KEY (customer_id),
  KEY (heater_id)
) 
;


CREATE TABLE employees
(
  id INTEGER NOT NULL AUTO_INCREMENT,
  name VARCHAR(60) NOT NULL,
  assembly_team_id INTEGER NULL,
  PRIMARY KEY (id),
  UNIQUE UQ_employees_id(id),
  UNIQUE UQ_employees_name(name),
  KEY (assembly_team_id)
) 
;


CREATE TABLE assembly_teams
(
  id INTEGER NOT NULL AUTO_INCREMENT,
  name VARCHAR(60) NOT NULL,
  PRIMARY KEY (id),
  UNIQUE UQ_assembly_teams_id(id),
  UNIQUE UQ_assembly_teams_name(name)
) 
;


CREATE TABLE assemblies
(
  id INTEGER NOT NULL AUTO_INCREMENT,
  start_at DATE NOT NULL,
  end_at DATE NULL,
  assembly_team_id INTEGER NULL,
  car_id INTEGER NULL,
  PRIMARY KEY (id),
  UNIQUE UQ_assemblies_id(id),
  KEY (assembly_team_id),
  KEY (car_id)
) 
;


CREATE TABLE cars
(
  id INTEGER NOT NULL AUTO_INCREMENT,
  license_plate VARCHAR(7) NOT NULL,
  note VARCHAR(255) NULL,
  PRIMARY KEY (id),
  UNIQUE UQ_cars_id(id),
  UNIQUE UQ_cars_license_plate(license_plate)
) 
;





# Create Foreign Key Constraints 
ALTER TABLE cabins ADD CONSTRAINT FK_cabins_assemblies 
  FOREIGN KEY (assembly_id) REFERENCES assemblies (id)
;

ALTER TABLE cabins ADD CONSTRAINT FK_cabins_facings 
  FOREIGN KEY (facing_in_id) REFERENCES facings (id)
;

ALTER TABLE cabins ADD CONSTRAINT FK_cabins_facings 
  FOREIGN KEY (facing_out_id) REFERENCES facings (id)
;

ALTER TABLE cabins ADD CONSTRAINT FK_cabins_control_units 
  FOREIGN KEY (control_unit_id) REFERENCES control_units (id)
;

ALTER TABLE cabins ADD CONSTRAINT FK_cabins_designs 
  FOREIGN KEY (design_id) REFERENCES designs (id)
;

ALTER TABLE cabins ADD CONSTRAINT FK_cabins_customers 
  FOREIGN KEY (customer_id) REFERENCES customers (id)
;

ALTER TABLE cabins ADD CONSTRAINT FK_cabins_heaters 
  FOREIGN KEY (heater_id) REFERENCES heaters (id)
;

ALTER TABLE employees ADD CONSTRAINT FK_employees_assembly_teams 
  FOREIGN KEY (assembly_team_id) REFERENCES assembly_teams (id)
;

ALTER TABLE assemblies ADD CONSTRAINT FK_assemblies_assembly_teams 
  FOREIGN KEY (assembly_team_id) REFERENCES assembly_teams (id)
;

ALTER TABLE assemblies ADD CONSTRAINT FK_assemblies_cars 
  FOREIGN KEY (car_id) REFERENCES cars (id)
;
4.16 KB in 7 ms with coderay