I’m playing around with Sqlite3 in an attempt to get a handle on this SQL stuff. I’ve got a few questions on the topic.
Is there a basic structure that databases follow? I’m curious if I would model my database as though it were along the lines of a giant dictionary.
The problem I was thinking about:
If I wanted to have a program that could pull up the zip code or other general info for any city, I was thinking of a nested table kind of structure. Namely:
Countries Table:
+----+--------+--------+---------+
| US | Canada | Mexico | Etc... |
+----+--------+--------+---------+
|
|
|
|
| States Table:
+---------+----------+---------+--------+
| Alabama | Arkansas | Georgia | Etc... |
+---------+----------+---------+--------+
|
|
|
|
| Cities Table:
+-----------+---------+--------+---------+
| Alexander | Bauxite | Benton | Etc ... |
+-----------+---------+--------+---------+
|
|
+-----+------------+---------+------+--------------------+
| Key | population | zipcode | size | other random stuff |
+-----+------------+---------+------+--------------------+
But is that too much nesting..? Is that a bad design? The tip top stuff, the countries table doesn’t really do much, and I kind of had it in my head that you were supposed to be able to do really complex things easily with a database. If I went with my design, it seems like I’d be looping over a bunch of stuff before I finally got to what I wanted. So, I’m just curious if I’m going about this entire thing incorrectly.
Does anyone know of a good primer on the basics of using a database?
Relational Databases are based on the Entity-Relationship Model. If you want to grasp the concept behind RDBSMs, consider familiarizing with that theory first. Specifically relational schemes (tables, columns,relationships through foreign keys, …) are an (or the) application of the ERM.
Another keyword to search for is
Normalization. There are different “grades” of normalization and rules to transform from one grade to another, that topic is directly related to your question about the table structure. A general answer is, it depends. In general Normalization helps keeping data consistent – but a fully normalized table structure may incur a performance penalty (e.g. many joins for often used queries).I would suggest to first go with a stricter normalization and then check the performance. Selective denormalization may then help upping the performance.