-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNormalizing_Data.sql
170 lines (118 loc) · 4.11 KB
/
Normalizing_Data.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
CREATE TABLE VehicleBodyTypes (
vehicle_bodytype_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
body_type VARCHAR(64)
);
ALTER TABLE vehicletypes
ADD column vehicle_bodytype_id int
ALTER TABLE vehicletypes
ADD CONSTRAINT vehicletypes_vehiclebodytypes
FOREIGN KEY (vehicle_bodytype_id )
REFERENCES vehiclebodytypes (vehicle_bodytype_id );
UPDATE public.vehicleTypes
SET vehicle_bodytype_id = 1
WHERE vehicletypes.body_type = 'Car'
UPDATE public.vehicleTypes
SET vehicle_bodytype_id = 2
WHERE vehicletypes.body_type = 'Truck'
UPDATE public.vehicleTypes
SET vehicle_bodytype_id = 3
WHERE vehicletypes.body_type = 'Van'
UPDATE public.vehicleTypes
SET vehicle_bodytype_id = 4
WHERE vehicletypes.body_type = 'SUV'
ALTER TABLE vehicletypes
DROP column body_type
CREATE TABLE VehicleMakes (
vehicle_make_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
vehicle_make_name VARCHAR(64)
);
ALTER TABLE vehicletypes
ADD column vehicle_make_id int
ALTER TABLE vehicletypes
ADD CONSTRAINT vehicletypes_vehiclemakes
FOREIGN KEY (vehicle_make_id )
REFERENCES vehiclemakes (vehicle_make_id );
UPDATE public.vehicleTypes
SET vehicle_make_id = 1
WHERE vehicletypes.make = 'Chevrolet'
UPDATE public.vehicleTypes
SET vehicle_make_id = 2
WHERE vehicletypes.make = 'Mazda'
UPDATE public.vehicleTypes
SET vehicle_make_id = 3
WHERE vehicletypes.make = 'Nissan'
UPDATE public.vehicleTypes
SET vehicle_make_id = 4
WHERE vehicletypes.make = 'Ford'
UPDATE public.vehicleTypes
SET vehicle_make_id = 5
WHERE vehicletypes.make = 'Volkswagen'
ALTER TABLE vehicletypes
DROP column make
CREATE TABLE VehicleModels (
vehicle_model_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
model_name VARCHAR(64)
);
ALTER TABLE vehicletypes
ADD column vehicle_model_id int
ALTER TABLE vehicletypes
ADD CONSTRAINT vehicletypes_vehiclemodels
FOREIGN KEY (vehicle_model_id )
REFERENCES vehiclemodels (vehicle_model_id );
UPDATE public.vehicleTypes
SET vehicle_model_id = 1
WHERE vehicletypes.model = 'Corvette'
UPDATE public.vehicleTypes
SET vehicle_model_id = 2
WHERE vehicletypes.model = 'Blazer'
UPDATE public.vehicleTypes
SET vehicle_model_id = 3
WHERE vehicletypes.model = 'Silverado'
UPDATE public.vehicleTypes
SET vehicle_model_id = 4
WHERE vehicletypes.model = 'MX-5 Miata'
UPDATE public.vehicleTypes
SET vehicle_model_id = 5
WHERE vehicletypes.model = 'CX-5'
UPDATE public.vehicleTypes
SET vehicle_model_id = 6
WHERE vehicletypes.model = 'CX-9'
UPDATE public.vehicleTypes
SET vehicle_model_id = 7
WHERE vehicletypes.model = 'Maxima'
UPDATE public.vehicleTypes
SET vehicle_model_id = 8
WHERE vehicletypes.model = 'Altima'
UPDATE public.vehicleTypes
SET vehicle_model_id = 9
WHERE vehicletypes.model = 'Titan'
UPDATE public.vehicleTypes
SET vehicle_model_id = 10
WHERE vehicletypes.model = 'Fusion'
UPDATE public.vehicleTypes
SET vehicle_model_id = 11
WHERE vehicletypes.model = 'EcoSport'
UPDATE public.vehicleTypes
SET vehicle_model_id = 12
WHERE vehicletypes.model = 'F-250'
UPDATE public.vehicleTypes
SET vehicle_model_id = 13
WHERE vehicletypes.model = 'Beetle'
UPDATE public.vehicleTypes
SET vehicle_model_id = 14
WHERE vehicletypes.model = 'Passat'
UPDATE public.vehicleTypes
SET vehicle_model_id = 15
WHERE vehicletypes.model = 'Atlas'
UPDATE public.vehicleTypes
SET vehicle_model_id = 16
WHERE vehicletypes.model = 'Transit-150 Cargo'
ALTER TABLE vehicletypes
DROP column model
PRACTICE:
INSERT INTO public.customers(first_name, last_name, email, street, city, state, zipcode, company_name, phone_number)
VALUES ('Shelly', 'Jordan', '[email protected]', '104 Belle Lane', 'Murfreesboro', 'TN', '31111', 'Shelly Bakes Cakes', '908-273-1423')
INSERT INTO public.vehicletypes(vehicle_bodytype_id, vehicle_make_id, vehicle_model_id)
VALUES (1, 4, 17)
INSERT INTO public.vehicles(vin, engine_type, vehicle_type_id, exterior_color, interior_color, floor_price, msr_price, miles_count, year_of_car, is_sold, is_new, dealership_location_id)
VALUES ('GRMZE2H36CS189509', 'V6', 31, 'Tangerine', 'Khaki', 24899, 15487, 53944, 1973, false, false, 11)