Introduction to databases
A database is a collection of data that can be stored and accessed or updated at any point in time by computers.
If you were to compare computers to a human brain, databases would be the "long term memory" section of the brain.
When you put data in a database it can always be retrieved in the future unless it's explicitly been updated, deleted, or something unexpected has happened to the computer it's running on.
All that said, there are many different types of databases -- each with their own strengths and weaknesses and best-suited to different use cases.
Types of databases
There are many different types of databases, but we'll just focus on three in this high-level overview:
- Relational databases
- Object-oriented databases
- Column-oriented databases
Relational databases are the most common type of database and are used to store data in tables.
In relational databases tables hold information in rows and each row corresponds to a single record.
If you're unfamiliar with a database table, you can think of them as being quite similar (visually) to an Excel spreadsheet.
Relational databases allow for quick retrieval of data because each table is indexed by a unique column value, which can also be used or referenced in other tables to create relationships between different types of data.
Additionally, relational databases offer more security than other types of databases as they require access to specific columns in order to modify or delete records.
Object-oriented databases are used to store data as objects, which can be more efficient than storing data in tables but provides fewer "guard rails" than relational databases to prevent users from making mistakes.
Objects are organized into classes that represent specific business concepts and the objects within those classes inherit properties from their parent class.
This means that no matter what object you create, it will have certain common features (such as an ID property) defined by the object's class definition.
Because objects are self-contained, you don't need access to another object's underlying tables or fields when working with them; instead, all interaction with an object occurs through its methods and properties.
Additionally, inheritance allows you to easily create complex hierarchies of objects without needing to write code explicitly governing how relationships among objects should work.
Column-oriented databases are used to store data in columns, which can be more efficient than storing data in rows.
Columns correspond one-to-one with records stored in a table and every column is automatically assigned a primary key value when the database is created.
Unlike row-based storage systems where every record must contain information about every column included in the table, column-based storage systems allow for greater efficiency as only those records which actually use a given column will include that column's value(s).
This allows for faster searches across large datasets since only the relevant records will need be retrieved rather than reading all of the data in a table before finding the record you're looking for.
How to choose the right database for your project
Choosing the right databases is all about weighing the pros and cons of each of your choices and thinking critically about what your constraints and priorities are.
Here are some key questions to consider:
- What kind of data do you need to store? Are there many different types of data with complicated relationships? If so, a relational database might be the right pick for you. But maybe an object-oriented database could be a good fit if your data is simple and unlikely to change in the future.
- Are "bugs" are no big deal for you or are they catastrophic and completely unacceptable? If the cost of having a bug isn't significant, object-oriented databases might be a good light-weight solution for you. But if it's critical that you have all possible safety in place to avoid bugs, a relational database might be a better fit.
Additionally, it's worth considering your project's scalability when choosing a database. Will your project need to handle a lot of data or traffic?
However - when in doubt - though it might take a bit more work upfront, it's usually a safer bet to go with a mature SQL database like PostgreSQL.