I’m new to relational databases and all the material I’ve read covered primary and foreign keys, normal forms, and joins but left out to populate the database once it’s created.
How do you import a CSV file so the fields match their related table?
Say you were tying to build a beer database and had a CSV file with each line as a record.
Header: brewer, beer_name, country,
city, state, beer_category, beer_type,
alcohol_contentRecord 1: Anheuser-Busch, Budweiser,
United States, St. Louis, Mo, Pale lager,
Regular,
5.0%Record 2: Anheuser-Busch, Bud Light,
United States, St. Louis, Mo, Pale lager
Light, 4.2%Record 3: Miller Brewing Company,
Miller Lite, United States, Milwaukee,
WI, Pale lager, Light,
4.2%
You can create a “Brewer” table and a “Beer” table. When importing how does you connect the primary keys between the tables?
You define Primary and Foreign key relationships when you create the tables. Once they’ve been set up you can just about forget them, unless the database throws an error if you try to do something that violates the relationships.
For example, if you try to add a record into the child (foreign) table that does not have a value in the parent table, the database will complain, if the relationships are set up correctly.
e.g. Adding Record 1 into the Beer table will fail unless you have already added
Anheuser-Busch into the Brewers table.
A suggestion on setting up your tables:
Then manually add the brewers (because there’s not too many of them)
Then fiddle your CSV file to replace all the brewer details with the brewer’s respective id, and use that to populate your Beer table:
So: The brewerId links each beer to a brewer in the brewer table. That is the foreign key – each beer automatically gets all the brewer’s details because it’s brewerId matches the brewer’s id (and you set that relationship up when you created the tables).
This lets you do really cool suff. Say Miller was bought by another company… all you have to do is change the details in the Brewer table, and voila – all the beers that were owned by Miller now belong to the new brewer. You can also calculate sales by Brewer, state, etc.
The basic rule of thumb is that Primary and Foreign keys are like a Parent – Child relationship. The Child stores the Parent’s id. This way each Parent can have multiple Children. It can get a WHOLE lot more complicated, but this will give you a very good starting approach to most of your database solutions.