Hi,
This is a bit of a longish post, sorry.
For a bit of practice I am working on a personal project. The idea being I take some json data, wrangle it into a relational database, and hopefully at the end create a nice form that will filter results e.g. based on mileage, colour choice, make, model etc.
My question relates to the database design, something I am bit of a novice on.
This is an example of the json data I have created.
locations.json
[
{"county": "Mid Glamorgan", "town": "Aberdare"},
{"county": "Aberdeenshire", "town": "Aberdeen"},
{"county": "Gwynedd", "town": "Aberdovey"},
{"county": "Gwent", "town": "Abergavenny"},
{"county": "Clwyd", "town": "Abergele"},
{"county": "Gwent", "town": "Abertillery"}
...
]
A limited car choice in that I haven’t included the model variation, M series etc.
cars.json
[
...
{
"model": "Puma",
"make": "Ford",
"transmissions": ["manual", "automatic"],
"fuel_types": ["petrol", "diesel", "hybrid", "electric"],
"colours": ["black", "blue", "grey", "white", "silver", "red", "green", "brown", "gold", "orange", "purple", "yellow"],
"retail_price": 28000
},
{
"model": "model y",
"make": "tesla",
"transmissions": ["automatic"],
"fuel_types": ["electric"],
"colours": ["black", "blue", "grey", "white", "silver", "red"],
"retail_price": 56000
},
...
]
Using the above with a bit of randomness and some calculations taken into account e.g depreciation for year, mileage I can generate however many cars for sale.
cars_for_sale.json
[
...
{
"make": "Mercedes-Benz",
"model": "C-Class",
"year": 2017,
"fuel_type": "hybrid",
"transmission": "automatic",
"color": "bronze",
"location": {
"county": "Bedfordshire",
"town": "Biggleswade"
},
"mileage": 42596,
"price": 13330
},
{
"make": "BMW",
"model": "1 Series",
"year": 2016,
"fuel_type": "petrol",
"transmission": "manual",
"color": "green",
"location": {
"county": "West Sussex",
"town": "Arundel"
},
"mileage": 71571,
"price": 9191
}
...
]
Using Pandas and SQL alchemy I will be creating the tables.
I have been considering the relationships and come up with the following
Towns and Counties (many-to-many)
A county has many towns
A town has many counties
e.g. The town Newport can be found in Gwent, Isle of Wight and Shropshire
Makes and Models of car (one-to-many)
A make can have many models
A model has only one make
Models and Colours (many-to-many)
A model can have many colours
A colour can have many models
Models and Fuel Types (many-to-many)
A model can have a few fueltypes
A fueltype can have many models
Models and Transmissions (many-to-many)
A model can have a few transmission types
A transmission type can have many models
Cars for sale
I am thinking the following schema (note have removed on delete cascade etc. just to simplify)
TABLE cars_for_sale {
car_id INTEGER [primary key]
model_id INTEGER
colour_id INTEGER
fuel_type_id INTEGER
transmission_id INTEGER
town_county_id INTEGER
year INTEGER
price FLOAT
mileage INTEGER
FOREIGN KEY(model_id) REFERENCES models (model_id)
FOREIGN KEY(colour_id) REFERENCES colours (colour_id)
FOREIGN KEY(fuel_type_id) REFERENCES fuel_types (fuel_type_id)
FOREIGN KEY(transmission_id) REFERENCES transmissions (transmission_id)
FOREIGN KEY(town_county_id) REFERENCES towns_counties (town_county_id)
}
Finally thanks to the wonderful dbdiagram.io
here is an ERD
Any pointers would be appreciated. If you could please bare in mind I am a bit of simpleton when it comes to this